Esempio n. 1
0
    def _test_encoding(self, encoding, xml_encoding_name=None):
        foo = """<?xml version='1.0' encoding='%s'?>\n<tag attrib='123'></tag>""" % (
            xml_encoding_name or encoding)
        root = etree.fromstring(foo.encode(encoding))
        self.assertEqual('tag', root.tag)

        doc_encoding = root.getroottree().docinfo.encoding
        self.assertTrue(doc_encoding.lower().rstrip('lbe'),
                        (xml_encoding_name or encoding).lower().rstrip('lbe'))
Esempio n. 2
0
    def test_dtd_attrs(self):
        dtd = etree.DTD(fileUrlInTestDir("test.dtd"))

        # Test DTD.system_url attribute
        self.assertTrue(dtd.system_url.endswith("test.dtd"))

        # Test elements and their attributes
        a = dtd.elements()[0]
        self.assertEqual(a.name, "a")
        self.assertEqual(a.type, "element")
        self.assertEqual(a.content.name, "b")
        self.assertEqual(a.content.type, "element")
        self.assertEqual(a.content.occur, "once")

        aattr = a.attributes()[0]
        self.assertEqual(aattr.name, "default")
        self.assertEqual(aattr.type, "enumeration")
        self.assertEqual(aattr.values(), ["valueA", "valueB"])
        self.assertEqual(aattr.default_value, "valueA")

        b = dtd.elements()[1]
        self.assertEqual(b.name, "b")
        self.assertEqual(b.type, "empty")
        self.assertEqual(b.content, None)

        # Test entities and their attributes
        c = dtd.entities()[0]
        self.assertEqual(c.name, "c")
        self.assertEqual(c.orig, "&#42;")
        self.assertEqual(c.content, "*")

        # Test DTD.name attribute
        root = etree.XML(
            _bytes('''
        <!DOCTYPE a SYSTEM "none" [
        <!ELEMENT a EMPTY>
        ]>
        <a/>
        '''))
        dtd = etree.ElementTree(root).docinfo.internalDTD
        self.assertEqual(dtd.name, "a")

        # Test DTD.name and DTD.systemID attributes
        parser = etree.XMLParser(dtd_validation=True)
        xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>'
        root = etree.fromstring(xml,
                                parser=parser,
                                base_url=fileUrlInTestDir("test.xml"))

        dtd = root.getroottree().docinfo.internalDTD
        self.assertEqual(dtd.name, "a")
        self.assertEqual(dtd.system_url, "test.dtd")
Esempio n. 3
0
    def test_dtd_attrs(self):
        dtd = etree.DTD(fileInTestDir("test.dtd"))

        # Test DTD.system_url attribute
        self.assert_(dtd.system_url.endswith("test.dtd"))

        # Test elements and their attributes
        a = dtd.elements()[0]
        self.assertEqual(a.name, "a")
        self.assertEqual(a.type, "element")
        self.assertEqual(a.content.name, "b")
        self.assertEqual(a.content.type, "element")
        self.assertEqual(a.content.occur, "once")

        aattr = a.attributes()[0]
        self.assertEqual(aattr.name, "default")
        self.assertEqual(aattr.type, "enumeration")
        self.assertEqual(aattr.values(), ["valueA", "valueB"])
        self.assertEqual(aattr.default_value, "valueA")

        b = dtd.elements()[1]
        self.assertEqual(b.name, "b")
        self.assertEqual(b.type, "empty")
        self.assertEqual(b.content, None)

        # Test entities and their attributes
        c = dtd.entities()[0]
        self.assertEqual(c.name, "c")
        self.assertEqual(c.orig, "&#42;")
        self.assertEqual(c.content, "*")

        # Test DTD.name attribute
        root = etree.XML(_bytes('''
        <!DOCTYPE a SYSTEM "none" [
        <!ELEMENT a EMPTY>
        ]>
        <a/>
        '''))
        dtd = etree.ElementTree(root).docinfo.internalDTD
        self.assertEqual(dtd.name, "a")

        # Test DTD.name and DTD.systemID attributes
        parser = etree.XMLParser(dtd_validation=True)
        xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>'
        root = etree.fromstring(xml, parser=parser,
                                base_url=fileInTestDir("test.xml"))

        dtd = root.getroottree().docinfo.internalDTD
        self.assertEqual(dtd.name, "a")
        self.assertEqual(dtd.system_url, "test.dtd")
Esempio n. 4
0
 def test_dtd_parse_valid_relative_file_url(self):
     parser = etree.XMLParser(dtd_validation=True)
     xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>'
     root = etree.fromstring(xml,
                             parser=parser,
                             base_url=fileUrlInTestDir("test.xml"))
Esempio n. 5
0
 def test_dtd_parse_valid_file_url(self):
     parser = etree.XMLParser(dtd_validation=True)
     xml = ('<!DOCTYPE a SYSTEM "%s"><a><b/></a>' %
            fileUrlInTestDir("test.dtd"))
     root = etree.fromstring(xml, parser=parser)
Esempio n. 6
0
 def test_comment_before_dtd(self):
     data = '<!--comment--><!DOCTYPE test>\n<!-- --><test/>'
     doc = etree.fromstring(data).getroottree()
     self.assertEqual(etree.tostring(doc), _bytes(data))
Esempio n. 7
0
 def test_dtd_parse_valid_relative(self):
     parser = etree.XMLParser(dtd_validation=True)
     xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>'
     root = etree.fromstring(xml, parser=parser,
                             base_url=fileInTestDir("test.xml"))
Esempio n. 8
0
 def test_dtd_parse_valid(self):
     parser = etree.XMLParser(dtd_validation=True)
     xml = '<!DOCTYPE a SYSTEM "%s"><a><b/></a>' % fileInTestDir("test.dtd")
     root = etree.fromstring(xml, parser=parser)
Esempio n. 9
0
 def test_comment_before_dtd(self):
     data = '<!--comment--><!DOCTYPE test>\n<!-- --><test/>'
     doc = etree.fromstring(data).getroottree()
     self.assertEqual(etree.tostring(doc),
                      _bytes(data))