1.1 --- a/README.txt Sat Aug 30 00:51:04 2008 +0200
1.2 +++ b/README.txt Sun Aug 31 01:31:31 2008 +0200
1.3 @@ -77,6 +77,7 @@
1.4 * Introduced support for validation, together with the libxml2dom.errors
1.5 module.
1.6 * Improved error messages related to parsing.
1.7 + * Added DOMConfiguration support to documents.
1.8
1.9 New in libxml2dom 0.4.6 (Changes since libxml2dom 0.4.5)
1.10 --------------------------------------------------------
2.1 --- a/libxml2dom/__init__.py Sat Aug 30 00:51:04 2008 +0200
2.2 +++ b/libxml2dom/__init__.py Sun Aug 31 01:31:31 2008 +0200
2.3 @@ -29,6 +29,7 @@
2.4 toString as Node_toString, toStream as Node_toStream, \
2.5 toFile as Node_toFile
2.6 import urllib # for parseURI in HTML mode
2.7 +import libxml2dom.errors
2.8
2.9 # Standard namespaces.
2.10
2.11 @@ -392,9 +393,9 @@
2.12
2.13 def insertBefore(self, tmp, oldNode):
2.14 if tmp.ownerDocument != self.ownerDocument:
2.15 - raise xml.dom.DOMException(xml.dom.WRONG_DOCUMENT_ERR)
2.16 + raise xml.dom.WrongDocumentErr()
2.17 if oldNode.parentNode != self:
2.18 - raise xml.dom.DOMException(xml.dom.NOT_FOUND_ERR)
2.19 + raise xml.dom.NotFoundErr()
2.20 if hasattr(tmp, "as_native_node"):
2.21 return self.impl.get_node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self)
2.22 else:
2.23 @@ -402,9 +403,9 @@
2.24
2.25 def replaceChild(self, tmp, oldNode):
2.26 if tmp.ownerDocument != self.ownerDocument:
2.27 - raise xml.dom.DOMException(xml.dom.WRONG_DOCUMENT_ERR)
2.28 + raise xml.dom.WrongDocumentErr()
2.29 if oldNode.parentNode != self:
2.30 - raise xml.dom.DOMException(xml.dom.NOT_FOUND_ERR)
2.31 + raise xml.dom.NotFoundErr()
2.32 if hasattr(tmp, "as_native_node"):
2.33 return self.impl.get_node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self)
2.34 else:
2.35 @@ -412,7 +413,7 @@
2.36
2.37 def appendChild(self, tmp):
2.38 if tmp.ownerDocument != self.ownerDocument:
2.39 - raise xml.dom.DOMException(xml.dom.WRONG_DOCUMENT_ERR)
2.40 + raise xml.dom.WrongDocumentErr()
2.41 if hasattr(tmp, "as_native_node"):
2.42 return self.impl.get_node(Node_appendChild(self._node, tmp.as_native_node()), self)
2.43 else:
2.44 @@ -558,12 +559,23 @@
2.45
2.46 """
2.47 An abstract class providing document-level housekeeping and distinct
2.48 - functionality.
2.49 + functionality. Configuration of the document is also supported.
2.50 + See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMConfiguration
2.51 """
2.52
2.53 + # Constants from
2.54 + # See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-NodeEditVAL
2.55 +
2.56 + VAL_TRUE = 5
2.57 + VAL_FALSE = 6
2.58 + VAL_UNKNOWN = 7
2.59 +
2.60 def __init__(self, node, impl):
2.61 self._node = node
2.62 self.implementation = self.impl = impl
2.63 + self.error_handler = libxml2dom.errors.DOMErrorHandler()
2.64 +
2.65 + # Standard DOM properties and their implementations.
2.66
2.67 def _documentElement(self):
2.68 return self.xpath("*")[0]
2.69 @@ -578,6 +590,53 @@
2.70 documentElement = property(_documentElement)
2.71 ownerDocument = property(_ownerDocument)
2.72
2.73 + # DOM Level 3 Core DOMConfiguration methods.
2.74 +
2.75 + def setParameter(self, name, value):
2.76 + if name == "error-handler":
2.77 + raise xml.dom.NotSupportedErr()
2.78 + raise xml.dom.NotFoundErr()
2.79 +
2.80 + def getParameter(self, name):
2.81 + if name == "error-handler":
2.82 + return self.error_handler
2.83 + raise xml.dom.NotFoundErr()
2.84 +
2.85 + def canSetParameter(self, name, value):
2.86 + return 0
2.87 +
2.88 + def _parameterNames(self):
2.89 + return []
2.90 +
2.91 + # Extensions to the usual PyXML API.
2.92 +
2.93 + def validate(self, doc):
2.94 +
2.95 + """
2.96 + Validate the document against the given schema document, 'doc'.
2.97 + """
2.98 +
2.99 + if hasattr(doc, "as_native_node"):
2.100 + _schema = Document_schema(doc.as_native_node())
2.101 + else:
2.102 + _schema = Document_schemaFromString(doc.toString())
2.103 + try:
2.104 + self.error_handler.reset()
2.105 + return Document_validate(_schema, self._node, self.error_handler)
2.106 + finally:
2.107 + Schema_free(_schema)
2.108 +
2.109 + # DOM Level 3 Validation methods.
2.110 +
2.111 + def validateDocument(self, doc):
2.112 +
2.113 + """
2.114 + Validate the document against the given schema document, 'doc'.
2.115 + See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-DocumentEditVAL-validateDocument
2.116 + """
2.117 +
2.118 + return self.validate(doc) and self.VAL_TRUE or self.VAL_FALSE
2.119 +
2.120 class Document(_Document, Node):
2.121
2.122 """
3.1 --- a/libxml2dom/errors.py Sat Aug 30 00:51:04 2008 +0200
3.2 +++ b/libxml2dom/errors.py Sun Aug 31 01:31:31 2008 +0200
3.3 @@ -2,7 +2,6 @@
3.4
3.5 """
3.6 Errors for DOM Level 3.
3.7 -See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#ERROR-Interfaces-DOMError
3.8
3.9 Copyright (C) 2008 Paul Boddie <paul@boddie.org.uk>
3.10
3.11 @@ -22,7 +21,10 @@
3.12
3.13 class DOMError:
3.14
3.15 - "DOM Level 3 Core exception."
3.16 + """
3.17 + DOM Level 3 Core exception.
3.18 + See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#ERROR-Interfaces-DOMError
3.19 + """
3.20
3.21 SEVERITY_WARNING = 1
3.22 SEVERITY_ERROR = 2
3.23 @@ -42,4 +44,30 @@
3.24 def __str__(self):
3.25 return "DOMError: %s" % self.message
3.26
3.27 +# NOTE: Find a reasonable way of exposing error details.
3.28 +
3.29 +class DOMErrorHandler:
3.30 +
3.31 + """
3.32 + DOM Level 3 Core error handler.
3.33 + See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#ERROR-Interfaces-DOMErrorHandler
3.34 + """
3.35 +
3.36 + def __init__(self):
3.37 + self.errors = []
3.38 +
3.39 + def handleError(self, error):
3.40 + self.errors.append(error)
3.41 +
3.42 + # Special extension methods.
3.43 +
3.44 + def reset(self):
3.45 + self.errors = []
3.46 +
3.47 + def __repr__(self):
3.48 + return "DOMErrorHandler()"
3.49 +
3.50 + def __str__(self):
3.51 + return "DOMErrorHandler: %r" % self.errors
3.52 +
3.53 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/libxml2dom/macrolib/macrolib.py Sat Aug 30 00:51:04 2008 +0200
4.2 +++ b/libxml2dom/macrolib/macrolib.py Sun Aug 31 01:31:31 2008 +0200
4.3 @@ -459,7 +459,7 @@
4.4 elif Node_nodeType(other) == xml.dom.Node.CDATA_SECTION_NODE:
4.5 return Node_createCDATASection(node, Node_data(other))
4.6
4.7 - raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other)))
4.8 + raise xml.dom.NotSupportedErr("Node type '%s' (%d) not supported." % (other, Node_nodeType(other)))
4.9
4.10 def Node_importNode_DOM(node, other, deep):
4.11 if other.nodeType == xml.dom.Node.ELEMENT_NODE:
4.12 @@ -484,8 +484,7 @@
4.13 elif other.nodeType == xml.dom.Node.CDATA_SECTION_NODE:
4.14 return Node_createCDATASection(node, other.data)
4.15
4.16 - raise xml.dom.DOMException(
4.17 - xml.dom.NOT_SUPPORTED_ERR,
4.18 + raise xml.dom.NotSupportedErr(
4.19 "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType)
4.20 )
4.21
4.22 @@ -637,6 +636,7 @@
4.23
4.24 finally:
4.25 Parser_resetError(error)
4.26 + libxml2mod.xmlFreeParserCtxt(context)
4.27
4.28 def toString(node, encoding=None, prettyprint=0):
4.29 return libxml2mod.serializeNode(node, encoding, prettyprint)
4.30 @@ -735,4 +735,47 @@
4.31 def Parser_parse(context):
4.32 libxml2mod.xmlParseDocument(context)
4.33
4.34 +# Schema and validation helper functions and classes.
4.35 +
4.36 +def Document_schema(doc):
4.37 + return Schema_parse(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc))
4.38 +
4.39 +def Document_schemaFromString(s):
4.40 + return Schema_parse(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s)))
4.41 +
4.42 +def Document_validate(schema, doc, error_handler):
4.43 + validator_context = libxml2mod.xmlRelaxNGNewValidCtxt(schema)
4.44 + handler = ValidationHandler(error_handler)
4.45 + libxml2mod.xmlRelaxNGSetValidErrors(validator_context, handler.error, handler.warning, None)
4.46 + try:
4.47 + status = libxml2mod.xmlRelaxNGValidateDoc(validator_context, doc)
4.48 + return status == 0
4.49 + finally:
4.50 + libxml2mod.xmlRelaxNGFreeValidCtxt(validator_context)
4.51 +
4.52 +def Schema_parse(context):
4.53 + try:
4.54 + return libxml2mod.xmlRelaxNGParse(context)
4.55 + finally:
4.56 + libxml2mod.xmlRelaxNGFreeParserCtxt(context)
4.57 +
4.58 +def Schema_free(schema):
4.59 + libxml2mod.xmlRelaxNGFree(schema)
4.60 +
4.61 +class ValidationHandler:
4.62 +
4.63 + """
4.64 + A handler which collects validation errors and warnings and passes them to a
4.65 + DOMErrorHandler.
4.66 + """
4.67 +
4.68 + def __init__(self, error_handler):
4.69 + self.error_handler = error_handler
4.70 +
4.71 + def error(self, msg, arg):
4.72 + self.error_handler.handleError(DOMError(DOMError.SEVERITY_FATAL_ERROR, msg.strip()))
4.73 +
4.74 + def warning(self, msg, arg):
4.75 + self.error_handler.handleError(DOMError(DOMError.SEVERITY_WARNING, msg.strip()))
4.76 +
4.77 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/packages/debian-etch/python-libxml2dom/debian/changelog Sat Aug 30 00:51:04 2008 +0200
5.2 +++ b/packages/debian-etch/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200
5.3 @@ -11,6 +11,7 @@
5.4 * Introduced support for validation, together with the
5.5 libxml2dom.errors module.
5.6 * Improved error messages related to parsing.
5.7 + * Added DOMConfiguration support to documents.
5.8
5.9 -- Paul Boddie <paul@boddie.org.uk> Tue, 26 Aug 2008 00:12:15 +0200
5.10
6.1 --- a/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sat Aug 30 00:51:04 2008 +0200
6.2 +++ b/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200
6.3 @@ -11,6 +11,7 @@
6.4 * Introduced support for validation, together with the
6.5 libxml2dom.errors module.
6.6 * Improved error messages related to parsing.
6.7 + * Added DOMConfiguration support to documents.
6.8
6.9 -- Paul Boddie <paul@boddie.org.uk> Tue, 26 Aug 2008 00:12:33 +0200
6.10
7.1 --- a/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sat Aug 30 00:51:04 2008 +0200
7.2 +++ b/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200
7.3 @@ -11,6 +11,7 @@
7.4 * Introduced support for validation, together with the
7.5 libxml2dom.errors module.
7.6 * Improved error messages related to parsing.
7.7 + * Added DOMConfiguration support to documents.
7.8
7.9 -- Paul Boddie <paul@boddie.org.uk> Tue, 26 Aug 2008 00:11:32 +0200
7.10
8.1 --- a/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sat Aug 30 00:51:04 2008 +0200
8.2 +++ b/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200
8.3 @@ -11,6 +11,7 @@
8.4 * Introduced support for validation, together with the
8.5 libxml2dom.errors module.
8.6 * Improved error messages related to parsing.
8.7 + * Added DOMConfiguration support to documents.
8.8
8.9 -- Paul Boddie <paul@boddie.org.uk> Tue, 26 Aug 2008 00:10:37 +0200
8.10
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/tests/test_invalid_relaxng.xml Sun Aug 31 01:31:31 2008 +0200
9.3 @@ -0,0 +1,10 @@
9.4 +<?xml version="1.0"?>
9.5 +<element name="zoo" xmlns="http://relaxng.org/ns/structure/1.0">
9.6 + <zeroOrMore>
9.7 + <element name="cage">
9.8 + <attribute name="id"><text/></attribute>
9.9 + <attribute name="description"><text/></attribute>
9.10 + <text/>
9.11 + </element>
9.12 + </zeroOrMore>
9.13 +</element>
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/tests/test_valid_relaxng.py Sun Aug 31 01:31:31 2008 +0200
10.3 @@ -0,0 +1,17 @@
10.4 +#!/usr/bin/env python
10.5 +
10.6 +import libxml2dom
10.7 +
10.8 +schema = libxml2dom.parse("tests/test_valid_relaxng.xml")
10.9 +d = libxml2dom.parse("tests/test_valid.xml")
10.10 +print d.validate(schema)
10.11 +print d.validateDocument(schema)
10.12 +print d.getParameter("error-handler")
10.13 +
10.14 +schema = libxml2dom.parse("tests/test_invalid_relaxng.xml")
10.15 +d = libxml2dom.parse("tests/test_invalid.xml")
10.16 +print d.validate(schema)
10.17 +print d.validateDocument(schema)
10.18 +print d.getParameter("error-handler")
10.19 +
10.20 +# vim: tabstop=4 expandtab shiftwidth=4
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/tests/test_valid_relaxng.xml Sun Aug 31 01:31:31 2008 +0200
11.3 @@ -0,0 +1,9 @@
11.4 +<?xml version="1.0"?>
11.5 +<element name="zoo" xmlns="http://relaxng.org/ns/structure/1.0">
11.6 + <zeroOrMore>
11.7 + <element name="cage">
11.8 + <attribute name="id"><text/></attribute>
11.9 + <text/>
11.10 + </element>
11.11 + </zeroOrMore>
11.12 +</element>