1#!/usr/bin/python -u
2#
3# this tests the entities substitutions with the XmlTextReader interface
4#
5import sys
6import StringIO
7import libxml2
8
9# Memory debug specific
10libxml2.debugMemory(1)
11
12result = ""
13def processNode(reader):
14    global result
15
16    result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
17			   reader.Name(), reader.IsEmptyElement())
18
19#
20# Parse a document testing the readerForxxx API
21#
22docstr="""<foo>
23<label>some text</label>
24<item>100</item>
25</foo>"""
26expect="""0 1 foo 0
271 14 #text 0
281 1 label 0
292 3 #text 0
301 15 label 0
311 14 #text 0
321 1 item 0
332 3 #text 0
341 15 item 0
351 14 #text 0
360 15 foo 0
37"""
38result = ""
39
40doc = libxml2.parseDoc(docstr)
41reader = doc.readerWalker();
42ret = reader.Read()
43while ret == 1:
44    processNode(reader)
45    ret = reader.Read()
46
47if ret != 0:
48    print "Error parsing the document test1"
49    sys.exit(1)
50
51if result != expect:
52    print "Unexpected result for test1"
53    print result
54    sys.exit(1)
55
56doc.freeDoc()
57
58#
59# Reuse the reader for another document testing the ReaderNewWalker API
60#
61docstr="""<foo>
62<label>some text</label>
63<item>1000</item>
64</foo>"""
65expect="""0 1 foo 0
661 14 #text 0
671 1 label 0
682 3 #text 0
691 15 label 0
701 14 #text 0
711 1 item 0
722 3 #text 0
731 15 item 0
741 14 #text 0
750 15 foo 0
76"""
77result = ""
78
79doc = libxml2.parseDoc(docstr)
80reader.NewWalker(doc)
81
82ret = reader.Read()
83while ret == 1:
84    processNode(reader)
85    ret = reader.Read()
86
87if ret != 0:
88    print "Error parsing the document test2"
89    sys.exit(1)
90
91if result != expect:
92    print "Unexpected result for test2"
93    print result
94    sys.exit(1)
95
96doc.freeDoc()
97
98#
99# Reuse the reader for another document testing the ReaderNewxxx API
100#
101docstr="""<foo>
102<label>some text</label>
103<item>1000</item>
104</foo>"""
105expect="""0 1 foo 0
1061 14 #text 0
1071 1 label 0
1082 3 #text 0
1091 15 label 0
1101 14 #text 0
1111 1 item 0
1122 3 #text 0
1131 15 item 0
1141 14 #text 0
1150 15 foo 0
116"""
117result = ""
118
119reader.NewDoc(docstr, "test3", None, 0)
120ret = reader.Read()
121while ret == 1:
122    processNode(reader)
123    ret = reader.Read()
124
125if ret != 0:
126    print "Error parsing the document test3"
127    sys.exit(1)
128
129if result != expect:
130    print "Unexpected result for test3"
131    print result
132    sys.exit(1)
133
134#
135# cleanup
136#
137del reader
138
139# Memory debug specific
140libxml2.cleanupParser()
141if libxml2.debugMemory(1) == 0:
142    print "OK"
143else:
144    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
145    libxml2.dumpMemory()
146