1#!/usr/bin/python -u
2import sys
3import libxml2
4
5# Memory debug specific
6libxml2.debugMemory(1)
7
8log = ""
9
10class callback:
11    def startDocument(self):
12        global log
13        log = log + "startDocument:"
14
15    def endDocument(self):
16        global log
17        log = log + "endDocument:"
18
19    def startElement(self, tag, attrs):
20        global log
21        log = log + "startElement %s %s:" % (tag, attrs)
22
23    def endElement(self, tag):
24        global log
25        log = log + "endElement %s:" % (tag)
26
27    def characters(self, data):
28        global log
29        log = log + "characters: %s:" % (data)
30
31    def warning(self, msg):
32        global log
33        log = log + "warning: %s:" % (msg)
34
35    def error(self, msg):
36        global log
37        log = log + "error: %s:" % (msg)
38
39    def fatalError(self, msg):
40        global log
41        log = log + "fatalError: %s:" % (msg)
42
43handler = callback()
44
45log=""
46chunk="""<foo><bar2/>"""
47ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
48ctxt.parseChunk(chunk, len(chunk), 0)
49ctxt=None
50
51reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
52if log != reference:
53    print("Error got: %s" % log)
54    print("Expected: %s" % reference)
55    sys.exit(1)
56
57log=""
58chunk="""<foo><bar2></bar2>"""
59ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
60ctxt.parseChunk(chunk, len(chunk), 0)
61ctxt=None
62
63reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
64if log != reference:
65    print("Error got: %s" % log)
66    print("Expected: %s" % reference)
67    sys.exit(1)
68
69log=""
70chunk="""<foo><bar2>"""
71ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
72ctxt.parseChunk(chunk, len(chunk), 0)
73ctxt=None
74
75reference = "startDocument:startElement foo None:startElement bar2 None:"
76if log != reference:
77    print("Error got: %s" % log)
78    print("Expected: %s" % reference)
79    sys.exit(1)
80
81log=""
82chunk="""<foo><bar2 a="1" b='2' />"""
83ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
84ctxt.parseChunk(chunk, len(chunk), 0)
85ctxt=None
86
87reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
88reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:"
89if log not in (reference1, reference2):
90    print("Error got: %s" % log)
91    print("Expected: %s" % reference)
92    sys.exit(1)
93
94log=""
95chunk="""<foo><bar2 a="1" b='2' >"""
96ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
97ctxt.parseChunk(chunk, len(chunk), 0)
98ctxt=None
99
100reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:"
101reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:"
102if log not in (reference1, reference2):
103    print("Error got: %s" % log)
104    print("Expected: %s" % reference)
105    sys.exit(1)
106
107log=""
108chunk="""<foo><bar2 a="1" b='2' ></bar2>"""
109ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
110ctxt.parseChunk(chunk, len(chunk), 0)
111ctxt=None
112
113reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
114reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:"
115if log not in (reference1, reference2):
116    print("Error got: %s" % log)
117    print("Expected: %s" % reference)
118    sys.exit(1)
119
120log=""
121chunk="""<foo><bar2 a="b='1' />"""
122ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
123ctxt.parseChunk(chunk, len(chunk), 0)
124ctxt=None
125
126reference = "startDocument:startElement foo None:"
127if log != reference:
128    print("Error got: %s" % log)
129    print("Expected: %s" % reference)
130    sys.exit(1)
131
132# Memory debug specific
133libxml2.cleanupParser()
134if libxml2.debugMemory(1) == 0:
135    print("OK")
136else:
137    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
138    libxml2.dumpMemory()
139