1.1 --- a/libxml2dom/macrolib/macrolib.py Sat Aug 30 00:51:04 2008 +0200
1.2 +++ b/libxml2dom/macrolib/macrolib.py Sun Aug 31 01:31:31 2008 +0200
1.3 @@ -459,7 +459,7 @@
1.4 elif Node_nodeType(other) == xml.dom.Node.CDATA_SECTION_NODE:
1.5 return Node_createCDATASection(node, Node_data(other))
1.6
1.7 - raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other)))
1.8 + raise xml.dom.NotSupportedErr("Node type '%s' (%d) not supported." % (other, Node_nodeType(other)))
1.9
1.10 def Node_importNode_DOM(node, other, deep):
1.11 if other.nodeType == xml.dom.Node.ELEMENT_NODE:
1.12 @@ -484,8 +484,7 @@
1.13 elif other.nodeType == xml.dom.Node.CDATA_SECTION_NODE:
1.14 return Node_createCDATASection(node, other.data)
1.15
1.16 - raise xml.dom.DOMException(
1.17 - xml.dom.NOT_SUPPORTED_ERR,
1.18 + raise xml.dom.NotSupportedErr(
1.19 "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType)
1.20 )
1.21
1.22 @@ -637,6 +636,7 @@
1.23
1.24 finally:
1.25 Parser_resetError(error)
1.26 + libxml2mod.xmlFreeParserCtxt(context)
1.27
1.28 def toString(node, encoding=None, prettyprint=0):
1.29 return libxml2mod.serializeNode(node, encoding, prettyprint)
1.30 @@ -735,4 +735,47 @@
1.31 def Parser_parse(context):
1.32 libxml2mod.xmlParseDocument(context)
1.33
1.34 +# Schema and validation helper functions and classes.
1.35 +
1.36 +def Document_schema(doc):
1.37 + return Schema_parse(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc))
1.38 +
1.39 +def Document_schemaFromString(s):
1.40 + return Schema_parse(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s)))
1.41 +
1.42 +def Document_validate(schema, doc, error_handler):
1.43 + validator_context = libxml2mod.xmlRelaxNGNewValidCtxt(schema)
1.44 + handler = ValidationHandler(error_handler)
1.45 + libxml2mod.xmlRelaxNGSetValidErrors(validator_context, handler.error, handler.warning, None)
1.46 + try:
1.47 + status = libxml2mod.xmlRelaxNGValidateDoc(validator_context, doc)
1.48 + return status == 0
1.49 + finally:
1.50 + libxml2mod.xmlRelaxNGFreeValidCtxt(validator_context)
1.51 +
1.52 +def Schema_parse(context):
1.53 + try:
1.54 + return libxml2mod.xmlRelaxNGParse(context)
1.55 + finally:
1.56 + libxml2mod.xmlRelaxNGFreeParserCtxt(context)
1.57 +
1.58 +def Schema_free(schema):
1.59 + libxml2mod.xmlRelaxNGFree(schema)
1.60 +
1.61 +class ValidationHandler:
1.62 +
1.63 + """
1.64 + A handler which collects validation errors and warnings and passes them to a
1.65 + DOMErrorHandler.
1.66 + """
1.67 +
1.68 + def __init__(self, error_handler):
1.69 + self.error_handler = error_handler
1.70 +
1.71 + def error(self, msg, arg):
1.72 + self.error_handler.handleError(DOMError(DOMError.SEVERITY_FATAL_ERROR, msg.strip()))
1.73 +
1.74 + def warning(self, msg, arg):
1.75 + self.error_handler.handleError(DOMError(DOMError.SEVERITY_WARNING, msg.strip()))
1.76 +
1.77 # vim: tabstop=4 expandtab shiftwidth=4