Exemplo n.º 1
0
 def __init__(self,log,autoattr=True):
   PipeStage.__init__(self,log)
   self.autoattr = autoattr  # autoconvert simple elements to attributes
   self._parser = expatreader.create_parser()
   self._parser.setContentHandler(self)
   self._parser.setErrorHandler(self)
   self.reset()
Exemplo n.º 2
0
def test_expat_locator_withinfo():
    result = StringIO()
    xmlgen = XMLGenerator(result)
    parser = create_parser()
    parser.setContentHandler(xmlgen)
    parser.parse(findfile("test.xml"))

    return parser.getSystemId() == findfile("test.xml") and parser.getPublicId() is None
Exemplo n.º 3
0
def test_expat_incomplete():
    parser = create_parser()
    parser.setContentHandler(ContentHandler())  # do nothing
    try:
        parser.parse(StringIO("<foo>"))
    except SAXParseException:
        return 1  # ok, error found
    else:
        return 0
Exemplo n.º 4
0
def test_expat_inpsource_sysid():
    parser = create_parser()
    result = StringIO()
    xmlgen = XMLGenerator(result)

    parser.setContentHandler(xmlgen)
    parser.parse(InputSource(findfile("test" + os.extsep + "xml")))

    return result.getvalue() == xml_test_out
Exemplo n.º 5
0
    def test_expat_inpsource_filename(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        parser.parse(findfile("test"+os.extsep+"xml"))

        self.assertEquals(result.getvalue(), xml_test_out)
Exemplo n.º 6
0
def test_expat_inpsource_filename():
    parser = create_parser()
    result = StringIO()
    xmlgen = XMLGenerator(result)

    parser.setContentHandler(xmlgen)
    parser.parse(findfile("test.xml"))

    return result.getvalue() == xml_test_out
Exemplo n.º 7
0
def test_expat_attrs_empty():
    parser = create_parser()
    gather = AttrGatherer()
    parser.setContentHandler(gather)

    parser.feed("<doc/>")
    parser.close()

    return verify_empty_attrs(gather._attrs)
Exemplo n.º 8
0
    def test_expat_attrs_wattr(self):
        parser = create_parser()
        gather = self.AttrGatherer()
        parser.setContentHandler(gather)

        parser.feed("<doc attr='val'/>")
        parser.close()

        self.verify_attrs_wattr(gather._attrs)
Exemplo n.º 9
0
    def test_expat_inpsource_filename(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        parser.parse(TEST_XMLFILE)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 10
0
    def test_expat_locator_withinfo(self):
        result = BytesIO()
        xmlgen = XMLGenerator(result)
        parser = create_parser()
        parser.setContentHandler(xmlgen)
        parser.parse(TEST_XMLFILE)

        self.assertEqual(parser.getSystemId(), TEST_XMLFILE)
        self.assertEqual(parser.getPublicId(), None)
Exemplo n.º 11
0
    def test_expat_nsattrs_empty(self):
        parser = create_parser(1)
        gather = self.AttrGatherer()
        parser.setContentHandler(gather)

        parser.feed("<doc/>")
        parser.close()

        self.verify_empty_nsattrs(gather._attrs)
Exemplo n.º 12
0
    def test_expat_inpsource_sysid(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        parser.parse(InputSource(findfile("test.xml")))

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 13
0
    def test_expat_inpsource_sysid(self):
        parser = create_parser()
        result = BytesIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        parser.parse(InputSource(TEST_XMLFILE))

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 14
0
    def test_expat_file(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        parser.parse(open(findfile("test.xml")))

        self.assertEquals(result.getvalue(), xml_test_out)
Exemplo n.º 15
0
    def test_expat_locator_withinfo(self):
        result = StringIO()
        xmlgen = XMLGenerator(result)
        parser = create_parser()
        parser.setContentHandler(xmlgen)
        parser.parse(findfile("test.xml"))

        self.assertEqual(parser.getSystemId(), findfile("test.xml"))
        self.assertEqual(parser.getPublicId(), None)
Exemplo n.º 16
0
    def test_expat_text_file(self):
        parser = create_parser()
        result = BytesIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
            parser.parse(f)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 17
0
    def test_expat_file(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        with open(TEST_XMLFILE) as f:
            parser.parse(f)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 18
0
    def test_expat_incremental(self):
        result = StringIO()
        xmlgen = XMLGenerator(result)
        parser = create_parser()
        parser.setContentHandler(xmlgen)

        parser.feed("<doc>")
        parser.feed("</doc>")
        parser.close()

        self.assertEqual(result.getvalue(), start + "<doc></doc>")
Exemplo n.º 19
0
def test_expat_locator_noinfo():
    result = StringIO()
    xmlgen = XMLGenerator(result)
    parser = create_parser()
    parser.setContentHandler(xmlgen)

    parser.feed("<doc>")
    parser.feed("</doc>")
    parser.close()

    return parser.getSystemId() is None and parser.getPublicId() is None and parser.getLineNumber() == 1
Exemplo n.º 20
0
    def test_expat_binary_file_int_name(self):
        parser = create_parser()
        result = BytesIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        with open(TEST_XMLFILE, 'rb') as f:
            with open(f.fileno(), 'rb', closefd=False) as f2:
                parser.parse(f2)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 21
0
    def test_expat_inpsource_stream(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        inpsrc = InputSource()
        inpsrc.setByteStream(open(findfile("test.xml")))
        parser.parse(inpsrc)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 22
0
    def test_expat_binary_file_bytes_name(self):
        fname = os.fsencode(TEST_XMLFILE)
        parser = create_parser()
        result = BytesIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        with open(fname, 'rb') as f:
            parser.parse(f)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 23
0
    def test_expat_external_dtd_default(self):
        parser = create_parser()
        resolver = self.TestEntityRecorder()
        parser.setEntityResolver(resolver)

        parser.feed(
            '<!DOCTYPE external SYSTEM "unsupported://non-existing">\n'
        )
        parser.feed('<doc />')
        parser.close()
        self.assertEqual(resolver.entities, [])
Exemplo n.º 24
0
def test_expat_inpsource_stream():
    parser = create_parser()
    result = StringIO()
    xmlgen = XMLGenerator(result)

    parser.setContentHandler(xmlgen)
    inpsrc = InputSource()
    inpsrc.setByteStream(open(findfile("test" + os.extsep + "xml")))
    parser.parse(inpsrc)

    return result.getvalue() == xml_test_out
Exemplo n.º 25
0
def make_test_output():
    parser = create_parser()
    result = StringIO()
    xmlgen = XMLGenerator(result)

    parser.setContentHandler(xmlgen)
    parser.parse(findfile("test" + os.extsep + "xml"))

    outf = open(findfile("test" + os.extsep + "xml" + os.extsep + "out"), "w")
    outf.write(result.getvalue())
    outf.close()
Exemplo n.º 26
0
def test_expat_inpsource_location():
    parser = create_parser()
    parser.setContentHandler(ContentHandler())  # do nothing
    source = InputSource()
    source.setByteStream(StringIO("<foo bar foobar>"))  # ill-formed
    name = "a file name"
    source.setSystemId(name)
    try:
        parser.parse(source)
    except SAXException, e:
        return e.getSystemId() == name
Exemplo n.º 27
0
def test_expat_incremental():
    result = StringIO()
    xmlgen = XMLGenerator(result)
    parser = expatreader.create_parser()
    parser.setContentHandler(xmlgen)

    parser.feed("<doc>")
    parser.feed("</doc>")
    parser.close()

    return result.getvalue() == start + "<doc></doc>"
Exemplo n.º 28
0
    def test_expat_inpsource_character_stream(self):
        parser = create_parser()
        result = BytesIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        inpsrc = InputSource()
        with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
            inpsrc.setCharacterStream(f)
            parser.parse(inpsrc)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 29
0
    def test_expat_inpsource_stream(self):
        parser = create_parser()
        result = StringIO()
        xmlgen = XMLGenerator(result)

        parser.setContentHandler(xmlgen)
        inpsrc = InputSource()
        with open(TEST_XMLFILE) as f:
            inpsrc.setByteStream(f)
            parser.parse(inpsrc)

        self.assertEqual(result.getvalue(), xml_test_out)
Exemplo n.º 30
0
 def test_expat_inpsource_location(self):
     parser = create_parser()
     parser.setContentHandler(ContentHandler()) # do nothing
     source = InputSource()
     source.setByteStream(StringIO("<foo bar foobar>"))   #ill-formed
     name = "a file name"
     source.setSystemId(name)
     try:
         parser.parse(source)
         self.fail()
     except SAXException as e:
         self.assertEqual(e.getSystemId(), name)
Exemplo n.º 31
0
    def test_expat_incremental_reset(self):
        result = StringIO()
        xmlgen = XMLGenerator(result)
        parser = create_parser()
        parser.setContentHandler(xmlgen)

        parser.feed("<doc>")
        parser.feed("text")

        result = StringIO()
        xmlgen = XMLGenerator(result)
        parser.setContentHandler(xmlgen)
        parser.reset()

        parser.feed("<doc>")
        parser.feed("text")
        parser.feed("</doc>")
        parser.close()

        self.assertEqual(result.getvalue(), start + "<doc>text</doc>")
Exemplo n.º 32
0
    def test_expat_dtdhandler(self):
        parser = create_parser()
        handler = self.TestDTDHandler()
        parser.setDTDHandler(handler)

        parser.feed('<!DOCTYPE doc [\n')
        parser.feed('  <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n')
        parser.feed(
            '  <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n'
        )
        parser.feed(']>\n')
        parser.feed('<doc></doc>')
        parser.close()

        self.assertEqual(
            handler._notations,
            [("GIF",
              "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN",
              None)])
        self.assertEqual(handler._entities,
                         [("img", None, "expat.gif", "GIF")])
Exemplo n.º 33
0
def test_expat_nsattrs_wattr():
    parser = create_parser(1)
    gather = AttrGatherer()
    parser.setContentHandler(gather)

    parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
    parser.close()

    attrs = gather._attrs

    return attrs.getLength() == 1 and \
           attrs.getNames() == [(ns_uri, "attr")] and \
           (attrs.getQNames() == [] or attrs.getQNames() == ["ns:attr"]) and \
           len(attrs) == 1 and \
           attrs.has_key((ns_uri, "attr")) and \
           attrs.keys() == [(ns_uri, "attr")] and \
           attrs.get((ns_uri, "attr")) == "val" and \
           attrs.get((ns_uri, "attr"), 25) == "val" and \
           attrs.items() == [((ns_uri, "attr"), "val")] and \
           attrs.values() == ["val"] and \
           attrs.getValue((ns_uri, "attr")) == "val" and \
           attrs[(ns_uri, "attr")] == "val"
Exemplo n.º 34
0
    def test_expat_nsattrs_wattr(self):
        parser = create_parser(1)
        gather = self.AttrGatherer()
        parser.setContentHandler(gather)

        parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
        parser.close()

        attrs = gather._attrs

        self.assertEqual(attrs.getLength(), 1)
        self.assertEqual(attrs.getNames(), [(ns_uri, "attr")])
        self.assertTrue((attrs.getQNames() == []
                         or attrs.getQNames() == ["ns:attr"]))
        self.assertEqual(len(attrs), 1)
        self.assertIn((ns_uri, "attr"), attrs)
        self.assertEqual(attrs.get((ns_uri, "attr")), "val")
        self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val")
        self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")])
        self.assertEqual(list(attrs.values()), ["val"])
        self.assertEqual(attrs.getValue((ns_uri, "attr")), "val")
        self.assertEqual(attrs[(ns_uri, "attr")], "val")
Exemplo n.º 35
0
# regression test for SAX 2.0
Exemplo n.º 36
0
 def test_expat_incomplete(self):
     parser = create_parser()
     parser.setContentHandler(ContentHandler())  # do nothing
     self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>"))
     self.assertEqual(parser.getColumnNumber(), 5)
     self.assertEqual(parser.getLineNumber(), 1)
Exemplo n.º 37
0
 def setUp(self):
     self.parser = create_parser()
     self.parser.setFeature(handler.feature_namespaces, 1)
     self.parser.setFeature(handler.feature_namespace_prefixes, 1)
Exemplo n.º 38
0
import xml.sax.expatreader as bad
import defusedxml.expatreader as good

p = bad.create_parser()
b = good.create_parser()
Exemplo n.º 39
0
 def test_expat_incomplete(self):
     parser = create_parser()
     parser.setContentHandler(ContentHandler())  # do nothing
     self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>"))