Пример #1
0
 def __init__(self):
     XMLReader.__init__(self)
     self._lex_handler = LexicalHandler()
     self._decl_handler = DeclHandler()
     self._ns = 0
     self._ns_prfx = 1
     self._parsing = 0
Пример #2
0
 def __init__(self):
     XMLReader.__init__(self)
     self._lex_handler = LexicalHandler()
     self._decl_handler = DeclHandler()
     self._ns = 0
     self._ns_prfx = 1
     self._parsing = 0
Пример #3
0
class Dom2SaxParser(XMLReader):
    """  Generate SAX events from a DOM tree
    
    handle _ feature_namespaces
           _ feature_namespace_prefixes,
           _ property_lexical_handler
           _ property_declaration_handler (not yet fully)
           
    differences with standard sax parser:
         _ no error handling (we start from a dom tree !!)
         _ no locator (same reason)
    """
    def __init__(self):
        XMLReader.__init__(self)
        self._lex_handler = LexicalHandler()
        self._decl_handler = DeclHandler()
        self._ns = 0
        self._ns_prfx = 1
        self._parsing = 0

    ## properties and features ##################################################
    def getFeature(self, name):
        if name == handler.feature_namespaces:
            return self._ns
        elif name == handler.feature_namespace_prefixes:
            return self._ns_prfx
        raise SAXNotRecognizedException("Feature '%s' not recognized" % name)

    def setFeature(self, name, state):
        if self._parsing:
            raise SAXNotSupportedException("Cannot set features while parsing")

        if name == handler.feature_namespaces:
            self._ns = state
        elif name == handler.feature_namespace_prefixes:
            self._ns_prfx = state
        else:
            raise SAXNotRecognizedException("Feature '%s' not recognized" %
                                            name)

    def getProperty(self, name):
        if name == handler.property_lexical_handler:
            return self._lex_handler_prop
        if name == handler.property_declaration_handler:
            return self._decl_handler_prop
        raise SAXNotRecognizedException("Property '%s' not recognized" % name)

    def setProperty(self, name, value):
        if self._parsing:
            raise SAXNotSupportedException(
                "Cannot set properties while parsing")

        if name == handler.property_lexical_handler:
            self._lex_handler = value
        elif name == handler.property_declaration_handler:
            self._decl_handler = value
        else:
            raise SAXNotRecognizedException("Property '%s' not recognized" %
                                            name)

    ##  parsing ################################################################
    def parse(self, dom):
        if self._parsing:
            raise SAXNotSupportedException("Ask for parse while parsing")

        self._parsing = 1
        if self._ns: self._element_ = self._element_ns
        else: self._element_ = self._element
        self._from_dom(dom)
        self._parsing = 0

    ## private #################################################################
    def _from_dom(self, n):
        while n:
            type = n.nodeType
            if type == Node.ELEMENT_NODE:
                self._element_(n)
            elif type == Node.TEXT_NODE:
                self._cont_handler.characters(n.data)
            elif type == Node.PROCESSING_INSTRUCTION_NODE:
                self._cont_handler.processingInstruction(n.target, n.data)
            elif type == Node.DOCUMENT_NODE:
                self._cont_handler.startDocument()
                self._from_dom(n.firstChild)
                self._cont_handler.endDocument()
            elif type == Node.DOCUMENT_FRAGMENT_NODE:
                for n in n.childNodes:
                    self._cont_handler.startDocument()
                    self._from_dom(n.firstChild)
                    self._cont_handler.endDocument()
            elif type == Node.CDATA_SECTION_NODE:
                self._lex_handler.startCDATA()
                self._cont_handler.characters(n.data)
                self._lex_handler.endCDATA()
            elif type == Node.COMMENT_NODE:
                self._lex_handler.comment(n.data)
            elif type == Node.DOCUMENT_TYPE_NODE:
                self._lex_handler.startDTD(n.name, n.publicId, n.systemId)
                for i in range(n.entities.length):
                    e = n.entities.item(i)
                    if e.publicId or e.systemId:
                        self._decl_handler.externalEntityDecl(
                            e.notationName, e.publicId, e.systemId)
                    else:
                        self._decl_handler.externalEntityDecl(e.name, e.value)
                self._lex_handler.endDTD()
            elif type == Node.ENTITY_REFERENCE_NODE:
                self._lex_handler.startEntity(n.nodeName)
                self._from_dom(n.firstChild)
                self._lex_handler.endEntity(n.nodeName)
            #elif type == Node.ENTITY_NODE:
            #elif type == Node.NOTATION_NODE:
            n = n.nextSibling

    def _element(self, n):
        """ handle an ElementNode without NS interface"""
        ## convert DOM namedNodeMap to SAX attributes
        nnm = n.attributes
        attrs = {}
        for a in nnm.values():
            attrs[a.nodeName] = a.value
        ## handle element
        name = n.nodeName
        self._cont_handler.startElement(name, AttributesImpl(attrs))
        self._from_dom(n.firstChild)

        self._cont_handler.endElement(name)

    def _element_ns(self, n):
        """ handle an ElementNode with NS interface"""
        ## convert DOM namedNodeMap to SAX attributes NS
        prefix_list = []
        nnm = n.attributes
        attrs, qnames = {}, {}
        for a in nnm.values():
            a_uri = a.namespaceURI
            if a_uri == XMLNS_NS:
                prefix, val = a.localName, a.value
                self._cont_handler.startPrefixMapping(prefix, val)
                prefix_list.append(prefix)
                if self._ns_prfx:
                    name = (a_uri, prefix)
                    attrs[name] = val
                    qnames[name] = a.nodeName
            else:
                name = (a_uri, a.localName)
                attrs[name] = a.value
                qnames[name] = a.nodeName
        ## handle element NS
        name = (n.namespaceURI, n.localName)
        self._cont_handler.startElementNS(name, n.nodeName,
                                          AttributesNSImpl(attrs, qnames))
        self._from_dom(n.firstChild)
        self._cont_handler.endElementNS(name, n.nodeName)
        prefix_list.reverse()
        map(self._cont_handler.endPrefixMapping, prefix_list)
Пример #4
0
class Dom2SaxParser(XMLReader):
    """  Generate SAX events from a DOM tree
    
    handle _ feature_namespaces
           _ feature_namespace_prefixes,
           _ property_lexical_handler
           _ property_declaration_handler (not yet fully)
           
    differences with standard sax parser:
         _ no error handling (we start from a dom tree !!)
         _ no locator (same reason)
    """
    def __init__(self):
        XMLReader.__init__(self)
        self._lex_handler = LexicalHandler()
        self._decl_handler = DeclHandler()
        self._ns = 0
        self._ns_prfx = 1
        self._parsing = 0

        
    ## properties and features ##################################################
    def getFeature(self, name):
        if name == handler.feature_namespaces:
            return self._ns
        elif name == handler.feature_namespace_prefixes:
            return self._ns_prfx
        raise SAXNotRecognizedException("Feature '%s' not recognized"%name)

    
    def setFeature(self, name, state):
        if self._parsing:
            raise SAXNotSupportedException("Cannot set features while parsing")
        
        if name == handler.feature_namespaces:
            self._ns = state
        elif name == handler.feature_namespace_prefixes:
            self._ns_prfx = state
        else:
            raise SAXNotRecognizedException("Feature '%s' not recognized"%name)

        
    def getProperty(self, name):
        if name == handler.property_lexical_handler:
            return self._lex_handler_prop
        if name == handler.property_declaration_handler:
            return self._decl_handler_prop
        raise SAXNotRecognizedException("Property '%s' not recognized"%name)

    
    def setProperty(self, name, value):
        if self._parsing:
            raise SAXNotSupportedException("Cannot set properties while parsing")
        
        if name == handler.property_lexical_handler:
            self._lex_handler = value
        elif name == handler.property_declaration_handler:
            self._decl_handler = value
        else:
            raise SAXNotRecognizedException("Property '%s' not recognized"%name)

        
    ##  parsing ################################################################
    def parse(self, dom):
        if self._parsing:
            raise SAXNotSupportedException("Ask for parse while parsing")
        
        self._parsing = 1
        if self._ns: self._element_ = self._element_ns
        else: self._element_ = self._element
        self._from_dom(dom)
        self._parsing = 0


    ## private #################################################################
    def _from_dom(self, n):
        while n:
            type = n.nodeType
            if type == Node.ELEMENT_NODE:
                self._element_(n)
            elif type == Node.TEXT_NODE:
                self._cont_handler.characters(n.data)
            elif type == Node.PROCESSING_INSTRUCTION_NODE:
                self._cont_handler.processingInstruction(n.target, n.data)
            elif type == Node.DOCUMENT_NODE:
                self._cont_handler.startDocument()
                self._from_dom(n.firstChild)
                self._cont_handler.endDocument()
            elif type == Node.DOCUMENT_FRAGMENT_NODE:
                for n in n.childNodes:
                    self._cont_handler.startDocument()
                    self._from_dom(n.firstChild)
                    self._cont_handler.endDocument()
            elif type == Node.CDATA_SECTION_NODE:
                self._lex_handler.startCDATA()
                self._cont_handler.characters(n.data)
                self._lex_handler.endCDATA()
            elif type == Node.COMMENT_NODE:
                self._lex_handler.comment(n.data)
            elif type == Node.DOCUMENT_TYPE_NODE:
                self._lex_handler.startDTD(n.name, n.publicId, n.systemId)
                for i in range(n.entities.length):
                    e = n.entities.item(i)
                    if e.publicId or e.systemId:
                        self._decl_handler.externalEntityDecl(
                            e.notationName, e.publicId, e.systemId)
                    else:
                        self._decl_handler.externalEntityDecl(
                            e.name, e.value)
                self._lex_handler.endDTD()
            elif type == Node.ENTITY_REFERENCE_NODE:
                self._lex_handler.startEntity(n.nodeName)
                self._from_dom(n.firstChild)
                self._lex_handler.endEntity(n.nodeName)
            #elif type == Node.ENTITY_NODE:
            #elif type == Node.NOTATION_NODE:
            n = n.nextSibling

  
    def _element(self, n):
        """ handle an ElementNode without NS interface"""
        ## convert DOM namedNodeMap to SAX attributes
        nnm = n.attributes
        attrs = {}
        for a in nnm.values():
            attrs[a.nodeName] = a.value
        ## handle element
        name = n.nodeName
        self._cont_handler.startElement(name, AttributesImpl(attrs))
        self._from_dom(n.firstChild)

        self._cont_handler.endElement(name)


    def _element_ns(self, n):
        """ handle an ElementNode with NS interface"""
        ## convert DOM namedNodeMap to SAX attributes NS
        prefix_list = []
        nnm = n.attributes
        attrs, qnames = {}, {}
        for a in nnm.values():
            a_uri = a.namespaceURI
            if a_uri == XMLNS_NS:
                prefix, val = a.localName, a.value
                self._cont_handler.startPrefixMapping(prefix, val)
                prefix_list.append(prefix)
                if self._ns_prfx:
                    name = (a_uri, prefix)
                    attrs[name] = val
                    qnames[name] = a.nodeName
            else:
                name = (a_uri, a.localName)
                attrs[name] = a.value
                qnames[name] = a.nodeName
        ## handle element NS
        name = (n.namespaceURI, n.localName)
        self._cont_handler.startElementNS(name, n.nodeName,
                                          AttributesNSImpl(attrs, qnames))
        self._from_dom(n.firstChild)
        self._cont_handler.endElementNS(name, n.nodeName)
        prefix_list.reverse()
        map(self._cont_handler.endPrefixMapping, prefix_list)