VirtualBox

source: vbox/trunk/src/libs/libxml2-2.9.2/python/tests/reader6.py@ 64246

最後變更 在這個檔案從64246是 58072,由 vboxsync 提交於 9 年 前

libxml 2.9.2 unmodified

  • 屬性 svn:eol-style 設為 LF
  • 屬性 svn:executable 設為 *
檔案大小: 2.6 KB
 
1#!/usr/bin/python -u
2#
3# this tests the entities substitutions with the XmlTextReader interface
4#
5import sys
6import libxml2
7try:
8 import StringIO
9 str_io = StringIO.StringIO
10except:
11 import io
12 str_io = io.StringIO
13
14schema="""<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
32libxml2.debugMemory(1)
33
34#
35# Parse the Relax NG Schemas
36#
37rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
38rngs = rngp.relaxNGParse()
39del rngp
40
41#
42# Parse and validate the correct document
43#
44docstr="""<foo>
45<label>some text</label>
46<item>100</item>
47</foo>"""
48
49f = str_io(docstr)
50input = libxml2.inputBuffer(f)
51reader = input.newTextReader("correct")
52reader.RelaxNGSetSchema(rngs)
53ret = reader.Read()
54while ret == 1:
55 ret = reader.Read()
56
57if ret != 0:
58 print("Error parsing the document")
59 sys.exit(1)
60
61if reader.IsValid() != 1:
62 print("Document failed to validate")
63 sys.exit(1)
64
65#
66# Parse and validate the incorrect document
67#
68docstr="""<foo>
69<label>some text</label>
70<item>1000</item>
71</foo>"""
72
73err=""
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#"""
82expect="""Type byte doesn't allow value '1000'
83Error validating datatype byte
84Element item failed to validate content
85"""
86
87def callback(ctx, str):
88 global err
89 err = err + "%s" % (str)
90libxml2.registerErrorHandler(callback, "")
91
92f = str_io(docstr)
93input = libxml2.inputBuffer(f)
94reader = input.newTextReader("error")
95reader.RelaxNGSetSchema(rngs)
96ret = reader.Read()
97while ret == 1:
98 ret = reader.Read()
99
100if ret != 0:
101 print("Error parsing the document")
102 sys.exit(1)
103
104if reader.IsValid() != 0:
105 print("Document failed to detect the validation error")
106 sys.exit(1)
107
108if err != expect:
109 print("Did not get the expected error message:")
110 print(err)
111 sys.exit(1)
112
113#
114# cleanup
115#
116del f
117del input
118del reader
119del rngs
120libxml2.relaxNGCleanupTypes()
121
122# Memory debug specific
123libxml2.cleanupParser()
124if libxml2.debugMemory(1) == 0:
125 print("OK")
126else:
127 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
128 libxml2.dumpMemory()
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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