VirtualBox

source: vbox/trunk/src/libs/libxml2-2.9.4/python/tests/reader3.py@ 71370

最後變更 在這個檔案從71370是 65950,由 vboxsync 提交於 8 年 前

libxml 2.9.4: fix export

  • 屬性 svn:eol-style 設為 LF
  • 屬性 svn:executable 設為 *
檔案大小: 4.1 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
14docstr="""<?xml version='1.0'?>
15<!DOCTYPE doc [
16<!ENTITY tst "<p>test</p>">
17]>
18<doc>&tst;</doc>"""
19
20# Memory debug specific
21libxml2.debugMemory(1)
22
23#
24# First test, normal don't substitute entities.
25#
26f = str_io(docstr)
27input = libxml2.inputBuffer(f)
28reader = input.newTextReader("test_noent")
29ret = reader.Read()
30if ret != 1:
31 print("Error reading to root")
32 sys.exit(1)
33if reader.Name() == "doc" or reader.NodeType() == 10:
34 ret = reader.Read()
35if ret != 1:
36 print("Error reading to root")
37 sys.exit(1)
38if reader.Name() != "doc" or reader.NodeType() != 1:
39 print("test_normal: Error reading the root element")
40 sys.exit(1)
41ret = reader.Read()
42if ret != 1:
43 print("test_normal: Error reading to the entity")
44 sys.exit(1)
45if reader.Name() != "tst" or reader.NodeType() != 5:
46 print("test_normal: Error reading the entity")
47 sys.exit(1)
48ret = reader.Read()
49if ret != 1:
50 print("test_normal: Error reading to the end of root")
51 sys.exit(1)
52if reader.Name() != "doc" or reader.NodeType() != 15:
53 print("test_normal: Error reading the end of the root element")
54 sys.exit(1)
55ret = reader.Read()
56if ret != 0:
57 print("test_normal: Error detecting the end")
58 sys.exit(1)
59
60#
61# Second test, completely substitute the entities.
62#
63f = str_io(docstr)
64input = libxml2.inputBuffer(f)
65reader = input.newTextReader("test_noent")
66reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
67ret = reader.Read()
68if ret != 1:
69 print("Error reading to root")
70 sys.exit(1)
71if reader.Name() == "doc" or reader.NodeType() == 10:
72 ret = reader.Read()
73if ret != 1:
74 print("Error reading to root")
75 sys.exit(1)
76if reader.Name() != "doc" or reader.NodeType() != 1:
77 print("test_noent: Error reading the root element")
78 sys.exit(1)
79ret = reader.Read()
80if ret != 1:
81 print("test_noent: Error reading to the entity content")
82 sys.exit(1)
83if reader.Name() != "p" or reader.NodeType() != 1:
84 print("test_noent: Error reading the p element from entity")
85 sys.exit(1)
86ret = reader.Read()
87if ret != 1:
88 print("test_noent: Error reading to the text node")
89 sys.exit(1)
90if reader.NodeType() != 3 or reader.Value() != "test":
91 print("test_noent: Error reading the text node")
92 sys.exit(1)
93ret = reader.Read()
94if ret != 1:
95 print("test_noent: Error reading to the end of p element")
96 sys.exit(1)
97if reader.Name() != "p" or reader.NodeType() != 15:
98 print("test_noent: Error reading the end of the p element")
99 sys.exit(1)
100ret = reader.Read()
101if ret != 1:
102 print("test_noent: Error reading to the end of root")
103 sys.exit(1)
104if reader.Name() != "doc" or reader.NodeType() != 15:
105 print("test_noent: Error reading the end of the root element")
106 sys.exit(1)
107ret = reader.Read()
108if ret != 0:
109 print("test_noent: Error detecting the end")
110 sys.exit(1)
111
112#
113# third test, crazy stuff about empty element in external parsed entities
114#
115s = """<!DOCTYPE struct [
116<!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
117]>
118<struct>&simplestruct2.ent;</struct>
119"""
120expect="""10 struct 0 0
1211 struct 0 0
1221 descr 1 1
12315 struct 0 0
124"""
125res=""
126simplestruct2_ent="""<descr/>"""
127
128def myResolver(URL, ID, ctxt):
129 if URL == "simplestruct2.ent":
130 return(str_io(simplestruct2_ent))
131 return None
132
133libxml2.setEntityLoader(myResolver)
134
135input = libxml2.inputBuffer(str_io(s))
136reader = input.newTextReader("test3")
137reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
138while reader.Read() == 1:
139 res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
140 reader.Depth(),reader.IsEmptyElement())
141
142if res != expect:
143 print("test3 failed: unexpected output")
144 print(res)
145 sys.exit(1)
146
147#
148# cleanup
149#
150del f
151del input
152del reader
153
154# Memory debug specific
155libxml2.cleanupParser()
156if libxml2.debugMemory(1) == 0:
157 print("OK")
158else:
159 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
160 libxml2.dumpMemory()
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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