VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.31/python/tests/reader2.py@ 55391

最後變更 在這個檔案從55391是 39915,由 vboxsync 提交於 13 年 前

libxml-2.6.31 unmodified

  • 屬性 svn:eol-style 設為 native
檔案大小: 5.1 KB
 
1#!/usr/bin/python -u
2#
3# this tests the DTD validation with the XmlTextReader interface
4#
5import sys
6import glob
7import string
8import StringIO
9import libxml2
10
11# Memory debug specific
12libxml2.debugMemory(1)
13
14err=""
15expect="""../../test/valid/rss.xml:177: element rss: validity error : Element rss does not carry attribute version
16</rss>
17 ^
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
20 ^
21../../test/valid/xlink.xml:530: validity error : attribute def line 199 references an unknown ID "dt-xlg"
22
23^
24"""
25def callback(ctx, str):
26 global err
27 err = err + "%s" % (str)
28libxml2.registerErrorHandler(callback, "")
29
30valid_files = glob.glob("../../test/valid/*.x*")
31valid_files.sort()
32for file in valid_files:
33 if string.find(file, "t8") != -1:
34 continue
35 reader = libxml2.newTextReaderFilename(file)
36 #print "%s:" % (file)
37 reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
38 ret = reader.Read()
39 while ret == 1:
40 ret = reader.Read()
41 if ret != 0:
42 print "Error parsing and validating %s" % (file)
43 #sys.exit(1)
44
45if err != expect:
46 print err
47
48#
49# another separate test based on Stephane Bidoul one
50#
51s = """
52<!DOCTYPE test [
53<!ELEMENT test (x,b)>
54<!ELEMENT x (c)>
55<!ELEMENT b (#PCDATA)>
56<!ELEMENT c (#PCDATA)>
57<!ENTITY x "<x><c>xxx</c></x>">
58]>
59<test>
60 &x;
61 <b>bbb</b>
62</test>
63"""
64expect="""10,test
651,test
6614,#text
671,x
681,c
693,#text
7015,c
7115,x
7214,#text
731,b
743,#text
7515,b
7614,#text
7715,test
78"""
79res=""
80err=""
81
82input = libxml2.inputBuffer(StringIO.StringIO(s))
83reader = input.newTextReader("test2")
84reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
85reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
86reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
87reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
88while reader.Read() == 1:
89 res = res + "%s,%s\n" % (reader.NodeType(),reader.Name())
90
91if res != expect:
92 print "test2 failed: unexpected output"
93 print res
94 sys.exit(1)
95if err != "":
96 print "test2 failed: validation error found"
97 print err
98 sys.exit(1)
99
100#
101# Another test for external entity parsing and validation
102#
103
104s = """<!DOCTYPE test [
105<!ELEMENT test (x)>
106<!ELEMENT x (#PCDATA)>
107<!ENTITY e SYSTEM "tst.ent">
108]>
109<test>
110 &e;
111</test>
112"""
113tst_ent = """<x>hello</x>"""
114expect="""10 test
1151 test
11614 #text
1171 x
1183 #text
11915 x
12014 #text
12115 test
122"""
123res=""
124
125def myResolver(URL, ID, ctxt):
126 if URL == "tst.ent":
127 return(StringIO.StringIO(tst_ent))
128 return None
129
130libxml2.setEntityLoader(myResolver)
131
132input = libxml2.inputBuffer(StringIO.StringIO(s))
133reader = input.newTextReader("test3")
134reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
135reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
136reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
137reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
138while reader.Read() == 1:
139 res = res + "%s %s\n" % (reader.NodeType(),reader.Name())
140
141if res != expect:
142 print "test3 failed: unexpected output"
143 print res
144 sys.exit(1)
145if err != "":
146 print "test3 failed: validation error found"
147 print err
148 sys.exit(1)
149
150#
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
153#
154
155s = """<!DOCTYPE test [
156<!ELEMENT test (x, x)>
157<!ELEMENT x (y)>
158<!ELEMENT y (#PCDATA)>
159<!ENTITY x "<x>&y;</x>">
160<!ENTITY y "<y>yyy</y>">
161]>
162<test>
163 &x;
164 &x;
165</test>"""
166expect="""10 test 0
1671 test 0
16814 #text 1
1691 x 1
1701 y 2
1713 #text 3
17215 y 2
17315 x 1
17414 #text 1
1751 x 1
1761 y 2
1773 #text 3
17815 y 2
17915 x 1
18014 #text 1
18115 test 0
182"""
183res=""
184err=""
185
186input = libxml2.inputBuffer(StringIO.StringIO(s))
187reader = input.newTextReader("test4")
188reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
189reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
190reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
191reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
192while reader.Read() == 1:
193 res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
194
195if res != expect:
196 print "test4 failed: unexpected output"
197 print res
198 sys.exit(1)
199if err != "":
200 print "test4 failed: validation error found"
201 print err
202 sys.exit(1)
203
204#
205# The same test but without entity substitution this time
206#
207
208s = """<!DOCTYPE test [
209<!ELEMENT test (x, x)>
210<!ELEMENT x (y)>
211<!ELEMENT y (#PCDATA)>
212<!ENTITY x "<x>&y;</x>">
213<!ENTITY y "<y>yyy</y>">
214]>
215<test>
216 &x;
217 &x;
218</test>"""
219expect="""10 test 0
2201 test 0
22114 #text 1
2225 x 1
22314 #text 1
2245 x 1
22514 #text 1
22615 test 0
227"""
228res=""
229err=""
230
231input = libxml2.inputBuffer(StringIO.StringIO(s))
232reader = input.newTextReader("test5")
233reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
234while reader.Read() == 1:
235 res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
236
237if res != expect:
238 print "test5 failed: unexpected output"
239 print res
240if err != "":
241 print "test5 failed: validation error found"
242 print err
243
244#
245# cleanup
246#
247del input
248del reader
249
250# Memory debug specific
251libxml2.cleanupParser()
252if libxml2.debugMemory(1) == 0:
253 print "OK"
254else:
255 print "Memory leak %d bytes" % (libxml2.debugMemory(1))
256 libxml2.dumpMemory()
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette