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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# XSLT extension elements
|
| 2 |
+
|
| 3 |
+
cdef class XSLTExtension:
|
| 4 |
+
"""Base class of an XSLT extension element.
|
| 5 |
+
"""
|
| 6 |
+
def execute(self, context, self_node, input_node, output_parent):
|
| 7 |
+
"""execute(self, context, self_node, input_node, output_parent)
|
| 8 |
+
Execute this extension element.
|
| 9 |
+
|
| 10 |
+
Subclasses must override this method. They may append
|
| 11 |
+
elements to the `output_parent` element here, or set its text
|
| 12 |
+
content. To this end, the `input_node` provides read-only
|
| 13 |
+
access to the current node in the input document, and the
|
| 14 |
+
`self_node` points to the extension element in the stylesheet.
|
| 15 |
+
|
| 16 |
+
Note that the `output_parent` parameter may be `None` if there
|
| 17 |
+
is no parent element in the current context (e.g. no content
|
| 18 |
+
was added to the output tree yet).
|
| 19 |
+
"""
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
def apply_templates(self, _XSLTContext context not None, node, output_parent=None,
|
| 23 |
+
*, elements_only=False, remove_blank_text=False):
|
| 24 |
+
"""apply_templates(self, context, node, output_parent=None, elements_only=False, remove_blank_text=False)
|
| 25 |
+
|
| 26 |
+
Call this method to retrieve the result of applying templates
|
| 27 |
+
to an element.
|
| 28 |
+
|
| 29 |
+
The return value is a list of elements or text strings that
|
| 30 |
+
were generated by the XSLT processor. If you pass
|
| 31 |
+
``elements_only=True``, strings will be discarded from the result
|
| 32 |
+
list. The option ``remove_blank_text=True`` will only discard
|
| 33 |
+
strings that consist entirely of whitespace (e.g. formatting).
|
| 34 |
+
These options do not apply to Elements, only to bare string results.
|
| 35 |
+
|
| 36 |
+
If you pass an Element as `output_parent` parameter, the result
|
| 37 |
+
will instead be appended to the element (including attributes
|
| 38 |
+
etc.) and the return value will be `None`. This is a safe way
|
| 39 |
+
to generate content into the output document directly, without
|
| 40 |
+
having to take care of special values like text or attributes.
|
| 41 |
+
Note that the string discarding options will be ignored in this
|
| 42 |
+
case.
|
| 43 |
+
"""
|
| 44 |
+
cdef xmlNode* c_parent
|
| 45 |
+
cdef xmlNode* c_node
|
| 46 |
+
cdef xmlNode* c_context_node
|
| 47 |
+
assert context._xsltCtxt is not NULL, "XSLT context not initialised"
|
| 48 |
+
c_context_node = _roNodeOf(node)
|
| 49 |
+
#assert c_context_node.doc is context._xsltContext.node.doc, \
|
| 50 |
+
# "switching input documents during transformation is not currently supported"
|
| 51 |
+
|
| 52 |
+
if output_parent is not None:
|
| 53 |
+
c_parent = _nonRoNodeOf(output_parent)
|
| 54 |
+
else:
|
| 55 |
+
c_parent = tree.xmlNewDocNode(
|
| 56 |
+
context._xsltCtxt.output, NULL, <unsigned char*>"fake-parent", NULL)
|
| 57 |
+
|
| 58 |
+
c_node = context._xsltCtxt.insert
|
| 59 |
+
context._xsltCtxt.insert = c_parent
|
| 60 |
+
xslt.xsltProcessOneNode(
|
| 61 |
+
context._xsltCtxt, c_context_node, NULL)
|
| 62 |
+
context._xsltCtxt.insert = c_node
|
| 63 |
+
|
| 64 |
+
if output_parent is not None:
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
return self._collectXSLTResultContent(
|
| 69 |
+
context, c_parent, elements_only, remove_blank_text)
|
| 70 |
+
finally:
|
| 71 |
+
# free all intermediate nodes that will not be freed by proxies
|
| 72 |
+
tree.xmlFreeNode(c_parent)
|
| 73 |
+
|
| 74 |
+
def process_children(self, _XSLTContext context not None, output_parent=None,
|
| 75 |
+
*, elements_only=False, remove_blank_text=False):
|
| 76 |
+
"""process_children(self, context, output_parent=None, elements_only=False, remove_blank_text=False)
|
| 77 |
+
|
| 78 |
+
Call this method to process the XSLT content of the extension
|
| 79 |
+
element itself.
|
| 80 |
+
|
| 81 |
+
The return value is a list of elements or text strings that
|
| 82 |
+
were generated by the XSLT processor. If you pass
|
| 83 |
+
``elements_only=True``, strings will be discarded from the result
|
| 84 |
+
list. The option ``remove_blank_text=True`` will only discard
|
| 85 |
+
strings that consist entirely of whitespace (e.g. formatting).
|
| 86 |
+
These options do not apply to Elements, only to bare string results.
|
| 87 |
+
|
| 88 |
+
If you pass an Element as `output_parent` parameter, the result
|
| 89 |
+
will instead be appended to the element (including attributes
|
| 90 |
+
etc.) and the return value will be `None`. This is a safe way
|
| 91 |
+
to generate content into the output document directly, without
|
| 92 |
+
having to take care of special values like text or attributes.
|
| 93 |
+
Note that the string discarding options will be ignored in this
|
| 94 |
+
case.
|
| 95 |
+
"""
|
| 96 |
+
cdef xmlNode* c_parent
|
| 97 |
+
cdef xslt.xsltTransformContext* c_ctxt = context._xsltCtxt
|
| 98 |
+
cdef xmlNode* c_old_output_parent = c_ctxt.insert
|
| 99 |
+
assert context._xsltCtxt is not NULL, "XSLT context not initialised"
|
| 100 |
+
|
| 101 |
+
# output_parent node is used for adding results instead of
|
| 102 |
+
# elements list used in apply_templates, that's easier and allows to
|
| 103 |
+
# use attributes added to extension element with <xsl:attribute>.
|
| 104 |
+
|
| 105 |
+
if output_parent is not None:
|
| 106 |
+
c_parent = _nonRoNodeOf(output_parent)
|
| 107 |
+
else:
|
| 108 |
+
c_parent = tree.xmlNewDocNode(
|
| 109 |
+
context._xsltCtxt.output, NULL, <unsigned char*>"fake-parent", NULL)
|
| 110 |
+
|
| 111 |
+
c_ctxt.insert = c_parent
|
| 112 |
+
xslt.xsltApplyOneTemplate(c_ctxt,
|
| 113 |
+
c_ctxt.node, c_ctxt.inst.children, NULL, NULL)
|
| 114 |
+
c_ctxt.insert = c_old_output_parent
|
| 115 |
+
|
| 116 |
+
if output_parent is not None:
|
| 117 |
+
return None
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
return self._collectXSLTResultContent(
|
| 121 |
+
context, c_parent, elements_only, remove_blank_text)
|
| 122 |
+
finally:
|
| 123 |
+
# free all intermediate nodes that will not be freed by proxies
|
| 124 |
+
tree.xmlFreeNode(c_parent)
|
| 125 |
+
|
| 126 |
+
cdef _collectXSLTResultContent(self, _XSLTContext context, xmlNode* c_parent,
|
| 127 |
+
bint elements_only, bint remove_blank_text):
|
| 128 |
+
cdef xmlNode* c_node
|
| 129 |
+
cdef xmlNode* c_next
|
| 130 |
+
cdef _ReadOnlyProxy proxy
|
| 131 |
+
cdef list results = [] # or maybe _collectAttributes(c_parent, 2) ?
|
| 132 |
+
c_node = c_parent.children
|
| 133 |
+
while c_node is not NULL:
|
| 134 |
+
c_next = c_node.next
|
| 135 |
+
if c_node.type == tree.XML_TEXT_NODE:
|
| 136 |
+
if not elements_only:
|
| 137 |
+
s = funicode(c_node.content)
|
| 138 |
+
if not remove_blank_text or s.strip():
|
| 139 |
+
results.append(s)
|
| 140 |
+
s = None
|
| 141 |
+
elif c_node.type == tree.XML_ELEMENT_NODE:
|
| 142 |
+
proxy = _newReadOnlyProxy(
|
| 143 |
+
context._extension_element_proxy, c_node)
|
| 144 |
+
results.append(proxy)
|
| 145 |
+
# unlink node and make sure it will be freed later on
|
| 146 |
+
tree.xmlUnlinkNode(c_node)
|
| 147 |
+
proxy.free_after_use()
|
| 148 |
+
else:
|
| 149 |
+
raise TypeError, \
|
| 150 |
+
f"unsupported XSLT result type: {c_node.type}"
|
| 151 |
+
c_node = c_next
|
| 152 |
+
return results
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
cdef _registerXSLTExtensions(xslt.xsltTransformContext* c_ctxt,
|
| 156 |
+
extension_dict):
|
| 157 |
+
for ns_utf, name_utf in extension_dict:
|
| 158 |
+
xslt.xsltRegisterExtElement(
|
| 159 |
+
c_ctxt, _xcstr(name_utf), _xcstr(ns_utf),
|
| 160 |
+
<xslt.xsltTransformFunction>_callExtensionElement)
|
| 161 |
+
|
| 162 |
+
cdef void _callExtensionElement(xslt.xsltTransformContext* c_ctxt,
|
| 163 |
+
xmlNode* c_context_node,
|
| 164 |
+
xmlNode* c_inst_node,
|
| 165 |
+
void* dummy) noexcept with gil:
|
| 166 |
+
cdef _XSLTContext context
|
| 167 |
+
cdef XSLTExtension extension
|
| 168 |
+
cdef python.PyObject* dict_result
|
| 169 |
+
cdef xmlNode* c_node
|
| 170 |
+
cdef _ReadOnlyProxy context_node = None, self_node = None
|
| 171 |
+
cdef object output_parent # not restricted to ro-nodes
|
| 172 |
+
c_uri = _getNs(c_inst_node)
|
| 173 |
+
if c_uri is NULL:
|
| 174 |
+
# not allowed, and should never happen
|
| 175 |
+
return
|
| 176 |
+
if c_ctxt.xpathCtxt.userData is NULL:
|
| 177 |
+
# just for safety, should never happen
|
| 178 |
+
return
|
| 179 |
+
context = <_XSLTContext>c_ctxt.xpathCtxt.userData
|
| 180 |
+
try:
|
| 181 |
+
try:
|
| 182 |
+
dict_result = python.PyDict_GetItem(
|
| 183 |
+
context._extension_elements, (c_uri, c_inst_node.name))
|
| 184 |
+
if dict_result is NULL:
|
| 185 |
+
raise KeyError, f"extension element {funicode(c_inst_node.name)} not found"
|
| 186 |
+
extension = <object>dict_result
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
# build the context proxy nodes
|
| 190 |
+
self_node = _newReadOnlyProxy(None, c_inst_node)
|
| 191 |
+
if _isElement(c_ctxt.insert):
|
| 192 |
+
output_parent = _newAppendOnlyProxy(self_node, c_ctxt.insert)
|
| 193 |
+
else:
|
| 194 |
+
# may be the document node or other stuff
|
| 195 |
+
output_parent = _newOpaqueAppendOnlyNodeWrapper(c_ctxt.insert)
|
| 196 |
+
if c_context_node.type in (tree.XML_DOCUMENT_NODE,
|
| 197 |
+
tree.XML_HTML_DOCUMENT_NODE):
|
| 198 |
+
c_node = tree.xmlDocGetRootElement(<xmlDoc*>c_context_node)
|
| 199 |
+
if c_node is not NULL:
|
| 200 |
+
context_node = _newReadOnlyProxy(self_node, c_node)
|
| 201 |
+
else:
|
| 202 |
+
context_node = None
|
| 203 |
+
elif c_context_node.type in (tree.XML_ATTRIBUTE_NODE,
|
| 204 |
+
tree.XML_TEXT_NODE,
|
| 205 |
+
tree.XML_CDATA_SECTION_NODE):
|
| 206 |
+
# this isn't easy to support using read-only
|
| 207 |
+
# nodes, as the smart-string factory must
|
| 208 |
+
# instantiate the parent proxy somehow...
|
| 209 |
+
raise TypeError(f"Unsupported element type: {c_context_node.type}")
|
| 210 |
+
else:
|
| 211 |
+
context_node = _newReadOnlyProxy(self_node, c_context_node)
|
| 212 |
+
|
| 213 |
+
# run the XSLT extension
|
| 214 |
+
context._extension_element_proxy = self_node
|
| 215 |
+
extension.execute(context, self_node, context_node, output_parent)
|
| 216 |
+
finally:
|
| 217 |
+
context._extension_element_proxy = None
|
| 218 |
+
if self_node is not None:
|
| 219 |
+
_freeReadOnlyProxies(self_node)
|
| 220 |
+
except Exception as e:
|
| 221 |
+
try:
|
| 222 |
+
e = unicode(e).encode("UTF-8")
|
| 223 |
+
except:
|
| 224 |
+
e = repr(e).encode("UTF-8")
|
| 225 |
+
message = python.PyBytes_FromFormat(
|
| 226 |
+
"Error executing extension element '%s': %s",
|
| 227 |
+
c_inst_node.name, _cstr(e))
|
| 228 |
+
xslt.xsltTransformError(c_ctxt, NULL, c_inst_node, "%s", message)
|
| 229 |
+
context._exc._store_raised()
|
| 230 |
+
except:
|
| 231 |
+
# just in case
|
| 232 |
+
message = python.PyBytes_FromFormat(
|
| 233 |
+
"Error executing extension element '%s'", c_inst_node.name)
|
| 234 |
+
xslt.xsltTransformError(c_ctxt, NULL, c_inst_node, "%s", message)
|
| 235 |
+
context._exc._store_raised()
|
| 236 |
+
except:
|
| 237 |
+
# no Python functions here - everything can fail...
|
| 238 |
+
xslt.xsltTransformError(c_ctxt, NULL, c_inst_node,
|
| 239 |
+
"Error during XSLT extension element evaluation")
|
| 240 |
+
context._exc._store_raised()
|
| 241 |
+
finally:
|
| 242 |
+
return # swallow any further exceptions
|