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, make_doctest, BytesIO, _bytes
14 from lxml import sax
15 from xml.dom import pulldom
16 from xml.sax.handler import ContentHandler
17
18
20
22 tree = self.parse('<a>ab<b/>ba</a>')
23 xml_out = self._saxify_serialize(tree)
24 self.assertEqual(_bytes('<a>ab<b/>ba</a>'),
25 xml_out)
26
28 tree = self.parse('<a>ab<b>bb</b>ba</a>')
29 xml_out = self._saxify_serialize(tree)
30 self.assertEqual(_bytes('<a>ab<b>bb</b>ba</a>'),
31 xml_out)
32
38
40 tree = self.parse('<a>ab<?this and that?>ba</a>')
41 xml_out = self._saxify_serialize(tree)
42 self.assertEqual(_bytes('<a>ab<?this and that?>ba</a>'),
43 xml_out)
44
50
52 tree = self.parse('<?this and that?><a>ab</a>')
53 xml_out = self._saxify_serialize(tree)
54 self.assertEqual(_bytes('<?this and that?><a>ab</a>'),
55 xml_out)
56
58 tree = self.parse('<a aa="5">ab<b b="5"/>ba</a>')
59 xml_out = self._saxify_serialize(tree)
60 self.assertEqual(_bytes('<a aa="5">ab<b b="5"/>ba</a>'),
61 xml_out)
62
71
73 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
74 new_tree = self._saxify_unsaxify(tree)
75 root = new_tree.getroot()
76 self.assertEqual('{blaA}a',
77 root.tag)
78 self.assertEqual('{blaB}b',
79 root[0].tag)
80
82 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
83 handler = pulldom.SAX2DOM()
84 sax.saxify(tree, handler)
85 dom = handler.document
86
87 self.assertEqual('a',
88 dom.firstChild.localName)
89 self.assertEqual('blaA',
90 dom.firstChild.namespaceURI)
91 self.assertEqual(None,
92 dom.firstChild.prefix)
93
94 children = dom.firstChild.childNodes
95 self.assertEqual('ab',
96 children[0].nodeValue)
97 self.assertEqual('blaB',
98 children[1].namespaceURI)
99 self.assertEqual('ba',
100 children[2].nodeValue)
101
103 tree = self.parse('<a xmlns="blaA" xmlns:a="blaA"></a>')
104 handler = pulldom.SAX2DOM()
105 sax.saxify(tree, handler)
106 dom = handler.document
107
108
109
110 self.assertEqual('a',
111 dom.firstChild.localName)
112 self.assertEqual('blaA',
113 dom.firstChild.namespaceURI)
114 self.assertEqual(None,
115 dom.firstChild.prefix)
116
117 tree = self.parse('<a:a xmlns="blaA" xmlns:a="blaA"></a:a>')
118 handler = pulldom.SAX2DOM()
119 sax.saxify(tree, handler)
120 dom = handler.document
121
122 self.assertEqual('a',
123 dom.firstChild.localName)
124 self.assertEqual('blaA',
125 dom.firstChild.namespaceURI)
126 self.assertEqual('a',
127 dom.firstChild.prefix)
128
141
160
162 handler = sax.ElementTreeContentHandler()
163 handler.startDocument()
164 handler.startPrefixMapping(None, 'blaA')
165 handler.startElementNS(('blaA', 'a'), 'a', {})
166 handler.startPrefixMapping(None, 'blaB')
167 handler.startElementNS(('blaB', 'b'), 'b', {})
168 handler.endElementNS( ('blaB', 'b'), 'b')
169 handler.endPrefixMapping(None)
170 handler.startElementNS(('blaA', 'c'), 'c', {})
171 handler.endElementNS( ('blaA', 'c'), 'c')
172 handler.endElementNS( ('blaA', 'a'), 'a')
173 handler.endPrefixMapping(None)
174 handler.endDocument()
175
176 new_tree = handler.etree
177 root = new_tree.getroot()
178 self.assertEqual('{blaA}a',
179 root.tag)
180 self.assertEqual('{blaB}b',
181 root[0].tag)
182 self.assertEqual('{blaA}c',
183 root[1].tag)
184
186 handler = sax.ElementTreeContentHandler()
187 handler.startDocument()
188 handler.startPrefixMapping(None, 'blaA')
189 handler.startElementNS((None, 'a'), 'a', {})
190 handler.startPrefixMapping(None, 'blaB')
191 handler.startElementNS((None, 'b'), 'b', {})
192 handler.endElementNS( (None, 'b'), 'b')
193 handler.endPrefixMapping(None)
194 handler.startElementNS((None, 'c'), 'c', {})
195 handler.endElementNS( (None, 'c'), 'c')
196 handler.endElementNS( (None, 'a'), 'a')
197 handler.endPrefixMapping(None)
198 handler.endDocument()
199
200 new_tree = handler.etree
201 root = new_tree.getroot()
202 self.assertEqual('{blaA}a',
203 root.tag)
204 self.assertEqual('{blaB}b',
205 root[0].tag)
206 self.assertEqual('{blaA}c',
207 root[1].tag)
208
210 handler = sax.ElementTreeContentHandler()
211 handler.startDocument()
212 handler.startPrefixMapping('ns', 'blaA')
213 handler.startElementNS(('blaA', 'a'), 'ns:a', {})
214 handler.startPrefixMapping('ns', 'blaB')
215 handler.startElementNS(('blaB', 'b'), 'ns:b', {})
216 handler.endElementNS( ('blaB', 'b'), 'ns:b')
217 handler.endPrefixMapping('ns')
218 handler.startElementNS(('blaA', 'c'), 'ns:c', {})
219 handler.endElementNS( ('blaA', 'c'), 'ns:c')
220 handler.endElementNS( ('blaA', 'a'), 'ns:a')
221 handler.endPrefixMapping('ns')
222 handler.endDocument()
223
224 new_tree = handler.etree
225 root = new_tree.getroot()
226 self.assertEqual('{blaA}a',
227 root.tag)
228 self.assertEqual('{blaB}b',
229 root[0].tag)
230 self.assertEqual('{blaA}c',
231 root[1].tag)
232
249
265
274
280
287
292
298
299
300 -class SimpleContentHandler(ContentHandler, object):
301 """A SAX content handler that just stores the events"""
302
303 - def __init__(self):
304 self.sax_events = []
305 super(SimpleContentHandler, self).__init__()
306
307 - def startDocument(self):
308 self.sax_events.append(('startDocument',))
309
310 - def endDocument(self):
311 self.sax_events.append(('endDocument',))
312
313 - def startPrefixMapping(self, prefix, uri):
314 self.sax_events.append(('startPrefixMapping', prefix, uri))
315
316 - def endPrefixMapping(self, prefix):
317 self.sax_events.append(('endPrefixMapping', prefix))
318
319 - def startElement(self, name, attrs):
320 self.sax_events.append(('startElement', name, dict(attrs)))
321
322 - def endElement(self, name):
323 self.sax_events.append(('endElement', name))
324
325 - def startElementNS(self, name, qname, attrs):
326 self.sax_events.append(('startElementNS', name, qname, attrs._qnames))
327
328 - def endElementNS(self, name, qname):
329 self.sax_events.append(('endElementNS', name, qname))
330
331 - def characters(self, content):
332 self.sax_events.append(('characters', content))
333
334 - def ignorableWhitespace(self, whitespace):
335 self.sax_events.append(('ignorableWhitespace', whitespace))
336
337 - def processingInstruction(self, target, data):
338 self.sax_events.append(('processingInstruction', target, data))
339
340 - def skippedEntity(self, name):
341 self.sax_events.append(('skippedEntity', name))
342
343
345 """Testing that namespaces generate the right SAX events"""
346
351
353
354 tree = self.parse('<a:a xmlns:a="blaA" xmlns:c="blaC">'
355 '<d a:attr="value" c:attr="value" /></a:a>')
356 a = tree.getroot()
357
358 self.assertEqual(
359 [('startElementNS', ('blaA', 'a'), 'a:a', {}),
360 ('startElementNS', (None, 'd'), 'd',
361 {('blaA', 'attr'): 'a:attr', ('blaC', 'attr'): 'c:attr'}),
362 ('endElementNS', (None, 'd'), 'd'),
363 ('endElementNS', ('blaA', 'a'), 'a:a'),
364 ],
365 self._saxify(a)[3:7])
366
368
369 tree = self.parse('<a xmlns="blaA"><b attr="value" /></a>')
370 a = tree.getroot()
371
372 self.assertEqual(
373 [('startDocument',),
374
375 ('startPrefixMapping', None, 'blaA'),
376 ('startElementNS', ('blaA', 'a'), 'a', {}),
377
378 ('startElementNS', ('blaA', 'b'), 'b', {(None, 'attr'): 'attr'}),
379 ('endElementNS', ('blaA', 'b'), 'b'),
380 ('endElementNS', ('blaA', 'a'), 'a'),
381
382 ('endPrefixMapping', None),
383 ('endDocument',)],
384 self._saxify(a))
385
386
387
388 tree = self.parse('<a xmlns="bla" xmlns:a="bla">'
389 '<b a:attr="value" /></a>')
390 a = tree.getroot()
391
392 self.assertEqual(
393 ('startElementNS', ('bla', 'b'), 'b', {('bla', 'attr'): 'a:attr'}),
394 self._saxify(a)[4])
395
397
398 tree = self.parse('<a xmlns:b="bla" xmlns:c="bla">'
399 '<d c:attr="attr" /></a>')
400 a = tree.getroot()
401
402 self.assertEqual(
403
404 ('startElementNS', (None, 'd'), 'd', {('bla', 'attr'): 'b:attr'}),
405 self._saxify(a)[4])
406
407
415
416
417 if __name__ == '__main__':
418 print('to test use test.py %s' % __file__)
419