f73afee61c231196ae87a8a022b46e785e36d835
[platform/upstream/python-lxml.git] / src / lxml / xinclude.pxi
1 # XInclude processing
2
3 from lxml.includes cimport xinclude
4
5
6 cdef class XIncludeError(LxmlError):
7     u"""Error during XInclude processing.
8     """
9
10
11 cdef class XInclude:
12     u"""XInclude(self)
13     XInclude processor.
14
15     Create an instance and call it on an Element to run XInclude
16     processing.
17     """
18     cdef _ErrorLog _error_log
19     def __init__(self):
20         self._error_log = _ErrorLog()
21
22     @property
23     def error_log(self):
24         assert self._error_log is not None, "XInclude instance not initialised"
25         return self._error_log.copy()
26
27     def __call__(self, _Element node not None):
28         u"__call__(self, node)"
29         # We cannot pass the XML_PARSE_NOXINCNODE option as this would free
30         # the XInclude nodes - there may still be Python references to them!
31         # Therefore, we allow XInclude nodes to be converted to
32         # XML_XINCLUDE_START nodes.  XML_XINCLUDE_END nodes are added as
33         # siblings.  Tree traversal will simply ignore them as they are not
34         # typed as elements.  The included fragment is added between the two,
35         # i.e. as a sibling, which does not conflict with traversal.
36         cdef int result
37         _assertValidNode(node)
38         assert self._error_log is not None, "XInclude processor not initialised"
39         if node._doc._parser is not None:
40             parse_options = node._doc._parser._parse_options
41             context = node._doc._parser._getParserContext()
42             c_context = <void*>context
43         else:
44             parse_options = 0
45             context = None
46             c_context = NULL
47
48         self._error_log.connect()
49         if tree.LIBXML_VERSION < 20704 or not c_context:
50             __GLOBAL_PARSER_CONTEXT.pushImpliedContext(context)
51         with nogil:
52             if c_context:
53                 result = xinclude.xmlXIncludeProcessTreeFlagsData(
54                     node._c_node, parse_options, c_context)
55             else:
56                 result = xinclude.xmlXIncludeProcessTree(node._c_node)
57         if tree.LIBXML_VERSION < 20704 or not c_context:
58             __GLOBAL_PARSER_CONTEXT.popImpliedContext()
59         self._error_log.disconnect()
60
61         if result == -1:
62             raise XIncludeError(
63                 self._error_log._buildExceptionMessage(
64                     u"XInclude processing failed"),
65                 self._error_log)