File: testxmlgen.py

package info (click to toggle)
python-odf 1.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,356 kB
  • ctags: 1,902
  • sloc: python: 21,654; makefile: 352; sh: 10; xml: 2
file content (134 lines) | stat: -rw-r--r-- 5,376 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 Søren Roug, European Environment Agency
#
# This is free software.  You may redistribute it under the terms
# of the Apache license and the GNU General Public License Version
# 2 or at your option any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Contributor(s):
#

import xml.sax, xml.sax.saxutils
import io
import tempfile
import unittest
import sys

class MyGen(xml.sax.saxutils.XMLGenerator):

    def _qname(self, name):
        """Builds a qualified name from a (ns_url, localname) pair"""
        if name[0]:
            if name[0] == u'http://www.w3.org/XML/1998/namespace':
                return u'xml' + ":" + name[1]
            # The name is in a non-empty namespace
            prefix = self._current_context[name[0]]
            if prefix:
                # If it is not the default namespace, prepend the prefix
                return prefix + ":" + name[1]
        # Return the unqualified name
        return name[1]

class TestXMLGenerator(unittest.TestCase):

    def test_xmlgenerator(self):
        """ Test that the xml namespace is understood by XMLGenerator """
        outfp = tempfile.TemporaryFile()
        c = xml.sax.saxutils.XMLGenerator(outfp,'utf-8')
        parser = xml.sax.make_parser()
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        parser.setContentHandler(c)
        testcontent="""<?xml version="1.0"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        parser.feed(testcontent)
        parser.close()
        expectedresult = """<?xml version="1.0" encoding="utf-8"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        outfp.seek(0)
        self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
        outfp.close()


    def test_xmlgenerator_wo_ns(self):
        """ Test that the missing xml namespace is understood by XMLGenerator """
        outfp = tempfile.TemporaryFile()
        c = xml.sax.saxutils.XMLGenerator(outfp,'utf-8')
        parser = xml.sax.make_parser()
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        parser.setContentHandler(c)
        testcontent="""<?xml version="1.0"?>
<a:greetings xmlns:a="http://example.com/ns">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        # There is a bug in older versions of saxutils
        if sys.version_info[0] == 2 and sys.version_info[1] == 6:
            self.assertRaises(KeyError, parser.feed, testcontent)
        else:
            parser.feed(testcontent)
            parser.close()
            expectedresult="""<?xml version="1.0" encoding="utf-8"?>
<a:greetings xmlns:a="http://example.com/ns">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
            outfp.seek(0)
            self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
            outfp.close()

    def test_myxml(self):
        """ Test that my patch works """
        outfp = tempfile.TemporaryFile()
        c = MyGen(outfp,'utf-8')
        parser = xml.sax.make_parser()
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        parser.setContentHandler(c)
        testcontent="""<?xml version="1.0"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        parser.feed(testcontent)
        parser.close()
        outfp.seek(0)
        expectedresult = """<?xml version="1.0" encoding="utf-8"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
        outfp.close()

    def test_myxml_wo_xml(self):
        """ Test that my patch understands the missing xml namespace """
        outfp = tempfile.TemporaryFile()
        c = MyGen(outfp,'utf-8')
        parser = xml.sax.make_parser()
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        parser.setContentHandler(c)
        testcontent="""<?xml version="1.0"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        parser.feed(testcontent)
        parser.close()
        outfp.seek(0)
        expectedresult = """<?xml version="1.0" encoding="utf-8"?>
<a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <a:greet xml:lang="en">Hello world</a:greet>
</a:greetings>"""
        self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
        outfp.close()

if __name__ == '__main__':
    unittest.main()