3 # this tests the entities substitutions with the XmlTextReader interface
9 docstr="""<?xml version='1.0'?>
11 <!ENTITY tst "<p>test</p>">
15 # Memory debug specific
16 libxml2.debugMemory(1)
19 # First test, normal don't substitute entities.
21 f = StringIO.StringIO(docstr)
22 input = libxml2.inputBuffer(f)
23 reader = input.newTextReader("test_noent")
26 print "Error reading to root"
28 if reader.Name() == "doc" or reader.NodeType() == 10:
31 print "Error reading to root"
33 if reader.Name() != "doc" or reader.NodeType() != 1:
34 print "test_normal: Error reading the root element"
38 print "test_normal: Error reading to the entity"
40 if reader.Name() != "tst" or reader.NodeType() != 5:
41 print "test_normal: Error reading the entity"
45 print "test_normal: Error reading to the end of root"
47 if reader.Name() != "doc" or reader.NodeType() != 15:
48 print "test_normal: Error reading the end of the root element"
52 print "test_normal: Error detecting the end"
56 # Second test, completely substitute the entities.
58 f = StringIO.StringIO(docstr)
59 input = libxml2.inputBuffer(f)
60 reader = input.newTextReader("test_noent")
61 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
64 print "Error reading to root"
66 if reader.Name() == "doc" or reader.NodeType() == 10:
69 print "Error reading to root"
71 if reader.Name() != "doc" or reader.NodeType() != 1:
72 print "test_noent: Error reading the root element"
76 print "test_noent: Error reading to the entity content"
78 if reader.Name() != "p" or reader.NodeType() != 1:
79 print "test_noent: Error reading the p element from entity"
83 print "test_noent: Error reading to the text node"
85 if reader.NodeType() != 3 or reader.Value() != "test":
86 print "test_noent: Error reading the text node"
90 print "test_noent: Error reading to the end of p element"
92 if reader.Name() != "p" or reader.NodeType() != 15:
93 print "test_noent: Error reading the end of the p element"
97 print "test_noent: Error reading to the end of root"
99 if reader.Name() != "doc" or reader.NodeType() != 15:
100 print "test_noent: Error reading the end of the root element"
104 print "test_noent: Error detecting the end"
108 # third test, crazy stuff about empty element in external parsed entities
110 s = """<!DOCTYPE struct [
111 <!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
113 <struct>&simplestruct2.ent;</struct>
115 expect="""10 struct 0 0
121 simplestruct2_ent="""<descr/>"""
123 def myResolver(URL, ID, ctxt):
124 if URL == "simplestruct2.ent":
125 return(StringIO.StringIO(simplestruct2_ent))
128 libxml2.setEntityLoader(myResolver)
130 input = libxml2.inputBuffer(StringIO.StringIO(s))
131 reader = input.newTextReader("test3")
132 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
133 while reader.Read() == 1:
134 res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
135 reader.Depth(),reader.IsEmptyElement())
138 print "test3 failed: unexpected output"
149 # Memory debug specific
150 libxml2.cleanupParser()
151 if libxml2.debugMemory(1) == 0:
154 print "Memory leak %d bytes" % (libxml2.debugMemory(1))