lxml 6.0.0__cp310-cp310-manylinux_2_31_armv7l.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. lxml/ElementInclude.py +244 -0
  2. lxml/__init__.py +22 -0
  3. lxml/_elementpath.cpython-310-arm-linux-gnueabihf.so +0 -0
  4. lxml/_elementpath.py +343 -0
  5. lxml/apihelpers.pxi +1801 -0
  6. lxml/builder.cpython-310-arm-linux-gnueabihf.so +0 -0
  7. lxml/builder.py +243 -0
  8. lxml/classlookup.pxi +580 -0
  9. lxml/cleanup.pxi +215 -0
  10. lxml/cssselect.py +101 -0
  11. lxml/debug.pxi +36 -0
  12. lxml/docloader.pxi +178 -0
  13. lxml/doctestcompare.py +488 -0
  14. lxml/dtd.pxi +479 -0
  15. lxml/etree.cpython-310-arm-linux-gnueabihf.so +0 -0
  16. lxml/etree.h +244 -0
  17. lxml/etree.pyx +3853 -0
  18. lxml/etree_api.h +204 -0
  19. lxml/extensions.pxi +830 -0
  20. lxml/html/ElementSoup.py +10 -0
  21. lxml/html/__init__.py +1927 -0
  22. lxml/html/_diffcommand.py +86 -0
  23. lxml/html/_difflib.cpython-310-arm-linux-gnueabihf.so +0 -0
  24. lxml/html/_difflib.py +2106 -0
  25. lxml/html/_html5builder.py +100 -0
  26. lxml/html/_setmixin.py +56 -0
  27. lxml/html/builder.py +173 -0
  28. lxml/html/clean.py +21 -0
  29. lxml/html/defs.py +135 -0
  30. lxml/html/diff.cpython-310-arm-linux-gnueabihf.so +0 -0
  31. lxml/html/diff.py +972 -0
  32. lxml/html/formfill.py +299 -0
  33. lxml/html/html5parser.py +260 -0
  34. lxml/html/soupparser.py +314 -0
  35. lxml/html/usedoctest.py +13 -0
  36. lxml/includes/__init__.pxd +0 -0
  37. lxml/includes/__init__.py +0 -0
  38. lxml/includes/c14n.pxd +25 -0
  39. lxml/includes/config.pxd +3 -0
  40. lxml/includes/dtdvalid.pxd +18 -0
  41. lxml/includes/etree_defs.h +379 -0
  42. lxml/includes/etreepublic.pxd +237 -0
  43. lxml/includes/extlibs/__init__.py +0 -0
  44. lxml/includes/extlibs/libcharset.h +45 -0
  45. lxml/includes/extlibs/localcharset.h +137 -0
  46. lxml/includes/extlibs/zconf.h +543 -0
  47. lxml/includes/extlibs/zlib.h +1938 -0
  48. lxml/includes/htmlparser.pxd +56 -0
  49. lxml/includes/libexslt/__init__.py +0 -0
  50. lxml/includes/libexslt/exslt.h +108 -0
  51. lxml/includes/libexslt/exsltconfig.h +70 -0
  52. lxml/includes/libexslt/exsltexports.h +63 -0
  53. lxml/includes/libxml/HTMLparser.h +339 -0
  54. lxml/includes/libxml/HTMLtree.h +148 -0
  55. lxml/includes/libxml/SAX.h +18 -0
  56. lxml/includes/libxml/SAX2.h +170 -0
  57. lxml/includes/libxml/__init__.py +0 -0
  58. lxml/includes/libxml/c14n.h +115 -0
  59. lxml/includes/libxml/catalog.h +183 -0
  60. lxml/includes/libxml/chvalid.h +230 -0
  61. lxml/includes/libxml/debugXML.h +79 -0
  62. lxml/includes/libxml/dict.h +82 -0
  63. lxml/includes/libxml/encoding.h +307 -0
  64. lxml/includes/libxml/entities.h +147 -0
  65. lxml/includes/libxml/globals.h +25 -0
  66. lxml/includes/libxml/hash.h +251 -0
  67. lxml/includes/libxml/list.h +137 -0
  68. lxml/includes/libxml/nanoftp.h +16 -0
  69. lxml/includes/libxml/nanohttp.h +98 -0
  70. lxml/includes/libxml/parser.h +1633 -0
  71. lxml/includes/libxml/parserInternals.h +591 -0
  72. lxml/includes/libxml/relaxng.h +224 -0
  73. lxml/includes/libxml/schemasInternals.h +959 -0
  74. lxml/includes/libxml/schematron.h +143 -0
  75. lxml/includes/libxml/threads.h +81 -0
  76. lxml/includes/libxml/tree.h +1326 -0
  77. lxml/includes/libxml/uri.h +106 -0
  78. lxml/includes/libxml/valid.h +485 -0
  79. lxml/includes/libxml/xinclude.h +141 -0
  80. lxml/includes/libxml/xlink.h +193 -0
  81. lxml/includes/libxml/xmlIO.h +419 -0
  82. lxml/includes/libxml/xmlautomata.h +163 -0
  83. lxml/includes/libxml/xmlerror.h +962 -0
  84. lxml/includes/libxml/xmlexports.h +96 -0
  85. lxml/includes/libxml/xmlmemory.h +188 -0
  86. lxml/includes/libxml/xmlmodule.h +61 -0
  87. lxml/includes/libxml/xmlreader.h +444 -0
  88. lxml/includes/libxml/xmlregexp.h +116 -0
  89. lxml/includes/libxml/xmlsave.h +111 -0
  90. lxml/includes/libxml/xmlschemas.h +254 -0
  91. lxml/includes/libxml/xmlschemastypes.h +152 -0
  92. lxml/includes/libxml/xmlstring.h +140 -0
  93. lxml/includes/libxml/xmlunicode.h +15 -0
  94. lxml/includes/libxml/xmlversion.h +332 -0
  95. lxml/includes/libxml/xmlwriter.h +489 -0
  96. lxml/includes/libxml/xpath.h +569 -0
  97. lxml/includes/libxml/xpathInternals.h +639 -0
  98. lxml/includes/libxml/xpointer.h +48 -0
  99. lxml/includes/libxslt/__init__.py +0 -0
  100. lxml/includes/libxslt/attributes.h +39 -0
  101. lxml/includes/libxslt/documents.h +93 -0
  102. lxml/includes/libxslt/extensions.h +262 -0
  103. lxml/includes/libxslt/extra.h +72 -0
  104. lxml/includes/libxslt/functions.h +78 -0
  105. lxml/includes/libxslt/imports.h +75 -0
  106. lxml/includes/libxslt/keys.h +53 -0
  107. lxml/includes/libxslt/namespaces.h +68 -0
  108. lxml/includes/libxslt/numbersInternals.h +73 -0
  109. lxml/includes/libxslt/pattern.h +84 -0
  110. lxml/includes/libxslt/preproc.h +43 -0
  111. lxml/includes/libxslt/security.h +104 -0
  112. lxml/includes/libxslt/templates.h +77 -0
  113. lxml/includes/libxslt/transform.h +207 -0
  114. lxml/includes/libxslt/variables.h +118 -0
  115. lxml/includes/libxslt/xslt.h +110 -0
  116. lxml/includes/libxslt/xsltInternals.h +1995 -0
  117. lxml/includes/libxslt/xsltconfig.h +146 -0
  118. lxml/includes/libxslt/xsltexports.h +64 -0
  119. lxml/includes/libxslt/xsltlocale.h +44 -0
  120. lxml/includes/libxslt/xsltutils.h +343 -0
  121. lxml/includes/lxml-version.h +3 -0
  122. lxml/includes/relaxng.pxd +64 -0
  123. lxml/includes/schematron.pxd +34 -0
  124. lxml/includes/tree.pxd +492 -0
  125. lxml/includes/uri.pxd +5 -0
  126. lxml/includes/xinclude.pxd +22 -0
  127. lxml/includes/xmlerror.pxd +852 -0
  128. lxml/includes/xmlparser.pxd +303 -0
  129. lxml/includes/xmlschema.pxd +35 -0
  130. lxml/includes/xpath.pxd +136 -0
  131. lxml/includes/xslt.pxd +190 -0
  132. lxml/isoschematron/__init__.py +348 -0
  133. lxml/isoschematron/resources/rng/iso-schematron.rng +709 -0
  134. lxml/isoschematron/resources/xsl/RNG2Schtrn.xsl +75 -0
  135. lxml/isoschematron/resources/xsl/XSD2Schtrn.xsl +77 -0
  136. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_abstract_expand.xsl +313 -0
  137. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_dsdl_include.xsl +1160 -0
  138. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_message.xsl +55 -0
  139. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_skeleton_for_xslt1.xsl +1796 -0
  140. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl +588 -0
  141. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/readme.txt +84 -0
  142. lxml/iterparse.pxi +438 -0
  143. lxml/lxml.etree.h +244 -0
  144. lxml/lxml.etree_api.h +204 -0
  145. lxml/nsclasses.pxi +281 -0
  146. lxml/objectify.cpython-310-arm-linux-gnueabihf.so +0 -0
  147. lxml/objectify.pyx +2149 -0
  148. lxml/objectpath.pxi +332 -0
  149. lxml/parser.pxi +2059 -0
  150. lxml/parsertarget.pxi +180 -0
  151. lxml/proxy.pxi +619 -0
  152. lxml/public-api.pxi +178 -0
  153. lxml/pyclasslookup.py +3 -0
  154. lxml/readonlytree.pxi +565 -0
  155. lxml/relaxng.pxi +165 -0
  156. lxml/sax.cpython-310-arm-linux-gnueabihf.so +0 -0
  157. lxml/sax.py +286 -0
  158. lxml/saxparser.pxi +875 -0
  159. lxml/schematron.pxi +173 -0
  160. lxml/serializer.pxi +1849 -0
  161. lxml/usedoctest.py +13 -0
  162. lxml/xinclude.pxi +67 -0
  163. lxml/xmlerror.pxi +1654 -0
  164. lxml/xmlid.pxi +179 -0
  165. lxml/xmlschema.pxi +215 -0
  166. lxml/xpath.pxi +487 -0
  167. lxml/xslt.pxi +957 -0
  168. lxml/xsltext.pxi +242 -0
  169. lxml-6.0.0.dist-info/METADATA +163 -0
  170. lxml-6.0.0.dist-info/RECORD +174 -0
  171. lxml-6.0.0.dist-info/WHEEL +5 -0
  172. lxml-6.0.0.dist-info/licenses/LICENSE.txt +31 -0
  173. lxml-6.0.0.dist-info/licenses/LICENSES.txt +29 -0
  174. lxml-6.0.0.dist-info/top_level.txt +1 -0
lxml/xmlid.pxi ADDED
@@ -0,0 +1,179 @@
1
+ cdef object _find_id_attributes
2
+
3
+ def XMLID(text, parser=None, *, base_url=None):
4
+ """XMLID(text, parser=None, base_url=None)
5
+
6
+ Parse the text and return a tuple (root node, ID dictionary). The root
7
+ node is the same as returned by the XML() function. The dictionary
8
+ contains string-element pairs. The dictionary keys are the values of 'id'
9
+ attributes. The elements referenced by the ID are stored as dictionary
10
+ values.
11
+ """
12
+ cdef dict dic
13
+ global _find_id_attributes
14
+ if _find_id_attributes is None:
15
+ _find_id_attributes = XPath('//*[string(@id)]')
16
+
17
+ # ElementTree compatible implementation: parse and look for 'id' attributes
18
+ root = XML(text, parser, base_url=base_url)
19
+ dic = {}
20
+ for elem in _find_id_attributes(root):
21
+ dic[elem.get('id')] = elem
22
+ return root, dic
23
+
24
+ def XMLDTDID(text, parser=None, *, base_url=None):
25
+ """XMLDTDID(text, parser=None, base_url=None)
26
+
27
+ Parse the text and return a tuple (root node, ID dictionary). The root
28
+ node is the same as returned by the XML() function. The dictionary
29
+ contains string-element pairs. The dictionary keys are the values of ID
30
+ attributes as defined by the DTD. The elements referenced by the ID are
31
+ stored as dictionary values.
32
+
33
+ Note that you must not modify the XML tree if you use the ID dictionary.
34
+ The results are undefined.
35
+ """
36
+ cdef _Element root
37
+ root = XML(text, parser, base_url=base_url)
38
+ # xml:id spec compatible implementation: use DTD ID attributes from libxml2
39
+ if root._doc._c_doc.ids is NULL:
40
+ return root, {}
41
+ else:
42
+ return root, _IDDict(root)
43
+
44
+ def parseid(source, parser=None, *, base_url=None):
45
+ """parseid(source, parser=None)
46
+
47
+ Parses the source into a tuple containing an ElementTree object and an
48
+ ID dictionary. If no parser is provided as second argument, the default
49
+ parser is used.
50
+
51
+ Note that you must not modify the XML tree if you use the ID dictionary.
52
+ The results are undefined.
53
+ """
54
+ cdef _Document doc
55
+ doc = _parseDocument(source, parser, base_url)
56
+ return _elementTreeFactory(doc, None), _IDDict(doc)
57
+
58
+ cdef class _IDDict:
59
+ """IDDict(self, etree)
60
+ A dictionary-like proxy class that mapps ID attributes to elements.
61
+
62
+ The dictionary must be instantiated with the root element of a parsed XML
63
+ document, otherwise the behaviour is undefined. Elements and XML trees
64
+ that were created or modified 'by hand' are not supported.
65
+ """
66
+ cdef _Document _doc
67
+ cdef object _keys
68
+ cdef object _items
69
+ def __cinit__(self, etree):
70
+ cdef _Document doc
71
+ doc = _documentOrRaise(etree)
72
+ if doc._c_doc.ids is NULL:
73
+ raise ValueError, "No ID dictionary available."
74
+ self._doc = doc
75
+ self._keys = None
76
+ self._items = None
77
+
78
+ def copy(self):
79
+ return _IDDict(self._doc)
80
+
81
+ def __getitem__(self, id_name):
82
+ cdef tree.xmlHashTable* c_ids
83
+ cdef tree.xmlID* c_id
84
+ cdef xmlAttr* c_attr
85
+ c_ids = self._doc._c_doc.ids
86
+ id_utf = _utf8(id_name)
87
+ c_id = <tree.xmlID*>tree.xmlHashLookup(c_ids, _xcstr(id_utf))
88
+ if c_id is NULL:
89
+ raise KeyError, "key not found."
90
+ c_attr = c_id.attr
91
+ if c_attr is NULL or c_attr.parent is NULL:
92
+ raise KeyError, "ID attribute not found."
93
+ return _elementFactory(self._doc, c_attr.parent)
94
+
95
+ def get(self, id_name):
96
+ return self[id_name]
97
+
98
+ def __contains__(self, id_name):
99
+ cdef tree.xmlID* c_id
100
+ id_utf = _utf8(id_name)
101
+ c_id = <tree.xmlID*>tree.xmlHashLookup(
102
+ self._doc._c_doc.ids, _xcstr(id_utf))
103
+ return c_id is not NULL
104
+
105
+ def has_key(self, id_name):
106
+ return id_name in self
107
+
108
+ def __repr__(self):
109
+ return repr(dict(self))
110
+
111
+ def keys(self):
112
+ if self._keys is None:
113
+ self._keys = self._build_keys()
114
+ return self._keys[:]
115
+
116
+ def __iter__(self):
117
+ if self._keys is None:
118
+ self._keys = self._build_keys()
119
+ return iter(self._keys)
120
+
121
+ def iterkeys(self):
122
+ return self
123
+
124
+ def __len__(self):
125
+ if self._keys is None:
126
+ self._keys = self._build_keys()
127
+ return len(self._keys)
128
+
129
+ def items(self):
130
+ if self._items is None:
131
+ self._items = self._build_items()
132
+ return self._items[:]
133
+
134
+ def iteritems(self):
135
+ if self._items is None:
136
+ self._items = self._build_items()
137
+ return iter(self._items)
138
+
139
+ def values(self):
140
+ cdef list values = []
141
+ if self._items is None:
142
+ self._items = self._build_items()
143
+ for item in self._items:
144
+ value = python.PyTuple_GET_ITEM(item, 1)
145
+ python.Py_INCREF(value)
146
+ values.append(value)
147
+ return values
148
+
149
+ def itervalues(self):
150
+ return iter(self.values())
151
+
152
+ cdef object _build_keys(self):
153
+ keys = []
154
+ tree.xmlHashScan(<tree.xmlHashTable*>self._doc._c_doc.ids,
155
+ <tree.xmlHashScanner>_collectIdHashKeys, <python.PyObject*>keys)
156
+ return keys
157
+
158
+ cdef object _build_items(self):
159
+ items = []
160
+ context = (items, self._doc)
161
+ tree.xmlHashScan(<tree.xmlHashTable*>self._doc._c_doc.ids,
162
+ <tree.xmlHashScanner>_collectIdHashItemList, <python.PyObject*>context)
163
+ return items
164
+
165
+ cdef void _collectIdHashItemList(void* payload, void* context, xmlChar* name) noexcept:
166
+ # collect elements from ID attribute hash table
167
+ cdef list lst
168
+ c_id = <tree.xmlID*>payload
169
+ if c_id is NULL or c_id.attr is NULL or c_id.attr.parent is NULL:
170
+ return
171
+ lst, doc = <tuple>context
172
+ element = _elementFactory(doc, c_id.attr.parent)
173
+ lst.append( (funicode(name), element) )
174
+
175
+ cdef void _collectIdHashKeys(void* payload, void* collect_list, xmlChar* name) noexcept:
176
+ c_id = <tree.xmlID*>payload
177
+ if c_id is NULL or c_id.attr is NULL or c_id.attr.parent is NULL:
178
+ return
179
+ (<list>collect_list).append(funicode(name))
lxml/xmlschema.pxi ADDED
@@ -0,0 +1,215 @@
1
+ # support for XMLSchema validation
2
+ from lxml.includes cimport xmlschema
3
+
4
+
5
+ cdef class XMLSchemaError(LxmlError):
6
+ """Base class of all XML Schema errors
7
+ """
8
+
9
+ cdef class XMLSchemaParseError(XMLSchemaError):
10
+ """Error while parsing an XML document as XML Schema.
11
+ """
12
+
13
+ cdef class XMLSchemaValidateError(XMLSchemaError):
14
+ """Error while validating an XML document with an XML Schema.
15
+ """
16
+
17
+
18
+ ################################################################################
19
+ # XMLSchema
20
+
21
+ cdef XPath _check_for_default_attributes = XPath(
22
+ "boolean(//xs:attribute[@default or @fixed][1])",
23
+ namespaces={'xs': 'http://www.w3.org/2001/XMLSchema'})
24
+
25
+
26
+ cdef class XMLSchema(_Validator):
27
+ """XMLSchema(self, etree=None, file=None)
28
+ Turn a document into an XML Schema validator.
29
+
30
+ Either pass a schema as Element or ElementTree, or pass a file or
31
+ filename through the ``file`` keyword argument.
32
+
33
+ Passing the ``attribute_defaults`` boolean option will make the
34
+ schema insert default/fixed attributes into validated documents.
35
+ """
36
+ cdef xmlschema.xmlSchema* _c_schema
37
+ cdef _Document _doc
38
+ cdef bint _has_default_attributes
39
+ cdef bint _add_attribute_defaults
40
+
41
+ def __cinit__(self):
42
+ self._has_default_attributes = True # play it safe
43
+ self._add_attribute_defaults = False
44
+
45
+ def __init__(self, etree=None, *, file=None, bint attribute_defaults=False):
46
+ cdef xmlschema.xmlSchemaParserCtxt* parser_ctxt
47
+ cdef xmlDoc* c_doc
48
+
49
+ self._add_attribute_defaults = attribute_defaults
50
+ _Validator.__init__(self)
51
+ c_doc = NULL
52
+ if etree is not None:
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:
59
+ file = _getFSPathOrObject(file)
60
+ if _isString(file):
61
+ filename = _encodeFilename(file)
62
+ parser_ctxt = xmlschema.xmlSchemaNewParserCtxt(_cstr(filename))
63
+ else:
64
+ self._doc = _parseDocument(file, None, None)
65
+ parser_ctxt = xmlschema.xmlSchemaNewDocParserCtxt(self._doc._c_doc)
66
+ else:
67
+ raise XMLSchemaParseError, "No tree or file given"
68
+
69
+ if parser_ctxt is NULL:
70
+ raise MemoryError()
71
+
72
+ # Need a cast here because older libxml2 releases do not use 'const' in the functype.
73
+ xmlschema.xmlSchemaSetParserStructuredErrors(
74
+ parser_ctxt, <xmlerror.xmlStructuredErrorFunc> _receiveError, <void*>self._error_log)
75
+ if self._doc is not None:
76
+ # calling xmlSchemaParse on a schema with imports or
77
+ # includes will cause libxml2 to create an internal
78
+ # context for parsing, so push an implied context to route
79
+ # resolve requests to the document's parser
80
+ __GLOBAL_PARSER_CONTEXT.pushImpliedContextFromParser(self._doc._parser)
81
+ with nogil:
82
+ orig_loader = _register_document_loader()
83
+ self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
84
+ _reset_document_loader(orig_loader)
85
+ if self._doc is not None:
86
+ __GLOBAL_PARSER_CONTEXT.popImpliedContext()
87
+ xmlschema.xmlSchemaFreeParserCtxt(parser_ctxt)
88
+
89
+ if self._c_schema is NULL:
90
+ raise XMLSchemaParseError(
91
+ self._error_log._buildExceptionMessage(
92
+ "Document is not valid XML Schema"),
93
+ self._error_log)
94
+
95
+ if self._doc is not None:
96
+ self._has_default_attributes = _check_for_default_attributes(self._doc)
97
+ self._add_attribute_defaults = attribute_defaults and self._has_default_attributes
98
+
99
+ def __dealloc__(self):
100
+ xmlschema.xmlSchemaFree(self._c_schema)
101
+
102
+ def __call__(self, etree):
103
+ """__call__(self, etree)
104
+
105
+ Validate doc using XML Schema.
106
+
107
+ Returns true if document is valid, false if not.
108
+ """
109
+ cdef xmlschema.xmlSchemaValidCtxt* valid_ctxt
110
+ cdef _Document doc
111
+ cdef _Element root_node
112
+ cdef xmlDoc* c_doc
113
+ cdef int ret
114
+
115
+ assert self._c_schema is not NULL, "Schema instance not initialised"
116
+ doc = _documentOrRaise(etree)
117
+ root_node = _rootNodeOrRaise(etree)
118
+
119
+ valid_ctxt = xmlschema.xmlSchemaNewValidCtxt(self._c_schema)
120
+ if valid_ctxt is NULL:
121
+ raise MemoryError()
122
+
123
+ try:
124
+ if self._add_attribute_defaults:
125
+ xmlschema.xmlSchemaSetValidOptions(
126
+ valid_ctxt, xmlschema.XML_SCHEMA_VAL_VC_I_CREATE)
127
+
128
+ self._error_log.clear()
129
+ # Need a cast here because older libxml2 releases do not use 'const' in the functype.
130
+ xmlschema.xmlSchemaSetValidStructuredErrors(
131
+ valid_ctxt, <xmlerror.xmlStructuredErrorFunc> _receiveError, <void*>self._error_log)
132
+
133
+ c_doc = _fakeRootDoc(doc._c_doc, root_node._c_node)
134
+ with nogil:
135
+ ret = xmlschema.xmlSchemaValidateDoc(valid_ctxt, c_doc)
136
+ _destroyFakeDoc(doc._c_doc, c_doc)
137
+ finally:
138
+ xmlschema.xmlSchemaFreeValidCtxt(valid_ctxt)
139
+
140
+ if ret == -1:
141
+ raise XMLSchemaValidateError(
142
+ "Internal error in XML Schema validation.",
143
+ self._error_log)
144
+ if ret == 0:
145
+ return True
146
+ else:
147
+ return False
148
+
149
+ cdef _ParserSchemaValidationContext _newSaxValidator(
150
+ self, bint add_default_attributes):
151
+ cdef _ParserSchemaValidationContext context
152
+ context = _ParserSchemaValidationContext.__new__(_ParserSchemaValidationContext)
153
+ context._schema = self
154
+ context._add_default_attributes = (self._has_default_attributes and (
155
+ add_default_attributes or self._add_attribute_defaults))
156
+ return context
157
+
158
+ @cython.final
159
+ @cython.internal
160
+ cdef class _ParserSchemaValidationContext:
161
+ cdef XMLSchema _schema
162
+ cdef xmlschema.xmlSchemaValidCtxt* _valid_ctxt
163
+ cdef xmlschema.xmlSchemaSAXPlugStruct* _sax_plug
164
+ cdef bint _add_default_attributes
165
+ def __cinit__(self):
166
+ self._valid_ctxt = NULL
167
+ self._sax_plug = NULL
168
+ self._add_default_attributes = False
169
+
170
+ def __dealloc__(self):
171
+ self.disconnect()
172
+ if self._valid_ctxt:
173
+ xmlschema.xmlSchemaFreeValidCtxt(self._valid_ctxt)
174
+
175
+ cdef _ParserSchemaValidationContext copy(self):
176
+ assert self._schema is not None, "_ParserSchemaValidationContext not initialised"
177
+ return self._schema._newSaxValidator(
178
+ self._add_default_attributes)
179
+
180
+ cdef void inject_default_attributes(self, xmlDoc* c_doc) noexcept:
181
+ # we currently need to insert default attributes manually
182
+ # after parsing, as libxml2 does not support this at parse
183
+ # time
184
+ if self._add_default_attributes:
185
+ with nogil:
186
+ xmlschema.xmlSchemaValidateDoc(self._valid_ctxt, c_doc)
187
+
188
+ cdef int connect(self, xmlparser.xmlParserCtxt* c_ctxt, _BaseErrorLog error_log) except -1:
189
+ if self._valid_ctxt is NULL:
190
+ self._valid_ctxt = xmlschema.xmlSchemaNewValidCtxt(
191
+ self._schema._c_schema)
192
+ if self._valid_ctxt is NULL:
193
+ raise MemoryError()
194
+ if self._add_default_attributes:
195
+ xmlschema.xmlSchemaSetValidOptions(
196
+ self._valid_ctxt, xmlschema.XML_SCHEMA_VAL_VC_I_CREATE)
197
+ if error_log is not None:
198
+ # Need a cast here because older libxml2 releases do not use 'const' in the functype.
199
+ xmlschema.xmlSchemaSetValidStructuredErrors(
200
+ self._valid_ctxt, <xmlerror.xmlStructuredErrorFunc> _receiveError, <void*>error_log)
201
+ self._sax_plug = xmlschema.xmlSchemaSAXPlug(
202
+ self._valid_ctxt, &c_ctxt.sax, &c_ctxt.userData)
203
+
204
+ cdef void disconnect(self) noexcept:
205
+ if self._sax_plug is not NULL:
206
+ xmlschema.xmlSchemaSAXUnplug(self._sax_plug)
207
+ self._sax_plug = NULL
208
+ if self._valid_ctxt is not NULL:
209
+ xmlschema.xmlSchemaSetValidStructuredErrors(
210
+ self._valid_ctxt, NULL, NULL)
211
+
212
+ cdef bint isvalid(self) noexcept:
213
+ if self._valid_ctxt is NULL:
214
+ return 1 # valid
215
+ return xmlschema.xmlSchemaIsValid(self._valid_ctxt)