libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / python / tests / reader6.py
blobef33b1845eb1b000d56b845f4101cfc81e637463
1 #!/usr/bin/python -u
3 # this tests the entities substitutions with the XmlTextReader interface
5 import sys
6 import libxml2
7 try:
8 import StringIO
9 str_io = StringIO.StringIO
10 except:
11 import io
12 str_io = io.StringIO
14 schema="""<element name="foo" xmlns="http://relaxng.org/ns/structure/1.0"
15 datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
16 <oneOrMore>
17 <element name="label">
18 <text/>
19 </element>
20 <optional>
21 <element name="opt">
22 <empty/>
23 </element>
24 </optional>
25 <element name="item">
26 <data type="byte"/>
27 </element>
28 </oneOrMore>
29 </element>
30 """
31 # Memory debug specific
32 libxml2.debugMemory(1)
35 # Parse the Relax NG Schemas
37 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
38 rngs = rngp.relaxNGParse()
39 del rngp
42 # Parse and validate the correct document
44 docstr="""<foo>
45 <label>some text</label>
46 <item>100</item>
47 </foo>"""
49 f = str_io(docstr)
50 input = libxml2.inputBuffer(f)
51 reader = input.newTextReader("correct")
52 reader.RelaxNGSetSchema(rngs)
53 ret = reader.Read()
54 while ret == 1:
55 ret = reader.Read()
57 if ret != 0:
58 print("Error parsing the document")
59 sys.exit(1)
61 if reader.IsValid() != 1:
62 print("Document failed to validate")
63 sys.exit(1)
66 # Parse and validate the incorrect document
68 docstr="""<foo>
69 <label>some text</label>
70 <item>1000</item>
71 </foo>"""
73 err=""
74 # RNG errors are not as good as before , TODO
75 #expect="""RNG validity error: file error line 3 element text
76 #Type byte doesn't allow value '1000'
77 #RNG validity error: file error line 3 element text
78 #Error validating datatype byte
79 #RNG validity error: file error line 3 element text
80 #Element item failed to validate content
81 #"""
82 expect="""Type byte doesn't allow value '1000'
83 Error validating datatype byte
84 Element item failed to validate content
85 """
87 def callback(ctx, str):
88 global err
89 err = err + "%s" % (str)
90 libxml2.registerErrorHandler(callback, "")
92 f = str_io(docstr)
93 input = libxml2.inputBuffer(f)
94 reader = input.newTextReader("error")
95 reader.RelaxNGSetSchema(rngs)
96 ret = reader.Read()
97 while ret == 1:
98 ret = reader.Read()
100 if ret != 0:
101 print("Error parsing the document")
102 sys.exit(1)
104 if reader.IsValid() != 0:
105 print("Document failed to detect the validation error")
106 sys.exit(1)
108 if err != expect:
109 print("Did not get the expected error message:")
110 print(err)
111 sys.exit(1)
114 # cleanup
116 del f
117 del input
118 del reader
119 del rngs
120 libxml2.relaxNGCleanupTypes()
122 # Memory debug specific
123 libxml2.cleanupParser()
124 if libxml2.debugMemory(1) == 0:
125 print("OK")
126 else:
127 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
128 libxml2.dumpMemory()