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
40reader = libxml2.readerForDoc(docstr, "test1", None, 0)
41ret = reader.Read()
42while ret == 1:
43    processNode(reader)
44    ret = reader.Read()
45
46if ret != 0:
47    print "Error parsing the document test1"
48    sys.exit(1)
49
50if result != expect:
51    print "Unexpected result for test1"
52    print result
53    sys.exit(1)
54
55#
56# Reuse the reader for another document testing the ReaderNewxxx API
57#
58docstr="""<foo>
59<label>some text</label>
60<item>1000</item>
61</foo>"""
62expect="""0 1 foo 0
631 14 #text 0
641 1 label 0
652 3 #text 0
661 15 label 0
671 14 #text 0
681 1 item 0
692 3 #text 0
701 15 item 0
711 14 #text 0
720 15 foo 0
73"""
74result = ""
75
76reader.NewDoc(docstr, "test2", None, 0)
77ret = reader.Read()
78while ret == 1:
79    processNode(reader)
80    ret = reader.Read()
81
82if ret != 0:
83    print "Error parsing the document test2"
84    sys.exit(1)
85
86if result != expect:
87    print "Unexpected result for test2"
88    print result
89    sys.exit(1)
90
91#
92# cleanup
93#
94del reader
95
96# Memory debug specific
97libxml2.cleanupParser()
98if libxml2.debugMemory(1) == 0:
99    print "OK"
100else:
101    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
102    libxml2.dumpMemory()
103