reader6.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. schema="""<element name="foo" xmlns="http://relaxng.org/ns/structure/1.0"
  9. datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  10. <oneOrMore>
  11. <element name="label">
  12. <text/>
  13. </element>
  14. <optional>
  15. <element name="opt">
  16. <empty/>
  17. </element>
  18. </optional>
  19. <element name="item">
  20. <data type="byte"/>
  21. </element>
  22. </oneOrMore>
  23. </element>
  24. """
  25. # Memory debug specific
  26. libxml2.debugMemory(1)
  27. #
  28. # Parse the Relax NG Schemas
  29. #
  30. rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
  31. rngs = rngp.relaxNGParse()
  32. del rngp
  33. #
  34. # Parse and validate the correct document
  35. #
  36. docstr="""<foo>
  37. <label>some text</label>
  38. <item>100</item>
  39. </foo>"""
  40. f = StringIO.StringIO(docstr)
  41. input = libxml2.inputBuffer(f)
  42. reader = input.newTextReader("correct")
  43. reader.RelaxNGSetSchema(rngs)
  44. ret = reader.Read()
  45. while ret == 1:
  46. ret = reader.Read()
  47. if ret != 0:
  48. print "Error parsing the document"
  49. sys.exit(1)
  50. if reader.IsValid() != 1:
  51. print "Document failed to validate"
  52. sys.exit(1)
  53. #
  54. # Parse and validate the incorrect document
  55. #
  56. docstr="""<foo>
  57. <label>some text</label>
  58. <item>1000</item>
  59. </foo>"""
  60. err=""
  61. # RNG errors are not as good as before , TODO
  62. #expect="""RNG validity error: file error line 3 element text
  63. #Type byte doesn't allow value '1000'
  64. #RNG validity error: file error line 3 element text
  65. #Error validating datatype byte
  66. #RNG validity error: file error line 3 element text
  67. #Element item failed to validate content
  68. #"""
  69. expect="""Type byte doesn't allow value '1000'
  70. Error validating datatype byte
  71. Element item failed to validate content
  72. """
  73. def callback(ctx, str):
  74. global err
  75. err = err + "%s" % (str)
  76. libxml2.registerErrorHandler(callback, "")
  77. f = StringIO.StringIO(docstr)
  78. input = libxml2.inputBuffer(f)
  79. reader = input.newTextReader("error")
  80. reader.RelaxNGSetSchema(rngs)
  81. ret = reader.Read()
  82. while ret == 1:
  83. ret = reader.Read()
  84. if ret != 0:
  85. print "Error parsing the document"
  86. sys.exit(1)
  87. if reader.IsValid() != 0:
  88. print "Document failed to detect the validation error"
  89. sys.exit(1)
  90. if err != expect:
  91. print "Did not get the expected error message:"
  92. print err
  93. sys.exit(1)
  94. #
  95. # cleanup
  96. #
  97. del f
  98. del input
  99. del reader
  100. del rngs
  101. libxml2.relaxNGCleanupTypes()
  102. # Memory debug specific
  103. libxml2.cleanupParser()
  104. if libxml2.debugMemory(1) == 0:
  105. print "OK"
  106. else:
  107. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  108. libxml2.dumpMemory()