reader3.py 4.0 KB

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