3 # this tests the DTD validation with the XmlTextReader interface
11 # Memory debug specific
12 libxml2
.debugMemory(1)
15 expect
="""../../test/valid/rss.xml:177: element rss: validity error : Element rss does not carry attribute version
18 ../../test/valid/xlink.xml:450: element termdef: validity error : ID dt-arc already defined
19 <p><termdef id="dt-arc" term="Arc">An <ter
21 ../../test/valid/xlink.xml:530: validity error : attribute def line 199 references an unknown ID "dt-xlg"
25 def callback(ctx
, str):
27 err
= err
+ "%s" % (str)
28 libxml2
.registerErrorHandler(callback
, "")
30 valid_files
= glob
.glob("../../test/valid/*.x*")
32 for file in valid_files
:
33 if string
.find(file, "t8") != -1:
35 reader
= libxml2
.newTextReaderFilename(file)
37 reader
.SetParserProp(libxml2
.PARSER_VALIDATE
, 1)
42 print "Error parsing and validating %s" % (file)
49 # another separate test based on Stephane Bidoul one
55 <!ELEMENT b (#PCDATA)>
56 <!ELEMENT c (#PCDATA)>
57 <!ENTITY x "<x><c>xxx</c></x>">
82 input = libxml2
.inputBuffer(StringIO
.StringIO(s
))
83 reader
= input.newTextReader("test2")
84 reader
.SetParserProp(libxml2
.PARSER_LOADDTD
,1)
85 reader
.SetParserProp(libxml2
.PARSER_DEFAULTATTRS
,1)
86 reader
.SetParserProp(libxml2
.PARSER_SUBST_ENTITIES
,1)
87 reader
.SetParserProp(libxml2
.PARSER_VALIDATE
,1)
88 while reader
.Read() == 1:
89 res
= res
+ "%s,%s\n" % (reader
.NodeType(),reader
.Name())
92 print "test2 failed: unexpected output"
96 print "test2 failed: validation error found"
101 # Another test for external entity parsing and validation
104 s
= """<!DOCTYPE test [
106 <!ELEMENT x (#PCDATA)>
107 <!ENTITY e SYSTEM "tst.ent">
113 tst_ent
= """<x>hello</x>"""
125 def myResolver(URL
, ID
, ctxt
):
127 return(StringIO
.StringIO(tst_ent
))
130 libxml2
.setEntityLoader(myResolver
)
132 input = libxml2
.inputBuffer(StringIO
.StringIO(s
))
133 reader
= input.newTextReader("test3")
134 reader
.SetParserProp(libxml2
.PARSER_LOADDTD
,1)
135 reader
.SetParserProp(libxml2
.PARSER_DEFAULTATTRS
,1)
136 reader
.SetParserProp(libxml2
.PARSER_SUBST_ENTITIES
,1)
137 reader
.SetParserProp(libxml2
.PARSER_VALIDATE
,1)
138 while reader
.Read() == 1:
139 res
= res
+ "%s %s\n" % (reader
.NodeType(),reader
.Name())
142 print "test3 failed: unexpected output"
146 print "test3 failed: validation error found"
151 # Another test for recursive entity parsing, validation, and replacement of
152 # entities, making sure the entity ref node doesn't show up in that case
155 s
= """<!DOCTYPE test [
156 <!ELEMENT test (x, x)>
158 <!ELEMENT y (#PCDATA)>
159 <!ENTITY x "<x>&y;</x>">
160 <!ENTITY y "<y>yyy</y>">
186 input = libxml2
.inputBuffer(StringIO
.StringIO(s
))
187 reader
= input.newTextReader("test4")
188 reader
.SetParserProp(libxml2
.PARSER_LOADDTD
,1)
189 reader
.SetParserProp(libxml2
.PARSER_DEFAULTATTRS
,1)
190 reader
.SetParserProp(libxml2
.PARSER_SUBST_ENTITIES
,1)
191 reader
.SetParserProp(libxml2
.PARSER_VALIDATE
,1)
192 while reader
.Read() == 1:
193 res
= res
+ "%s %s %d\n" % (reader
.NodeType(),reader
.Name(),reader
.Depth())
196 print "test4 failed: unexpected output"
200 print "test4 failed: validation error found"
205 # The same test but without entity substitution this time
208 s
= """<!DOCTYPE test [
209 <!ELEMENT test (x, x)>
211 <!ELEMENT y (#PCDATA)>
212 <!ENTITY x "<x>&y;</x>">
213 <!ENTITY y "<y>yyy</y>">
231 input = libxml2
.inputBuffer(StringIO
.StringIO(s
))
232 reader
= input.newTextReader("test5")
233 reader
.SetParserProp(libxml2
.PARSER_VALIDATE
,1)
234 while reader
.Read() == 1:
235 res
= res
+ "%s %s %d\n" % (reader
.NodeType(),reader
.Name(),reader
.Depth())
238 print "test5 failed: unexpected output"
241 print "test5 failed: validation error found"
250 # Memory debug specific
251 libxml2
.cleanupParser()
252 if libxml2
.debugMemory(1) == 0:
255 print "Memory leak %d bytes" % (libxml2
.debugMemory(1))