⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javadom.py

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 PY
📖 第 1 页 / 共 2 页
字号:
            return Attr(node)

    def setAttributeNode(self, attr):
        self._impl.setAttributeNode(attr._impl)

    def removeAttributeNode(self, attr):
        self._impl.removeAttributeNode(attr._impl)

    def getElementsByTagName(self, name):
        return NodeList(self._impl.getElementsByTagName(name))

    # python

    def __repr__(self):
        return "<Element '%s' with %d attributes and %d children>" % \
               (self._impl.getTagName(),
                self._impl.getAttributes().getLength(),
                self._impl.getChildNodes().getLength())

# ===== CharacterData

class CharacterData(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_data']     = self._impl.getData
        self.__dict__['_set_data']     = self._impl.setData
        self.__dict__['_get_length']   = self._impl.getLength

        self.__dict__['substringData'] = self._impl.substringData
        self.__dict__['appendData']    = self._impl.appendData
        self.__dict__['insertData']    = self._impl.insertData
        self.__dict__['deleteData']    = self._impl.deleteData
        self.__dict__['replaceData']   = self._impl.replaceData

# ===== Comment

class Comment(CharacterData):

    def __repr__(self):
        return "<Comment of length %d>" % self.getLength()

# ===== ProcessingInstruction

class ProcessingInstruction(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_target'] = self._impl.getTarget
        self.__dict__['_get_data']   = self._impl.getData
        self.__dict__['_set_data']   = self._impl.setData

    def __repr__(self):
        return "<PI with target '%s'>" % self._impl.getTarget()

# ===== Text

class Text(CharacterData):

    def splitText(self, offset):
        return Text(self._impl.splitText(offset))

    def __repr__(self):
        return "<Text of length %d>" % self._impl.getLength()

# ===== CDATASection

class CDATASection(Text):

    def __repr__(self):
        return "<CDATA section of length %d>" % self._impl.getLength()

# ===== Attr

class Attr(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_name']      = self._impl.getName
        self.__dict__['_get_specified'] = self._impl.getSpecified
        self.__dict__['_get_value']     = self._impl.getValue
        self.__dict__['_set_value']     = self._impl.setValue

    def __repr__(self):
        return "<Attr '%s'>" % self._impl.getName()

# ===== EntityReference

class EntityReference(Node):

    def __repr__(self):
        return "<EntityReference '%s'>" % self.getNodeName()

# ===== DocumentType

class DocumentType(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_name'] = self._impl.getName

    def _get_entities(self):
        return NamedNodeMap(self._impl.getEntities())

    def _get_notations(self):
        return NamedNodeMap(self._impl.getNotations())

    def __repr__(self):
        return "<DocumentType '%s'>" % self._impl.getNodeName()

# ===== Notation

class Notation(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_publicId'] = self._impl.getPublicId
        self.__dict__['_get_systemId'] = self._impl.getSystemId

    def __repr__(self):
        return "<Notation '%s'>" % self._impl.getNodeName()

# ===== Entity

class Entity(Node):

    def __init__(self, impl):
        Node.__init__(self, impl)

        self.__dict__['_get_publicId']     = self._impl.getPublicId
        self.__dict__['_get_systemId']     = self._impl.getSystemId
        self.__dict__['_get_notationName'] = self._impl.getNotationName

    def __repr__(self):
        return "<Entity '%s'>" % self._impl.getNodeName()

# ===== DocumentFragment

class DocumentFragment(Node):

    def __repr__(self):
        return "<DocumentFragment>"

# ===== NodeList

class NodeList:

    def __init__(self, impl):
        self._impl = impl

        self.__dict__['__len__']     = self._impl.getLength
        self.__dict__['_get_length'] = self._impl.getLength
        self.__dict__['item']        = self._impl.item

    # Python list methods

    def __getitem__(self, ix):
        if ix < 0:
            ix = len(self) + ix

        node = self._impl.item(ix)
        if node == None:
            raise IndexError, ix
        else:
            return _wrap_node(node)

    def __setitem__(self, ix, item):
        raise TypeError, "NodeList instances don't support item assignment"

    def __delitem__(self, ix, item):
        raise TypeError, "NodeList instances don't support item deletion"

    def __setslice__(self, i, j, list):
        raise TypeError, "NodeList instances don't support slice assignment"

    def __delslice__(self, i, j):
        raise TypeError, "NodeList instances don't support slice deletion"

    def append(self, item):
        raise TypeError, "NodeList instances don't support .append()"

    def insert(self, i, item):
        raise TypeError, "NodeList instances don't support .insert()"

    def pop(self, i=-1):
        raise TypeError, "NodeList instances don't support .pop()"

    def remove(self, item):
        raise TypeError, "NodeList instances don't support .remove()"

    def reverse(self):
        raise TypeError, "NodeList instances don't support .reverse()"

    def sort(self, *args):
        raise TypeError, "NodeList instances don't support .sort()"

    def __add__(self, *args):
        raise TypeError, "NodeList instances don't support +"

    def __radd__(self, *args):
        raise TypeError, "NodeList instances don't support +"

    def __mul__(self, *args):
        raise TypeError, "NodeList instances don't support *"

    def __rmul__(self, *args):
        raise TypeError, "NodeList instances don't support *"

    def count(self, *args):
        raise TypeError, "NodeList instances can't support count without equality"

    def count(self, *args):
        raise TypeError, "NodeList instances can't support index without equality"

    def __getslice__(self, i, j):
        if i < len(self):
            i = len(self) + i
        if j < len(self):
            j = len(self) + j

        slice = []
        for ix in range(i, min(j, len(self))):
            slice.append(self[ix])
        return slice

    def __repr__(self):
        return "<NodeList [ %s ]>" % string.join(map(repr, self), ", ")

# ===== NamedNodeMap

class NamedNodeMap:

    def __init__(self, impl):
        self._impl = impl

        self.__dict__['_get_length'] = self._impl.getLength
        self.__dict__['__len__']     = self._impl.getLength

    # methods

    def getNamedItem(self, name):
        return _wrap_node(self._impl.getNamedItem(name))

    def setNamedItem(self, node):
        return _wrap_node(self._impl.setNamedItem(node._impl))

    def removeNamedItem(self, name):
        return _wrap_node(self._impl.removeNamedItem(name))

    def item(self, index):
        return _wrap_node(self._impl.item(index))

    # Python dictionary methods

    def __getitem__(self, key):
        node = self._impl.getNamedItem(key)

        if node is None:
            raise KeyError, key
        else:
            return _wrap_node(node)

    def get(self, key, alternative = None):
        node = self._impl.getNamedItem(key)
        if node is None:
            return alternative
        else:
            return _wrap_node(node)

    def has_key(self, key):
        return self._impl.getNamedItem(key) != None

    def items(self):
        list = []
        for ix in range(self._impl.getLength()):
            node = self._impl.item(ix)
            list.append((node.getNodeName(), _wrap_node(node)))
        return list

    def keys(self):
        list = []
        for ix in range(self._impl.getLength()):
            list.append(self._impl.item(ix)._get_nodeName())
        return list

    def values(self):
        list = []
        for ix in range(self._impl.getLength()):
            list.append(_wrap_node(self._impl.item(ix)))
        return list

    def __setitem__(self, key, item):
        assert key == item._impl._get_nodeName()
        self._impl.setNamedItem(item._impl)

    def update(self, nnm):
        for v in nnm.values():
            self._impl.setNamedItem(v._impl)

    def __repr__(self):
        pairs = []
        for pair in self.items():
            pairs.append("'%s' : %s" % pair)
        return "<NamedNodeMap { %s }>" % string.join(pairs, ", ")

# ===== Various stuff

NODE_CLASS_MAP = {
    ELEMENT_NODE : Element,
    ATTRIBUTE_NODE : Attr,
    TEXT_NODE : Text,
    CDATA_SECTION_NODE : CDATASection,
    ENTITY_REFERENCE_NODE : EntityReference,
    ENTITY_NODE : Entity,
    PROCESSING_INSTRUCTION_NODE : ProcessingInstruction,
    COMMENT_NODE : Comment,
    DOCUMENT_NODE : Document,
    DOCUMENT_TYPE_NODE : DocumentType,
    DOCUMENT_FRAGMENT_NODE : DocumentFragment,
    NOTATION_NODE : Notation
    }

# ===== Self-test

if __name__ == "__main__":
    impl = BrownellDomImplementation() #XercesDomImplementation()  #SunDomImplementation()
    doc2 = impl.createDocument()
    print doc2
    print doc2._get_implementation()
    root = doc2.createElement("doc")
    print root
    doc2.appendChild(root)
    txt = doc2.createTextNode("This is a simple sample \n")
    print txt
    root.appendChild(txt)

    print root._get_childNodes()[0]
    print root._get_childNodes()

    root.setAttribute("huba", "haba")
    print root
    print root._get_attributes()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -