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