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.
 
 
 
 

488 rivejä
19 KiB

  1. # XPath evaluation
  2. class XPathSyntaxError(LxmlSyntaxError, XPathError):
  3. pass
  4. ################################################################################
  5. # XPath
  6. cdef object _XPATH_SYNTAX_ERRORS = (
  7. xmlerror.XML_XPATH_NUMBER_ERROR,
  8. xmlerror.XML_XPATH_UNFINISHED_LITERAL_ERROR,
  9. xmlerror.XML_XPATH_VARIABLE_REF_ERROR,
  10. xmlerror.XML_XPATH_INVALID_PREDICATE_ERROR,
  11. xmlerror.XML_XPATH_UNCLOSED_ERROR,
  12. xmlerror.XML_XPATH_INVALID_CHAR_ERROR
  13. )
  14. cdef object _XPATH_EVAL_ERRORS = (
  15. xmlerror.XML_XPATH_UNDEF_VARIABLE_ERROR,
  16. xmlerror.XML_XPATH_UNDEF_PREFIX_ERROR,
  17. xmlerror.XML_XPATH_UNKNOWN_FUNC_ERROR,
  18. xmlerror.XML_XPATH_INVALID_OPERAND,
  19. xmlerror.XML_XPATH_INVALID_TYPE,
  20. xmlerror.XML_XPATH_INVALID_ARITY,
  21. xmlerror.XML_XPATH_INVALID_CTXT_SIZE,
  22. xmlerror.XML_XPATH_INVALID_CTXT_POSITION
  23. )
  24. cdef int _register_xpath_function(void* ctxt, name_utf, ns_utf) noexcept:
  25. if ns_utf is None:
  26. return xpath.xmlXPathRegisterFunc(
  27. <xpath.xmlXPathContext*>ctxt, _xcstr(name_utf),
  28. _xpath_function_call)
  29. else:
  30. return xpath.xmlXPathRegisterFuncNS(
  31. <xpath.xmlXPathContext*>ctxt, _xcstr(name_utf), _xcstr(ns_utf),
  32. _xpath_function_call)
  33. cdef int _unregister_xpath_function(void* ctxt, name_utf, ns_utf) noexcept:
  34. if ns_utf is None:
  35. return xpath.xmlXPathRegisterFunc(
  36. <xpath.xmlXPathContext*>ctxt, _xcstr(name_utf), NULL)
  37. else:
  38. return xpath.xmlXPathRegisterFuncNS(
  39. <xpath.xmlXPathContext*>ctxt, _xcstr(name_utf), _xcstr(ns_utf), NULL)
  40. @cython.final
  41. @cython.internal
  42. cdef class _XPathContext(_BaseContext):
  43. cdef object _variables
  44. def __init__(self, namespaces, extensions, error_log, enable_regexp, variables,
  45. build_smart_strings):
  46. self._variables = variables
  47. _BaseContext.__init__(self, namespaces, extensions, error_log, enable_regexp,
  48. build_smart_strings)
  49. cdef set_context(self, xpath.xmlXPathContext* xpathCtxt):
  50. self._set_xpath_context(xpathCtxt)
  51. # This would be a good place to set up the XPath parser dict, but
  52. # we cannot use the current thread dict as we do not know which
  53. # thread will execute the XPath evaluator - so, no dict for now.
  54. self.registerLocalNamespaces()
  55. self.registerLocalFunctions(xpathCtxt, _register_xpath_function)
  56. cdef register_context(self, _Document doc):
  57. self._register_context(doc)
  58. self.registerGlobalNamespaces()
  59. self.registerGlobalFunctions(self._xpathCtxt, _register_xpath_function)
  60. self.registerExsltFunctions()
  61. if self._variables is not None:
  62. self.registerVariables(self._variables)
  63. cdef unregister_context(self):
  64. self.unregisterGlobalFunctions(
  65. self._xpathCtxt, _unregister_xpath_function)
  66. self.unregisterGlobalNamespaces()
  67. xpath.xmlXPathRegisteredVariablesCleanup(self._xpathCtxt)
  68. self._cleanup_context()
  69. cdef void registerExsltFunctions(self) noexcept:
  70. if xslt.LIBXSLT_VERSION < 10125:
  71. # we'd only execute dummy functions anyway
  72. return
  73. tree.xmlHashScan(
  74. self._xpathCtxt.nsHash, _registerExsltFunctionsForNamespaces,
  75. self._xpathCtxt)
  76. cdef registerVariables(self, variable_dict):
  77. for name, value in variable_dict.items():
  78. name_utf = self._to_utf(name)
  79. xpath.xmlXPathRegisterVariable(
  80. self._xpathCtxt, _xcstr(name_utf), _wrapXPathObject(value, None, None))
  81. cdef registerVariable(self, name, value):
  82. name_utf = self._to_utf(name)
  83. xpath.xmlXPathRegisterVariable(
  84. self._xpathCtxt, _xcstr(name_utf), _wrapXPathObject(value, None, None))
  85. cdef void _registerExsltFunctionsForNamespaces(
  86. void* _c_href, void* _ctxt, const_xmlChar* c_prefix) noexcept:
  87. c_href = <const_xmlChar*> _c_href
  88. ctxt = <xpath.xmlXPathContext*> _ctxt
  89. if tree.xmlStrcmp(c_href, xslt.EXSLT_DATE_NAMESPACE) == 0:
  90. xslt.exsltDateXpathCtxtRegister(ctxt, c_prefix)
  91. elif tree.xmlStrcmp(c_href, xslt.EXSLT_SETS_NAMESPACE) == 0:
  92. xslt.exsltSetsXpathCtxtRegister(ctxt, c_prefix)
  93. elif tree.xmlStrcmp(c_href, xslt.EXSLT_MATH_NAMESPACE) == 0:
  94. xslt.exsltMathXpathCtxtRegister(ctxt, c_prefix)
  95. elif tree.xmlStrcmp(c_href, xslt.EXSLT_STRINGS_NAMESPACE) == 0:
  96. xslt.exsltStrXpathCtxtRegister(ctxt, c_prefix)
  97. cdef class _XPathEvaluatorBase:
  98. cdef xpath.xmlXPathContext* _xpathCtxt
  99. cdef _XPathContext _context
  100. cdef python.PyThread_type_lock _eval_lock
  101. cdef _ErrorLog _error_log
  102. def __cinit__(self):
  103. self._xpathCtxt = NULL
  104. if config.ENABLE_THREADING:
  105. self._eval_lock = python.PyThread_allocate_lock()
  106. if self._eval_lock is NULL:
  107. raise MemoryError()
  108. self._error_log = _ErrorLog()
  109. def __init__(self, namespaces, extensions, enable_regexp,
  110. smart_strings):
  111. self._context = _XPathContext(namespaces, extensions, self._error_log,
  112. enable_regexp, None, smart_strings)
  113. @property
  114. def error_log(self):
  115. assert self._error_log is not None, "XPath evaluator not initialised"
  116. return self._error_log.copy()
  117. def __dealloc__(self):
  118. if self._xpathCtxt is not NULL:
  119. xpath.xmlXPathFreeContext(self._xpathCtxt)
  120. if config.ENABLE_THREADING:
  121. if self._eval_lock is not NULL:
  122. python.PyThread_free_lock(self._eval_lock)
  123. cdef set_context(self, xpath.xmlXPathContext* xpathCtxt):
  124. self._xpathCtxt = xpathCtxt
  125. self._context.set_context(xpathCtxt)
  126. cdef bint _checkAbsolutePath(self, char* path) noexcept:
  127. cdef char c
  128. if path is NULL:
  129. return 0
  130. c = path[0]
  131. while c == c' ' or c == c'\t':
  132. path = path + 1
  133. c = path[0]
  134. return c == c'/'
  135. @cython.final
  136. cdef int _lock(self) except -1:
  137. cdef int result
  138. if config.ENABLE_THREADING and self._eval_lock != NULL:
  139. with nogil:
  140. result = python.PyThread_acquire_lock(
  141. self._eval_lock, python.WAIT_LOCK)
  142. if result == 0:
  143. raise XPathError, "XPath evaluator locking failed"
  144. return 0
  145. @cython.final
  146. cdef void _unlock(self) noexcept:
  147. if config.ENABLE_THREADING and self._eval_lock != NULL:
  148. python.PyThread_release_lock(self._eval_lock)
  149. cdef _build_parse_error(self):
  150. cdef _BaseErrorLog entries
  151. entries = self._error_log.filter_types(_XPATH_SYNTAX_ERRORS)
  152. if entries:
  153. message = entries._buildExceptionMessage(None)
  154. if message is not None:
  155. return XPathSyntaxError(message, self._error_log)
  156. return XPathSyntaxError(
  157. self._error_log._buildExceptionMessage("Error in xpath expression"),
  158. self._error_log)
  159. cdef _build_eval_error(self):
  160. cdef _BaseErrorLog entries
  161. entries = self._error_log.filter_types(_XPATH_EVAL_ERRORS)
  162. if not entries:
  163. entries = self._error_log.filter_types(_XPATH_SYNTAX_ERRORS)
  164. if entries:
  165. message = entries._buildExceptionMessage(None)
  166. if message is not None:
  167. return XPathEvalError(message, self._error_log)
  168. return XPathEvalError(
  169. self._error_log._buildExceptionMessage("Error in xpath expression"),
  170. self._error_log)
  171. cdef object _handle_result(self, xpath.xmlXPathObject* xpathObj, _Document doc):
  172. if self._context._exc._has_raised():
  173. if xpathObj is not NULL:
  174. _freeXPathObject(xpathObj)
  175. xpathObj = NULL
  176. self._context._release_temp_refs()
  177. self._context._exc._raise_if_stored()
  178. if xpathObj is NULL:
  179. self._context._release_temp_refs()
  180. raise self._build_eval_error()
  181. try:
  182. result = _unwrapXPathObject(xpathObj, doc, self._context)
  183. finally:
  184. _freeXPathObject(xpathObj)
  185. self._context._release_temp_refs()
  186. return result
  187. cdef class XPathElementEvaluator(_XPathEvaluatorBase):
  188. """XPathElementEvaluator(self, element, namespaces=None, extensions=None, regexp=True, smart_strings=True)
  189. Create an XPath evaluator for an element.
  190. Absolute XPath expressions (starting with '/') will be evaluated against
  191. the ElementTree as returned by getroottree().
  192. Additional namespace declarations can be passed with the
  193. 'namespace' keyword argument. EXSLT regular expression support
  194. can be disabled with the 'regexp' boolean keyword (defaults to
  195. True). Smart strings will be returned for string results unless
  196. you pass ``smart_strings=False``.
  197. """
  198. cdef _Element _element
  199. def __init__(self, _Element element not None, *, namespaces=None,
  200. extensions=None, regexp=True, smart_strings=True):
  201. cdef xpath.xmlXPathContext* xpathCtxt
  202. cdef int ns_register_status
  203. cdef _Document doc
  204. _assertValidNode(element)
  205. _assertValidDoc(element._doc)
  206. self._element = element
  207. doc = element._doc
  208. _XPathEvaluatorBase.__init__(self, namespaces, extensions,
  209. regexp, smart_strings)
  210. xpathCtxt = xpath.xmlXPathNewContext(doc._c_doc)
  211. if xpathCtxt is NULL:
  212. raise MemoryError()
  213. self.set_context(xpathCtxt)
  214. def register_namespace(self, prefix, uri):
  215. """Register a namespace with the XPath context.
  216. """
  217. assert self._xpathCtxt is not NULL, "XPath context not initialised"
  218. self._context.addNamespace(prefix, uri)
  219. def register_namespaces(self, namespaces):
  220. """Register a prefix -> uri dict.
  221. """
  222. assert self._xpathCtxt is not NULL, "XPath context not initialised"
  223. for prefix, uri in namespaces.items():
  224. self._context.addNamespace(prefix, uri)
  225. def __call__(self, _path, **_variables):
  226. """__call__(self, _path, **_variables)
  227. Evaluate an XPath expression on the document.
  228. Variables may be provided as keyword arguments. Note that namespaces
  229. are currently not supported for variables.
  230. Absolute XPath expressions (starting with '/') will be evaluated
  231. against the ElementTree as returned by getroottree().
  232. """
  233. cdef xpath.xmlXPathObject* xpathObj
  234. cdef _Document doc
  235. assert self._xpathCtxt is not NULL, "XPath context not initialised"
  236. path = _utf8(_path)
  237. doc = self._element._doc
  238. self._lock()
  239. self._xpathCtxt.node = self._element._c_node
  240. try:
  241. self._context.register_context(doc)
  242. self._context.registerVariables(_variables)
  243. c_path = _xcstr(path)
  244. with nogil:
  245. xpathObj = xpath.xmlXPathEvalExpression(
  246. c_path, self._xpathCtxt)
  247. result = self._handle_result(xpathObj, doc)
  248. finally:
  249. self._context.unregister_context()
  250. self._unlock()
  251. return result
  252. cdef class XPathDocumentEvaluator(XPathElementEvaluator):
  253. """XPathDocumentEvaluator(self, etree, namespaces=None, extensions=None, regexp=True, smart_strings=True)
  254. Create an XPath evaluator for an ElementTree.
  255. Additional namespace declarations can be passed with the
  256. 'namespace' keyword argument. EXSLT regular expression support
  257. can be disabled with the 'regexp' boolean keyword (defaults to
  258. True). Smart strings will be returned for string results unless
  259. you pass ``smart_strings=False``.
  260. """
  261. def __init__(self, _ElementTree etree not None, *, namespaces=None,
  262. extensions=None, regexp=True, smart_strings=True):
  263. XPathElementEvaluator.__init__(
  264. self, etree._context_node, namespaces=namespaces,
  265. extensions=extensions, regexp=regexp,
  266. smart_strings=smart_strings)
  267. def __call__(self, _path, **_variables):
  268. """__call__(self, _path, **_variables)
  269. Evaluate an XPath expression on the document.
  270. Variables may be provided as keyword arguments. Note that namespaces
  271. are currently not supported for variables.
  272. """
  273. cdef xpath.xmlXPathObject* xpathObj
  274. cdef xmlDoc* c_doc
  275. cdef _Document doc
  276. assert self._xpathCtxt is not NULL, "XPath context not initialised"
  277. path = _utf8(_path)
  278. doc = self._element._doc
  279. self._lock()
  280. try:
  281. self._context.register_context(doc)
  282. c_doc = _fakeRootDoc(doc._c_doc, self._element._c_node)
  283. try:
  284. self._context.registerVariables(_variables)
  285. c_path = _xcstr(path)
  286. with nogil:
  287. self._xpathCtxt.doc = c_doc
  288. self._xpathCtxt.node = tree.xmlDocGetRootElement(c_doc)
  289. xpathObj = xpath.xmlXPathEvalExpression(
  290. c_path, self._xpathCtxt)
  291. result = self._handle_result(xpathObj, doc)
  292. finally:
  293. _destroyFakeDoc(doc._c_doc, c_doc)
  294. self._context.unregister_context()
  295. finally:
  296. self._unlock()
  297. return result
  298. def XPathEvaluator(etree_or_element, *, namespaces=None, extensions=None,
  299. regexp=True, smart_strings=True):
  300. """XPathEvaluator(etree_or_element, namespaces=None, extensions=None, regexp=True, smart_strings=True)
  301. Creates an XPath evaluator for an ElementTree or an Element.
  302. The resulting object can be called with an XPath expression as argument
  303. and XPath variables provided as keyword arguments.
  304. Additional namespace declarations can be passed with the
  305. 'namespace' keyword argument. EXSLT regular expression support
  306. can be disabled with the 'regexp' boolean keyword (defaults to
  307. True). Smart strings will be returned for string results unless
  308. you pass ``smart_strings=False``.
  309. """
  310. if isinstance(etree_or_element, _ElementTree):
  311. return XPathDocumentEvaluator(
  312. etree_or_element, namespaces=namespaces,
  313. extensions=extensions, regexp=regexp, smart_strings=smart_strings)
  314. else:
  315. return XPathElementEvaluator(
  316. etree_or_element, namespaces=namespaces,
  317. extensions=extensions, regexp=regexp, smart_strings=smart_strings)
  318. cdef class XPath(_XPathEvaluatorBase):
  319. """XPath(self, path, namespaces=None, extensions=None, regexp=True, smart_strings=True)
  320. A compiled XPath expression that can be called on Elements and ElementTrees.
  321. Besides the XPath expression, you can pass prefix-namespace
  322. mappings and extension functions to the constructor through the
  323. keyword arguments ``namespaces`` and ``extensions``. EXSLT
  324. regular expression support can be disabled with the 'regexp'
  325. boolean keyword (defaults to True). Smart strings will be
  326. returned for string results unless you pass
  327. ``smart_strings=False``.
  328. """
  329. cdef xpath.xmlXPathCompExpr* _xpath
  330. cdef bytes _path
  331. def __cinit__(self):
  332. self._xpath = NULL
  333. def __init__(self, path, *, namespaces=None, extensions=None,
  334. regexp=True, smart_strings=True):
  335. cdef xpath.xmlXPathContext* xpathCtxt
  336. _XPathEvaluatorBase.__init__(self, namespaces, extensions,
  337. regexp, smart_strings)
  338. self._path = _utf8(path)
  339. xpathCtxt = xpath.xmlXPathNewContext(NULL)
  340. if xpathCtxt is NULL:
  341. raise MemoryError()
  342. self.set_context(xpathCtxt)
  343. self._xpath = xpath.xmlXPathCtxtCompile(xpathCtxt, _xcstr(self._path))
  344. if self._xpath is NULL:
  345. raise self._build_parse_error()
  346. def __call__(self, _etree_or_element, **_variables):
  347. "__call__(self, _etree_or_element, **_variables)"
  348. cdef xpath.xmlXPathObject* xpathObj
  349. cdef _Document document
  350. cdef _Element element
  351. assert self._xpathCtxt is not NULL, "XPath context not initialised"
  352. document = _documentOrRaise(_etree_or_element)
  353. element = _rootNodeOrRaise(_etree_or_element)
  354. self._lock()
  355. self._xpathCtxt.doc = document._c_doc
  356. self._xpathCtxt.node = element._c_node
  357. try:
  358. self._context.register_context(document)
  359. self._context.registerVariables(_variables)
  360. with nogil:
  361. xpathObj = xpath.xmlXPathCompiledEval(
  362. self._xpath, self._xpathCtxt)
  363. result = self._handle_result(xpathObj, document)
  364. finally:
  365. self._context.unregister_context()
  366. self._unlock()
  367. return result
  368. @property
  369. def path(self):
  370. """The literal XPath expression.
  371. """
  372. return self._path.decode('UTF-8')
  373. def __dealloc__(self):
  374. if self._xpath is not NULL:
  375. xpath.xmlXPathFreeCompExpr(self._xpath)
  376. def __repr__(self):
  377. return self.path
  378. cdef object _replace_strings = re.compile(b'("[^"]*")|(\'[^\']*\')').sub
  379. cdef object _find_namespaces = re.compile(b'({[^}]+})').findall
  380. cdef class ETXPath(XPath):
  381. """ETXPath(self, path, extensions=None, regexp=True, smart_strings=True)
  382. Special XPath class that supports the ElementTree {uri} notation for namespaces.
  383. Note that this class does not accept the ``namespace`` keyword
  384. argument. All namespaces must be passed as part of the path
  385. string. Smart strings will be returned for string results unless
  386. you pass ``smart_strings=False``.
  387. """
  388. def __init__(self, path, *, extensions=None, regexp=True,
  389. smart_strings=True):
  390. path, namespaces = self._nsextract_path(path)
  391. XPath.__init__(self, path, namespaces=namespaces,
  392. extensions=extensions, regexp=regexp,
  393. smart_strings=smart_strings)
  394. cdef _nsextract_path(self, path):
  395. # replace {namespaces} by new prefixes
  396. cdef dict namespaces = {}
  397. cdef list namespace_defs = []
  398. cdef int i
  399. path_utf = _utf8(path)
  400. stripped_path = _replace_strings(b'', path_utf) # remove string literals
  401. i = 1
  402. for namespace_def in _find_namespaces(stripped_path):
  403. if namespace_def not in namespace_defs:
  404. prefix = python.PyBytes_FromFormat("__xpp%02d", i)
  405. i += 1
  406. namespace_defs.append(namespace_def)
  407. namespace = namespace_def[1:-1] # remove '{}'
  408. namespace = (<bytes>namespace).decode('utf8')
  409. namespaces[prefix.decode('utf8')] = namespace
  410. prefix_str = prefix + b':'
  411. # FIXME: this also replaces {namespaces} within strings!
  412. path_utf = path_utf.replace(namespace_def, prefix_str)
  413. path = path_utf.decode('utf8')
  414. return path, namespaces