libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / python / tests / validRNG.py
blob57f13a45ce0bbff4420d5f4e1b048b600ff808fb
1 #!/usr/bin/python -u
2 import libxml2
3 import sys
5 ARG = 'test string'
7 class ErrorHandler:
9 def __init__(self):
10 self.errors = []
12 def handler(self, msg, data):
13 if data != ARG:
14 raise Exception("Error handler did not receive correct argument")
15 self.errors.append(msg)
17 # Memory debug specific
18 libxml2.debugMemory(1)
20 schema="""<?xml version="1.0"?>
21 <element name="foo"
22 xmlns="http://relaxng.org/ns/structure/1.0"
23 xmlns:a="http://relaxng.org/ns/annotation/1.0"
24 xmlns:ex1="http://www.example.com/n1"
25 xmlns:ex2="http://www.example.com/n2">
26 <a:documentation>A foo element.</a:documentation>
27 <element name="ex1:bar1">
28 <empty/>
29 </element>
30 <element name="ex2:bar2">
31 <empty/>
32 </element>
33 </element>
34 """
36 valid="""<?xml version="1.0"?>
37 <foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1"/><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""
39 invalid="""<?xml version="1.0"?>
40 <foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1">bad</pre1:bar1><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""
42 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
43 rngs = rngp.relaxNGParse()
44 ctxt = rngs.relaxNGNewValidCtxt()
45 e = ErrorHandler()
46 ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
48 # Test valid document
49 doc = libxml2.parseDoc(valid)
50 ret = doc.relaxNGValidateDoc(ctxt)
51 if ret != 0 or e.errors:
52 print("error doing RelaxNG validation")
53 sys.exit(1)
54 doc.freeDoc()
56 # Test invalid document
57 doc = libxml2.parseDoc(invalid)
58 ret = doc.relaxNGValidateDoc(ctxt)
59 if ret == 0 or not e.errors:
60 print("Error: document supposed to be RelaxNG invalid")
61 sys.exit(1)
62 doc.freeDoc()
64 del rngp
65 del rngs
66 del ctxt
67 libxml2.relaxNGCleanupTypes()
69 # Memory debug specific
70 libxml2.cleanupParser()
71 if libxml2.debugMemory(1) == 0:
72 print("OK")
73 else:
74 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
75 libxml2.dumpMemory()