1
2
3 """
4 Test cases related to SAX I/O
5 """
6
7 from __future__ import absolute_import
8
9 import unittest
10 from xml.dom import pulldom
11 from xml.sax.handler import ContentHandler
12
13 from .common_imports import HelperTestCase, make_doctest, BytesIO, _bytes
14 from lxml import sax
15
16
18
20 tree = self.parse('<a>ab<b/>ba</a>')
21 xml_out = self._saxify_serialize(tree)
22 self.assertEqual(_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.assertEqual(_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.assertEqual(_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.assertEqual(_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.assertEqual(_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 self.assertEqual(None,
90 dom.firstChild.prefix)
91
92 children = dom.firstChild.childNodes
93 self.assertEqual('ab',
94 children[0].nodeValue)
95 self.assertEqual('blaB',
96 children[1].namespaceURI)
97 self.assertEqual('ba',
98 children[2].nodeValue)
99
101 tree = self.parse('<a xmlns="blaA" xmlns:a="blaA"></a>')
102 handler = pulldom.SAX2DOM()
103 sax.saxify(tree, handler)
104 dom = handler.document
105
106
107
108 self.assertEqual('a',
109 dom.firstChild.localName)
110 self.assertEqual('blaA',
111 dom.firstChild.namespaceURI)
112 self.assertEqual(None,
113 dom.firstChild.prefix)
114
115 tree = self.parse('<a:a xmlns="blaA" xmlns:a="blaA"></a:a>')
116 handler = pulldom.SAX2DOM()
117 sax.saxify(tree, handler)
118 dom = handler.document
119
120 self.assertEqual('a',
121 dom.firstChild.localName)
122 self.assertEqual('blaA',
123 dom.firstChild.namespaceURI)
124 self.assertEqual('a',
125 dom.firstChild.prefix)
126
139
158
160 handler = sax.ElementTreeContentHandler()
161 handler.startDocument()
162 handler.startPrefixMapping(None, 'blaA')
163 handler.startElementNS(('blaA', 'a'), 'a', {})
164 handler.startPrefixMapping(None, 'blaB')
165 handler.startElementNS(('blaB', 'b'), 'b', {})
166 handler.endElementNS( ('blaB', 'b'), 'b')
167 handler.endPrefixMapping(None)
168 handler.startElementNS(('blaA', 'c'), 'c', {})
169 handler.endElementNS( ('blaA', 'c'), 'c')
170 handler.endElementNS( ('blaA', 'a'), 'a')
171 handler.endPrefixMapping(None)
172 handler.endDocument()
173
174 new_tree = handler.etree
175 root = new_tree.getroot()
176 self.assertEqual('{blaA}a',
177 root.tag)
178 self.assertEqual('{blaB}b',
179 root[0].tag)
180 self.assertEqual('{blaA}c',
181 root[1].tag)
182
184 handler = sax.ElementTreeContentHandler()
185 handler.startDocument()
186 handler.startPrefixMapping(None, 'blaA')
187 handler.startElementNS((None, 'a'), 'a', {})
188 handler.startPrefixMapping(None, 'blaB')
189 handler.startElementNS((None, 'b'), 'b', {})
190 handler.endElementNS( (None, 'b'), 'b')
191 handler.endPrefixMapping(None)
192 handler.startElementNS((None, 'c'), 'c', {})
193 handler.endElementNS( (None, 'c'), 'c')
194 handler.endElementNS( (None, 'a'), 'a')
195 handler.endPrefixMapping(None)
196 handler.endDocument()
197
198 new_tree = handler.etree
199 root = new_tree.getroot()
200 self.assertEqual('{blaA}a',
201 root.tag)
202 self.assertEqual('{blaB}b',
203 root[0].tag)
204 self.assertEqual('{blaA}c',
205 root[1].tag)
206
208 handler = sax.ElementTreeContentHandler()
209 handler.startDocument()
210 handler.startPrefixMapping('ns', 'blaA')
211 handler.startElementNS(('blaA', 'a'), 'ns:a', {})
212 handler.startPrefixMapping('ns', 'blaB')
213 handler.startElementNS(('blaB', 'b'), 'ns:b', {})
214 handler.endElementNS( ('blaB', 'b'), 'ns:b')
215 handler.endPrefixMapping('ns')
216 handler.startElementNS(('blaA', 'c'), 'ns:c', {})
217 handler.endElementNS( ('blaA', 'c'), 'ns:c')
218 handler.endElementNS( ('blaA', 'a'), 'ns:a')
219 handler.endPrefixMapping('ns')
220 handler.endDocument()
221
222 new_tree = handler.etree
223 root = new_tree.getroot()
224 self.assertEqual('{blaA}a',
225 root.tag)
226 self.assertEqual('{blaB}b',
227 root[0].tag)
228 self.assertEqual('{blaA}c',
229 root[1].tag)
230
247
263
272
278
285
290
296
297
298 -class SimpleContentHandler(ContentHandler, object):
299 """A SAX content handler that just stores the events"""
300
301 - def __init__(self):
302 self.sax_events = []
303 super(SimpleContentHandler, self).__init__()
304
305 - def startDocument(self):
306 self.sax_events.append(('startDocument',))
307
308 - def endDocument(self):
309 self.sax_events.append(('endDocument',))
310
311 - def startPrefixMapping(self, prefix, uri):
312 self.sax_events.append(('startPrefixMapping', prefix, uri))
313
314 - def endPrefixMapping(self, prefix):
315 self.sax_events.append(('endPrefixMapping', prefix))
316
317 - def startElement(self, name, attrs):
318 self.sax_events.append(('startElement', name, dict(attrs)))
319
320 - def endElement(self, name):
321 self.sax_events.append(('endElement', name))
322
323 - def startElementNS(self, name, qname, attrs):
324 self.sax_events.append(('startElementNS', name, qname, attrs._qnames))
325
326 - def endElementNS(self, name, qname):
327 self.sax_events.append(('endElementNS', name, qname))
328
329 - def characters(self, content):
330 self.sax_events.append(('characters', content))
331
332 - def ignorableWhitespace(self, whitespace):
333 self.sax_events.append(('ignorableWhitespace', whitespace))
334
335 - def processingInstruction(self, target, data):
336 self.sax_events.append(('processingInstruction', target, data))
337
338 - def skippedEntity(self, name):
339 self.sax_events.append(('skippedEntity', name))
340
341
343 """Testing that namespaces generate the right SAX events"""
344
349
351
352 tree = self.parse('<a:a xmlns:a="blaA" xmlns:c="blaC">'
353 '<d a:attr="value" c:attr="value" /></a:a>')
354 a = tree.getroot()
355
356 self.assertEqual(
357 [('startElementNS', ('blaA', 'a'), 'a:a', {}),
358 ('startElementNS', (None, 'd'), 'd',
359 {('blaA', 'attr'): 'a:attr', ('blaC', 'attr'): 'c:attr'}),
360 ('endElementNS', (None, 'd'), 'd'),
361 ('endElementNS', ('blaA', 'a'), 'a:a'),
362 ],
363 self._saxify(a)[3:7])
364
366
367 tree = self.parse('<a xmlns="blaA"><b attr="value" /></a>')
368 a = tree.getroot()
369
370 self.assertEqual(
371 [('startDocument',),
372
373 ('startPrefixMapping', None, 'blaA'),
374 ('startElementNS', ('blaA', 'a'), 'a', {}),
375
376 ('startElementNS', ('blaA', 'b'), 'b', {(None, 'attr'): 'attr'}),
377 ('endElementNS', ('blaA', 'b'), 'b'),
378 ('endElementNS', ('blaA', 'a'), 'a'),
379
380 ('endPrefixMapping', None),
381 ('endDocument',)],
382 self._saxify(a))
383
384
385
386 tree = self.parse('<a xmlns="bla" xmlns:a="bla">'
387 '<b a:attr="value" /></a>')
388 a = tree.getroot()
389
390 self.assertEqual(
391 ('startElementNS', ('bla', 'b'), 'b', {('bla', 'attr'): 'a:attr'}),
392 self._saxify(a)[4])
393
395
396 tree = self.parse('<a xmlns:b="bla" xmlns:c="bla">'
397 '<d c:attr="attr" /></a>')
398 a = tree.getroot()
399
400 self.assertEqual(
401
402 ('startElementNS', (None, 'd'), 'd', {('bla', 'attr'): 'b:attr'}),
403 self._saxify(a)[4])
404
405
413
414
415 if __name__ == '__main__':
416 print('to test use test.py %s' % __file__)
417