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

📄 javadom.py

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 PY
📖 第 1 页 / 共 2 页
字号:
"""An adapter for Java DOM implementations that makes it possible to
access them through the same interface as the Python DOM implementations.

Supports:
- Sun's Java Project X
- Xerces
- David Brownell's SAX 2.0 Utilities / DOM2
- Indelv DOM
- SXP
- OpenXML

$Id$
"""

# Todo:
# - extend test suite
# - start using _set_up_attributes, or give up as too slow?
# - support level 2

import string

# --- Supported Java DOM implementations

class BaseDomImplementation:
    """An abstract DomImplementation with some reusable implementations
    of build* methods that depend on a lower-level _parse_from_source
    method."""

    def buildDocumentString(self, string):
        from java.io import StringReader
        from org.xml.sax import InputSource
        return self._parse_from_source(InputSource(StringReader(string)))

    def buildDocumentUrl(self, url):
        return self._parse_from_source(url)

    def buildDocumentFile(self, filename):
        return self.buildDocumentUrl(filetourl(filename))

class SunDomImplementation:

    def createDocument(self):
        from com.sun.xml.tree import XmlDocument
        return Document(XmlDocument())

    def buildDocumentString(self, string):
        from com.sun.xml.tree import XmlDocumentBuilder
        return Document(XmlDocumentBuilder.createXmlDocument(string))

    def buildDocumentUrl(self, url):
        from com.sun.xml.tree import XmlDocument
        return Document(XmlDocument.createXmlDocument(url))

    def buildDocumentFile(self, filename):
        return self.buildDocumentUrl(filetourl(filename))

class XercesDomImplementation(BaseDomImplementation):

    def createDocument(self):
        from org.apache.xerces.dom import DocumentImpl
        return Document(DocumentImpl())

    def _parse_from_source(self, source):
        from org.apache.xerces.parsers import DOMParser
        p = DOMParser()
        p.parse(source)
        return Document(p.getDocument())

class BrownellDomImplementation(BaseDomImplementation):

    def createDocument(self):
        from org.brownell.xml.dom import DomDocument
        return Document(DomDocument())

    def _parse_from_source(self, source):
        from org.brownell.xml import DomBuilder
        return Document(DomBuilder.createDocument(source))

class IndelvDomImplementation(BaseDomImplementation):

    def createDocument(self):
        from com.indelv.dom import DOMImpl
        return Document(DOMImpl.createNewDocument())

    def _parse_from_source(self, source):
        from com.indelv.dom.util import XMLReader
        from org.xml.sax import InputSource
        return Document(XMLReader.parseDocument(InputSource(source)))

class SxpDomImplementation(BaseDomImplementation):

    def createDocument(self):
        from fr.loria.xml import DOMFactory
        return Document(DOMFactory().createDocument())

    def _parse_from_source(self, source):
        from fr.loria.xml import DocumentLoader
        loader = DocumentLoader()

        if type(source) == type(""):
            doc = loader.loadDocument(source)
        elif source.getCharacterStream() != None:
            doc = loader.loadDocument(source.getCharacterStream())
        elif source.getByteStream() != None:
            doc = loader.loadDocument(source.getByteStream())
        elif source.getSystemId() != None:
            doc = loader.loadDocument(source.getSystemId())

        return Document(doc)

class OpenXmlDomImplementation(BaseDomImplementation):

    def createDocument(self):
        from org.openxml.dom import DocumentImpl
        return Document(DocumentImpl())

    def _parse_from_source(self, source):
        from org.openxml.dom import SAXBuilder
        from org.openxml.parser import XMLSAXParser

        builder = SAXBuilder()
        parser = XMLSAXParser()
        parser.setDocumentHandler(builder)
        parser.parse(source)
        return Document(builder.getDocument())

# ===== Utilities

def filetourl(file):
    # A Python port of James Clark's fileToURL from XMLTest.java.
    from java.io import File
    from java.net import URL
    from java.lang import System

    file = File(file).getAbsolutePath()
    sep = System.getProperty("file.separator")

    if sep != None and len(sep) == 1:
        file = file.replace(sep[0], '/')

    if len(file) > 0 and file[0] != '/':
        file = '/' + file

    return URL('file', None, file).toString()

def _wrap_node(node):
    if node == None:
        return None

    return NODE_CLASS_MAP[node.getNodeType()] (node)

# ===== Constants

ELEMENT_NODE                = 1
ATTRIBUTE_NODE              = 2
TEXT_NODE                   = 3
CDATA_SECTION_NODE          = 4
ENTITY_REFERENCE_NODE       = 5
ENTITY_NODE                 = 6
PROCESSING_INSTRUCTION_NODE = 7
COMMENT_NODE                = 8
DOCUMENT_NODE               = 9
DOCUMENT_TYPE_NODE          = 10
DOCUMENT_FRAGMENT_NODE      = 11
NOTATION_NODE               = 12

# ===== DOMException

try:
    from org.w3c.dom import DOMException
except ImportError, e:
    pass

# ===== DOMImplementation

class DOMImplementation:

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

    def hasFeature(self, feature, version):
        if version == None or version == "1.0":
            return string.lower(feature) == "xml" and \
                   self._impl.hasFeature(feature, version)
        else:
            return 0

    def __repr__(self):
        return "<DOMImplementation javadom.py, using '%s'>" % self._impl

# ===== Node

class Node:

    def __init__(self, impl):
        self.__dict__['_impl'] = impl

    # attributes

    def _get_nodeName(self):
        return self._impl.getNodeName()

    def _get_nodeValue(self):
        return self._impl.getNodeValue()

    def _get_nodeType(self):
        return self._impl.getNodeType()

    def _get_parentNode(self):
        return _wrap_node(self._impl.getParentNode())

    def _get_childNodes(self):
        children = self._impl.getChildNodes()
        if children is None:
            return children
        else:
            return NodeList(children)

    def _get_firstChild(self):
        return _wrap_node(self._impl.getFirstChild())

    def _get_lastChild(self):
        return _wrap_node(self._impl.getLastChild())

    def _get_previousSibling(self):
        return _wrap_node(self._impl.getPreviousSibling())

    def _get_nextSibling(self):
        return _wrap_node(self._impl.getNextSibling())

    def _get_ownerDocument(self):
        return _wrap_node(self._impl.getOwnerDocument())

    def _get_attributes(self):
        atts = self._impl.getAttributes()
        if atts is None:
            return None
        else:
            return NamedNodeMap(atts)

    # methods

    def insertBefore(self, new, neighbour):
        self._impl.insertBefore(new._impl, neighbour._impl)

    def replaceChild(self, new, old):
        self._impl.replaceChild(new._impl, old._impl)
        return old

    def removeChild(self, old):
        self._impl.removeChild(old._impl)
        return old

    def appendChild(self, new):
        return self._impl.appendChild(new._impl)

    def hasChildNodes(self):
        return self._impl.hasChildNodes()

    def cloneNode(self):
        return _wrap_node(self._impl.cloneNode())

    # python

    def __getattr__(self, name):
        if name[ : 5] != '_get_':
            return getattr(self, '_get_' + name) ()

        raise AttributeError, name

    def __setattr__(self, name, value):
        getattr(self, '_set_' + name) (value)

# ===== Document

class Document(Node):

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

    # methods

    def createTextNode(self, data):
        return Text(self._impl.createTextNode(data))

    def createEntityReference(self, name):
        return EntityReference(self._impl.createEntityReference(name))

    def createElement(self, name):
        return Element(self._impl.createElement(name))

    def createDocumentFragment(self):
        return DocumentFragment(self._impl.createDocumentFragment())

    def createComment(self, data):
        return Comment(self._impl.createComment(data))

    def createCDATASection(self, data):
        return CDATASection(self._impl.createCDATASection(data))

    def createProcessingInstruction(self, target, data):
        return ProcessingInstruction(self._impl.createProcessingInstruction(target, data))

    def createAttribute(self, name):
        return Attr(self._impl.createAttribute(name))

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

    # attributes

    def _get_doctype(self):
        return self._impl.getDoctype()

    def _get_implementation(self):
        return DOMImplementation(self._impl.getImplementation())

    def _get_documentElement(self):
        return _wrap_node(self._impl.getDocumentElement())

    # python

    def __repr__(self):
        docelm = self._impl.getDocumentElement()
        if docelm:
            return "<Document with root '%s'>" % docelm.getTagName()
        else:
            return "<Document with no root>"

# ===== Element

class Element(Node):

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

        self.__dict__['_get_tagName']    = self._impl.getTagName
        self.__dict__['getAttribute']    = self._impl.getAttribute
        self.__dict__['setAttribute']    = self._impl.setAttribute
        self.__dict__['removeAttribute'] = self._impl.removeAttribute
        self.__dict__['normalize']       = self._impl.normalize

    # methods

    def getAttributeNode(self, name):
        node = self._impl.getAttributeNode(name)
        if node == None:
            return node
        else:

⌨️ 快捷键说明

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