1
2
3 """
4 Test cases related to SAX I/O
5 """
6
7 import unittest
8 from StringIO import StringIO
9
10 from common_imports import HelperTestCase, doctest
11 from lxml import sax
12 from xml.dom import pulldom
13
15
17 tree = self.parse('<a>ab<b/>ba</a>')
18 xml_out = self._saxify_serialize(tree)
19 self.assertEquals('<a>ab<b/>ba</a>',
20 xml_out)
21
23 tree = self.parse('<a>ab<b>bb</b>ba</a>')
24 xml_out = self._saxify_serialize(tree)
25 self.assertEquals('<a>ab<b>bb</b>ba</a>',
26 xml_out)
27
33
35 tree = self.parse('<a>ab<?this and that?>ba</a>')
36 xml_out = self._saxify_serialize(tree)
37 self.assertEquals('<a>ab<?this and that?>ba</a>',
38 xml_out)
39
45
47 tree = self.parse('<?this and that?><a>ab</a>')
48 xml_out = self._saxify_serialize(tree)
49 self.assertEquals('<?this and that?><a>ab</a>',
50 xml_out)
51
53 tree = self.parse('<a aa="5">ab<b b="5"/>ba</a>')
54 xml_out = self._saxify_serialize(tree)
55 self.assertEquals('<a aa="5">ab<b b="5"/>ba</a>',
56 xml_out)
57
59 tree = self.parse('<a xmlns="bla">ab<b>bb</b>ba</a>')
60 new_tree = self._saxify_unsaxify(tree)
61 root = new_tree.getroot()
62 self.assertEqual('{bla}a',
63 root.tag)
64 self.assertEqual('{bla}b',
65 root[0].tag)
66
68 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
69 new_tree = self._saxify_unsaxify(tree)
70 root = new_tree.getroot()
71 self.assertEqual('{blaA}a',
72 root.tag)
73 self.assertEqual('{blaB}b',
74 root[0].tag)
75
77 tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
78 handler = pulldom.SAX2DOM()
79 sax.saxify(tree, handler)
80 dom = handler.document
81
82 self.assertEqual('a',
83 dom.firstChild.localName)
84 self.assertEqual('blaA',
85 dom.firstChild.namespaceURI)
86
87 children = dom.firstChild.childNodes
88 self.assertEqual('ab',
89 children[0].nodeValue)
90 self.assertEqual('blaB',
91 children[1].namespaceURI)
92 self.assertEqual('ba',
93 children[2].nodeValue)
94
96 tree = self.parse('<a><b/></a>')
97 a = tree.getroot()
98 b = a[0]
99
100 xml_out = self._saxify_serialize(a)
101 self.assertEquals('<a><b/></a>',
102 xml_out)
103
104 xml_out = self._saxify_serialize(b)
105 self.assertEquals('<b/>',
106 xml_out)
107
109 tree = self.parse('<a:a xmlns:a="blaA"><b/></a:a>')
110 a = tree.getroot()
111 b = a[0]
112
113 new_tree = self._saxify_unsaxify(a)
114 root = new_tree.getroot()
115 self.assertEqual('{blaA}a',
116 root.tag)
117 self.assertEqual('b',
118 root[0].tag)
119
120 new_tree = self._saxify_unsaxify(b)
121 root = new_tree.getroot()
122 self.assertEqual('b',
123 root.tag)
124 self.assertEqual(0,
125 len(root))
126
128 handler = sax.ElementTreeContentHandler()
129 handler.startDocument()
130 handler.startPrefixMapping(None, 'blaA')
131 handler.startElementNS(('blaA', 'a'), 'a', {})
132 handler.startPrefixMapping(None, 'blaB')
133 handler.startElementNS(('blaB', 'b'), 'b', {})
134 handler.endElementNS( ('blaB', 'b'), 'b')
135 handler.endPrefixMapping(None)
136 handler.startElementNS(('blaA', 'c'), 'c', {})
137 handler.endElementNS( ('blaA', 'c'), 'c')
138 handler.endElementNS( ('blaA', 'a'), 'a')
139 handler.endPrefixMapping(None)
140 handler.endDocument()
141
142 new_tree = handler.etree
143 root = new_tree.getroot()
144 self.assertEqual('{blaA}a',
145 root.tag)
146 self.assertEqual('{blaB}b',
147 root[0].tag)
148 self.assertEqual('{blaA}c',
149 root[1].tag)
150
152 handler = sax.ElementTreeContentHandler()
153 handler.startDocument()
154 handler.startPrefixMapping('ns', 'blaA')
155 handler.startElementNS(('blaA', 'a'), 'ns:a', {})
156 handler.startPrefixMapping('ns', 'blaB')
157 handler.startElementNS(('blaB', 'b'), 'ns:b', {})
158 handler.endElementNS( ('blaB', 'b'), 'ns:b')
159 handler.endPrefixMapping('ns')
160 handler.startElementNS(('blaA', 'c'), 'ns:c', {})
161 handler.endElementNS( ('blaA', 'c'), 'ns:c')
162 handler.endElementNS( ('blaA', 'a'), 'ns:a')
163 handler.endPrefixMapping('ns')
164 handler.endDocument()
165
166 new_tree = handler.etree
167 root = new_tree.getroot()
168 self.assertEqual('{blaA}a',
169 root.tag)
170 self.assertEqual('{blaB}b',
171 root[0].tag)
172 self.assertEqual('{blaA}c',
173 root[1].tag)
174
191
197
204
209
211 new_tree = self._saxify_unsaxify(tree)
212 f = StringIO()
213 new_tree.write(f)
214 return f.getvalue()
215
216
218 suite = unittest.TestSuite()
219 suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
220 suite.addTests(
221 [doctest.DocFileSuite('../../../doc/sax.txt')])
222 return suite
223
224 if __name__ == '__main__':
225 unittest.main()
226