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