home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-libxml2 / examples / reader6.py < prev    next >
Encoding:
Python Source  |  2003-10-20  |  2.5 KB  |  124 lines

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