lxml 5.3.2__cp310-cp310-win32.win32.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 (175) hide show
  1. lxml/ElementInclude.py +244 -0
  2. lxml/__init__.py +22 -0
  3. lxml/_elementpath.cp310-win32.pyd +0 -0
  4. lxml/_elementpath.py +341 -0
  5. lxml/apihelpers.pxi +1793 -0
  6. lxml/builder.cp310-win32.pyd +0 -0
  7. lxml/builder.py +232 -0
  8. lxml/classlookup.pxi +580 -0
  9. lxml/cleanup.pxi +215 -0
  10. lxml/cssselect.py +101 -0
  11. lxml/debug.pxi +90 -0
  12. lxml/docloader.pxi +178 -0
  13. lxml/doctestcompare.py +488 -0
  14. lxml/dtd.pxi +479 -0
  15. lxml/etree.cp310-win32.pyd +0 -0
  16. lxml/etree.h +248 -0
  17. lxml/etree.pyx +3732 -0
  18. lxml/etree_api.h +195 -0
  19. lxml/extensions.pxi +833 -0
  20. lxml/html/ElementSoup.py +10 -0
  21. lxml/html/__init__.py +1923 -0
  22. lxml/html/_diffcommand.py +86 -0
  23. lxml/html/_html5builder.py +100 -0
  24. lxml/html/_setmixin.py +56 -0
  25. lxml/html/builder.py +133 -0
  26. lxml/html/clean.py +21 -0
  27. lxml/html/defs.py +135 -0
  28. lxml/html/diff.cp310-win32.pyd +0 -0
  29. lxml/html/diff.py +878 -0
  30. lxml/html/formfill.py +299 -0
  31. lxml/html/html5parser.py +260 -0
  32. lxml/html/soupparser.py +314 -0
  33. lxml/html/usedoctest.py +13 -0
  34. lxml/includes/__init__.pxd +0 -0
  35. lxml/includes/__init__.py +0 -0
  36. lxml/includes/c14n.pxd +25 -0
  37. lxml/includes/config.pxd +3 -0
  38. lxml/includes/dtdvalid.pxd +18 -0
  39. lxml/includes/etree_defs.h +379 -0
  40. lxml/includes/etreepublic.pxd +237 -0
  41. lxml/includes/extlibs/__init__.py +0 -0
  42. lxml/includes/extlibs/zconf.h +543 -0
  43. lxml/includes/extlibs/zlib.h +1938 -0
  44. lxml/includes/htmlparser.pxd +56 -0
  45. lxml/includes/libexslt/__init__.py +0 -0
  46. lxml/includes/libexslt/exslt.h +108 -0
  47. lxml/includes/libexslt/exsltconfig.h +70 -0
  48. lxml/includes/libexslt/exsltexports.h +63 -0
  49. lxml/includes/libexslt/libexslt.h +29 -0
  50. lxml/includes/libxml/HTMLparser.h +320 -0
  51. lxml/includes/libxml/HTMLtree.h +147 -0
  52. lxml/includes/libxml/SAX.h +204 -0
  53. lxml/includes/libxml/SAX2.h +173 -0
  54. lxml/includes/libxml/__init__.py +0 -0
  55. lxml/includes/libxml/c14n.h +128 -0
  56. lxml/includes/libxml/catalog.h +182 -0
  57. lxml/includes/libxml/chvalid.h +230 -0
  58. lxml/includes/libxml/debugXML.h +217 -0
  59. lxml/includes/libxml/dict.h +81 -0
  60. lxml/includes/libxml/encoding.h +233 -0
  61. lxml/includes/libxml/entities.h +151 -0
  62. lxml/includes/libxml/globals.h +529 -0
  63. lxml/includes/libxml/hash.h +236 -0
  64. lxml/includes/libxml/list.h +137 -0
  65. lxml/includes/libxml/nanoftp.h +186 -0
  66. lxml/includes/libxml/nanohttp.h +81 -0
  67. lxml/includes/libxml/parser.h +1265 -0
  68. lxml/includes/libxml/parserInternals.h +662 -0
  69. lxml/includes/libxml/pattern.h +100 -0
  70. lxml/includes/libxml/relaxng.h +218 -0
  71. lxml/includes/libxml/schemasInternals.h +958 -0
  72. lxml/includes/libxml/schematron.h +142 -0
  73. lxml/includes/libxml/threads.h +94 -0
  74. lxml/includes/libxml/tree.h +1314 -0
  75. lxml/includes/libxml/uri.h +94 -0
  76. lxml/includes/libxml/valid.h +448 -0
  77. lxml/includes/libxml/xinclude.h +129 -0
  78. lxml/includes/libxml/xlink.h +189 -0
  79. lxml/includes/libxml/xmlIO.h +369 -0
  80. lxml/includes/libxml/xmlautomata.h +146 -0
  81. lxml/includes/libxml/xmlerror.h +919 -0
  82. lxml/includes/libxml/xmlexports.h +50 -0
  83. lxml/includes/libxml/xmlmemory.h +228 -0
  84. lxml/includes/libxml/xmlmodule.h +57 -0
  85. lxml/includes/libxml/xmlreader.h +428 -0
  86. lxml/includes/libxml/xmlregexp.h +222 -0
  87. lxml/includes/libxml/xmlsave.h +88 -0
  88. lxml/includes/libxml/xmlschemas.h +246 -0
  89. lxml/includes/libxml/xmlschemastypes.h +152 -0
  90. lxml/includes/libxml/xmlstring.h +140 -0
  91. lxml/includes/libxml/xmlunicode.h +202 -0
  92. lxml/includes/libxml/xmlversion.h +526 -0
  93. lxml/includes/libxml/xmlwriter.h +488 -0
  94. lxml/includes/libxml/xpath.h +575 -0
  95. lxml/includes/libxml/xpathInternals.h +632 -0
  96. lxml/includes/libxml/xpointer.h +137 -0
  97. lxml/includes/libxslt/__init__.py +0 -0
  98. lxml/includes/libxslt/attributes.h +39 -0
  99. lxml/includes/libxslt/documents.h +93 -0
  100. lxml/includes/libxslt/extensions.h +262 -0
  101. lxml/includes/libxslt/extra.h +72 -0
  102. lxml/includes/libxslt/functions.h +78 -0
  103. lxml/includes/libxslt/imports.h +75 -0
  104. lxml/includes/libxslt/keys.h +53 -0
  105. lxml/includes/libxslt/libxslt.h +36 -0
  106. lxml/includes/libxslt/namespaces.h +68 -0
  107. lxml/includes/libxslt/numbersInternals.h +73 -0
  108. lxml/includes/libxslt/preproc.h +43 -0
  109. lxml/includes/libxslt/security.h +104 -0
  110. lxml/includes/libxslt/templates.h +77 -0
  111. lxml/includes/libxslt/transform.h +207 -0
  112. lxml/includes/libxslt/trio.h +216 -0
  113. lxml/includes/libxslt/triodef.h +220 -0
  114. lxml/includes/libxslt/variables.h +118 -0
  115. lxml/includes/libxslt/win32config.h +51 -0
  116. lxml/includes/libxslt/xslt.h +110 -0
  117. lxml/includes/libxslt/xsltInternals.h +1992 -0
  118. lxml/includes/libxslt/xsltconfig.h +179 -0
  119. lxml/includes/libxslt/xsltexports.h +64 -0
  120. lxml/includes/libxslt/xsltlocale.h +44 -0
  121. lxml/includes/libxslt/xsltutils.h +343 -0
  122. lxml/includes/lxml-version.h +3 -0
  123. lxml/includes/relaxng.pxd +64 -0
  124. lxml/includes/schematron.pxd +34 -0
  125. lxml/includes/tree.pxd +494 -0
  126. lxml/includes/uri.pxd +5 -0
  127. lxml/includes/xinclude.pxd +22 -0
  128. lxml/includes/xmlerror.pxd +852 -0
  129. lxml/includes/xmlparser.pxd +265 -0
  130. lxml/includes/xmlschema.pxd +35 -0
  131. lxml/includes/xpath.pxd +136 -0
  132. lxml/includes/xslt.pxd +190 -0
  133. lxml/isoschematron/__init__.py +348 -0
  134. lxml/isoschematron/resources/rng/iso-schematron.rng +709 -0
  135. lxml/isoschematron/resources/xsl/RNG2Schtrn.xsl +75 -0
  136. lxml/isoschematron/resources/xsl/XSD2Schtrn.xsl +77 -0
  137. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_abstract_expand.xsl +313 -0
  138. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_dsdl_include.xsl +1160 -0
  139. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_message.xsl +55 -0
  140. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_skeleton_for_xslt1.xsl +1796 -0
  141. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl +588 -0
  142. lxml/isoschematron/resources/xsl/iso-schematron-xslt1/readme.txt +84 -0
  143. lxml/iterparse.pxi +438 -0
  144. lxml/lxml.etree.h +248 -0
  145. lxml/lxml.etree_api.h +195 -0
  146. lxml/nsclasses.pxi +281 -0
  147. lxml/objectify.cp310-win32.pyd +0 -0
  148. lxml/objectify.pyx +2145 -0
  149. lxml/objectpath.pxi +332 -0
  150. lxml/parser.pxi +2000 -0
  151. lxml/parsertarget.pxi +180 -0
  152. lxml/proxy.pxi +619 -0
  153. lxml/public-api.pxi +178 -0
  154. lxml/pyclasslookup.py +3 -0
  155. lxml/readonlytree.pxi +565 -0
  156. lxml/relaxng.pxi +165 -0
  157. lxml/sax.cp310-win32.pyd +0 -0
  158. lxml/sax.py +275 -0
  159. lxml/saxparser.pxi +875 -0
  160. lxml/schematron.pxi +168 -0
  161. lxml/serializer.pxi +1781 -0
  162. lxml/usedoctest.py +13 -0
  163. lxml/xinclude.pxi +67 -0
  164. lxml/xmlerror.pxi +1654 -0
  165. lxml/xmlid.pxi +179 -0
  166. lxml/xmlschema.pxi +215 -0
  167. lxml/xpath.pxi +487 -0
  168. lxml/xslt.pxi +950 -0
  169. lxml/xsltext.pxi +242 -0
  170. lxml-5.3.2.dist-info/METADATA +100 -0
  171. lxml-5.3.2.dist-info/RECORD +175 -0
  172. lxml-5.3.2.dist-info/WHEEL +5 -0
  173. lxml-5.3.2.dist-info/licenses/LICENSE.txt +29 -0
  174. lxml-5.3.2.dist-info/licenses/LICENSES.txt +29 -0
  175. lxml-5.3.2.dist-info/top_level.txt +1 -0
lxml/relaxng.pxi ADDED
@@ -0,0 +1,165 @@
1
+ # support for RelaxNG validation
2
+ from lxml.includes cimport relaxng
3
+
4
+ cdef object _rnc2rng
5
+ try:
6
+ import rnc2rng as _rnc2rng
7
+ except ImportError:
8
+ _rnc2rng = None
9
+
10
+
11
+ cdef int _require_rnc2rng() except -1:
12
+ if _rnc2rng is None:
13
+ raise RelaxNGParseError(
14
+ 'compact syntax not supported (please install rnc2rng)')
15
+ return 0
16
+
17
+
18
+ cdef class RelaxNGError(LxmlError):
19
+ """Base class for RelaxNG errors.
20
+ """
21
+
22
+ cdef class RelaxNGParseError(RelaxNGError):
23
+ """Error while parsing an XML document as RelaxNG.
24
+ """
25
+
26
+ cdef class RelaxNGValidateError(RelaxNGError):
27
+ """Error while validating an XML document with a RelaxNG schema.
28
+ """
29
+
30
+
31
+ ################################################################################
32
+ # RelaxNG
33
+
34
+ cdef class RelaxNG(_Validator):
35
+ """RelaxNG(self, etree=None, file=None)
36
+ Turn a document into a Relax NG validator.
37
+
38
+ Either pass a schema as Element or ElementTree, or pass a file or
39
+ filename through the ``file`` keyword argument.
40
+ """
41
+ cdef relaxng.xmlRelaxNG* _c_schema
42
+ def __cinit__(self):
43
+ self._c_schema = NULL
44
+
45
+ def __init__(self, etree=None, *, file=None):
46
+ cdef _Document doc
47
+ cdef _Element root_node
48
+ cdef xmlDoc* fake_c_doc = NULL
49
+ cdef relaxng.xmlRelaxNGParserCtxt* parser_ctxt = NULL
50
+ _Validator.__init__(self)
51
+ if etree is not None:
52
+ doc = _documentOrRaise(etree)
53
+ root_node = _rootNodeOrRaise(etree)
54
+ fake_c_doc = _fakeRootDoc(doc._c_doc, root_node._c_node)
55
+ parser_ctxt = relaxng.xmlRelaxNGNewDocParserCtxt(fake_c_doc)
56
+ elif file is not None:
57
+ if _isString(file):
58
+ if file[-4:].lower() == '.rnc':
59
+ _require_rnc2rng()
60
+ rng_data_utf8 = _utf8(_rnc2rng.dumps(_rnc2rng.load(file)))
61
+ doc = _parseMemoryDocument(rng_data_utf8, parser=None, url=file)
62
+ parser_ctxt = relaxng.xmlRelaxNGNewDocParserCtxt(doc._c_doc)
63
+ else:
64
+ doc = None
65
+ filename = _encodeFilename(file)
66
+ with self._error_log:
67
+ orig_loader = _register_document_loader()
68
+ parser_ctxt = relaxng.xmlRelaxNGNewParserCtxt(_cstr(filename))
69
+ _reset_document_loader(orig_loader)
70
+ elif (_getFilenameForFile(file) or '')[-4:].lower() == '.rnc':
71
+ _require_rnc2rng()
72
+ rng_data_utf8 = _utf8(_rnc2rng.dumps(_rnc2rng.load(file)))
73
+ doc = _parseMemoryDocument(
74
+ rng_data_utf8, parser=None, url=_getFilenameForFile(file))
75
+ parser_ctxt = relaxng.xmlRelaxNGNewDocParserCtxt(doc._c_doc)
76
+ else:
77
+ doc = _parseDocument(file, parser=None, base_url=None)
78
+ parser_ctxt = relaxng.xmlRelaxNGNewDocParserCtxt(doc._c_doc)
79
+ else:
80
+ raise RelaxNGParseError, "No tree or file given"
81
+
82
+ if parser_ctxt is NULL:
83
+ if fake_c_doc is not NULL:
84
+ _destroyFakeDoc(doc._c_doc, fake_c_doc)
85
+ raise RelaxNGParseError(
86
+ self._error_log._buildExceptionMessage(
87
+ "Document is not parsable as Relax NG"),
88
+ self._error_log)
89
+
90
+ # Need a cast here because older libxml2 releases do not use 'const' in the functype.
91
+ relaxng.xmlRelaxNGSetParserStructuredErrors(
92
+ parser_ctxt, <xmlerror.xmlStructuredErrorFunc> _receiveError, <void*>self._error_log)
93
+ _connectGenericErrorLog(self._error_log, xmlerror.XML_FROM_RELAXNGP)
94
+ self._c_schema = relaxng.xmlRelaxNGParse(parser_ctxt)
95
+ _connectGenericErrorLog(None)
96
+
97
+ relaxng.xmlRelaxNGFreeParserCtxt(parser_ctxt)
98
+ if self._c_schema is NULL:
99
+ if fake_c_doc is not NULL:
100
+ _destroyFakeDoc(doc._c_doc, fake_c_doc)
101
+ raise RelaxNGParseError(
102
+ self._error_log._buildExceptionMessage(
103
+ "Document is not valid Relax NG"),
104
+ self._error_log)
105
+ if fake_c_doc is not NULL:
106
+ _destroyFakeDoc(doc._c_doc, fake_c_doc)
107
+
108
+ def __dealloc__(self):
109
+ relaxng.xmlRelaxNGFree(self._c_schema)
110
+
111
+ def __call__(self, etree):
112
+ """__call__(self, etree)
113
+
114
+ Validate doc using Relax NG.
115
+
116
+ Returns true if document is valid, false if not."""
117
+ cdef _Document doc
118
+ cdef _Element root_node
119
+ cdef xmlDoc* c_doc
120
+ cdef relaxng.xmlRelaxNGValidCtxt* valid_ctxt
121
+ cdef int ret
122
+
123
+ assert self._c_schema is not NULL, "RelaxNG instance not initialised"
124
+ doc = _documentOrRaise(etree)
125
+ root_node = _rootNodeOrRaise(etree)
126
+
127
+ valid_ctxt = relaxng.xmlRelaxNGNewValidCtxt(self._c_schema)
128
+ if valid_ctxt is NULL:
129
+ raise MemoryError()
130
+
131
+ try:
132
+ self._error_log.clear()
133
+ # Need a cast here because older libxml2 releases do not use 'const' in the functype.
134
+ relaxng.xmlRelaxNGSetValidStructuredErrors(
135
+ valid_ctxt, <xmlerror.xmlStructuredErrorFunc> _receiveError, <void*>self._error_log)
136
+ _connectGenericErrorLog(self._error_log, xmlerror.XML_FROM_RELAXNGV)
137
+ c_doc = _fakeRootDoc(doc._c_doc, root_node._c_node)
138
+ with nogil:
139
+ ret = relaxng.xmlRelaxNGValidateDoc(valid_ctxt, c_doc)
140
+ _destroyFakeDoc(doc._c_doc, c_doc)
141
+ finally:
142
+ _connectGenericErrorLog(None)
143
+ relaxng.xmlRelaxNGFreeValidCtxt(valid_ctxt)
144
+
145
+ if ret == -1:
146
+ raise RelaxNGValidateError(
147
+ "Internal error in Relax NG validation",
148
+ self._error_log)
149
+ if ret == 0:
150
+ return True
151
+ else:
152
+ return False
153
+
154
+ @classmethod
155
+ def from_rnc_string(cls, src, base_url=None):
156
+ """Parse a RelaxNG schema in compact syntax from a text string
157
+
158
+ Requires the rnc2rng package to be installed.
159
+
160
+ Passing the source URL or file path of the source as 'base_url'
161
+ will enable resolving resource references relative to the source.
162
+ """
163
+ _require_rnc2rng()
164
+ rng_str = utf8(_rnc2rng.dumps(_rnc2rng.loads(src)))
165
+ return cls(_parseMemoryDocument(rng_str, parser=None, url=base_url))
Binary file
lxml/sax.py ADDED
@@ -0,0 +1,275 @@
1
+ # cython: language_level=2
2
+
3
+ """
4
+ SAX-based adapter to copy trees from/to the Python standard library.
5
+
6
+ Use the `ElementTreeContentHandler` class to build an ElementTree from
7
+ SAX events.
8
+
9
+ Use the `ElementTreeProducer` class or the `saxify()` function to fire
10
+ the SAX events of an ElementTree against a SAX ContentHandler.
11
+
12
+ See https://lxml.de/sax.html
13
+ """
14
+
15
+
16
+ from xml.sax.handler import ContentHandler
17
+ from lxml import etree
18
+ from lxml.etree import ElementTree, SubElement
19
+ from lxml.etree import Comment, ProcessingInstruction
20
+
21
+
22
+ class SaxError(etree.LxmlError):
23
+ """General SAX error.
24
+ """
25
+
26
+
27
+ def _getNsTag(tag):
28
+ if tag[0] == '{':
29
+ return tuple(tag[1:].split('}', 1))
30
+ else:
31
+ return None, tag
32
+
33
+
34
+ class ElementTreeContentHandler(ContentHandler):
35
+ """Build an lxml ElementTree from SAX events.
36
+ """
37
+ def __init__(self, makeelement=None):
38
+ ContentHandler.__init__(self)
39
+ self._root = None
40
+ self._root_siblings = []
41
+ self._element_stack = []
42
+ self._default_ns = None
43
+ self._ns_mapping = { None : [None] }
44
+ self._new_mappings = {}
45
+ if makeelement is None:
46
+ makeelement = etree.Element
47
+ self._makeelement = makeelement
48
+
49
+ def _get_etree(self):
50
+ "Contains the generated ElementTree after parsing is finished."
51
+ return ElementTree(self._root)
52
+
53
+ etree = property(_get_etree, doc=_get_etree.__doc__)
54
+
55
+ def setDocumentLocator(self, locator):
56
+ pass
57
+
58
+ def startDocument(self):
59
+ pass
60
+
61
+ def endDocument(self):
62
+ pass
63
+
64
+ def startPrefixMapping(self, prefix, uri):
65
+ self._new_mappings[prefix] = uri
66
+ try:
67
+ self._ns_mapping[prefix].append(uri)
68
+ except KeyError:
69
+ self._ns_mapping[prefix] = [uri]
70
+ if prefix is None:
71
+ self._default_ns = uri
72
+
73
+ def endPrefixMapping(self, prefix):
74
+ ns_uri_list = self._ns_mapping[prefix]
75
+ ns_uri_list.pop()
76
+ if prefix is None:
77
+ self._default_ns = ns_uri_list[-1]
78
+
79
+ def _buildTag(self, ns_name_tuple):
80
+ ns_uri, local_name = ns_name_tuple
81
+ if ns_uri:
82
+ el_tag = "{%s}%s" % ns_name_tuple
83
+ elif self._default_ns:
84
+ el_tag = "{%s}%s" % (self._default_ns, local_name)
85
+ else:
86
+ el_tag = local_name
87
+ return el_tag
88
+
89
+ def startElementNS(self, ns_name, qname, attributes=None):
90
+ el_name = self._buildTag(ns_name)
91
+ if attributes:
92
+ attrs = {}
93
+ try:
94
+ iter_attributes = attributes.iteritems()
95
+ except AttributeError:
96
+ iter_attributes = attributes.items()
97
+
98
+ for name_tuple, value in iter_attributes:
99
+ if name_tuple[0]:
100
+ attr_name = "{%s}%s" % name_tuple
101
+ else:
102
+ attr_name = name_tuple[1]
103
+ attrs[attr_name] = value
104
+ else:
105
+ attrs = None
106
+
107
+ element_stack = self._element_stack
108
+ if self._root is None:
109
+ element = self._root = \
110
+ self._makeelement(el_name, attrs, self._new_mappings)
111
+ if self._root_siblings and hasattr(element, 'addprevious'):
112
+ for sibling in self._root_siblings:
113
+ element.addprevious(sibling)
114
+ del self._root_siblings[:]
115
+ else:
116
+ element = SubElement(element_stack[-1], el_name,
117
+ attrs, self._new_mappings)
118
+ element_stack.append(element)
119
+
120
+ self._new_mappings.clear()
121
+
122
+ def processingInstruction(self, target, data):
123
+ pi = ProcessingInstruction(target, data)
124
+ if self._root is None:
125
+ self._root_siblings.append(pi)
126
+ else:
127
+ self._element_stack[-1].append(pi)
128
+
129
+ def endElementNS(self, ns_name, qname):
130
+ element = self._element_stack.pop()
131
+ el_tag = self._buildTag(ns_name)
132
+ if el_tag != element.tag:
133
+ raise SaxError("Unexpected element closed: " + el_tag)
134
+
135
+ def startElement(self, name, attributes=None):
136
+ if attributes:
137
+ attributes = {(None, k): v for k, v in attributes.items()}
138
+ self.startElementNS((None, name), name, attributes)
139
+
140
+ def endElement(self, name):
141
+ self.endElementNS((None, name), name)
142
+
143
+ def characters(self, data):
144
+ last_element = self._element_stack[-1]
145
+ try:
146
+ # if there already is a child element, we must append to its tail
147
+ last_element = last_element[-1]
148
+ last_element.tail = (last_element.tail or '') + data
149
+ except IndexError:
150
+ # otherwise: append to the text
151
+ last_element.text = (last_element.text or '') + data
152
+
153
+ ignorableWhitespace = characters
154
+
155
+
156
+ class ElementTreeProducer:
157
+ """Produces SAX events for an element and children.
158
+ """
159
+ def __init__(self, element_or_tree, content_handler):
160
+ try:
161
+ element = element_or_tree.getroot()
162
+ except AttributeError:
163
+ element = element_or_tree
164
+ self._element = element
165
+ self._content_handler = content_handler
166
+ from xml.sax.xmlreader import AttributesNSImpl as attr_class
167
+ self._attr_class = attr_class
168
+ self._empty_attributes = attr_class({}, {})
169
+
170
+ def saxify(self):
171
+ self._content_handler.startDocument()
172
+
173
+ element = self._element
174
+ if hasattr(element, 'getprevious'):
175
+ siblings = []
176
+ sibling = element.getprevious()
177
+ while getattr(sibling, 'tag', None) is ProcessingInstruction:
178
+ siblings.append(sibling)
179
+ sibling = sibling.getprevious()
180
+ for sibling in siblings[::-1]:
181
+ self._recursive_saxify(sibling, {})
182
+
183
+ self._recursive_saxify(element, {})
184
+
185
+ if hasattr(element, 'getnext'):
186
+ sibling = element.getnext()
187
+ while getattr(sibling, 'tag', None) is ProcessingInstruction:
188
+ self._recursive_saxify(sibling, {})
189
+ sibling = sibling.getnext()
190
+
191
+ self._content_handler.endDocument()
192
+
193
+ def _recursive_saxify(self, element, parent_nsmap):
194
+ content_handler = self._content_handler
195
+ tag = element.tag
196
+ if tag is Comment or tag is ProcessingInstruction:
197
+ if tag is ProcessingInstruction:
198
+ content_handler.processingInstruction(
199
+ element.target, element.text)
200
+ tail = element.tail
201
+ if tail:
202
+ content_handler.characters(tail)
203
+ return
204
+
205
+ element_nsmap = element.nsmap
206
+ new_prefixes = []
207
+ if element_nsmap != parent_nsmap:
208
+ # There have been updates to the namespace
209
+ for prefix, ns_uri in element_nsmap.items():
210
+ if parent_nsmap.get(prefix) != ns_uri:
211
+ new_prefixes.append( (prefix, ns_uri) )
212
+
213
+ attribs = element.items()
214
+ if attribs:
215
+ attr_values = {}
216
+ attr_qnames = {}
217
+ for attr_ns_name, value in attribs:
218
+ attr_ns_tuple = _getNsTag(attr_ns_name)
219
+ attr_values[attr_ns_tuple] = value
220
+ attr_qnames[attr_ns_tuple] = self._build_qname(
221
+ attr_ns_tuple[0], attr_ns_tuple[1], element_nsmap,
222
+ preferred_prefix=None, is_attribute=True)
223
+ sax_attributes = self._attr_class(attr_values, attr_qnames)
224
+ else:
225
+ sax_attributes = self._empty_attributes
226
+
227
+ ns_uri, local_name = _getNsTag(tag)
228
+ qname = self._build_qname(
229
+ ns_uri, local_name, element_nsmap, element.prefix, is_attribute=False)
230
+
231
+ for prefix, uri in new_prefixes:
232
+ content_handler.startPrefixMapping(prefix, uri)
233
+ content_handler.startElementNS(
234
+ (ns_uri, local_name), qname, sax_attributes)
235
+ text = element.text
236
+ if text:
237
+ content_handler.characters(text)
238
+ for child in element:
239
+ self._recursive_saxify(child, element_nsmap)
240
+ content_handler.endElementNS((ns_uri, local_name), qname)
241
+ for prefix, uri in new_prefixes:
242
+ content_handler.endPrefixMapping(prefix)
243
+ tail = element.tail
244
+ if tail:
245
+ content_handler.characters(tail)
246
+
247
+ def _build_qname(self, ns_uri, local_name, nsmap, preferred_prefix, is_attribute):
248
+ if ns_uri is None:
249
+ return local_name
250
+
251
+ if not is_attribute and nsmap.get(preferred_prefix) == ns_uri:
252
+ prefix = preferred_prefix
253
+ else:
254
+ # Pick the first matching prefix, in alphabetical order.
255
+ candidates = [
256
+ pfx for (pfx, uri) in nsmap.items()
257
+ if pfx is not None and uri == ns_uri
258
+ ]
259
+ prefix = (
260
+ candidates[0] if len(candidates) == 1
261
+ else min(candidates) if candidates
262
+ else None
263
+ )
264
+
265
+ if prefix is None:
266
+ # Default namespace
267
+ return local_name
268
+ return prefix + ':' + local_name
269
+
270
+
271
+ def saxify(element_or_tree, content_handler):
272
+ """One-shot helper to generate SAX events from an XML tree and fire
273
+ them against a SAX ContentHandler.
274
+ """
275
+ return ElementTreeProducer(element_or_tree, content_handler).saxify()