reader2.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/bin/python -u
  2. #
  3. # this tests the DTD validation with the XmlTextReader interface
  4. #
  5. import sys
  6. import glob
  7. import string
  8. import StringIO
  9. import libxml2
  10. # Memory debug specific
  11. libxml2.debugMemory(1)
  12. err=""
  13. expect="""../../test/valid/rss.xml:177: element rss: validity error : Element rss does not carry attribute version
  14. </rss>
  15. ^
  16. ../../test/valid/xlink.xml:450: element termdef: validity error : ID dt-arc already defined
  17. <p><termdef id="dt-arc" term="Arc">An <ter
  18. ^
  19. ../../test/valid/xlink.xml:530: validity error : attribute def line 199 references an unknown ID "dt-xlg"
  20. ^
  21. """
  22. def callback(ctx, str):
  23. global err
  24. err = err + "%s" % (str)
  25. libxml2.registerErrorHandler(callback, "")
  26. valid_files = glob.glob("../../test/valid/*.x*")
  27. valid_files.sort()
  28. for file in valid_files:
  29. if string.find(file, "t8") != -1:
  30. continue
  31. reader = libxml2.newTextReaderFilename(file)
  32. #print "%s:" % (file)
  33. reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
  34. ret = reader.Read()
  35. while ret == 1:
  36. ret = reader.Read()
  37. if ret != 0:
  38. print "Error parsing and validating %s" % (file)
  39. #sys.exit(1)
  40. if err != expect:
  41. print err
  42. #
  43. # another separate test based on Stephane Bidoul one
  44. #
  45. s = """
  46. <!DOCTYPE test [
  47. <!ELEMENT test (x,b)>
  48. <!ELEMENT x (c)>
  49. <!ELEMENT b (#PCDATA)>
  50. <!ELEMENT c (#PCDATA)>
  51. <!ENTITY x "<x><c>xxx</c></x>">
  52. ]>
  53. <test>
  54. &x;
  55. <b>bbb</b>
  56. </test>
  57. """
  58. expect="""10,test
  59. 1,test
  60. 14,#text
  61. 1,x
  62. 1,c
  63. 3,#text
  64. 15,c
  65. 15,x
  66. 14,#text
  67. 1,b
  68. 3,#text
  69. 15,b
  70. 14,#text
  71. 15,test
  72. """
  73. res=""
  74. err=""
  75. input = libxml2.inputBuffer(StringIO.StringIO(s))
  76. reader = input.newTextReader("test2")
  77. reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
  78. reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
  79. reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
  80. reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
  81. while reader.Read() == 1:
  82. res = res + "%s,%s\n" % (reader.NodeType(),reader.Name())
  83. if res != expect:
  84. print "test2 failed: unexpected output"
  85. print res
  86. sys.exit(1)
  87. if err != "":
  88. print "test2 failed: validation error found"
  89. print err
  90. sys.exit(1)
  91. #
  92. # Another test for external entity parsing and validation
  93. #
  94. s = """<!DOCTYPE test [
  95. <!ELEMENT test (x)>
  96. <!ELEMENT x (#PCDATA)>
  97. <!ENTITY e SYSTEM "tst.ent">
  98. ]>
  99. <test>
  100. &e;
  101. </test>
  102. """
  103. tst_ent = """<x>hello</x>"""
  104. expect="""10 test
  105. 1 test
  106. 14 #text
  107. 1 x
  108. 3 #text
  109. 15 x
  110. 14 #text
  111. 15 test
  112. """
  113. res=""
  114. def myResolver(URL, ID, ctxt):
  115. if URL == "tst.ent":
  116. return(StringIO.StringIO(tst_ent))
  117. return None
  118. libxml2.setEntityLoader(myResolver)
  119. input = libxml2.inputBuffer(StringIO.StringIO(s))
  120. reader = input.newTextReader("test3")
  121. reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
  122. reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
  123. reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
  124. reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
  125. while reader.Read() == 1:
  126. res = res + "%s %s\n" % (reader.NodeType(),reader.Name())
  127. if res != expect:
  128. print "test3 failed: unexpected output"
  129. print res
  130. sys.exit(1)
  131. if err != "":
  132. print "test3 failed: validation error found"
  133. print err
  134. sys.exit(1)
  135. #
  136. # Another test for recursive entity parsing, validation, and replacement of
  137. # entities, making sure the entity ref node doesn't show up in that case
  138. #
  139. s = """<!DOCTYPE test [
  140. <!ELEMENT test (x, x)>
  141. <!ELEMENT x (y)>
  142. <!ELEMENT y (#PCDATA)>
  143. <!ENTITY x "<x>&y;</x>">
  144. <!ENTITY y "<y>yyy</y>">
  145. ]>
  146. <test>
  147. &x;
  148. &x;
  149. </test>"""
  150. expect="""10 test 0
  151. 1 test 0
  152. 14 #text 1
  153. 1 x 1
  154. 1 y 2
  155. 3 #text 3
  156. 15 y 2
  157. 15 x 1
  158. 14 #text 1
  159. 1 x 1
  160. 1 y 2
  161. 3 #text 3
  162. 15 y 2
  163. 15 x 1
  164. 14 #text 1
  165. 15 test 0
  166. """
  167. res=""
  168. err=""
  169. input = libxml2.inputBuffer(StringIO.StringIO(s))
  170. reader = input.newTextReader("test4")
  171. reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
  172. reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
  173. reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
  174. reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
  175. while reader.Read() == 1:
  176. res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
  177. if res != expect:
  178. print "test4 failed: unexpected output"
  179. print res
  180. sys.exit(1)
  181. if err != "":
  182. print "test4 failed: validation error found"
  183. print err
  184. sys.exit(1)
  185. #
  186. # The same test but without entity substitution this time
  187. #
  188. s = """<!DOCTYPE test [
  189. <!ELEMENT test (x, x)>
  190. <!ELEMENT x (y)>
  191. <!ELEMENT y (#PCDATA)>
  192. <!ENTITY x "<x>&y;</x>">
  193. <!ENTITY y "<y>yyy</y>">
  194. ]>
  195. <test>
  196. &x;
  197. &x;
  198. </test>"""
  199. expect="""10 test 0
  200. 1 test 0
  201. 14 #text 1
  202. 5 x 1
  203. 14 #text 1
  204. 5 x 1
  205. 14 #text 1
  206. 15 test 0
  207. """
  208. res=""
  209. err=""
  210. input = libxml2.inputBuffer(StringIO.StringIO(s))
  211. reader = input.newTextReader("test5")
  212. reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
  213. while reader.Read() == 1:
  214. res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
  215. if res != expect:
  216. print "test5 failed: unexpected output"
  217. print res
  218. if err != "":
  219. print "test5 failed: validation error found"
  220. print err
  221. #
  222. # cleanup
  223. #
  224. del input
  225. del reader
  226. # Memory debug specific
  227. libxml2.cleanupParser()
  228. if libxml2.debugMemory(1) == 0:
  229. print "OK"
  230. else:
  231. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  232. libxml2.dumpMemory()