Annotation of embedaddon/libxml2/python/tests/reader3.py, revision 1.1.1.2
1.1 misho 1: #!/usr/bin/python -u
2: #
3: # this tests the entities substitutions with the XmlTextReader interface
4: #
5: import sys
6: import libxml2
1.1.1.2 ! misho 7: try:
! 8: import StringIO
! 9: str_io = StringIO.StringIO
! 10: except:
! 11: import io
! 12: str_io = io.StringIO
1.1 misho 13:
14: docstr="""<?xml version='1.0'?>
15: <!DOCTYPE doc [
16: <!ENTITY tst "<p>test</p>">
17: ]>
18: <doc>&tst;</doc>"""
19:
20: # Memory debug specific
21: libxml2.debugMemory(1)
22:
23: #
24: # First test, normal don't substitute entities.
25: #
1.1.1.2 ! misho 26: f = str_io(docstr)
1.1 misho 27: input = libxml2.inputBuffer(f)
28: reader = input.newTextReader("test_noent")
29: ret = reader.Read()
30: if ret != 1:
1.1.1.2 ! misho 31: print("Error reading to root")
1.1 misho 32: sys.exit(1)
33: if reader.Name() == "doc" or reader.NodeType() == 10:
34: ret = reader.Read()
35: if ret != 1:
1.1.1.2 ! misho 36: print("Error reading to root")
1.1 misho 37: sys.exit(1)
38: if reader.Name() != "doc" or reader.NodeType() != 1:
1.1.1.2 ! misho 39: print("test_normal: Error reading the root element")
1.1 misho 40: sys.exit(1)
41: ret = reader.Read()
42: if ret != 1:
1.1.1.2 ! misho 43: print("test_normal: Error reading to the entity")
1.1 misho 44: sys.exit(1)
45: if reader.Name() != "tst" or reader.NodeType() != 5:
1.1.1.2 ! misho 46: print("test_normal: Error reading the entity")
1.1 misho 47: sys.exit(1)
48: ret = reader.Read()
49: if ret != 1:
1.1.1.2 ! misho 50: print("test_normal: Error reading to the end of root")
1.1 misho 51: sys.exit(1)
52: if reader.Name() != "doc" or reader.NodeType() != 15:
1.1.1.2 ! misho 53: print("test_normal: Error reading the end of the root element")
1.1 misho 54: sys.exit(1)
55: ret = reader.Read()
56: if ret != 0:
1.1.1.2 ! misho 57: print("test_normal: Error detecting the end")
1.1 misho 58: sys.exit(1)
59:
60: #
61: # Second test, completely substitute the entities.
62: #
1.1.1.2 ! misho 63: f = str_io(docstr)
1.1 misho 64: input = libxml2.inputBuffer(f)
65: reader = input.newTextReader("test_noent")
66: reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
67: ret = reader.Read()
68: if ret != 1:
1.1.1.2 ! misho 69: print("Error reading to root")
1.1 misho 70: sys.exit(1)
71: if reader.Name() == "doc" or reader.NodeType() == 10:
72: ret = reader.Read()
73: if ret != 1:
1.1.1.2 ! misho 74: print("Error reading to root")
1.1 misho 75: sys.exit(1)
76: if reader.Name() != "doc" or reader.NodeType() != 1:
1.1.1.2 ! misho 77: print("test_noent: Error reading the root element")
1.1 misho 78: sys.exit(1)
79: ret = reader.Read()
80: if ret != 1:
1.1.1.2 ! misho 81: print("test_noent: Error reading to the entity content")
1.1 misho 82: sys.exit(1)
83: if reader.Name() != "p" or reader.NodeType() != 1:
1.1.1.2 ! misho 84: print("test_noent: Error reading the p element from entity")
1.1 misho 85: sys.exit(1)
86: ret = reader.Read()
87: if ret != 1:
1.1.1.2 ! misho 88: print("test_noent: Error reading to the text node")
1.1 misho 89: sys.exit(1)
90: if reader.NodeType() != 3 or reader.Value() != "test":
1.1.1.2 ! misho 91: print("test_noent: Error reading the text node")
1.1 misho 92: sys.exit(1)
93: ret = reader.Read()
94: if ret != 1:
1.1.1.2 ! misho 95: print("test_noent: Error reading to the end of p element")
1.1 misho 96: sys.exit(1)
97: if reader.Name() != "p" or reader.NodeType() != 15:
1.1.1.2 ! misho 98: print("test_noent: Error reading the end of the p element")
1.1 misho 99: sys.exit(1)
100: ret = reader.Read()
101: if ret != 1:
1.1.1.2 ! misho 102: print("test_noent: Error reading to the end of root")
1.1 misho 103: sys.exit(1)
104: if reader.Name() != "doc" or reader.NodeType() != 15:
1.1.1.2 ! misho 105: print("test_noent: Error reading the end of the root element")
1.1 misho 106: sys.exit(1)
107: ret = reader.Read()
108: if ret != 0:
1.1.1.2 ! misho 109: print("test_noent: Error detecting the end")
1.1 misho 110: sys.exit(1)
111:
112: #
113: # third test, crazy stuff about empty element in external parsed entities
114: #
115: s = """<!DOCTYPE struct [
116: <!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
117: ]>
118: <struct>&simplestruct2.ent;</struct>
119: """
120: expect="""10 struct 0 0
121: 1 struct 0 0
122: 1 descr 1 1
123: 15 struct 0 0
124: """
125: res=""
126: simplestruct2_ent="""<descr/>"""
127:
128: def myResolver(URL, ID, ctxt):
129: if URL == "simplestruct2.ent":
1.1.1.2 ! misho 130: return(str_io(simplestruct2_ent))
1.1 misho 131: return None
132:
133: libxml2.setEntityLoader(myResolver)
134:
1.1.1.2 ! misho 135: input = libxml2.inputBuffer(str_io(s))
1.1 misho 136: reader = input.newTextReader("test3")
137: reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
138: while reader.Read() == 1:
139: res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
140: reader.Depth(),reader.IsEmptyElement())
141:
142: if res != expect:
1.1.1.2 ! misho 143: print("test3 failed: unexpected output")
! 144: print(res)
1.1 misho 145: sys.exit(1)
146:
147: #
148: # cleanup
149: #
150: del f
151: del input
152: del reader
153:
154: # Memory debug specific
155: libxml2.cleanupParser()
156: if libxml2.debugMemory(1) == 0:
1.1.1.2 ! misho 157: print("OK")
1.1 misho 158: else:
1.1.1.2 ! misho 159: print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
1.1 misho 160: libxml2.dumpMemory()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>