Annotation of embedaddon/libxml2/python/tests/validSchemas.py, revision 1.1.1.2
1.1 misho 1: #!/usr/bin/python -u
2: import libxml2
3: import sys
4:
5: ARG = 'test string'
6:
7: class ErrorHandler:
8:
9: def __init__(self):
10: self.errors = []
11:
12: def handler(self, msg, data):
13: if data != ARG:
1.1.1.2 ! misho 14: raise Exception("Error handler did not receive correct argument")
1.1 misho 15: self.errors.append(msg)
16:
17: # Memory debug specific
18: libxml2.debugMemory(1)
19:
20: schema="""<?xml version="1.0" encoding="iso-8859-1"?>
21: <schema xmlns = "http://www.w3.org/2001/XMLSchema">
22: <element name = "Customer">
23: <complexType>
24: <sequence>
25: <element name = "FirstName" type = "string" />
26: <element name = "MiddleInitial" type = "string" />
27: <element name = "LastName" type = "string" />
28: </sequence>
29: <attribute name = "customerID" type = "integer" />
30: </complexType>
31: </element>
32: </schema>"""
33:
34: valid="""<?xml version="1.0" encoding="iso-8859-1"?>
35: <Customer customerID = "24332">
36: <FirstName>Raymond</FirstName>
37: <MiddleInitial>G</MiddleInitial>
38: <LastName>Bayliss</LastName>
39: </Customer>
40: """
41:
42: invalid="""<?xml version="1.0" encoding="iso-8859-1"?>
43: <Customer customerID = "24332">
44: <MiddleInitial>G</MiddleInitial>
45: <LastName>Bayliss</LastName>
46: </Customer>
47: """
48:
49: e = ErrorHandler()
50: ctxt_parser = libxml2.schemaNewMemParserCtxt(schema, len(schema))
51: ctxt_schema = ctxt_parser.schemaParse()
52: ctxt_valid = ctxt_schema.schemaNewValidCtxt()
53: ctxt_valid.setValidityErrorHandler(e.handler, e.handler, ARG)
54:
55: # Test valid document
56: doc = libxml2.parseDoc(valid)
57: ret = doc.schemaValidateDoc(ctxt_valid)
58: if ret != 0 or e.errors:
1.1.1.2 ! misho 59: print("error doing schema validation")
1.1 misho 60: sys.exit(1)
61: doc.freeDoc()
62:
63: # Test invalid document
64: doc = libxml2.parseDoc(invalid)
65: ret = doc.schemaValidateDoc(ctxt_valid)
66: if ret == 0 or not e.errors:
1.1.1.2 ! misho 67: print("Error: document supposer to be schema invalid")
1.1 misho 68: sys.exit(1)
69: doc.freeDoc()
70:
71: del ctxt_parser
72: del ctxt_schema
73: del ctxt_valid
74: libxml2.schemaCleanupTypes()
75:
76: # Memory debug specific
77: libxml2.cleanupParser()
78: if libxml2.debugMemory(1) == 0:
1.1.1.2 ! misho 79: print("OK")
1.1 misho 80: else:
1.1.1.2 ! misho 81: print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
1.1 misho 82: libxml2.dumpMemory()
83:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>