1 | #!/usr/bin/python -u
|
---|
2 | #
|
---|
3 | # this tests the entities substitutions with the XmlTextReader interface
|
---|
4 | #
|
---|
5 | import sys
|
---|
6 | import libxml2
|
---|
7 |
|
---|
8 | # Memory debug specific
|
---|
9 | libxml2.debugMemory(1)
|
---|
10 |
|
---|
11 | result = ""
|
---|
12 | def processNode(reader):
|
---|
13 | global result
|
---|
14 |
|
---|
15 | result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
|
---|
16 | reader.Name(), reader.IsEmptyElement())
|
---|
17 |
|
---|
18 | #
|
---|
19 | # Parse a document testing the readerForxxx API
|
---|
20 | #
|
---|
21 | docstr="""<foo>
|
---|
22 | <label>some text</label>
|
---|
23 | <item>100</item>
|
---|
24 | </foo>"""
|
---|
25 | expect="""0 1 foo 0
|
---|
26 | 1 14 #text 0
|
---|
27 | 1 1 label 0
|
---|
28 | 2 3 #text 0
|
---|
29 | 1 15 label 0
|
---|
30 | 1 14 #text 0
|
---|
31 | 1 1 item 0
|
---|
32 | 2 3 #text 0
|
---|
33 | 1 15 item 0
|
---|
34 | 1 14 #text 0
|
---|
35 | 0 15 foo 0
|
---|
36 | """
|
---|
37 | result = ""
|
---|
38 |
|
---|
39 | doc = libxml2.parseDoc(docstr)
|
---|
40 | reader = doc.readerWalker();
|
---|
41 | ret = reader.Read()
|
---|
42 | while ret == 1:
|
---|
43 | processNode(reader)
|
---|
44 | ret = reader.Read()
|
---|
45 |
|
---|
46 | if ret != 0:
|
---|
47 | print("Error parsing the document test1")
|
---|
48 | sys.exit(1)
|
---|
49 |
|
---|
50 | if result != expect:
|
---|
51 | print("Unexpected result for test1")
|
---|
52 | print(result)
|
---|
53 | sys.exit(1)
|
---|
54 |
|
---|
55 | doc.freeDoc()
|
---|
56 |
|
---|
57 | #
|
---|
58 | # Reuse the reader for another document testing the ReaderNewWalker API
|
---|
59 | #
|
---|
60 | docstr="""<foo>
|
---|
61 | <label>some text</label>
|
---|
62 | <item>1000</item>
|
---|
63 | </foo>"""
|
---|
64 | expect="""0 1 foo 0
|
---|
65 | 1 14 #text 0
|
---|
66 | 1 1 label 0
|
---|
67 | 2 3 #text 0
|
---|
68 | 1 15 label 0
|
---|
69 | 1 14 #text 0
|
---|
70 | 1 1 item 0
|
---|
71 | 2 3 #text 0
|
---|
72 | 1 15 item 0
|
---|
73 | 1 14 #text 0
|
---|
74 | 0 15 foo 0
|
---|
75 | """
|
---|
76 | result = ""
|
---|
77 |
|
---|
78 | doc = libxml2.parseDoc(docstr)
|
---|
79 | reader.NewWalker(doc)
|
---|
80 |
|
---|
81 | ret = reader.Read()
|
---|
82 | while ret == 1:
|
---|
83 | processNode(reader)
|
---|
84 | ret = reader.Read()
|
---|
85 |
|
---|
86 | if ret != 0:
|
---|
87 | print("Error parsing the document test2")
|
---|
88 | sys.exit(1)
|
---|
89 |
|
---|
90 | if result != expect:
|
---|
91 | print("Unexpected result for test2")
|
---|
92 | print(result)
|
---|
93 | sys.exit(1)
|
---|
94 |
|
---|
95 | doc.freeDoc()
|
---|
96 |
|
---|
97 | #
|
---|
98 | # Reuse the reader for another document testing the ReaderNewxxx API
|
---|
99 | #
|
---|
100 | docstr="""<foo>
|
---|
101 | <label>some text</label>
|
---|
102 | <item>1000</item>
|
---|
103 | </foo>"""
|
---|
104 | expect="""0 1 foo 0
|
---|
105 | 1 14 #text 0
|
---|
106 | 1 1 label 0
|
---|
107 | 2 3 #text 0
|
---|
108 | 1 15 label 0
|
---|
109 | 1 14 #text 0
|
---|
110 | 1 1 item 0
|
---|
111 | 2 3 #text 0
|
---|
112 | 1 15 item 0
|
---|
113 | 1 14 #text 0
|
---|
114 | 0 15 foo 0
|
---|
115 | """
|
---|
116 | result = ""
|
---|
117 |
|
---|
118 | reader.NewDoc(docstr, "test3", None, 0)
|
---|
119 | ret = reader.Read()
|
---|
120 | while ret == 1:
|
---|
121 | processNode(reader)
|
---|
122 | ret = reader.Read()
|
---|
123 |
|
---|
124 | if ret != 0:
|
---|
125 | print("Error parsing the document test3")
|
---|
126 | sys.exit(1)
|
---|
127 |
|
---|
128 | if result != expect:
|
---|
129 | print("Unexpected result for test3")
|
---|
130 | print(result)
|
---|
131 | sys.exit(1)
|
---|
132 |
|
---|
133 | #
|
---|
134 | # cleanup
|
---|
135 | #
|
---|
136 | del reader
|
---|
137 |
|
---|
138 | # Memory debug specific
|
---|
139 | libxml2.cleanupParser()
|
---|
140 | if libxml2.debugMemory(1) == 0:
|
---|
141 | print("OK")
|
---|
142 | else:
|
---|
143 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
144 | libxml2.dumpMemory()
|
---|