1
2
3 """
4 Test cases related to SAX I/O
5 """
6
7 import unittest, sys, os.path
8
9 this_dir = os.path.dirname(__file__)
10 if this_dir not in sys.path:
11 sys.path.insert(0, this_dir)
12
13 from common_imports import HelperTestCase, doctest, make_doctest, BytesIO, _bytes
14 from lxml import sax
15 from xml.dom import pulldom
16
18
20 tree = self.parse('<a>ab<b/>ba</a>')
21 xml_out = self._saxify_serialize(tree)
22 self.assertEquals(_bytes('<a>ab<b/>ba</a>'),
23 xml_out)
24
26 tree = self.parse('<a>ab<b>bb</b>ba</a>')
27 xml_out = self._saxify_serialize(tree)
28 self.assertEquals(_bytes('<a>ab<b>bb</b>ba</a>'),
29 xml_out)
30
36
38 tree = self.parse('<a>ab<?this and that?>ba</a>')
39 xml_out = self._saxify_serialize(tree)
40 self.assertEquals(_bytes('<a>ab<?this and that?>ba</a>'),
41 xml_out)
42
48
50 tree = self.parse('<?this and that?><a>ab</a>')
51 xml_out = self._saxify_serialize(tree)
52 self.assertEquals(_bytes('<?this and that?><a>ab</a>'),
53 xml_out)
54
56 tree = self.parse('<a aa="5">ab<b b="5"/>ba</a>')
57 xml_out = self._saxify_serialize(tree)
58 self.assertEquals(_bytes('<a aa="5">ab<b b="5"/>ba</a>'),
59 xml_out)
60
69
71 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
72 new_tree = self._saxify_unsaxify(tree)
73 root = new_tree.getroot()
74 self.assertEqual('{blaA}a',
75 root.tag)
76 self.assertEqual('{blaB}b',
77 root[0].tag)
78
80 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
81 handler = pulldom.SAX2DOM()
82 sax.saxify(tree, handler)
83 dom = handler.document
84
85 self.assertEqual('a',
86 dom.firstChild.localName)
87 self.assertEqual('blaA',
88 dom.firstChild.namespaceURI)
89
90 children = dom.firstChild.childNodes
91 self.assertEqual('ab',
92 children[0].nodeValue)
93 self.assertEqual('blaB',
94 children[1].namespaceURI)
95 self.assertEqual('ba',
96 children[2].nodeValue)
97
110
129
131 handler = sax.ElementTreeContentHandler()
132 handler.startDocument()
133 handler.startPrefixMapping(None, 'blaA')
134 handler.startElementNS(('blaA', 'a'), 'a', {})
135 handler.startPrefixMapping(None, 'blaB')
136 handler.startElementNS(('blaB', 'b'), 'b', {})
137 handler.endElementNS( ('blaB', 'b'), 'b')
138 handler.endPrefixMapping(None)
139 handler.startElementNS(('blaA', 'c'), 'c', {})
140 handler.endElementNS( ('blaA', 'c'), 'c')
141 handler.endElementNS( ('blaA', 'a'), 'a')
142 handler.endPrefixMapping(None)
143 handler.endDocument()
144
145 new_tree = handler.etree
146 root = new_tree.getroot()
147 self.assertEqual('{blaA}a',
148 root.tag)
149 self.assertEqual('{blaB}b',
150 root[0].tag)
151 self.assertEqual('{blaA}c',
152 root[1].tag)
153
155 handler = sax.ElementTreeContentHandler()
156 handler.startDocument()
157 handler.startPrefixMapping(None, 'blaA')
158 handler.startElementNS((None, 'a'), 'a', {})
159 handler.startPrefixMapping(None, 'blaB')
160 handler.startElementNS((None, 'b'), 'b', {})
161 handler.endElementNS( (None, 'b'), 'b')
162 handler.endPrefixMapping(None)
163 handler.startElementNS((None, 'c'), 'c', {})
164 handler.endElementNS( (None, 'c'), 'c')
165 handler.endElementNS( (None, 'a'), 'a')
166 handler.endPrefixMapping(None)
167 handler.endDocument()
168
169 new_tree = handler.etree
170 root = new_tree.getroot()
171 self.assertEqual('{blaA}a',
172 root.tag)
173 self.assertEqual('{blaB}b',
174 root[0].tag)
175 self.assertEqual('{blaA}c',
176 root[1].tag)
177
179 handler = sax.ElementTreeContentHandler()
180 handler.startDocument()
181 handler.startPrefixMapping('ns', 'blaA')
182 handler.startElementNS(('blaA', 'a'), 'ns:a', {})
183 handler.startPrefixMapping('ns', 'blaB')
184 handler.startElementNS(('blaB', 'b'), 'ns:b', {})
185 handler.endElementNS( ('blaB', 'b'), 'ns:b')
186 handler.endPrefixMapping('ns')
187 handler.startElementNS(('blaA', 'c'), 'ns:c', {})
188 handler.endElementNS( ('blaA', 'c'), 'ns:c')
189 handler.endElementNS( ('blaA', 'a'), 'ns:a')
190 handler.endPrefixMapping('ns')
191 handler.endDocument()
192
193 new_tree = handler.etree
194 root = new_tree.getroot()
195 self.assertEqual('{blaA}a',
196 root.tag)
197 self.assertEqual('{blaB}b',
198 root[0].tag)
199 self.assertEqual('{blaA}c',
200 root[1].tag)
201
218
224
231
236
242
243
245 suite = unittest.TestSuite()
246 suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
247 suite.addTests(
248 [make_doctest('../../../doc/sax.txt')])
249 return suite
250
251 if __name__ == '__main__':
252 print('to test use test.py %s' % __file__)
253