You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 line
2.4 KiB

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