Package xmlschema_acue :: Package validators :: Module assertions

Source Code for Module xmlschema_acue.validators.assertions

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (c), 2016-2019, SISSA (International School for Advanced Studies). 
 4  # All rights reserved. 
 5  # This file is distributed under the terms of the MIT License. 
 6  # See the file 'LICENSE' in the root directory of the present 
 7  # distribution, or http://opensource.org/licenses/MIT. 
 8  # 
 9  # @author Davide Brunato <brunato@sissa.it> 
10  # 
11  from __future__ import unicode_literals 
12  from elementpath import XPath2Parser, XPathContext, XMLSchemaProxy, ElementPathSyntaxError 
13   
14  from xmlschema_acue.qnames import XSD_ASSERT 
15  from xmlschema_acue.xpath import ElementPathMixin 
16   
17  from xmlschema_acue.validators.exceptions import XMLSchemaValidationError 
18  from xmlschema_acue.validators.xsdbase import XsdComponent 
19 20 21 -class XsdAssert(XsdComponent, ElementPathMixin):
22 """ 23 Class for XSD 'assert' constraint declaration. 24 25 <assert 26 id = ID 27 test = an XPath expression 28 xpathDefaultNamespace = (anyURI | (##defaultNamespace | ##targetNamespace | ##local)) 29 {any attributes with non-schema namespace . . .}> 30 Content: (annotation?) 31 </assert> 32 """ 33 _admitted_tags = {XSD_ASSERT} 34 token = None 35
36 - def __init__(self, elem, schema, parent, base_type):
37 self.base_type = base_type 38 super(XsdAssert, self).__init__(elem, schema, parent) 39 if not self.base_type.is_complex(): 40 self.parse_error("base_type={!r} is not a complexType definition", elem=self.elem) 41 self.path = 'true()'
42
43 - def _parse(self):
44 super(XsdAssert, self)._parse() 45 try: 46 self.path = self.elem.attrib['test'] 47 except KeyError as err: 48 self.parse_error(str(err), elem=self.elem) 49 self.path = 'true()' 50 51 if 'xpathDefaultNamespace' in self.elem.attrib: 52 self.xpath_default_namespace = self._parse_xpath_default_namespace(self.elem) 53 else: 54 self.xpath_default_namespace = self.schema.xpath_default_namespace 55 self.parser = XPath2Parser(self.namespaces, strict=False, default_namespace=self.xpath_default_namespace)
56 57 @property
58 - def built(self):
59 return self.token is not None and (self.base_type.is_global or self.base_type.built)
60
61 - def parse(self):
62 self.parser.schema = XMLSchemaProxy(self.schema, self) 63 try: 64 self.token = self.parser.parse(self.path) 65 except ElementPathSyntaxError as err: 66 self.parse_error(err, elem=self.elem) 67 self.token = self.parser.parse('true()')
68
69 - def __call__(self, elem):
70 if not self.token.evaluate(XPathContext(root=elem)): 71 msg = "expression is not true with test path %r." 72 yield XMLSchemaValidationError(self, obj=elem, reason=msg % self.path)
73 74 # For implementing ElementPathMixin
75 - def __iter__(self):
76 if not self.parent.has_simple_content(): 77 for e in self.parent.content_type.iter_subelements(): 78 yield e
79 80 @property
81 - def attrib(self):
82 return self.parent.attributes
83