3 # this tests the entities substitutions with the XmlTextReader interface
9 str_io = StringIO.StringIO
14 docstr="""<?xml version='1.0'?>
16 <!ENTITY tst "<p>test</p>">
20 # Memory debug specific
21 libxml2.debugMemory(1)
24 # First test, normal don't substitute entities.
27 input = libxml2.inputBuffer(f)
28 reader = input.newTextReader("test_noent")
31 print("Error reading to root")
33 if reader.Name() == "doc" or reader.NodeType() == 10:
36 print("Error reading to root")
38 if reader.Name() != "doc" or reader.NodeType() != 1:
39 print("test_normal: Error reading the root element")
43 print("test_normal: Error reading to the entity")
45 if reader.Name() != "tst" or reader.NodeType() != 5:
46 print("test_normal: Error reading the entity")
50 print("test_normal: Error reading to the end of root")
52 if reader.Name() != "doc" or reader.NodeType() != 15:
53 print("test_normal: Error reading the end of the root element")
57 print("test_normal: Error detecting the end")
61 # Second test, completely substitute the entities.
64 input = libxml2.inputBuffer(f)
65 reader = input.newTextReader("test_noent")
66 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
69 print("Error reading to root")
71 if reader.Name() == "doc" or reader.NodeType() == 10:
74 print("Error reading to root")
76 if reader.Name() != "doc" or reader.NodeType() != 1:
77 print("test_noent: Error reading the root element")
81 print("test_noent: Error reading to the entity content")
83 if reader.Name() != "p" or reader.NodeType() != 1:
84 print("test_noent: Error reading the p element from entity")
88 print("test_noent: Error reading to the text node")
90 if reader.NodeType() != 3 or reader.Value() != "test":
91 print("test_noent: Error reading the text node")
95 print("test_noent: Error reading to the end of p element")
97 if reader.Name() != "p" or reader.NodeType() != 15:
98 print("test_noent: Error reading the end of the p element")
102 print("test_noent: Error reading to the end of root")
104 if reader.Name() != "doc" or reader.NodeType() != 15:
105 print("test_noent: Error reading the end of the root element")
109 print("test_noent: Error detecting the end")
113 # third test, crazy stuff about empty element in external parsed entities
115 s = """<!DOCTYPE struct [
116 <!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
118 <struct>&simplestruct2.ent;</struct>
120 expect="""10 struct 0 0
126 simplestruct2_ent="""<descr/>"""
128 def myResolver(URL, ID, ctxt):
129 if URL == "simplestruct2.ent":
130 return(str_io(simplestruct2_ent))
133 libxml2.setEntityLoader(myResolver)
135 input = libxml2.inputBuffer(str_io(s))
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())
143 print("test3 failed: unexpected output")
154 # Memory debug specific
155 libxml2.cleanupParser()
156 if libxml2.debugMemory(1) == 0:
159 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))