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