1 # support for XMLSchema validation
2 from lxml.includes cimport xmlschema
5 cdef class XMLSchemaError(LxmlError):
6 """Base class of all XML Schema errors
9 cdef class XMLSchemaParseError(XMLSchemaError):
10 """Error while parsing an XML document as XML Schema.
13 cdef class XMLSchemaValidateError(XMLSchemaError):
14 """Error while validating an XML document with an XML Schema.
18 ################################################################################
21 cdef XPath _check_for_default_attributes = XPath(
22 u"boolean(//xs:attribute[@default or @fixed][1])",
23 namespaces={u'xs': u'http://www.w3.org/2001/XMLSchema'})
26 cdef class XMLSchema(_Validator):
27 u"""XMLSchema(self, etree=None, file=None)
28 Turn a document into an XML Schema validator.
30 Either pass a schema as Element or ElementTree, or pass a file or
31 filename through the ``file`` keyword argument.
33 Passing the ``attribute_defaults`` boolean option will make the
34 schema insert default/fixed attributes into validated documents.
36 cdef xmlschema.xmlSchema* _c_schema
38 cdef bint _has_default_attributes
39 cdef bint _add_attribute_defaults
42 self._has_default_attributes = True # play it safe
43 self._add_attribute_defaults = False
45 def __init__(self, etree=None, *, file=None, bint attribute_defaults=False):
46 cdef xmlschema.xmlSchemaParserCtxt* parser_ctxt
49 self._add_attribute_defaults = attribute_defaults
50 _Validator.__init__(self)
53 doc = _documentOrRaise(etree)
54 root_node = _rootNodeOrRaise(etree)
55 c_doc = _copyDocRoot(doc._c_doc, root_node._c_node)
56 self._doc = _documentFactory(c_doc, doc._parser)
57 parser_ctxt = xmlschema.xmlSchemaNewDocParserCtxt(c_doc)
58 elif file is not None:
60 filename = _encodeFilename(file)
61 parser_ctxt = xmlschema.xmlSchemaNewParserCtxt(_cstr(filename))
63 self._doc = _parseDocument(file, None, None)
64 parser_ctxt = xmlschema.xmlSchemaNewDocParserCtxt(self._doc._c_doc)
66 raise XMLSchemaParseError, u"No tree or file given"
68 if parser_ctxt is NULL:
71 xmlschema.xmlSchemaSetParserStructuredErrors(
72 parser_ctxt, _receiveError, <void*>self._error_log)
73 if self._doc is not None:
74 # calling xmlSchemaParse on a schema with imports or
75 # includes will cause libxml2 to create an internal
76 # context for parsing, so push an implied context to route
77 # resolve requests to the document's parser
78 __GLOBAL_PARSER_CONTEXT.pushImpliedContextFromParser(self._doc._parser)
80 orig_loader = _register_document_loader()
81 self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
82 _reset_document_loader(orig_loader)
83 if self._doc is not None:
84 __GLOBAL_PARSER_CONTEXT.popImpliedContext()
85 xmlschema.xmlSchemaFreeParserCtxt(parser_ctxt)
87 if self._c_schema is NULL:
88 raise XMLSchemaParseError(
89 self._error_log._buildExceptionMessage(
90 u"Document is not valid XML Schema"),
93 if self._doc is not None:
94 self._has_default_attributes = _check_for_default_attributes(self._doc)
95 self._add_attribute_defaults = attribute_defaults and self._has_default_attributes
97 def __dealloc__(self):
98 xmlschema.xmlSchemaFree(self._c_schema)
100 def __call__(self, etree):
101 u"""__call__(self, etree)
103 Validate doc using XML Schema.
105 Returns true if document is valid, false if not.
107 cdef xmlschema.xmlSchemaValidCtxt* valid_ctxt
109 cdef _Element root_node
113 assert self._c_schema is not NULL, "Schema instance not initialised"
114 doc = _documentOrRaise(etree)
115 root_node = _rootNodeOrRaise(etree)
117 valid_ctxt = xmlschema.xmlSchemaNewValidCtxt(self._c_schema)
118 if valid_ctxt is NULL:
122 if self._add_attribute_defaults:
123 xmlschema.xmlSchemaSetValidOptions(
124 valid_ctxt, xmlschema.XML_SCHEMA_VAL_VC_I_CREATE)
126 self._error_log.clear()
127 xmlschema.xmlSchemaSetValidStructuredErrors(
128 valid_ctxt, _receiveError, <void*>self._error_log)
130 c_doc = _fakeRootDoc(doc._c_doc, root_node._c_node)
132 ret = xmlschema.xmlSchemaValidateDoc(valid_ctxt, c_doc)
133 _destroyFakeDoc(doc._c_doc, c_doc)
135 xmlschema.xmlSchemaFreeValidCtxt(valid_ctxt)
138 raise XMLSchemaValidateError(
139 u"Internal error in XML Schema validation.",
146 cdef _ParserSchemaValidationContext _newSaxValidator(
147 self, bint add_default_attributes):
148 cdef _ParserSchemaValidationContext context
149 context = _ParserSchemaValidationContext.__new__(_ParserSchemaValidationContext)
150 context._schema = self
151 context._add_default_attributes = (self._has_default_attributes and (
152 add_default_attributes or self._add_attribute_defaults))
157 cdef class _ParserSchemaValidationContext:
158 cdef XMLSchema _schema
159 cdef xmlschema.xmlSchemaValidCtxt* _valid_ctxt
160 cdef xmlschema.xmlSchemaSAXPlugStruct* _sax_plug
161 cdef bint _add_default_attributes
163 self._valid_ctxt = NULL
164 self._sax_plug = NULL
165 self._add_default_attributes = False
167 def __dealloc__(self):
170 xmlschema.xmlSchemaFreeValidCtxt(self._valid_ctxt)
172 cdef _ParserSchemaValidationContext copy(self):
173 assert self._schema is not None, "_ParserSchemaValidationContext not initialised"
174 return self._schema._newSaxValidator(
175 self._add_default_attributes)
177 cdef void inject_default_attributes(self, xmlDoc* c_doc):
178 # we currently need to insert default attributes manually
179 # after parsing, as libxml2 does not support this at parse
181 if self._add_default_attributes:
183 xmlschema.xmlSchemaValidateDoc(self._valid_ctxt, c_doc)
185 cdef int connect(self, xmlparser.xmlParserCtxt* c_ctxt, _BaseErrorLog error_log) except -1:
186 if self._valid_ctxt is NULL:
187 self._valid_ctxt = xmlschema.xmlSchemaNewValidCtxt(
188 self._schema._c_schema)
189 if self._valid_ctxt is NULL:
191 if self._add_default_attributes:
192 xmlschema.xmlSchemaSetValidOptions(
193 self._valid_ctxt, xmlschema.XML_SCHEMA_VAL_VC_I_CREATE)
194 if error_log is not None:
195 xmlschema.xmlSchemaSetValidStructuredErrors(
196 self._valid_ctxt, _receiveError, <void*>error_log)
197 self._sax_plug = xmlschema.xmlSchemaSAXPlug(
198 self._valid_ctxt, &c_ctxt.sax, &c_ctxt.userData)
200 cdef void disconnect(self):
201 if self._sax_plug is not NULL:
202 xmlschema.xmlSchemaSAXUnplug(self._sax_plug)
203 self._sax_plug = NULL
204 if self._valid_ctxt is not NULL:
205 xmlschema.xmlSchemaSetValidStructuredErrors(
206 self._valid_ctxt, NULL, NULL)
208 cdef bint isvalid(self):
209 if self._valid_ctxt is NULL:
211 return xmlschema.xmlSchemaIsValid(self._valid_ctxt)