1 | #!/usr/bin/python -u
|
---|
2 | # -*- coding: ISO-8859-1 -*-
|
---|
3 | #
|
---|
4 | # this tests the basic APIs of the XmlTextReader interface
|
---|
5 | #
|
---|
6 | import libxml2
|
---|
7 | import sys
|
---|
8 | try:
|
---|
9 | import StringIO
|
---|
10 | str_io = StringIO.StringIO
|
---|
11 | except:
|
---|
12 | import io
|
---|
13 | str_io = io.StringIO
|
---|
14 |
|
---|
15 | # Memory debug specific
|
---|
16 | libxml2.debugMemory(1)
|
---|
17 |
|
---|
18 | f = str_io("""<a><b b1="b1"/><c>content of c</c></a>""")
|
---|
19 | input = libxml2.inputBuffer(f)
|
---|
20 | reader = input.newTextReader("test1")
|
---|
21 | ret = reader.Read()
|
---|
22 | if ret != 1:
|
---|
23 | print("test1: Error reading to first element")
|
---|
24 | sys.exit(1)
|
---|
25 | if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
|
---|
26 | reader.NodeType() != 1 or reader.HasAttributes() != 0:
|
---|
27 | print("test1: Error reading the first element")
|
---|
28 | sys.exit(1)
|
---|
29 | ret = reader.Read()
|
---|
30 | if ret != 1:
|
---|
31 | print("test1: Error reading to second element")
|
---|
32 | sys.exit(1)
|
---|
33 | if reader.Name() != "b" or reader.IsEmptyElement() != 1 or \
|
---|
34 | reader.NodeType() != 1 or reader.HasAttributes() != 1:
|
---|
35 | print("test1: Error reading the second element")
|
---|
36 | sys.exit(1)
|
---|
37 | ret = reader.Read()
|
---|
38 | if ret != 1:
|
---|
39 | print("test1: Error reading to third element")
|
---|
40 | sys.exit(1)
|
---|
41 | if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
|
---|
42 | reader.NodeType() != 1 or reader.HasAttributes() != 0:
|
---|
43 | print("test1: Error reading the third element")
|
---|
44 | sys.exit(1)
|
---|
45 | ret = reader.Read()
|
---|
46 | if ret != 1:
|
---|
47 | print("test1: Error reading to text node")
|
---|
48 | sys.exit(1)
|
---|
49 | if reader.Name() != "#text" or reader.IsEmptyElement() != 0 or \
|
---|
50 | reader.NodeType() != 3 or reader.HasAttributes() != 0 or \
|
---|
51 | reader.Value() != "content of c":
|
---|
52 | print("test1: Error reading the text node")
|
---|
53 | sys.exit(1)
|
---|
54 | ret = reader.Read()
|
---|
55 | if ret != 1:
|
---|
56 | print("test1: Error reading to end of third element")
|
---|
57 | sys.exit(1)
|
---|
58 | if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
|
---|
59 | reader.NodeType() != 15 or reader.HasAttributes() != 0:
|
---|
60 | print("test1: Error reading the end of third element")
|
---|
61 | sys.exit(1)
|
---|
62 | ret = reader.Read()
|
---|
63 | if ret != 1:
|
---|
64 | print("test1: Error reading to end of first element")
|
---|
65 | sys.exit(1)
|
---|
66 | if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
|
---|
67 | reader.NodeType() != 15 or reader.HasAttributes() != 0:
|
---|
68 | print("test1: Error reading the end of first element")
|
---|
69 | sys.exit(1)
|
---|
70 | ret = reader.Read()
|
---|
71 | if ret != 0:
|
---|
72 | print("test1: Error reading to end of document")
|
---|
73 | sys.exit(1)
|
---|
74 |
|
---|
75 | #
|
---|
76 | # example from the XmlTextReader docs
|
---|
77 | #
|
---|
78 | f = str_io("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""")
|
---|
79 | input = libxml2.inputBuffer(f)
|
---|
80 | reader = input.newTextReader("test2")
|
---|
81 |
|
---|
82 | ret = reader.Read()
|
---|
83 | if ret != 1:
|
---|
84 | print("Error reading test element")
|
---|
85 | sys.exit(1)
|
---|
86 | if reader.GetAttributeNo(0) != "urn:datatypes" or \
|
---|
87 | reader.GetAttributeNo(1) != "int" or \
|
---|
88 | reader.GetAttributeNs("type", "urn:datatypes") != "int" or \
|
---|
89 | reader.GetAttribute("dt:type") != "int":
|
---|
90 | print("error reading test attributes")
|
---|
91 | sys.exit(1)
|
---|
92 |
|
---|
93 | #
|
---|
94 | # example from the XmlTextReader docs
|
---|
95 | #
|
---|
96 | f = str_io("""<root xmlns:a="urn:456">
|
---|
97 | <item>
|
---|
98 | <ref href="a:b"/>
|
---|
99 | </item>
|
---|
100 | </root>""")
|
---|
101 | input = libxml2.inputBuffer(f)
|
---|
102 | reader = input.newTextReader("test3")
|
---|
103 |
|
---|
104 | ret = reader.Read()
|
---|
105 | while ret == 1:
|
---|
106 | if reader.Name() == "ref":
|
---|
107 | if reader.LookupNamespace("a") != "urn:456":
|
---|
108 | print("error resolving namespace prefix")
|
---|
109 | sys.exit(1)
|
---|
110 | break
|
---|
111 | ret = reader.Read()
|
---|
112 | if ret != 1:
|
---|
113 | print("Error finding the ref element")
|
---|
114 | sys.exit(1)
|
---|
115 |
|
---|
116 | #
|
---|
117 | # Home made example for the various attribute access functions
|
---|
118 | #
|
---|
119 | f = str_io("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""")
|
---|
120 | input = libxml2.inputBuffer(f)
|
---|
121 | reader = input.newTextReader("test4")
|
---|
122 | ret = reader.Read()
|
---|
123 | if ret != 1:
|
---|
124 | print("Error reading the testattr element")
|
---|
125 | sys.exit(1)
|
---|
126 | #
|
---|
127 | # Attribute exploration by index
|
---|
128 | #
|
---|
129 | if reader.MoveToAttributeNo(0) != 1:
|
---|
130 | print("Failed moveToAttribute(0)")
|
---|
131 | sys.exit(1)
|
---|
132 | if reader.Value() != "urn:1":
|
---|
133 | print("Failed to read attribute(0)")
|
---|
134 | sys.exit(1)
|
---|
135 | if reader.Name() != "xmlns":
|
---|
136 | print("Failed to read attribute(0) name")
|
---|
137 | sys.exit(1)
|
---|
138 | if reader.MoveToAttributeNo(1) != 1:
|
---|
139 | print("Failed moveToAttribute(1)")
|
---|
140 | sys.exit(1)
|
---|
141 | if reader.Value() != "urn:2":
|
---|
142 | print("Failed to read attribute(1)")
|
---|
143 | sys.exit(1)
|
---|
144 | if reader.Name() != "xmlns:a":
|
---|
145 | print("Failed to read attribute(1) name")
|
---|
146 | sys.exit(1)
|
---|
147 | if reader.MoveToAttributeNo(2) != 1:
|
---|
148 | print("Failed moveToAttribute(2)")
|
---|
149 | sys.exit(1)
|
---|
150 | if reader.Value() != "b":
|
---|
151 | print("Failed to read attribute(2)")
|
---|
152 | sys.exit(1)
|
---|
153 | if reader.Name() != "b":
|
---|
154 | print("Failed to read attribute(2) name")
|
---|
155 | sys.exit(1)
|
---|
156 | if reader.MoveToAttributeNo(3) != 1:
|
---|
157 | print("Failed moveToAttribute(3)")
|
---|
158 | sys.exit(1)
|
---|
159 | if reader.Value() != "a:b":
|
---|
160 | print("Failed to read attribute(3)")
|
---|
161 | sys.exit(1)
|
---|
162 | if reader.Name() != "a:b":
|
---|
163 | print("Failed to read attribute(3) name")
|
---|
164 | sys.exit(1)
|
---|
165 | #
|
---|
166 | # Attribute exploration by name
|
---|
167 | #
|
---|
168 | if reader.MoveToAttribute("xmlns") != 1:
|
---|
169 | print("Failed moveToAttribute('xmlns')")
|
---|
170 | sys.exit(1)
|
---|
171 | if reader.Value() != "urn:1":
|
---|
172 | print("Failed to read attribute('xmlns')")
|
---|
173 | sys.exit(1)
|
---|
174 | if reader.MoveToAttribute("xmlns:a") != 1:
|
---|
175 | print("Failed moveToAttribute('xmlns')")
|
---|
176 | sys.exit(1)
|
---|
177 | if reader.Value() != "urn:2":
|
---|
178 | print("Failed to read attribute('xmlns:a')")
|
---|
179 | sys.exit(1)
|
---|
180 | if reader.MoveToAttribute("b") != 1:
|
---|
181 | print("Failed moveToAttribute('b')")
|
---|
182 | sys.exit(1)
|
---|
183 | if reader.Value() != "b":
|
---|
184 | print("Failed to read attribute('b')")
|
---|
185 | sys.exit(1)
|
---|
186 | if reader.MoveToAttribute("a:b") != 1:
|
---|
187 | print("Failed moveToAttribute('a:b')")
|
---|
188 | sys.exit(1)
|
---|
189 | if reader.Value() != "a:b":
|
---|
190 | print("Failed to read attribute('a:b')")
|
---|
191 | sys.exit(1)
|
---|
192 | if reader.MoveToAttributeNs("b", "urn:2") != 1:
|
---|
193 | print("Failed moveToAttribute('b', 'urn:2')")
|
---|
194 | sys.exit(1)
|
---|
195 | if reader.Value() != "a:b":
|
---|
196 | print("Failed to read attribute('b', 'urn:2')")
|
---|
197 | sys.exit(1)
|
---|
198 | #
|
---|
199 | # Go back and read in sequence
|
---|
200 | #
|
---|
201 | if reader.MoveToElement() != 1:
|
---|
202 | print("Failed to move back to element")
|
---|
203 | sys.exit(1)
|
---|
204 | if reader.MoveToFirstAttribute() != 1:
|
---|
205 | print("Failed to move to first attribute")
|
---|
206 | sys.exit(1)
|
---|
207 | if reader.Value() != "urn:1":
|
---|
208 | print("Failed to read attribute(0)")
|
---|
209 | sys.exit(1)
|
---|
210 | if reader.Name() != "xmlns":
|
---|
211 | print("Failed to read attribute(0) name")
|
---|
212 | sys.exit(1)
|
---|
213 | if reader.MoveToNextAttribute() != 1:
|
---|
214 | print("Failed to move to next attribute")
|
---|
215 | sys.exit(1)
|
---|
216 | if reader.Value() != "urn:2":
|
---|
217 | print("Failed to read attribute(1)")
|
---|
218 | sys.exit(1)
|
---|
219 | if reader.Name() != "xmlns:a":
|
---|
220 | print("Failed to read attribute(1) name")
|
---|
221 | sys.exit(1)
|
---|
222 | if reader.MoveToNextAttribute() != 1:
|
---|
223 | print("Failed to move to next attribute")
|
---|
224 | sys.exit(1)
|
---|
225 | if reader.Value() != "b":
|
---|
226 | print("Failed to read attribute(2)")
|
---|
227 | sys.exit(1)
|
---|
228 | if reader.Name() != "b":
|
---|
229 | print("Failed to read attribute(2) name")
|
---|
230 | sys.exit(1)
|
---|
231 | if reader.MoveToNextAttribute() != 1:
|
---|
232 | print("Failed to move to next attribute")
|
---|
233 | sys.exit(1)
|
---|
234 | if reader.Value() != "a:b":
|
---|
235 | print("Failed to read attribute(3)")
|
---|
236 | sys.exit(1)
|
---|
237 | if reader.Name() != "a:b":
|
---|
238 | print("Failed to read attribute(3) name")
|
---|
239 | sys.exit(1)
|
---|
240 | if reader.MoveToNextAttribute() != 0:
|
---|
241 | print("Failed to detect last attribute")
|
---|
242 | sys.exit(1)
|
---|
243 |
|
---|
244 |
|
---|
245 | #
|
---|
246 | # a couple of tests for namespace nodes
|
---|
247 | #
|
---|
248 | f = str_io("""<a xmlns="http://example.com/foo"/>""")
|
---|
249 | input = libxml2.inputBuffer(f)
|
---|
250 | reader = input.newTextReader("test6")
|
---|
251 | ret = reader.Read()
|
---|
252 | if ret != 1:
|
---|
253 | print("test6: failed to Read()")
|
---|
254 | sys.exit(1)
|
---|
255 | ret = reader.MoveToFirstAttribute()
|
---|
256 | if ret != 1:
|
---|
257 | print("test6: failed to MoveToFirstAttribute()")
|
---|
258 | sys.exit(1)
|
---|
259 | if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
|
---|
260 | reader.LocalName() != "xmlns" or reader.Name() != "xmlns" or \
|
---|
261 | reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
|
---|
262 | print("test6: failed to read the namespace node")
|
---|
263 | sys.exit(1)
|
---|
264 |
|
---|
265 | f = str_io("""<a xmlns:prefix="http://example.com/foo"/>""")
|
---|
266 | input = libxml2.inputBuffer(f)
|
---|
267 | reader = input.newTextReader("test7")
|
---|
268 | ret = reader.Read()
|
---|
269 | if ret != 1:
|
---|
270 | print("test7: failed to Read()")
|
---|
271 | sys.exit(1)
|
---|
272 | ret = reader.MoveToFirstAttribute()
|
---|
273 | if ret != 1:
|
---|
274 | print("test7: failed to MoveToFirstAttribute()")
|
---|
275 | sys.exit(1)
|
---|
276 | if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
|
---|
277 | reader.LocalName() != "prefix" or reader.Name() != "xmlns:prefix" or \
|
---|
278 | reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
|
---|
279 | print("test7: failed to read the namespace node")
|
---|
280 | sys.exit(1)
|
---|
281 |
|
---|
282 | #
|
---|
283 | # Test for a limit case:
|
---|
284 | #
|
---|
285 | f = str_io("""<a/>""")
|
---|
286 | input = libxml2.inputBuffer(f)
|
---|
287 | reader = input.newTextReader("test8")
|
---|
288 | ret = reader.Read()
|
---|
289 | if ret != 1:
|
---|
290 | print("test8: failed to read the node")
|
---|
291 | sys.exit(1)
|
---|
292 | if reader.Name() != "a" or reader.IsEmptyElement() != 1:
|
---|
293 | print("test8: failed to analyze the node")
|
---|
294 | sys.exit(1)
|
---|
295 | ret = reader.Read()
|
---|
296 | if ret != 0:
|
---|
297 | print("test8: failed to detect the EOF")
|
---|
298 | sys.exit(1)
|
---|
299 |
|
---|
300 | #
|
---|
301 | # Another test provided by Stéphane Bidoul and checked with C#
|
---|
302 | #
|
---|
303 | def tst_reader(s):
|
---|
304 | f = str_io(s)
|
---|
305 | input = libxml2.inputBuffer(f)
|
---|
306 | reader = input.newTextReader("tst")
|
---|
307 | res = ""
|
---|
308 | while reader.Read():
|
---|
309 | res=res + "%s (%s) [%s] %d %d\n" % (reader.NodeType(),reader.Name(),
|
---|
310 | reader.Value(), reader.IsEmptyElement(),
|
---|
311 | reader.Depth())
|
---|
312 | if reader.NodeType() == 1: # Element
|
---|
313 | while reader.MoveToNextAttribute():
|
---|
314 | res = res + "-- %s (%s) [%s] %d %d\n" % (reader.NodeType(),
|
---|
315 | reader.Name(),reader.Value(),
|
---|
316 | reader.IsEmptyElement(), reader.Depth())
|
---|
317 | return res
|
---|
318 |
|
---|
319 | doc="""<a><b b1="b1"/><c>content of c</c></a>"""
|
---|
320 | expect="""1 (a) [None] 0 0
|
---|
321 | 1 (b) [None] 1 1
|
---|
322 | -- 2 (b1) [b1] 0 2
|
---|
323 | 1 (c) [None] 0 1
|
---|
324 | 3 (#text) [content of c] 0 2
|
---|
325 | 15 (c) [None] 0 1
|
---|
326 | 15 (a) [None] 0 0
|
---|
327 | """
|
---|
328 | res = tst_reader(doc)
|
---|
329 | if res != expect:
|
---|
330 | print("test5 failed")
|
---|
331 | print(res)
|
---|
332 | sys.exit(1)
|
---|
333 |
|
---|
334 | doc="""<test><b/><c/></test>"""
|
---|
335 | expect="""1 (test) [None] 0 0
|
---|
336 | 1 (b) [None] 1 1
|
---|
337 | 1 (c) [None] 1 1
|
---|
338 | 15 (test) [None] 0 0
|
---|
339 | """
|
---|
340 | res = tst_reader(doc)
|
---|
341 | if res != expect:
|
---|
342 | print("test9 failed")
|
---|
343 | print(res)
|
---|
344 | sys.exit(1)
|
---|
345 |
|
---|
346 | doc="""<a><b>bbb</b><c>ccc</c></a>"""
|
---|
347 | expect="""1 (a) [None] 0 0
|
---|
348 | 1 (b) [None] 0 1
|
---|
349 | 3 (#text) [bbb] 0 2
|
---|
350 | 15 (b) [None] 0 1
|
---|
351 | 1 (c) [None] 0 1
|
---|
352 | 3 (#text) [ccc] 0 2
|
---|
353 | 15 (c) [None] 0 1
|
---|
354 | 15 (a) [None] 0 0
|
---|
355 | """
|
---|
356 | res = tst_reader(doc)
|
---|
357 | if res != expect:
|
---|
358 | print("test10 failed")
|
---|
359 | print(res)
|
---|
360 | sys.exit(1)
|
---|
361 |
|
---|
362 | doc="""<test a="a"/>"""
|
---|
363 | expect="""1 (test) [None] 1 0
|
---|
364 | -- 2 (a) [a] 0 1
|
---|
365 | """
|
---|
366 | res = tst_reader(doc)
|
---|
367 | if res != expect:
|
---|
368 | print("test11 failed")
|
---|
369 | print(res)
|
---|
370 | sys.exit(1)
|
---|
371 |
|
---|
372 | doc="""<test><a>aaa</a><b/></test>"""
|
---|
373 | expect="""1 (test) [None] 0 0
|
---|
374 | 1 (a) [None] 0 1
|
---|
375 | 3 (#text) [aaa] 0 2
|
---|
376 | 15 (a) [None] 0 1
|
---|
377 | 1 (b) [None] 1 1
|
---|
378 | 15 (test) [None] 0 0
|
---|
379 | """
|
---|
380 | res = tst_reader(doc)
|
---|
381 | if res != expect:
|
---|
382 | print("test12 failed")
|
---|
383 | print(res)
|
---|
384 | sys.exit(1)
|
---|
385 |
|
---|
386 | doc="""<test><p></p></test>"""
|
---|
387 | expect="""1 (test) [None] 0 0
|
---|
388 | 1 (p) [None] 0 1
|
---|
389 | 15 (p) [None] 0 1
|
---|
390 | 15 (test) [None] 0 0
|
---|
391 | """
|
---|
392 | res = tst_reader(doc)
|
---|
393 | if res != expect:
|
---|
394 | print("test13 failed")
|
---|
395 | print(res)
|
---|
396 | sys.exit(1)
|
---|
397 |
|
---|
398 | doc="""<p></p>"""
|
---|
399 | expect="""1 (p) [None] 0 0
|
---|
400 | 15 (p) [None] 0 0
|
---|
401 | """
|
---|
402 | res = tst_reader(doc)
|
---|
403 | if res != expect:
|
---|
404 | print("test14 failed")
|
---|
405 | print(res)
|
---|
406 | sys.exit(1)
|
---|
407 |
|
---|
408 | #
|
---|
409 | # test from bug #108801
|
---|
410 | #
|
---|
411 | doc="""<?xml version="1.0" standalone="no"?>
|
---|
412 | <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
|
---|
413 | "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
|
---|
414 | ]>
|
---|
415 |
|
---|
416 | <article>
|
---|
417 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
---|
418 | </article>
|
---|
419 | """
|
---|
420 | expect="""10 (article) [None] 0 0
|
---|
421 | 1 (article) [None] 0 0
|
---|
422 | 3 (#text) [
|
---|
423 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
---|
424 | ] 0 1
|
---|
425 | 15 (article) [None] 0 0
|
---|
426 | """
|
---|
427 | res = tst_reader(doc)
|
---|
428 | if res != expect:
|
---|
429 | print("test15 failed")
|
---|
430 | print(res)
|
---|
431 | sys.exit(1)
|
---|
432 |
|
---|
433 | #
|
---|
434 | # cleanup for memory allocation counting
|
---|
435 | #
|
---|
436 | del f
|
---|
437 | del input
|
---|
438 | del reader
|
---|
439 |
|
---|
440 | # Memory debug specific
|
---|
441 | libxml2.cleanupParser()
|
---|
442 | if libxml2.debugMemory(1) == 0:
|
---|
443 | print("OK")
|
---|
444 | else:
|
---|
445 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
446 | libxml2.dumpMemory()
|
---|