walker.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # Memory debug specific
  9. libxml2.debugMemory(1)
  10. result = ""
  11. def processNode(reader):
  12. global result
  13. result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
  14. reader.Name(), reader.IsEmptyElement())
  15. #
  16. # Parse a document testing the readerForxxx API
  17. #
  18. docstr="""<foo>
  19. <label>some text</label>
  20. <item>100</item>
  21. </foo>"""
  22. expect="""0 1 foo 0
  23. 1 14 #text 0
  24. 1 1 label 0
  25. 2 3 #text 0
  26. 1 15 label 0
  27. 1 14 #text 0
  28. 1 1 item 0
  29. 2 3 #text 0
  30. 1 15 item 0
  31. 1 14 #text 0
  32. 0 15 foo 0
  33. """
  34. result = ""
  35. doc = libxml2.parseDoc(docstr)
  36. reader = doc.readerWalker();
  37. ret = reader.Read()
  38. while ret == 1:
  39. processNode(reader)
  40. ret = reader.Read()
  41. if ret != 0:
  42. print "Error parsing the document test1"
  43. sys.exit(1)
  44. if result != expect:
  45. print "Unexpected result for test1"
  46. print result
  47. sys.exit(1)
  48. doc.freeDoc()
  49. #
  50. # Reuse the reader for another document testing the ReaderNewWalker API
  51. #
  52. docstr="""<foo>
  53. <label>some text</label>
  54. <item>1000</item>
  55. </foo>"""
  56. expect="""0 1 foo 0
  57. 1 14 #text 0
  58. 1 1 label 0
  59. 2 3 #text 0
  60. 1 15 label 0
  61. 1 14 #text 0
  62. 1 1 item 0
  63. 2 3 #text 0
  64. 1 15 item 0
  65. 1 14 #text 0
  66. 0 15 foo 0
  67. """
  68. result = ""
  69. doc = libxml2.parseDoc(docstr)
  70. reader.NewWalker(doc)
  71. ret = reader.Read()
  72. while ret == 1:
  73. processNode(reader)
  74. ret = reader.Read()
  75. if ret != 0:
  76. print "Error parsing the document test2"
  77. sys.exit(1)
  78. if result != expect:
  79. print "Unexpected result for test2"
  80. print result
  81. sys.exit(1)
  82. doc.freeDoc()
  83. #
  84. # Reuse the reader for another document testing the ReaderNewxxx API
  85. #
  86. docstr="""<foo>
  87. <label>some text</label>
  88. <item>1000</item>
  89. </foo>"""
  90. expect="""0 1 foo 0
  91. 1 14 #text 0
  92. 1 1 label 0
  93. 2 3 #text 0
  94. 1 15 label 0
  95. 1 14 #text 0
  96. 1 1 item 0
  97. 2 3 #text 0
  98. 1 15 item 0
  99. 1 14 #text 0
  100. 0 15 foo 0
  101. """
  102. result = ""
  103. reader.NewDoc(docstr, "test3", None, 0)
  104. ret = reader.Read()
  105. while ret == 1:
  106. processNode(reader)
  107. ret = reader.Read()
  108. if ret != 0:
  109. print "Error parsing the document test3"
  110. sys.exit(1)
  111. if result != expect:
  112. print "Unexpected result for test3"
  113. print result
  114. sys.exit(1)
  115. #
  116. # cleanup
  117. #
  118. del reader
  119. # Memory debug specific
  120. libxml2.cleanupParser()
  121. if libxml2.debugMemory(1) == 0:
  122. print "OK"
  123. else:
  124. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  125. libxml2.dumpMemory()