[XML-SIG] More entity fun

Juergen Hermann Juergen Hermann" <jh@web.de
Thu, 07 Jun 2001 17:23:44 +0200


Hi!

This time a problem with nested entities (file listings at the end):

> python2.1 ent2.py
(0, 6, 5)
+++ parser is <xml.sax.drivers2.drv_xmlproc.XmlprocDriver instance at 0x81fb6d4>
Traceback (most recent call last):
  File "ent2.py", line 35, in ?
    test = TestLoader('ent2.xml')
  File "ent2.py", line 27, in __init__
    parser.parse(self.fileurl)
  File "/netsite/lib/python2.1/_xmlplus/sax/drivers2/drv_xmlproc.py", line 90, in parse
    parser.read_from(source.getByteStream(), bufsize)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlval.py", line 105, in read_from
    self.parser.read_from(file,bufsize)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 137, in read_from
    self.feed(buf)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 185, in feed
    self.do_parse()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlproc.py", line 104, in do_parse
    self.parse_doctype()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlproc.py", line 482, in parse_doctype
    self.parse_internal_dtd()    
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlproc.py", line 532, in parse_internal_dtd
    self.handle_internal_dtd(line,lb,self.get_region()[:-last_part_size])
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlproc.py", line 544, in handle_internal_dtd
    p.feed(int_dtd)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 185, in feed
    self.do_parse()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/dtdparser.py", line 257, in do_parse
    self.parse_pe_ref()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/dtdparser.py", line 449, in parse_pe_ref
    self.open_entity(sysid) # Does parsing and popping
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 102, in open_entity
    self.read_from(inf)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 137, in read_from
    self.feed(buf)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 185, in feed
    self.do_parse()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/dtdparser.py", line 257, in do_parse
    self.parse_pe_ref()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/dtdparser.py", line 449, in parse_pe_ref
    self.open_entity(sysid) # Does parsing and popping
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 102, in open_entity
    self.read_from(inf)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 137, in read_from
    self.feed(buf)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 185, in feed
    self.do_parse()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/dtdparser.py", line 259, in do_parse
    self.parse_pi(self.dtd_consumer)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 567, in parse_pi
    self.parse_xml_decl()
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 493, in parse_xml_decl
    self.report_error(3009)
  File "/netsite/lib/python2.1/_xmlplus/parsers/xmlproc/xmlutils.py", line 372, in report_error
    self.err.fatal(msg)
  File "/netsite/lib/python2.1/_xmlplus/sax/drivers2/drv_xmlproc.py", line 214, in fatal
    self._err_handler.fatalError(saxlib.SAXParseException(msg, None, self))
  File "/netsite/lib/python2.1/_xmlplus/sax/handler.py", line 38, in fatalError
    raise exception
xml.sax._exceptions.SAXParseException: file:///export/home/jhe/tmp/ent2.xml:8:3: Multiple text declarations in a single entity


To get no traceback, you can remove the xml decl in "ent2-1.ent", 
but I don't think this is mandated by the XML specification.


==> ent2.py <==
import os
import xml.sax
import xml.sax.saxutils
import xml.sax.handler
import xml.sax.sax2exts

class TestLoader(xml.sax.saxutils.DefaultHandler):

    def __init__(self, filename):
        self.fileurl = 'file://' + os.path.abspath(filename)

        # create parser
        parser = xml.sax.sax2exts.XMLValParserFactory.make_parser()
        print '+++ parser is', parser
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        parser.setFeature(xml.sax.handler.feature_validation, 1)
        parser.setFeature(xml.sax.handler.feature_external_ges, 1)
        parser.setFeature(xml.sax.handler.feature_external_pes, 1)

        # set handlers
        parser.setContentHandler(self)
        parser.setDTDHandler(self)
        parser.setErrorHandler(self)
        parser.setEntityResolver(self)

        # parse the XML into events
        parser.parse(self.fileurl)

    def startElementNS(self, name, qname, attrs):
        print name, qname, attrs.items()


if __name__ == "__main__":
    print xml.version_info
    test = TestLoader('ent2.xml')

==> ent2.xml <==
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>

<!DOCTYPE root [
    <!ELEMENT root EMPTY>

    <!ENTITY % ent SYSTEM "ent2.ent">
    %ent;
]>

<root/>

==> ent2.ent <==
<?xml version="1.0" encoding="ISO-8859-1"?>

<!ENTITY % ent21 SYSTEM "ent2-1.ent">
%ent21;

==> ent2-1.ent <==
<?xml version="1.0" encoding="ISO-8859-1"?>

<!ENTITY HTTP_PORT  "80">