reader.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/usr/bin/python -u
  2. # -*- coding: ISO-8859-1 -*-
  3. #
  4. # this tests the basic APIs of the XmlTextReader interface
  5. #
  6. import libxml2
  7. import StringIO
  8. import sys
  9. # Memory debug specific
  10. libxml2.debugMemory(1)
  11. f = StringIO.StringIO("""<a><b b1="b1"/><c>content of c</c></a>""")
  12. input = libxml2.inputBuffer(f)
  13. reader = input.newTextReader("test1")
  14. ret = reader.Read()
  15. if ret != 1:
  16. print "test1: Error reading to first element"
  17. sys.exit(1)
  18. if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
  19. reader.NodeType() != 1 or reader.HasAttributes() != 0:
  20. print "test1: Error reading the first element"
  21. sys.exit(1)
  22. ret = reader.Read()
  23. if ret != 1:
  24. print "test1: Error reading to second element"
  25. sys.exit(1)
  26. if reader.Name() != "b" or reader.IsEmptyElement() != 1 or \
  27. reader.NodeType() != 1 or reader.HasAttributes() != 1:
  28. print "test1: Error reading the second element"
  29. sys.exit(1)
  30. ret = reader.Read()
  31. if ret != 1:
  32. print "test1: Error reading to third element"
  33. sys.exit(1)
  34. if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
  35. reader.NodeType() != 1 or reader.HasAttributes() != 0:
  36. print "test1: Error reading the third element"
  37. sys.exit(1)
  38. ret = reader.Read()
  39. if ret != 1:
  40. print "test1: Error reading to text node"
  41. sys.exit(1)
  42. if reader.Name() != "#text" or reader.IsEmptyElement() != 0 or \
  43. reader.NodeType() != 3 or reader.HasAttributes() != 0 or \
  44. reader.Value() != "content of c":
  45. print "test1: Error reading the text node"
  46. sys.exit(1)
  47. ret = reader.Read()
  48. if ret != 1:
  49. print "test1: Error reading to end of third element"
  50. sys.exit(1)
  51. if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
  52. reader.NodeType() != 15 or reader.HasAttributes() != 0:
  53. print "test1: Error reading the end of third element"
  54. sys.exit(1)
  55. ret = reader.Read()
  56. if ret != 1:
  57. print "test1: Error reading to end of first element"
  58. sys.exit(1)
  59. if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
  60. reader.NodeType() != 15 or reader.HasAttributes() != 0:
  61. print "test1: Error reading the end of first element"
  62. sys.exit(1)
  63. ret = reader.Read()
  64. if ret != 0:
  65. print "test1: Error reading to end of document"
  66. sys.exit(1)
  67. #
  68. # example from the XmlTextReader docs
  69. #
  70. f = StringIO.StringIO("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""")
  71. input = libxml2.inputBuffer(f)
  72. reader = input.newTextReader("test2")
  73. ret = reader.Read()
  74. if ret != 1:
  75. print "Error reading test element"
  76. sys.exit(1)
  77. if reader.GetAttributeNo(0) != "urn:datatypes" or \
  78. reader.GetAttributeNo(1) != "int" or \
  79. reader.GetAttributeNs("type", "urn:datatypes") != "int" or \
  80. reader.GetAttribute("dt:type") != "int":
  81. print "error reading test attributes"
  82. sys.exit(1)
  83. #
  84. # example from the XmlTextReader docs
  85. #
  86. f = StringIO.StringIO("""<root xmlns:a="urn:456">
  87. <item>
  88. <ref href="a:b"/>
  89. </item>
  90. </root>""")
  91. input = libxml2.inputBuffer(f)
  92. reader = input.newTextReader("test3")
  93. ret = reader.Read()
  94. while ret == 1:
  95. if reader.Name() == "ref":
  96. if reader.LookupNamespace("a") != "urn:456":
  97. print "error resolving namespace prefix"
  98. sys.exit(1)
  99. break
  100. ret = reader.Read()
  101. if ret != 1:
  102. print "Error finding the ref element"
  103. sys.exit(1)
  104. #
  105. # Home made example for the various attribute access functions
  106. #
  107. f = StringIO.StringIO("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""")
  108. input = libxml2.inputBuffer(f)
  109. reader = input.newTextReader("test4")
  110. ret = reader.Read()
  111. if ret != 1:
  112. print "Error reading the testattr element"
  113. sys.exit(1)
  114. #
  115. # Attribute exploration by index
  116. #
  117. if reader.MoveToAttributeNo(0) != 1:
  118. print "Failed moveToAttribute(0)"
  119. sys.exit(1)
  120. if reader.Value() != "urn:1":
  121. print "Failed to read attribute(0)"
  122. sys.exit(1)
  123. if reader.Name() != "xmlns":
  124. print "Failed to read attribute(0) name"
  125. sys.exit(1)
  126. if reader.MoveToAttributeNo(1) != 1:
  127. print "Failed moveToAttribute(1)"
  128. sys.exit(1)
  129. if reader.Value() != "urn:2":
  130. print "Failed to read attribute(1)"
  131. sys.exit(1)
  132. if reader.Name() != "xmlns:a":
  133. print "Failed to read attribute(1) name"
  134. sys.exit(1)
  135. if reader.MoveToAttributeNo(2) != 1:
  136. print "Failed moveToAttribute(2)"
  137. sys.exit(1)
  138. if reader.Value() != "b":
  139. print "Failed to read attribute(2)"
  140. sys.exit(1)
  141. if reader.Name() != "b":
  142. print "Failed to read attribute(2) name"
  143. sys.exit(1)
  144. if reader.MoveToAttributeNo(3) != 1:
  145. print "Failed moveToAttribute(3)"
  146. sys.exit(1)
  147. if reader.Value() != "a:b":
  148. print "Failed to read attribute(3)"
  149. sys.exit(1)
  150. if reader.Name() != "a:b":
  151. print "Failed to read attribute(3) name"
  152. sys.exit(1)
  153. #
  154. # Attribute exploration by name
  155. #
  156. if reader.MoveToAttribute("xmlns") != 1:
  157. print "Failed moveToAttribute('xmlns')"
  158. sys.exit(1)
  159. if reader.Value() != "urn:1":
  160. print "Failed to read attribute('xmlns')"
  161. sys.exit(1)
  162. if reader.MoveToAttribute("xmlns:a") != 1:
  163. print "Failed moveToAttribute('xmlns')"
  164. sys.exit(1)
  165. if reader.Value() != "urn:2":
  166. print "Failed to read attribute('xmlns:a')"
  167. sys.exit(1)
  168. if reader.MoveToAttribute("b") != 1:
  169. print "Failed moveToAttribute('b')"
  170. sys.exit(1)
  171. if reader.Value() != "b":
  172. print "Failed to read attribute('b')"
  173. sys.exit(1)
  174. if reader.MoveToAttribute("a:b") != 1:
  175. print "Failed moveToAttribute('a:b')"
  176. sys.exit(1)
  177. if reader.Value() != "a:b":
  178. print "Failed to read attribute('a:b')"
  179. sys.exit(1)
  180. if reader.MoveToAttributeNs("b", "urn:2") != 1:
  181. print "Failed moveToAttribute('b', 'urn:2')"
  182. sys.exit(1)
  183. if reader.Value() != "a:b":
  184. print "Failed to read attribute('b', 'urn:2')"
  185. sys.exit(1)
  186. #
  187. # Go back and read in sequence
  188. #
  189. if reader.MoveToElement() != 1:
  190. print "Failed to move back to element"
  191. sys.exit(1)
  192. if reader.MoveToFirstAttribute() != 1:
  193. print "Failed to move to first attribute"
  194. sys.exit(1)
  195. if reader.Value() != "urn:1":
  196. print "Failed to read attribute(0)"
  197. sys.exit(1)
  198. if reader.Name() != "xmlns":
  199. print "Failed to read attribute(0) name"
  200. sys.exit(1)
  201. if reader.MoveToNextAttribute() != 1:
  202. print "Failed to move to next attribute"
  203. sys.exit(1)
  204. if reader.Value() != "urn:2":
  205. print "Failed to read attribute(1)"
  206. sys.exit(1)
  207. if reader.Name() != "xmlns:a":
  208. print "Failed to read attribute(1) name"
  209. sys.exit(1)
  210. if reader.MoveToNextAttribute() != 1:
  211. print "Failed to move to next attribute"
  212. sys.exit(1)
  213. if reader.Value() != "b":
  214. print "Failed to read attribute(2)"
  215. sys.exit(1)
  216. if reader.Name() != "b":
  217. print "Failed to read attribute(2) name"
  218. sys.exit(1)
  219. if reader.MoveToNextAttribute() != 1:
  220. print "Failed to move to next attribute"
  221. sys.exit(1)
  222. if reader.Value() != "a:b":
  223. print "Failed to read attribute(3)"
  224. sys.exit(1)
  225. if reader.Name() != "a:b":
  226. print "Failed to read attribute(3) name"
  227. sys.exit(1)
  228. if reader.MoveToNextAttribute() != 0:
  229. print "Failed to detect last attribute"
  230. sys.exit(1)
  231. #
  232. # a couple of tests for namespace nodes
  233. #
  234. f = StringIO.StringIO("""<a xmlns="http://example.com/foo"/>""")
  235. input = libxml2.inputBuffer(f)
  236. reader = input.newTextReader("test6")
  237. ret = reader.Read()
  238. if ret != 1:
  239. print "test6: failed to Read()"
  240. sys.exit(1)
  241. ret = reader.MoveToFirstAttribute()
  242. if ret != 1:
  243. print "test6: failed to MoveToFirstAttribute()"
  244. sys.exit(1)
  245. if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
  246. reader.LocalName() != "xmlns" or reader.Name() != "xmlns" or \
  247. reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
  248. print "test6: failed to read the namespace node"
  249. sys.exit(1)
  250. f = StringIO.StringIO("""<a xmlns:prefix="http://example.com/foo"/>""")
  251. input = libxml2.inputBuffer(f)
  252. reader = input.newTextReader("test7")
  253. ret = reader.Read()
  254. if ret != 1:
  255. print "test7: failed to Read()"
  256. sys.exit(1)
  257. ret = reader.MoveToFirstAttribute()
  258. if ret != 1:
  259. print "test7: failed to MoveToFirstAttribute()"
  260. sys.exit(1)
  261. if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
  262. reader.LocalName() != "prefix" or reader.Name() != "xmlns:prefix" or \
  263. reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
  264. print "test7: failed to read the namespace node"
  265. sys.exit(1)
  266. #
  267. # Test for a limit case:
  268. #
  269. f = StringIO.StringIO("""<a/>""")
  270. input = libxml2.inputBuffer(f)
  271. reader = input.newTextReader("test8")
  272. ret = reader.Read()
  273. if ret != 1:
  274. print "test8: failed to read the node"
  275. sys.exit(1)
  276. if reader.Name() != "a" or reader.IsEmptyElement() != 1:
  277. print "test8: failed to analyze the node"
  278. sys.exit(1)
  279. ret = reader.Read()
  280. if ret != 0:
  281. print "test8: failed to detect the EOF"
  282. sys.exit(1)
  283. #
  284. # Another test provided by Stéphane Bidoul and checked with C#
  285. #
  286. def tst_reader(s):
  287. f = StringIO.StringIO(s)
  288. input = libxml2.inputBuffer(f)
  289. reader = input.newTextReader("tst")
  290. res = ""
  291. while reader.Read():
  292. res=res + "%s (%s) [%s] %d %d\n" % (reader.NodeType(),reader.Name(),
  293. reader.Value(), reader.IsEmptyElement(),
  294. reader.Depth())
  295. if reader.NodeType() == 1: # Element
  296. while reader.MoveToNextAttribute():
  297. res = res + "-- %s (%s) [%s] %d %d\n" % (reader.NodeType(),
  298. reader.Name(),reader.Value(),
  299. reader.IsEmptyElement(), reader.Depth())
  300. return res
  301. doc="""<a><b b1="b1"/><c>content of c</c></a>"""
  302. expect="""1 (a) [None] 0 0
  303. 1 (b) [None] 1 1
  304. -- 2 (b1) [b1] 0 2
  305. 1 (c) [None] 0 1
  306. 3 (#text) [content of c] 0 2
  307. 15 (c) [None] 0 1
  308. 15 (a) [None] 0 0
  309. """
  310. res = tst_reader(doc)
  311. if res != expect:
  312. print "test5 failed"
  313. print res
  314. sys.exit(1)
  315. doc="""<test><b/><c/></test>"""
  316. expect="""1 (test) [None] 0 0
  317. 1 (b) [None] 1 1
  318. 1 (c) [None] 1 1
  319. 15 (test) [None] 0 0
  320. """
  321. res = tst_reader(doc)
  322. if res != expect:
  323. print "test9 failed"
  324. print res
  325. sys.exit(1)
  326. doc="""<a><b>bbb</b><c>ccc</c></a>"""
  327. expect="""1 (a) [None] 0 0
  328. 1 (b) [None] 0 1
  329. 3 (#text) [bbb] 0 2
  330. 15 (b) [None] 0 1
  331. 1 (c) [None] 0 1
  332. 3 (#text) [ccc] 0 2
  333. 15 (c) [None] 0 1
  334. 15 (a) [None] 0 0
  335. """
  336. res = tst_reader(doc)
  337. if res != expect:
  338. print "test10 failed"
  339. print res
  340. sys.exit(1)
  341. doc="""<test a="a"/>"""
  342. expect="""1 (test) [None] 1 0
  343. -- 2 (a) [a] 0 1
  344. """
  345. res = tst_reader(doc)
  346. if res != expect:
  347. print "test11 failed"
  348. print res
  349. sys.exit(1)
  350. doc="""<test><a>aaa</a><b/></test>"""
  351. expect="""1 (test) [None] 0 0
  352. 1 (a) [None] 0 1
  353. 3 (#text) [aaa] 0 2
  354. 15 (a) [None] 0 1
  355. 1 (b) [None] 1 1
  356. 15 (test) [None] 0 0
  357. """
  358. res = tst_reader(doc)
  359. if res != expect:
  360. print "test12 failed"
  361. print res
  362. sys.exit(1)
  363. doc="""<test><p></p></test>"""
  364. expect="""1 (test) [None] 0 0
  365. 1 (p) [None] 0 1
  366. 15 (p) [None] 0 1
  367. 15 (test) [None] 0 0
  368. """
  369. res = tst_reader(doc)
  370. if res != expect:
  371. print "test13 failed"
  372. print res
  373. sys.exit(1)
  374. doc="""<p></p>"""
  375. expect="""1 (p) [None] 0 0
  376. 15 (p) [None] 0 0
  377. """
  378. res = tst_reader(doc)
  379. if res != expect:
  380. print "test14 failed"
  381. print res
  382. sys.exit(1)
  383. #
  384. # test from bug #108801
  385. #
  386. doc="""<?xml version="1.0" standalone="no"?>
  387. <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  388. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
  389. ]>
  390. <article>
  391. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  392. </article>
  393. """
  394. expect="""10 (article) [None] 0 0
  395. 1 (article) [None] 0 0
  396. 3 (#text) [
  397. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  398. ] 0 1
  399. 15 (article) [None] 0 0
  400. """
  401. res = tst_reader(doc)
  402. if res != expect:
  403. print "test15 failed"
  404. print res
  405. sys.exit(1)
  406. #
  407. # cleanup for memory allocation counting
  408. #
  409. del f
  410. del input
  411. del reader
  412. # Memory debug specific
  413. libxml2.cleanupParser()
  414. if libxml2.debugMemory(1) == 0:
  415. print "OK"
  416. else:
  417. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  418. libxml2.dumpMemory()