Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xpath.c: XML Path Language implementation |
| 3 | * XPath is a language for addressing parts of an XML document, |
| 4 | * designed to be used by both XSLT and XPointer |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5 | *f |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6 | * Reference: W3C Recommendation 16 November 1999 |
| 7 | * http://www.w3.org/TR/1999/REC-xpath-19991116 |
| 8 | * Public reference: |
| 9 | * http://www.w3.org/TR/xpath |
| 10 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11 | * See Copyright for the status of this software |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 13 | * Author: daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 17 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 18 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
| 22 | #ifdef HAVE_SYS_TYPES_H |
| 23 | #include <sys/types.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_MATH_H |
| 26 | #include <math.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_FLOAT_H |
| 29 | #include <float.h> |
| 30 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | #ifdef HAVE_CTYPE_H |
| 32 | #include <ctype.h> |
| 33 | #endif |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SIGNAL_H |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 36 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | |
| 38 | #include <libxml/xmlmemory.h> |
| 39 | #include <libxml/tree.h> |
| 40 | #include <libxml/valid.h> |
| 41 | #include <libxml/xpath.h> |
| 42 | #include <libxml/xpathInternals.h> |
| 43 | #include <libxml/parserInternals.h> |
| 44 | #include <libxml/hash.h> |
| 45 | #ifdef LIBXML_XPTR_ENABLED |
| 46 | #include <libxml/xpointer.h> |
| 47 | #endif |
| 48 | #ifdef LIBXML_DEBUG_ENABLED |
| 49 | #include <libxml/debugXML.h> |
| 50 | #endif |
| 51 | #include <libxml/xmlerror.h> |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 52 | #include <libxml/threads.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 53 | #include <libxml/globals.h> |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 54 | #ifdef LIBXML_PATTERN_ENABLED |
| 55 | #include <libxml/pattern.h> |
| 56 | #endif |
| 57 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 58 | #include "buf.h" |
| 59 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 60 | #ifdef LIBXML_PATTERN_ENABLED |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 61 | #define XPATH_STREAMING |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 62 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 64 | #define TODO \ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 65 | xmlGenericError(xmlGenericErrorContext, \ |
| 66 | "Unimplemented block at %s:%d\n", \ |
| 67 | __FILE__, __LINE__); |
| 68 | |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 69 | /** |
| 70 | * WITH_TIM_SORT: |
| 71 | * |
| 72 | * Use the Timsort algorithm provided in timsort.h to sort |
| 73 | * nodeset as this is a great improvement over the old Shell sort |
| 74 | * used in xmlXPathNodeSetSort() |
| 75 | */ |
| 76 | #define WITH_TIM_SORT |
| 77 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 78 | /* |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 79 | * XP_OPTIMIZED_NON_ELEM_COMPARISON: |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 80 | * If defined, this will use xmlXPathCmpNodesExt() instead of |
| 81 | * xmlXPathCmpNodes(). The new function is optimized comparison of |
| 82 | * non-element nodes; actually it will speed up comparison only if |
| 83 | * xmlXPathOrderDocElems() was called in order to index the elements of |
| 84 | * a tree in document order; Libxslt does such an indexing, thus it will |
| 85 | * benefit from this optimization. |
| 86 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 87 | #define XP_OPTIMIZED_NON_ELEM_COMPARISON |
| 88 | |
| 89 | /* |
| 90 | * XP_OPTIMIZED_FILTER_FIRST: |
| 91 | * If defined, this will optimize expressions like "key('foo', 'val')[b][1]" |
| 92 | * in a way, that it stop evaluation at the first node. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 93 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 94 | #define XP_OPTIMIZED_FILTER_FIRST |
| 95 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 96 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 97 | * XP_DEBUG_OBJ_USAGE: |
| 98 | * Internal flag to enable tracking of how much XPath objects have been |
| 99 | * created. |
| 100 | */ |
| 101 | /* #define XP_DEBUG_OBJ_USAGE */ |
| 102 | |
| 103 | /* |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 104 | * XPATH_MAX_STEPS: |
| 105 | * when compiling an XPath expression we arbitrary limit the maximum |
| 106 | * number of step operation in the compiled expression. 1000000 is |
| 107 | * an insanely large value which should never be reached under normal |
| 108 | * circumstances |
| 109 | */ |
| 110 | #define XPATH_MAX_STEPS 1000000 |
| 111 | |
| 112 | /* |
| 113 | * XPATH_MAX_STACK_DEPTH: |
| 114 | * when evaluating an XPath expression we arbitrary limit the maximum |
| 115 | * number of object allowed to be pushed on the stack. 1000000 is |
| 116 | * an insanely large value which should never be reached under normal |
| 117 | * circumstances |
| 118 | */ |
| 119 | #define XPATH_MAX_STACK_DEPTH 1000000 |
| 120 | |
| 121 | /* |
| 122 | * XPATH_MAX_NODESET_LENGTH: |
| 123 | * when evaluating an XPath expression nodesets are created and we |
| 124 | * arbitrary limit the maximum length of those node set. 10000000 is |
| 125 | * an insanely large value which should never be reached under normal |
| 126 | * circumstances, one would first need to construct an in memory tree |
| 127 | * with more than 10 millions nodes. |
| 128 | */ |
| 129 | #define XPATH_MAX_NODESET_LENGTH 10000000 |
| 130 | |
| 131 | /* |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 132 | * TODO: |
| 133 | * There are a few spots where some tests are done which depend upon ascii |
| 134 | * data. These should be enhanced for full UTF8 support (see particularly |
| 135 | * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) |
| 136 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 137 | |
Denis Pauk | e28c8a1 | 2013-08-03 14:22:54 +0300 | [diff] [blame] | 138 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
| 139 | /** |
| 140 | * xmlXPathCmpNodesExt: |
| 141 | * @node1: the first node |
| 142 | * @node2: the second node |
| 143 | * |
| 144 | * Compare two nodes w.r.t document order. |
| 145 | * This one is optimized for handling of non-element nodes. |
| 146 | * |
| 147 | * Returns -2 in case of error 1 if first point < second point, 0 if |
| 148 | * it's the same node, -1 otherwise |
| 149 | */ |
| 150 | static int |
| 151 | xmlXPathCmpNodesExt(xmlNodePtr node1, xmlNodePtr node2) { |
| 152 | int depth1, depth2; |
| 153 | int misc = 0, precedence1 = 0, precedence2 = 0; |
| 154 | xmlNodePtr miscNode1 = NULL, miscNode2 = NULL; |
| 155 | xmlNodePtr cur, root; |
| 156 | long l1, l2; |
| 157 | |
| 158 | if ((node1 == NULL) || (node2 == NULL)) |
| 159 | return(-2); |
| 160 | |
| 161 | if (node1 == node2) |
| 162 | return(0); |
| 163 | |
| 164 | /* |
| 165 | * a couple of optimizations which will avoid computations in most cases |
| 166 | */ |
| 167 | switch (node1->type) { |
| 168 | case XML_ELEMENT_NODE: |
| 169 | if (node2->type == XML_ELEMENT_NODE) { |
| 170 | if ((0 > (long) node1->content) && /* TODO: Would a != 0 suffice here? */ |
| 171 | (0 > (long) node2->content) && |
| 172 | (node1->doc == node2->doc)) |
| 173 | { |
| 174 | l1 = -((long) node1->content); |
| 175 | l2 = -((long) node2->content); |
| 176 | if (l1 < l2) |
| 177 | return(1); |
| 178 | if (l1 > l2) |
| 179 | return(-1); |
| 180 | } else |
| 181 | goto turtle_comparison; |
| 182 | } |
| 183 | break; |
| 184 | case XML_ATTRIBUTE_NODE: |
| 185 | precedence1 = 1; /* element is owner */ |
| 186 | miscNode1 = node1; |
| 187 | node1 = node1->parent; |
| 188 | misc = 1; |
| 189 | break; |
| 190 | case XML_TEXT_NODE: |
| 191 | case XML_CDATA_SECTION_NODE: |
| 192 | case XML_COMMENT_NODE: |
| 193 | case XML_PI_NODE: { |
| 194 | miscNode1 = node1; |
| 195 | /* |
| 196 | * Find nearest element node. |
| 197 | */ |
| 198 | if (node1->prev != NULL) { |
| 199 | do { |
| 200 | node1 = node1->prev; |
| 201 | if (node1->type == XML_ELEMENT_NODE) { |
| 202 | precedence1 = 3; /* element in prev-sibl axis */ |
| 203 | break; |
| 204 | } |
| 205 | if (node1->prev == NULL) { |
| 206 | precedence1 = 2; /* element is parent */ |
| 207 | /* |
| 208 | * URGENT TODO: Are there any cases, where the |
| 209 | * parent of such a node is not an element node? |
| 210 | */ |
| 211 | node1 = node1->parent; |
| 212 | break; |
| 213 | } |
| 214 | } while (1); |
| 215 | } else { |
| 216 | precedence1 = 2; /* element is parent */ |
| 217 | node1 = node1->parent; |
| 218 | } |
| 219 | if ((node1 == NULL) || (node1->type != XML_ELEMENT_NODE) || |
| 220 | (0 <= (long) node1->content)) { |
| 221 | /* |
| 222 | * Fallback for whatever case. |
| 223 | */ |
| 224 | node1 = miscNode1; |
| 225 | precedence1 = 0; |
| 226 | } else |
| 227 | misc = 1; |
| 228 | } |
| 229 | break; |
| 230 | case XML_NAMESPACE_DECL: |
| 231 | /* |
| 232 | * TODO: why do we return 1 for namespace nodes? |
| 233 | */ |
| 234 | return(1); |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | switch (node2->type) { |
| 239 | case XML_ELEMENT_NODE: |
| 240 | break; |
| 241 | case XML_ATTRIBUTE_NODE: |
| 242 | precedence2 = 1; /* element is owner */ |
| 243 | miscNode2 = node2; |
| 244 | node2 = node2->parent; |
| 245 | misc = 1; |
| 246 | break; |
| 247 | case XML_TEXT_NODE: |
| 248 | case XML_CDATA_SECTION_NODE: |
| 249 | case XML_COMMENT_NODE: |
| 250 | case XML_PI_NODE: { |
| 251 | miscNode2 = node2; |
| 252 | if (node2->prev != NULL) { |
| 253 | do { |
| 254 | node2 = node2->prev; |
| 255 | if (node2->type == XML_ELEMENT_NODE) { |
| 256 | precedence2 = 3; /* element in prev-sibl axis */ |
| 257 | break; |
| 258 | } |
| 259 | if (node2->prev == NULL) { |
| 260 | precedence2 = 2; /* element is parent */ |
| 261 | node2 = node2->parent; |
| 262 | break; |
| 263 | } |
| 264 | } while (1); |
| 265 | } else { |
| 266 | precedence2 = 2; /* element is parent */ |
| 267 | node2 = node2->parent; |
| 268 | } |
| 269 | if ((node2 == NULL) || (node2->type != XML_ELEMENT_NODE) || |
Gaurav | fcd4583 | 2013-11-28 23:01:44 +0800 | [diff] [blame] | 270 | (0 <= (long) node2->content)) |
Denis Pauk | e28c8a1 | 2013-08-03 14:22:54 +0300 | [diff] [blame] | 271 | { |
| 272 | node2 = miscNode2; |
| 273 | precedence2 = 0; |
| 274 | } else |
| 275 | misc = 1; |
| 276 | } |
| 277 | break; |
| 278 | case XML_NAMESPACE_DECL: |
| 279 | return(1); |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | if (misc) { |
| 284 | if (node1 == node2) { |
| 285 | if (precedence1 == precedence2) { |
| 286 | /* |
| 287 | * The ugly case; but normally there aren't many |
| 288 | * adjacent non-element nodes around. |
| 289 | */ |
| 290 | cur = miscNode2->prev; |
| 291 | while (cur != NULL) { |
| 292 | if (cur == miscNode1) |
| 293 | return(1); |
| 294 | if (cur->type == XML_ELEMENT_NODE) |
| 295 | return(-1); |
| 296 | cur = cur->prev; |
| 297 | } |
| 298 | return (-1); |
| 299 | } else { |
| 300 | /* |
| 301 | * Evaluate based on higher precedence wrt to the element. |
| 302 | * TODO: This assumes attributes are sorted before content. |
| 303 | * Is this 100% correct? |
| 304 | */ |
| 305 | if (precedence1 < precedence2) |
| 306 | return(1); |
| 307 | else |
| 308 | return(-1); |
| 309 | } |
| 310 | } |
| 311 | /* |
| 312 | * Special case: One of the helper-elements is contained by the other. |
| 313 | * <foo> |
| 314 | * <node2> |
| 315 | * <node1>Text-1(precedence1 == 2)</node1> |
| 316 | * </node2> |
| 317 | * Text-6(precedence2 == 3) |
| 318 | * </foo> |
| 319 | */ |
| 320 | if ((precedence2 == 3) && (precedence1 > 1)) { |
| 321 | cur = node1->parent; |
| 322 | while (cur) { |
| 323 | if (cur == node2) |
| 324 | return(1); |
| 325 | cur = cur->parent; |
| 326 | } |
| 327 | } |
| 328 | if ((precedence1 == 3) && (precedence2 > 1)) { |
| 329 | cur = node2->parent; |
| 330 | while (cur) { |
| 331 | if (cur == node1) |
| 332 | return(-1); |
| 333 | cur = cur->parent; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Speedup using document order if availble. |
| 340 | */ |
| 341 | if ((node1->type == XML_ELEMENT_NODE) && |
| 342 | (node2->type == XML_ELEMENT_NODE) && |
| 343 | (0 > (long) node1->content) && |
| 344 | (0 > (long) node2->content) && |
| 345 | (node1->doc == node2->doc)) { |
| 346 | |
| 347 | l1 = -((long) node1->content); |
| 348 | l2 = -((long) node2->content); |
| 349 | if (l1 < l2) |
| 350 | return(1); |
| 351 | if (l1 > l2) |
| 352 | return(-1); |
| 353 | } |
| 354 | |
| 355 | turtle_comparison: |
| 356 | |
| 357 | if (node1 == node2->prev) |
| 358 | return(1); |
| 359 | if (node1 == node2->next) |
| 360 | return(-1); |
| 361 | /* |
| 362 | * compute depth to root |
| 363 | */ |
Nick Wellnhofer | 3eaedba | 2015-07-11 14:27:34 +0200 | [diff] [blame] | 364 | for (depth2 = 0, cur = node2; cur->parent != NULL; cur = cur->parent) { |
| 365 | if (cur->parent == node1) |
Denis Pauk | e28c8a1 | 2013-08-03 14:22:54 +0300 | [diff] [blame] | 366 | return(1); |
| 367 | depth2++; |
| 368 | } |
| 369 | root = cur; |
Nick Wellnhofer | 3eaedba | 2015-07-11 14:27:34 +0200 | [diff] [blame] | 370 | for (depth1 = 0, cur = node1; cur->parent != NULL; cur = cur->parent) { |
| 371 | if (cur->parent == node2) |
Denis Pauk | e28c8a1 | 2013-08-03 14:22:54 +0300 | [diff] [blame] | 372 | return(-1); |
| 373 | depth1++; |
| 374 | } |
| 375 | /* |
| 376 | * Distinct document (or distinct entities :-( ) case. |
| 377 | */ |
| 378 | if (root != cur) { |
| 379 | return(-2); |
| 380 | } |
| 381 | /* |
| 382 | * get the nearest common ancestor. |
| 383 | */ |
| 384 | while (depth1 > depth2) { |
| 385 | depth1--; |
| 386 | node1 = node1->parent; |
| 387 | } |
| 388 | while (depth2 > depth1) { |
| 389 | depth2--; |
| 390 | node2 = node2->parent; |
| 391 | } |
| 392 | while (node1->parent != node2->parent) { |
| 393 | node1 = node1->parent; |
| 394 | node2 = node2->parent; |
| 395 | /* should not happen but just in case ... */ |
| 396 | if ((node1 == NULL) || (node2 == NULL)) |
| 397 | return(-2); |
| 398 | } |
| 399 | /* |
| 400 | * Find who's first. |
| 401 | */ |
| 402 | if (node1 == node2->prev) |
| 403 | return(1); |
| 404 | if (node1 == node2->next) |
| 405 | return(-1); |
| 406 | /* |
| 407 | * Speedup using document order if availble. |
| 408 | */ |
| 409 | if ((node1->type == XML_ELEMENT_NODE) && |
| 410 | (node2->type == XML_ELEMENT_NODE) && |
| 411 | (0 > (long) node1->content) && |
| 412 | (0 > (long) node2->content) && |
| 413 | (node1->doc == node2->doc)) { |
| 414 | |
| 415 | l1 = -((long) node1->content); |
| 416 | l2 = -((long) node2->content); |
| 417 | if (l1 < l2) |
| 418 | return(1); |
| 419 | if (l1 > l2) |
| 420 | return(-1); |
| 421 | } |
| 422 | |
| 423 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 424 | if (cur == node2) |
| 425 | return(1); |
| 426 | return(-1); /* assume there is no sibling list corruption */ |
| 427 | } |
| 428 | #endif /* XP_OPTIMIZED_NON_ELEM_COMPARISON */ |
| 429 | |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 430 | /* |
| 431 | * Wrapper for the Timsort argorithm from timsort.h |
| 432 | */ |
| 433 | #ifdef WITH_TIM_SORT |
| 434 | #define SORT_NAME libxml_domnode |
| 435 | #define SORT_TYPE xmlNodePtr |
| 436 | /** |
| 437 | * wrap_cmp: |
| 438 | * @x: a node |
| 439 | * @y: another node |
| 440 | * |
| 441 | * Comparison function for the Timsort implementation |
| 442 | * |
Daniel Veillard | 510e758 | 2012-09-04 11:50:36 +0800 | [diff] [blame] | 443 | * Returns -2 in case of error -1 if first point < second point, 0 if |
| 444 | * it's the same node, +1 otherwise |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 445 | */ |
| 446 | static |
| 447 | int wrap_cmp( xmlNodePtr x, xmlNodePtr y ); |
| 448 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 449 | static int wrap_cmp( xmlNodePtr x, xmlNodePtr y ) |
| 450 | { |
| 451 | int res = xmlXPathCmpNodesExt(x, y); |
| 452 | return res == -2 ? res : -res; |
| 453 | } |
| 454 | #else |
| 455 | static int wrap_cmp( xmlNodePtr x, xmlNodePtr y ) |
| 456 | { |
| 457 | int res = xmlXPathCmpNodes(x, y); |
| 458 | return res == -2 ? res : -res; |
| 459 | } |
| 460 | #endif |
| 461 | #define SORT_CMP(x, y) (wrap_cmp(x, y)) |
| 462 | #include "timsort.h" |
| 463 | #endif /* WITH_TIM_SORT */ |
| 464 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 465 | #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 466 | |
| 467 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 468 | * * |
| 469 | * Floating point stuff * |
| 470 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 471 | ************************************************************************/ |
| 472 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 473 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 474 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 475 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 476 | #include "trionan.c" |
| 477 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 478 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 479 | * The lack of portability of this section of the libc is annoying ! |
| 480 | */ |
| 481 | double xmlXPathNAN = 0; |
| 482 | double xmlXPathPINF = 1; |
| 483 | double xmlXPathNINF = -1; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 484 | static double xmlXPathNZERO = 0; /* not exported from headers */ |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 485 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 486 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 487 | /** |
| 488 | * xmlXPathInit: |
| 489 | * |
| 490 | * Initialize the XPath environment |
| 491 | */ |
| 492 | void |
| 493 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 494 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 495 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 496 | xmlXPathPINF = trio_pinf(); |
| 497 | xmlXPathNINF = trio_ninf(); |
| 498 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 499 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 500 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 501 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 504 | /** |
| 505 | * xmlXPathIsNaN: |
| 506 | * @val: a double value |
| 507 | * |
| 508 | * Provides a portable isnan() function to detect whether a double |
| 509 | * is a NotaNumber. Based on trio code |
| 510 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 511 | * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 512 | * Returns 1 if the value is a NaN, 0 otherwise |
| 513 | */ |
| 514 | int |
| 515 | xmlXPathIsNaN(double val) { |
| 516 | return(trio_isnan(val)); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * xmlXPathIsInf: |
| 521 | * @val: a double value |
| 522 | * |
| 523 | * Provides a portable isinf() function to detect whether a double |
| 524 | * is a +Infinite or -Infinite. Based on trio code |
| 525 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 526 | * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 527 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 528 | */ |
| 529 | int |
| 530 | xmlXPathIsInf(double val) { |
| 531 | return(trio_isinf(val)); |
| 532 | } |
| 533 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 534 | #endif /* SCHEMAS or XPATH */ |
| 535 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 536 | /** |
| 537 | * xmlXPathGetSign: |
| 538 | * @val: a double value |
| 539 | * |
| 540 | * Provides a portable function to detect the sign of a double |
| 541 | * Modified from trio code |
| 542 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 543 | * |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 544 | * Returns 1 if the value is Negative, 0 if positive |
| 545 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 546 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 547 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 548 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 552 | /* |
| 553 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 554 | * the test should just be name[0] = ' ' |
| 555 | */ |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 556 | #ifdef DEBUG_XPATH_EXPRESSION |
| 557 | #define DEBUG_STEP |
| 558 | #define DEBUG_EXPR |
| 559 | #define DEBUG_EVAL_COUNTS |
| 560 | #endif |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 561 | |
| 562 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 563 | NULL, |
| 564 | XML_NAMESPACE_DECL, |
| 565 | XML_XML_NAMESPACE, |
| 566 | BAD_CAST "xml", |
William M. Brack | ee0b982 | 2007-03-07 08:15:01 +0000 | [diff] [blame] | 567 | NULL, |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 568 | NULL |
| 569 | }; |
| 570 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 571 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 572 | /* |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 573 | * Optimizer is disabled only when threaded apps are detected while |
| 574 | * the library ain't compiled for thread safety. |
| 575 | */ |
| 576 | static int xmlXPathDisableOptimizer = 0; |
| 577 | #endif |
| 578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 579 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 580 | * * |
| 581 | * Error handling routines * |
| 582 | * * |
| 583 | ************************************************************************/ |
| 584 | |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 585 | /** |
| 586 | * XP_ERRORNULL: |
| 587 | * @X: the error code |
| 588 | * |
| 589 | * Macro to raise an XPath error and return NULL. |
| 590 | */ |
| 591 | #define XP_ERRORNULL(X) \ |
| 592 | { xmlXPathErr(ctxt, X); return(NULL); } |
| 593 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 594 | /* |
| 595 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 596 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 597 | static const char *xmlXPathErrorMessages[] = { |
| 598 | "Ok\n", |
| 599 | "Number encoding\n", |
| 600 | "Unfinished literal\n", |
| 601 | "Start of literal\n", |
| 602 | "Expected $ for variable reference\n", |
| 603 | "Undefined variable\n", |
| 604 | "Invalid predicate\n", |
| 605 | "Invalid expression\n", |
| 606 | "Missing closing curly brace\n", |
| 607 | "Unregistered function\n", |
| 608 | "Invalid operand\n", |
| 609 | "Invalid type\n", |
| 610 | "Invalid number of arguments\n", |
| 611 | "Invalid context size\n", |
| 612 | "Invalid context position\n", |
| 613 | "Memory allocation error\n", |
| 614 | "Syntax error\n", |
| 615 | "Resource error\n", |
| 616 | "Sub resource error\n", |
| 617 | "Undefined namespace prefix\n", |
| 618 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 619 | "Char out of XML range\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 620 | "Invalid or incomplete context\n", |
Jan Pokorný | 7580165 | 2013-12-19 15:09:14 +0100 | [diff] [blame] | 621 | "Stack usage error\n", |
Daniel Veillard | 4788128 | 2012-09-07 14:24:50 +0800 | [diff] [blame] | 622 | "Forbidden variable\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 623 | "?? Unknown error ??\n" /* Must be last in the list! */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 624 | }; |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 625 | #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \ |
| 626 | sizeof(xmlXPathErrorMessages[0])) - 1) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 627 | /** |
| 628 | * xmlXPathErrMemory: |
| 629 | * @ctxt: an XPath context |
| 630 | * @extra: extra informations |
| 631 | * |
| 632 | * Handle a redefinition of attribute error |
| 633 | */ |
| 634 | static void |
| 635 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 636 | { |
| 637 | if (ctxt != NULL) { |
| 638 | if (extra) { |
| 639 | xmlChar buf[200]; |
| 640 | |
| 641 | xmlStrPrintf(buf, 200, |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 642 | "Memory allocation failed : %s\n", |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 643 | extra); |
| 644 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 645 | } else { |
| 646 | ctxt->lastError.message = (char *) |
| 647 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 648 | } |
| 649 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 650 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 651 | if (ctxt->error != NULL) |
| 652 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 653 | } else { |
| 654 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 655 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 656 | NULL, NULL, XML_FROM_XPATH, |
| 657 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 658 | extra, NULL, NULL, 0, 0, |
| 659 | "Memory allocation failed : %s\n", extra); |
| 660 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 661 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 662 | NULL, NULL, XML_FROM_XPATH, |
| 663 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 664 | NULL, NULL, NULL, 0, 0, |
| 665 | "Memory allocation failed\n"); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 670 | * xmlXPathPErrMemory: |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 671 | * @ctxt: an XPath parser context |
| 672 | * @extra: extra informations |
| 673 | * |
| 674 | * Handle a redefinition of attribute error |
| 675 | */ |
| 676 | static void |
| 677 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 678 | { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 679 | if (ctxt == NULL) |
| 680 | xmlXPathErrMemory(NULL, extra); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 681 | else { |
| 682 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 683 | xmlXPathErrMemory(ctxt->context, extra); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 684 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
| 688 | * xmlXPathErr: |
| 689 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 690 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 691 | * |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 692 | * Handle an XPath error |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 693 | */ |
| 694 | void |
| 695 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 696 | { |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 697 | if ((error < 0) || (error > MAXERRNO)) |
| 698 | error = MAXERRNO; |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 699 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 700 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 701 | NULL, NULL, XML_FROM_XPATH, |
| 702 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 703 | XML_ERR_ERROR, NULL, 0, |
| 704 | NULL, NULL, NULL, 0, 0, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 705 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 706 | return; |
| 707 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 708 | ctxt->error = error; |
| 709 | if (ctxt->context == NULL) { |
| 710 | __xmlRaiseError(NULL, NULL, NULL, |
| 711 | NULL, NULL, XML_FROM_XPATH, |
| 712 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 713 | XML_ERR_ERROR, NULL, 0, |
| 714 | (const char *) ctxt->base, NULL, NULL, |
| 715 | ctxt->cur - ctxt->base, 0, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 716 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 717 | return; |
| 718 | } |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 719 | |
| 720 | /* cleanup current last error */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 721 | xmlResetError(&ctxt->context->lastError); |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 722 | |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 723 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 724 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 725 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 726 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 727 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 728 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 729 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 730 | if (ctxt->context->error != NULL) { |
| 731 | ctxt->context->error(ctxt->context->userData, |
| 732 | &ctxt->context->lastError); |
| 733 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 734 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 735 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 736 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 737 | XML_ERR_ERROR, NULL, 0, |
| 738 | (const char *) ctxt->base, NULL, NULL, |
| 739 | ctxt->cur - ctxt->base, 0, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 740 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * xmlXPatherror: |
| 747 | * @ctxt: the XPath Parser context |
| 748 | * @file: the file name |
| 749 | * @line: the line number |
| 750 | * @no: the error number |
| 751 | * |
| 752 | * Formats an error message. |
| 753 | */ |
| 754 | void |
| 755 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 756 | int line ATTRIBUTE_UNUSED, int no) { |
| 757 | xmlXPathErr(ctxt, no); |
| 758 | } |
| 759 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 760 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 761 | * * |
| 762 | * Utilities * |
| 763 | * * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 764 | ************************************************************************/ |
| 765 | |
| 766 | /** |
| 767 | * xsltPointerList: |
| 768 | * |
| 769 | * Pointer-list for various purposes. |
| 770 | */ |
| 771 | typedef struct _xmlPointerList xmlPointerList; |
| 772 | typedef xmlPointerList *xmlPointerListPtr; |
| 773 | struct _xmlPointerList { |
| 774 | void **items; |
| 775 | int number; |
| 776 | int size; |
| 777 | }; |
| 778 | /* |
| 779 | * TODO: Since such a list-handling is used in xmlschemas.c and libxslt |
| 780 | * and here, we should make the functions public. |
| 781 | */ |
| 782 | static int |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 783 | xmlPointerListAddSize(xmlPointerListPtr list, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 784 | void *item, |
| 785 | int initialSize) |
| 786 | { |
| 787 | if (list->items == NULL) { |
| 788 | if (initialSize <= 0) |
| 789 | initialSize = 1; |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 790 | list->items = (void **) xmlMalloc(initialSize * sizeof(void *)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 791 | if (list->items == NULL) { |
| 792 | xmlXPathErrMemory(NULL, |
| 793 | "xmlPointerListCreate: allocating item\n"); |
| 794 | return(-1); |
| 795 | } |
| 796 | list->number = 0; |
| 797 | list->size = initialSize; |
| 798 | } else if (list->size <= list->number) { |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 799 | if (list->size > 50000000) { |
| 800 | xmlXPathErrMemory(NULL, |
| 801 | "xmlPointerListAddSize: re-allocating item\n"); |
| 802 | return(-1); |
| 803 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 804 | list->size *= 2; |
| 805 | list->items = (void **) xmlRealloc(list->items, |
| 806 | list->size * sizeof(void *)); |
| 807 | if (list->items == NULL) { |
| 808 | xmlXPathErrMemory(NULL, |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 809 | "xmlPointerListAddSize: re-allocating item\n"); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 810 | list->size = 0; |
| 811 | return(-1); |
| 812 | } |
| 813 | } |
| 814 | list->items[list->number++] = item; |
| 815 | return(0); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * xsltPointerListCreate: |
| 820 | * |
| 821 | * Creates an xsltPointerList structure. |
| 822 | * |
| 823 | * Returns a xsltPointerList structure or NULL in case of an error. |
| 824 | */ |
| 825 | static xmlPointerListPtr |
| 826 | xmlPointerListCreate(int initialSize) |
| 827 | { |
| 828 | xmlPointerListPtr ret; |
| 829 | |
| 830 | ret = xmlMalloc(sizeof(xmlPointerList)); |
| 831 | if (ret == NULL) { |
| 832 | xmlXPathErrMemory(NULL, |
| 833 | "xmlPointerListCreate: allocating item\n"); |
| 834 | return (NULL); |
| 835 | } |
| 836 | memset(ret, 0, sizeof(xmlPointerList)); |
| 837 | if (initialSize > 0) { |
| 838 | xmlPointerListAddSize(ret, NULL, initialSize); |
| 839 | ret->number = 0; |
| 840 | } |
| 841 | return (ret); |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * xsltPointerListFree: |
| 846 | * |
| 847 | * Frees the xsltPointerList structure. This does not free |
| 848 | * the content of the list. |
| 849 | */ |
| 850 | static void |
| 851 | xmlPointerListFree(xmlPointerListPtr list) |
| 852 | { |
| 853 | if (list == NULL) |
| 854 | return; |
| 855 | if (list->items != NULL) |
| 856 | xmlFree(list->items); |
| 857 | xmlFree(list); |
| 858 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 859 | |
| 860 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 861 | * * |
| 862 | * Parser Types * |
| 863 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 864 | ************************************************************************/ |
| 865 | |
| 866 | /* |
| 867 | * Types are private: |
| 868 | */ |
| 869 | |
| 870 | typedef enum { |
| 871 | XPATH_OP_END=0, |
| 872 | XPATH_OP_AND, |
| 873 | XPATH_OP_OR, |
| 874 | XPATH_OP_EQUAL, |
| 875 | XPATH_OP_CMP, |
| 876 | XPATH_OP_PLUS, |
| 877 | XPATH_OP_MULT, |
| 878 | XPATH_OP_UNION, |
| 879 | XPATH_OP_ROOT, |
| 880 | XPATH_OP_NODE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 881 | XPATH_OP_RESET, /* 10 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 882 | XPATH_OP_COLLECT, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 883 | XPATH_OP_VALUE, /* 12 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 884 | XPATH_OP_VARIABLE, |
| 885 | XPATH_OP_FUNCTION, |
| 886 | XPATH_OP_ARG, |
| 887 | XPATH_OP_PREDICATE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 888 | XPATH_OP_FILTER, /* 17 */ |
| 889 | XPATH_OP_SORT /* 18 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 890 | #ifdef LIBXML_XPTR_ENABLED |
| 891 | ,XPATH_OP_RANGETO |
| 892 | #endif |
| 893 | } xmlXPathOp; |
| 894 | |
| 895 | typedef enum { |
| 896 | AXIS_ANCESTOR = 1, |
| 897 | AXIS_ANCESTOR_OR_SELF, |
| 898 | AXIS_ATTRIBUTE, |
| 899 | AXIS_CHILD, |
| 900 | AXIS_DESCENDANT, |
| 901 | AXIS_DESCENDANT_OR_SELF, |
| 902 | AXIS_FOLLOWING, |
| 903 | AXIS_FOLLOWING_SIBLING, |
| 904 | AXIS_NAMESPACE, |
| 905 | AXIS_PARENT, |
| 906 | AXIS_PRECEDING, |
| 907 | AXIS_PRECEDING_SIBLING, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 908 | AXIS_SELF |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 909 | } xmlXPathAxisVal; |
| 910 | |
| 911 | typedef enum { |
| 912 | NODE_TEST_NONE = 0, |
| 913 | NODE_TEST_TYPE = 1, |
| 914 | NODE_TEST_PI = 2, |
| 915 | NODE_TEST_ALL = 3, |
| 916 | NODE_TEST_NS = 4, |
| 917 | NODE_TEST_NAME = 5 |
| 918 | } xmlXPathTestVal; |
| 919 | |
| 920 | typedef enum { |
| 921 | NODE_TYPE_NODE = 0, |
| 922 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 923 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 924 | NODE_TYPE_PI = XML_PI_NODE |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 925 | } xmlXPathTypeVal; |
| 926 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 927 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 928 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 929 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 930 | xmlXPathOp op; /* The identifier of the operation */ |
| 931 | int ch1; /* First child */ |
| 932 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 933 | int value; |
| 934 | int value2; |
| 935 | int value3; |
| 936 | void *value4; |
| 937 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 938 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 939 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 940 | }; |
| 941 | |
| 942 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 943 | int nbStep; /* Number of steps in this expression */ |
| 944 | int maxStep; /* Maximum number of steps allocated */ |
| 945 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 946 | int last; /* index of last step in expression */ |
| 947 | xmlChar *expr; /* the expression being computed */ |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 948 | xmlDictPtr dict; /* the dictionary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 949 | #ifdef DEBUG_EVAL_COUNTS |
| 950 | int nb; |
| 951 | xmlChar *string; |
| 952 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 953 | #ifdef XPATH_STREAMING |
| 954 | xmlPatternPtr stream; |
| 955 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 959 | * * |
| 960 | * Forward declarations * |
| 961 | * * |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 962 | ************************************************************************/ |
| 963 | static void |
| 964 | xmlXPathFreeValueTree(xmlNodeSetPtr obj); |
| 965 | static void |
| 966 | xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj); |
| 967 | static int |
| 968 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 969 | xmlXPathStepOpPtr op, xmlNodePtr *first); |
| 970 | static int |
| 971 | xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt, |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 972 | xmlXPathStepOpPtr op, |
| 973 | int isPredicate); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 974 | |
| 975 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 976 | * * |
| 977 | * Parser Type functions * |
| 978 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 979 | ************************************************************************/ |
| 980 | |
| 981 | /** |
| 982 | * xmlXPathNewCompExpr: |
| 983 | * |
| 984 | * Create a new Xpath component |
| 985 | * |
| 986 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 987 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 988 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 989 | xmlXPathNewCompExpr(void) { |
| 990 | xmlXPathCompExprPtr cur; |
| 991 | |
| 992 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 993 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 994 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 995 | return(NULL); |
| 996 | } |
| 997 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 998 | cur->maxStep = 10; |
| 999 | cur->nbStep = 0; |
| 1000 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 1001 | sizeof(xmlXPathStepOp)); |
| 1002 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1003 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1004 | xmlFree(cur); |
| 1005 | return(NULL); |
| 1006 | } |
| 1007 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 1008 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1009 | #ifdef DEBUG_EVAL_COUNTS |
| 1010 | cur->nb = 0; |
| 1011 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1012 | return(cur); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * xmlXPathFreeCompExpr: |
| 1017 | * @comp: an XPATH comp |
| 1018 | * |
| 1019 | * Free up the memory allocated by @comp |
| 1020 | */ |
| 1021 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1022 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 1023 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1024 | xmlXPathStepOpPtr op; |
| 1025 | int i; |
| 1026 | |
| 1027 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1028 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1029 | if (comp->dict == NULL) { |
| 1030 | for (i = 0; i < comp->nbStep; i++) { |
| 1031 | op = &comp->steps[i]; |
| 1032 | if (op->value4 != NULL) { |
| 1033 | if (op->op == XPATH_OP_VALUE) |
| 1034 | xmlXPathFreeObject(op->value4); |
| 1035 | else |
| 1036 | xmlFree(op->value4); |
| 1037 | } |
| 1038 | if (op->value5 != NULL) |
| 1039 | xmlFree(op->value5); |
| 1040 | } |
| 1041 | } else { |
| 1042 | for (i = 0; i < comp->nbStep; i++) { |
| 1043 | op = &comp->steps[i]; |
| 1044 | if (op->value4 != NULL) { |
| 1045 | if (op->op == XPATH_OP_VALUE) |
| 1046 | xmlXPathFreeObject(op->value4); |
| 1047 | } |
| 1048 | } |
| 1049 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1050 | } |
| 1051 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1052 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1053 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1054 | #ifdef DEBUG_EVAL_COUNTS |
| 1055 | if (comp->string != NULL) { |
| 1056 | xmlFree(comp->string); |
| 1057 | } |
| 1058 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1059 | #ifdef XPATH_STREAMING |
| 1060 | if (comp->stream != NULL) { |
| 1061 | xmlFreePatternList(comp->stream); |
| 1062 | } |
| 1063 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1064 | if (comp->expr != NULL) { |
| 1065 | xmlFree(comp->expr); |
| 1066 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1067 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1068 | xmlFree(comp); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * xmlXPathCompExprAdd: |
| 1073 | * @comp: the compiled expression |
| 1074 | * @ch1: first child index |
| 1075 | * @ch2: second child index |
| 1076 | * @op: an op |
| 1077 | * @value: the first int value |
| 1078 | * @value2: the second int value |
| 1079 | * @value3: the third int value |
| 1080 | * @value4: the first string value |
| 1081 | * @value5: the second string value |
| 1082 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1083 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1084 | * |
| 1085 | * Returns -1 in case of failure, the index otherwise |
| 1086 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1087 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1088 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 1089 | xmlXPathOp op, int value, |
| 1090 | int value2, int value3, void *value4, void *value5) { |
| 1091 | if (comp->nbStep >= comp->maxStep) { |
| 1092 | xmlXPathStepOp *real; |
| 1093 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 1094 | if (comp->maxStep >= XPATH_MAX_STEPS) { |
| 1095 | xmlXPathErrMemory(NULL, "adding step\n"); |
| 1096 | return(-1); |
| 1097 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1098 | comp->maxStep *= 2; |
| 1099 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 1100 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 1101 | if (real == NULL) { |
| 1102 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1103 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1104 | return(-1); |
| 1105 | } |
| 1106 | comp->steps = real; |
| 1107 | } |
| 1108 | comp->last = comp->nbStep; |
| 1109 | comp->steps[comp->nbStep].ch1 = ch1; |
| 1110 | comp->steps[comp->nbStep].ch2 = ch2; |
| 1111 | comp->steps[comp->nbStep].op = op; |
| 1112 | comp->steps[comp->nbStep].value = value; |
| 1113 | comp->steps[comp->nbStep].value2 = value2; |
| 1114 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1115 | if ((comp->dict != NULL) && |
| 1116 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 1117 | (op == XPATH_OP_COLLECT))) { |
| 1118 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 1119 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 1120 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1121 | xmlFree(value4); |
| 1122 | } else |
| 1123 | comp->steps[comp->nbStep].value4 = NULL; |
| 1124 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 1125 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 1126 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1127 | xmlFree(value5); |
| 1128 | } else |
| 1129 | comp->steps[comp->nbStep].value5 = NULL; |
| 1130 | } else { |
| 1131 | comp->steps[comp->nbStep].value4 = value4; |
| 1132 | comp->steps[comp->nbStep].value5 = value5; |
| 1133 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 1134 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1135 | return(comp->nbStep++); |
| 1136 | } |
| 1137 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1138 | /** |
| 1139 | * xmlXPathCompSwap: |
| 1140 | * @comp: the compiled expression |
| 1141 | * @op: operation index |
| 1142 | * |
| 1143 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1144 | */ |
| 1145 | static void |
| 1146 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 1147 | int tmp; |
| 1148 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 1149 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 1150 | /* |
| 1151 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1152 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 1153 | * application |
| 1154 | */ |
| 1155 | if (xmlXPathDisableOptimizer) |
| 1156 | return; |
| 1157 | #endif |
| 1158 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 1159 | tmp = op->ch1; |
| 1160 | op->ch1 = op->ch2; |
| 1161 | op->ch2 = tmp; |
| 1162 | } |
| 1163 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1164 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 1165 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 1166 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1167 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 1168 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 1169 | (op), (val), (val2), (val3), (val4), (val5)) |
| 1170 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1171 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1172 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 1173 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1174 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1175 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 1176 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1177 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1178 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 1179 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1180 | |
| 1181 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1182 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1183 | * XPath object cache structures * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1184 | * * |
| 1185 | ************************************************************************/ |
| 1186 | |
| 1187 | /* #define XP_DEFAULT_CACHE_ON */ |
| 1188 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1189 | #define XP_HAS_CACHE(c) ((c != NULL) && ((c)->cache != NULL)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1190 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1191 | typedef struct _xmlXPathContextCache xmlXPathContextCache; |
| 1192 | typedef xmlXPathContextCache *xmlXPathContextCachePtr; |
| 1193 | struct _xmlXPathContextCache { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 1194 | xmlPointerListPtr nodesetObjs; /* contains xmlXPathObjectPtr */ |
| 1195 | xmlPointerListPtr stringObjs; /* contains xmlXPathObjectPtr */ |
| 1196 | xmlPointerListPtr booleanObjs; /* contains xmlXPathObjectPtr */ |
| 1197 | xmlPointerListPtr numberObjs; /* contains xmlXPathObjectPtr */ |
| 1198 | xmlPointerListPtr miscObjs; /* contains xmlXPathObjectPtr */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1199 | int maxNodeset; |
| 1200 | int maxString; |
| 1201 | int maxBoolean; |
| 1202 | int maxNumber; |
| 1203 | int maxMisc; |
| 1204 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1205 | int dbgCachedAll; |
| 1206 | int dbgCachedNodeset; |
| 1207 | int dbgCachedString; |
| 1208 | int dbgCachedBool; |
| 1209 | int dbgCachedNumber; |
| 1210 | int dbgCachedPoint; |
| 1211 | int dbgCachedRange; |
| 1212 | int dbgCachedLocset; |
| 1213 | int dbgCachedUsers; |
| 1214 | int dbgCachedXSLTTree; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1215 | int dbgCachedUndefined; |
| 1216 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1217 | |
| 1218 | int dbgReusedAll; |
| 1219 | int dbgReusedNodeset; |
| 1220 | int dbgReusedString; |
| 1221 | int dbgReusedBool; |
| 1222 | int dbgReusedNumber; |
| 1223 | int dbgReusedPoint; |
| 1224 | int dbgReusedRange; |
| 1225 | int dbgReusedLocset; |
| 1226 | int dbgReusedUsers; |
| 1227 | int dbgReusedXSLTTree; |
| 1228 | int dbgReusedUndefined; |
| 1229 | |
| 1230 | #endif |
| 1231 | }; |
| 1232 | |
| 1233 | /************************************************************************ |
| 1234 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1235 | * Debugging related functions * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1236 | * * |
| 1237 | ************************************************************************/ |
| 1238 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1239 | #define STRANGE \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1240 | xmlGenericError(xmlGenericErrorContext, \ |
| 1241 | "Internal error at %s:%d\n", \ |
| 1242 | __FILE__, __LINE__); |
| 1243 | |
| 1244 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1245 | static void |
| 1246 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1247 | int i; |
| 1248 | char shift[100]; |
| 1249 | |
| 1250 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1251 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1252 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1253 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1254 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1255 | fprintf(output, "Node is NULL !\n"); |
| 1256 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 1261 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1262 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1263 | fprintf(output, " /\n"); |
| 1264 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 1265 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 1266 | else |
| 1267 | xmlDebugDumpOneNode(output, cur, depth); |
| 1268 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1269 | static void |
| 1270 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1271 | xmlNodePtr tmp; |
| 1272 | int i; |
| 1273 | char shift[100]; |
| 1274 | |
| 1275 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1276 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1277 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1278 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1279 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1280 | fprintf(output, "Node is NULL !\n"); |
| 1281 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1282 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | while (cur != NULL) { |
| 1286 | tmp = cur; |
| 1287 | cur = cur->next; |
| 1288 | xmlDebugDumpOneNode(output, tmp, depth); |
| 1289 | } |
| 1290 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1291 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1292 | static void |
| 1293 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | int i; |
| 1295 | char shift[100]; |
| 1296 | |
| 1297 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1298 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1299 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1300 | |
| 1301 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1302 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1303 | fprintf(output, "NodeSet is NULL !\n"); |
| 1304 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1305 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 1308 | if (cur != NULL) { |
| 1309 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 1310 | for (i = 0;i < cur->nodeNr;i++) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1311 | fprintf(output, "%s", shift); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 1312 | fprintf(output, "%d", i + 1); |
| 1313 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 1314 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1318 | static void |
| 1319 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1320 | int i; |
| 1321 | char shift[100]; |
| 1322 | |
| 1323 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1324 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1325 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1326 | |
| 1327 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1328 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1329 | fprintf(output, "Value Tree is NULL !\n"); |
| 1330 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1331 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1334 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1335 | fprintf(output, "%d", i + 1); |
| 1336 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 1337 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1338 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1339 | static void |
| 1340 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1341 | int i; |
| 1342 | char shift[100]; |
| 1343 | |
| 1344 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1345 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1346 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1347 | |
| 1348 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1349 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1350 | fprintf(output, "LocationSet is NULL !\n"); |
| 1351 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1352 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | for (i = 0;i < cur->locNr;i++) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1356 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | fprintf(output, "%d : ", i + 1); |
| 1358 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 1359 | } |
| 1360 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1361 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1362 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 1363 | /** |
| 1364 | * xmlXPathDebugDumpObject: |
| 1365 | * @output: the FILE * to dump the output |
| 1366 | * @cur: the object to inspect |
| 1367 | * @depth: indentation level |
| 1368 | * |
| 1369 | * Dump the content of the object for debugging purposes |
| 1370 | */ |
| 1371 | void |
| 1372 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1373 | int i; |
| 1374 | char shift[100]; |
| 1375 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1376 | if (output == NULL) return; |
| 1377 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1378 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1379 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1380 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1381 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1382 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1383 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (cur == NULL) { |
| 1386 | fprintf(output, "Object is empty (NULL)\n"); |
| 1387 | return; |
| 1388 | } |
| 1389 | switch(cur->type) { |
| 1390 | case XPATH_UNDEFINED: |
| 1391 | fprintf(output, "Object is uninitialized\n"); |
| 1392 | break; |
| 1393 | case XPATH_NODESET: |
| 1394 | fprintf(output, "Object is a Node Set :\n"); |
| 1395 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 1396 | break; |
| 1397 | case XPATH_XSLT_TREE: |
| 1398 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1399 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | case XPATH_BOOLEAN: |
| 1402 | fprintf(output, "Object is a Boolean : "); |
| 1403 | if (cur->boolval) fprintf(output, "true\n"); |
| 1404 | else fprintf(output, "false\n"); |
| 1405 | break; |
| 1406 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1407 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1408 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1409 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1410 | break; |
| 1411 | case -1: |
| 1412 | fprintf(output, "Object is a number : -Infinity\n"); |
| 1413 | break; |
| 1414 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1415 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1416 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1417 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 1418 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1419 | } else { |
| 1420 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 1421 | } |
| 1422 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | case XPATH_STRING: |
| 1425 | fprintf(output, "Object is a string : "); |
| 1426 | xmlDebugDumpString(output, cur->stringval); |
| 1427 | fprintf(output, "\n"); |
| 1428 | break; |
| 1429 | case XPATH_POINT: |
| 1430 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 1431 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 1432 | fprintf(output, "\n"); |
| 1433 | break; |
| 1434 | case XPATH_RANGE: |
| 1435 | if ((cur->user2 == NULL) || |
| 1436 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 1437 | fprintf(output, "Object is a collapsed range :\n"); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1438 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1439 | if (cur->index >= 0) |
| 1440 | fprintf(output, "index %d in ", cur->index); |
| 1441 | fprintf(output, "node\n"); |
| 1442 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 1443 | depth + 1); |
| 1444 | } else { |
| 1445 | fprintf(output, "Object is a range :\n"); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1446 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1447 | fprintf(output, "From "); |
| 1448 | if (cur->index >= 0) |
| 1449 | fprintf(output, "index %d in ", cur->index); |
| 1450 | fprintf(output, "node\n"); |
| 1451 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 1452 | depth + 1); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1453 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1454 | fprintf(output, "To "); |
| 1455 | if (cur->index2 >= 0) |
| 1456 | fprintf(output, "index %d in ", cur->index2); |
| 1457 | fprintf(output, "node\n"); |
| 1458 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 1459 | depth + 1); |
| 1460 | fprintf(output, "\n"); |
| 1461 | } |
| 1462 | break; |
| 1463 | case XPATH_LOCATIONSET: |
| 1464 | #if defined(LIBXML_XPTR_ENABLED) |
| 1465 | fprintf(output, "Object is a Location Set:\n"); |
| 1466 | xmlXPathDebugDumpLocationSet(output, |
| 1467 | (xmlLocationSetPtr) cur->user, depth); |
| 1468 | #endif |
| 1469 | break; |
| 1470 | case XPATH_USERS: |
| 1471 | fprintf(output, "Object is user defined\n"); |
| 1472 | break; |
| 1473 | } |
| 1474 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1475 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1476 | static void |
| 1477 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1478 | xmlXPathStepOpPtr op, int depth) { |
| 1479 | int i; |
| 1480 | char shift[100]; |
| 1481 | |
| 1482 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1483 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1484 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1485 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1486 | fprintf(output, "%s", shift); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1487 | if (op == NULL) { |
| 1488 | fprintf(output, "Step is NULL\n"); |
| 1489 | return; |
| 1490 | } |
| 1491 | switch (op->op) { |
| 1492 | case XPATH_OP_END: |
| 1493 | fprintf(output, "END"); break; |
| 1494 | case XPATH_OP_AND: |
| 1495 | fprintf(output, "AND"); break; |
| 1496 | case XPATH_OP_OR: |
| 1497 | fprintf(output, "OR"); break; |
| 1498 | case XPATH_OP_EQUAL: |
| 1499 | if (op->value) |
| 1500 | fprintf(output, "EQUAL ="); |
| 1501 | else |
| 1502 | fprintf(output, "EQUAL !="); |
| 1503 | break; |
| 1504 | case XPATH_OP_CMP: |
| 1505 | if (op->value) |
| 1506 | fprintf(output, "CMP <"); |
| 1507 | else |
| 1508 | fprintf(output, "CMP >"); |
| 1509 | if (!op->value2) |
| 1510 | fprintf(output, "="); |
| 1511 | break; |
| 1512 | case XPATH_OP_PLUS: |
| 1513 | if (op->value == 0) |
| 1514 | fprintf(output, "PLUS -"); |
| 1515 | else if (op->value == 1) |
| 1516 | fprintf(output, "PLUS +"); |
| 1517 | else if (op->value == 2) |
| 1518 | fprintf(output, "PLUS unary -"); |
| 1519 | else if (op->value == 3) |
| 1520 | fprintf(output, "PLUS unary - -"); |
| 1521 | break; |
| 1522 | case XPATH_OP_MULT: |
| 1523 | if (op->value == 0) |
| 1524 | fprintf(output, "MULT *"); |
| 1525 | else if (op->value == 1) |
| 1526 | fprintf(output, "MULT div"); |
| 1527 | else |
| 1528 | fprintf(output, "MULT mod"); |
| 1529 | break; |
| 1530 | case XPATH_OP_UNION: |
| 1531 | fprintf(output, "UNION"); break; |
| 1532 | case XPATH_OP_ROOT: |
| 1533 | fprintf(output, "ROOT"); break; |
| 1534 | case XPATH_OP_NODE: |
| 1535 | fprintf(output, "NODE"); break; |
| 1536 | case XPATH_OP_RESET: |
| 1537 | fprintf(output, "RESET"); break; |
| 1538 | case XPATH_OP_SORT: |
| 1539 | fprintf(output, "SORT"); break; |
| 1540 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1541 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 1542 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 1543 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1544 | const xmlChar *prefix = op->value4; |
| 1545 | const xmlChar *name = op->value5; |
| 1546 | |
| 1547 | fprintf(output, "COLLECT "); |
| 1548 | switch (axis) { |
| 1549 | case AXIS_ANCESTOR: |
| 1550 | fprintf(output, " 'ancestors' "); break; |
| 1551 | case AXIS_ANCESTOR_OR_SELF: |
| 1552 | fprintf(output, " 'ancestors-or-self' "); break; |
| 1553 | case AXIS_ATTRIBUTE: |
| 1554 | fprintf(output, " 'attributes' "); break; |
| 1555 | case AXIS_CHILD: |
| 1556 | fprintf(output, " 'child' "); break; |
| 1557 | case AXIS_DESCENDANT: |
| 1558 | fprintf(output, " 'descendant' "); break; |
| 1559 | case AXIS_DESCENDANT_OR_SELF: |
| 1560 | fprintf(output, " 'descendant-or-self' "); break; |
| 1561 | case AXIS_FOLLOWING: |
| 1562 | fprintf(output, " 'following' "); break; |
| 1563 | case AXIS_FOLLOWING_SIBLING: |
| 1564 | fprintf(output, " 'following-siblings' "); break; |
| 1565 | case AXIS_NAMESPACE: |
| 1566 | fprintf(output, " 'namespace' "); break; |
| 1567 | case AXIS_PARENT: |
| 1568 | fprintf(output, " 'parent' "); break; |
| 1569 | case AXIS_PRECEDING: |
| 1570 | fprintf(output, " 'preceding' "); break; |
| 1571 | case AXIS_PRECEDING_SIBLING: |
| 1572 | fprintf(output, " 'preceding-sibling' "); break; |
| 1573 | case AXIS_SELF: |
| 1574 | fprintf(output, " 'self' "); break; |
| 1575 | } |
| 1576 | switch (test) { |
| 1577 | case NODE_TEST_NONE: |
| 1578 | fprintf(output, "'none' "); break; |
| 1579 | case NODE_TEST_TYPE: |
| 1580 | fprintf(output, "'type' "); break; |
| 1581 | case NODE_TEST_PI: |
| 1582 | fprintf(output, "'PI' "); break; |
| 1583 | case NODE_TEST_ALL: |
| 1584 | fprintf(output, "'all' "); break; |
| 1585 | case NODE_TEST_NS: |
| 1586 | fprintf(output, "'namespace' "); break; |
| 1587 | case NODE_TEST_NAME: |
| 1588 | fprintf(output, "'name' "); break; |
| 1589 | } |
| 1590 | switch (type) { |
| 1591 | case NODE_TYPE_NODE: |
| 1592 | fprintf(output, "'node' "); break; |
| 1593 | case NODE_TYPE_COMMENT: |
| 1594 | fprintf(output, "'comment' "); break; |
| 1595 | case NODE_TYPE_TEXT: |
| 1596 | fprintf(output, "'text' "); break; |
| 1597 | case NODE_TYPE_PI: |
| 1598 | fprintf(output, "'PI' "); break; |
| 1599 | } |
| 1600 | if (prefix != NULL) |
| 1601 | fprintf(output, "%s:", prefix); |
| 1602 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1603 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1604 | break; |
| 1605 | |
| 1606 | } |
| 1607 | case XPATH_OP_VALUE: { |
| 1608 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1609 | |
| 1610 | fprintf(output, "ELEM "); |
| 1611 | xmlXPathDebugDumpObject(output, object, 0); |
| 1612 | goto finish; |
| 1613 | } |
| 1614 | case XPATH_OP_VARIABLE: { |
| 1615 | const xmlChar *prefix = op->value5; |
| 1616 | const xmlChar *name = op->value4; |
| 1617 | |
| 1618 | if (prefix != NULL) |
| 1619 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1620 | else |
| 1621 | fprintf(output, "VARIABLE %s", name); |
| 1622 | break; |
| 1623 | } |
| 1624 | case XPATH_OP_FUNCTION: { |
| 1625 | int nbargs = op->value; |
| 1626 | const xmlChar *prefix = op->value5; |
| 1627 | const xmlChar *name = op->value4; |
| 1628 | |
| 1629 | if (prefix != NULL) |
| 1630 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1631 | prefix, name, nbargs); |
| 1632 | else |
| 1633 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1634 | break; |
| 1635 | } |
| 1636 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1637 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1638 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1639 | #ifdef LIBXML_XPTR_ENABLED |
| 1640 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1641 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1642 | default: |
| 1643 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1644 | } |
| 1645 | fprintf(output, "\n"); |
| 1646 | finish: |
| 1647 | if (op->ch1 >= 0) |
| 1648 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1649 | if (op->ch2 >= 0) |
| 1650 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1651 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1652 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1653 | /** |
| 1654 | * xmlXPathDebugDumpCompExpr: |
| 1655 | * @output: the FILE * for the output |
| 1656 | * @comp: the precompiled XPath expression |
| 1657 | * @depth: the indentation level. |
| 1658 | * |
| 1659 | * Dumps the tree of the compiled XPath expression. |
| 1660 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1661 | void |
| 1662 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1663 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1664 | int i; |
| 1665 | char shift[100]; |
| 1666 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1667 | if ((output == NULL) || (comp == NULL)) return; |
| 1668 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1669 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1670 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1671 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1672 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1673 | fprintf(output, "%s", shift); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1674 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1675 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1676 | comp->nbStep); |
| 1677 | i = comp->last; |
| 1678 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1679 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1680 | |
| 1681 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1682 | |
| 1683 | /* |
| 1684 | * XPath object usage related debugging variables. |
| 1685 | */ |
| 1686 | static int xmlXPathDebugObjCounterUndefined = 0; |
| 1687 | static int xmlXPathDebugObjCounterNodeset = 0; |
| 1688 | static int xmlXPathDebugObjCounterBool = 0; |
| 1689 | static int xmlXPathDebugObjCounterNumber = 0; |
| 1690 | static int xmlXPathDebugObjCounterString = 0; |
| 1691 | static int xmlXPathDebugObjCounterPoint = 0; |
| 1692 | static int xmlXPathDebugObjCounterRange = 0; |
| 1693 | static int xmlXPathDebugObjCounterLocset = 0; |
| 1694 | static int xmlXPathDebugObjCounterUsers = 0; |
| 1695 | static int xmlXPathDebugObjCounterXSLTTree = 0; |
| 1696 | static int xmlXPathDebugObjCounterAll = 0; |
| 1697 | |
| 1698 | static int xmlXPathDebugObjTotalUndefined = 0; |
| 1699 | static int xmlXPathDebugObjTotalNodeset = 0; |
| 1700 | static int xmlXPathDebugObjTotalBool = 0; |
| 1701 | static int xmlXPathDebugObjTotalNumber = 0; |
| 1702 | static int xmlXPathDebugObjTotalString = 0; |
| 1703 | static int xmlXPathDebugObjTotalPoint = 0; |
| 1704 | static int xmlXPathDebugObjTotalRange = 0; |
| 1705 | static int xmlXPathDebugObjTotalLocset = 0; |
| 1706 | static int xmlXPathDebugObjTotalUsers = 0; |
| 1707 | static int xmlXPathDebugObjTotalXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1708 | static int xmlXPathDebugObjTotalAll = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1709 | |
| 1710 | static int xmlXPathDebugObjMaxUndefined = 0; |
| 1711 | static int xmlXPathDebugObjMaxNodeset = 0; |
| 1712 | static int xmlXPathDebugObjMaxBool = 0; |
| 1713 | static int xmlXPathDebugObjMaxNumber = 0; |
| 1714 | static int xmlXPathDebugObjMaxString = 0; |
| 1715 | static int xmlXPathDebugObjMaxPoint = 0; |
| 1716 | static int xmlXPathDebugObjMaxRange = 0; |
| 1717 | static int xmlXPathDebugObjMaxLocset = 0; |
| 1718 | static int xmlXPathDebugObjMaxUsers = 0; |
| 1719 | static int xmlXPathDebugObjMaxXSLTTree = 0; |
| 1720 | static int xmlXPathDebugObjMaxAll = 0; |
| 1721 | |
| 1722 | /* REVISIT TODO: Make this static when committing */ |
| 1723 | static void |
| 1724 | xmlXPathDebugObjUsageReset(xmlXPathContextPtr ctxt) |
| 1725 | { |
| 1726 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1727 | if (ctxt->cache != NULL) { |
| 1728 | xmlXPathContextCachePtr cache = |
| 1729 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1730 | |
| 1731 | cache->dbgCachedAll = 0; |
| 1732 | cache->dbgCachedNodeset = 0; |
| 1733 | cache->dbgCachedString = 0; |
| 1734 | cache->dbgCachedBool = 0; |
| 1735 | cache->dbgCachedNumber = 0; |
| 1736 | cache->dbgCachedPoint = 0; |
| 1737 | cache->dbgCachedRange = 0; |
| 1738 | cache->dbgCachedLocset = 0; |
| 1739 | cache->dbgCachedUsers = 0; |
| 1740 | cache->dbgCachedXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1741 | cache->dbgCachedUndefined = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1742 | |
| 1743 | cache->dbgReusedAll = 0; |
| 1744 | cache->dbgReusedNodeset = 0; |
| 1745 | cache->dbgReusedString = 0; |
| 1746 | cache->dbgReusedBool = 0; |
| 1747 | cache->dbgReusedNumber = 0; |
| 1748 | cache->dbgReusedPoint = 0; |
| 1749 | cache->dbgReusedRange = 0; |
| 1750 | cache->dbgReusedLocset = 0; |
| 1751 | cache->dbgReusedUsers = 0; |
| 1752 | cache->dbgReusedXSLTTree = 0; |
| 1753 | cache->dbgReusedUndefined = 0; |
| 1754 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1755 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1756 | |
| 1757 | xmlXPathDebugObjCounterUndefined = 0; |
| 1758 | xmlXPathDebugObjCounterNodeset = 0; |
| 1759 | xmlXPathDebugObjCounterBool = 0; |
| 1760 | xmlXPathDebugObjCounterNumber = 0; |
| 1761 | xmlXPathDebugObjCounterString = 0; |
| 1762 | xmlXPathDebugObjCounterPoint = 0; |
| 1763 | xmlXPathDebugObjCounterRange = 0; |
| 1764 | xmlXPathDebugObjCounterLocset = 0; |
| 1765 | xmlXPathDebugObjCounterUsers = 0; |
| 1766 | xmlXPathDebugObjCounterXSLTTree = 0; |
| 1767 | xmlXPathDebugObjCounterAll = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1768 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1769 | xmlXPathDebugObjTotalUndefined = 0; |
| 1770 | xmlXPathDebugObjTotalNodeset = 0; |
| 1771 | xmlXPathDebugObjTotalBool = 0; |
| 1772 | xmlXPathDebugObjTotalNumber = 0; |
| 1773 | xmlXPathDebugObjTotalString = 0; |
| 1774 | xmlXPathDebugObjTotalPoint = 0; |
| 1775 | xmlXPathDebugObjTotalRange = 0; |
| 1776 | xmlXPathDebugObjTotalLocset = 0; |
| 1777 | xmlXPathDebugObjTotalUsers = 0; |
| 1778 | xmlXPathDebugObjTotalXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1779 | xmlXPathDebugObjTotalAll = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1780 | |
| 1781 | xmlXPathDebugObjMaxUndefined = 0; |
| 1782 | xmlXPathDebugObjMaxNodeset = 0; |
| 1783 | xmlXPathDebugObjMaxBool = 0; |
| 1784 | xmlXPathDebugObjMaxNumber = 0; |
| 1785 | xmlXPathDebugObjMaxString = 0; |
| 1786 | xmlXPathDebugObjMaxPoint = 0; |
| 1787 | xmlXPathDebugObjMaxRange = 0; |
| 1788 | xmlXPathDebugObjMaxLocset = 0; |
| 1789 | xmlXPathDebugObjMaxUsers = 0; |
| 1790 | xmlXPathDebugObjMaxXSLTTree = 0; |
| 1791 | xmlXPathDebugObjMaxAll = 0; |
| 1792 | |
| 1793 | } |
| 1794 | |
| 1795 | static void |
| 1796 | xmlXPathDebugObjUsageRequested(xmlXPathContextPtr ctxt, |
| 1797 | xmlXPathObjectType objType) |
| 1798 | { |
| 1799 | int isCached = 0; |
| 1800 | |
| 1801 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1802 | if (ctxt->cache != NULL) { |
| 1803 | xmlXPathContextCachePtr cache = |
| 1804 | (xmlXPathContextCachePtr) ctxt->cache; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1805 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1806 | isCached = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1807 | |
| 1808 | cache->dbgReusedAll++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1809 | switch (objType) { |
| 1810 | case XPATH_UNDEFINED: |
| 1811 | cache->dbgReusedUndefined++; |
| 1812 | break; |
| 1813 | case XPATH_NODESET: |
| 1814 | cache->dbgReusedNodeset++; |
| 1815 | break; |
| 1816 | case XPATH_BOOLEAN: |
| 1817 | cache->dbgReusedBool++; |
| 1818 | break; |
| 1819 | case XPATH_NUMBER: |
| 1820 | cache->dbgReusedNumber++; |
| 1821 | break; |
| 1822 | case XPATH_STRING: |
| 1823 | cache->dbgReusedString++; |
| 1824 | break; |
| 1825 | case XPATH_POINT: |
| 1826 | cache->dbgReusedPoint++; |
| 1827 | break; |
| 1828 | case XPATH_RANGE: |
| 1829 | cache->dbgReusedRange++; |
| 1830 | break; |
| 1831 | case XPATH_LOCATIONSET: |
| 1832 | cache->dbgReusedLocset++; |
| 1833 | break; |
| 1834 | case XPATH_USERS: |
| 1835 | cache->dbgReusedUsers++; |
| 1836 | break; |
| 1837 | case XPATH_XSLT_TREE: |
| 1838 | cache->dbgReusedXSLTTree++; |
| 1839 | break; |
| 1840 | default: |
| 1841 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1842 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | switch (objType) { |
| 1847 | case XPATH_UNDEFINED: |
| 1848 | if (! isCached) |
| 1849 | xmlXPathDebugObjTotalUndefined++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1850 | xmlXPathDebugObjCounterUndefined++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1851 | if (xmlXPathDebugObjCounterUndefined > |
| 1852 | xmlXPathDebugObjMaxUndefined) |
| 1853 | xmlXPathDebugObjMaxUndefined = |
| 1854 | xmlXPathDebugObjCounterUndefined; |
| 1855 | break; |
| 1856 | case XPATH_NODESET: |
| 1857 | if (! isCached) |
| 1858 | xmlXPathDebugObjTotalNodeset++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1859 | xmlXPathDebugObjCounterNodeset++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1860 | if (xmlXPathDebugObjCounterNodeset > |
| 1861 | xmlXPathDebugObjMaxNodeset) |
| 1862 | xmlXPathDebugObjMaxNodeset = |
| 1863 | xmlXPathDebugObjCounterNodeset; |
| 1864 | break; |
| 1865 | case XPATH_BOOLEAN: |
| 1866 | if (! isCached) |
| 1867 | xmlXPathDebugObjTotalBool++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1868 | xmlXPathDebugObjCounterBool++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1869 | if (xmlXPathDebugObjCounterBool > |
| 1870 | xmlXPathDebugObjMaxBool) |
| 1871 | xmlXPathDebugObjMaxBool = |
| 1872 | xmlXPathDebugObjCounterBool; |
| 1873 | break; |
| 1874 | case XPATH_NUMBER: |
| 1875 | if (! isCached) |
| 1876 | xmlXPathDebugObjTotalNumber++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1877 | xmlXPathDebugObjCounterNumber++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1878 | if (xmlXPathDebugObjCounterNumber > |
| 1879 | xmlXPathDebugObjMaxNumber) |
| 1880 | xmlXPathDebugObjMaxNumber = |
| 1881 | xmlXPathDebugObjCounterNumber; |
| 1882 | break; |
| 1883 | case XPATH_STRING: |
| 1884 | if (! isCached) |
| 1885 | xmlXPathDebugObjTotalString++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1886 | xmlXPathDebugObjCounterString++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1887 | if (xmlXPathDebugObjCounterString > |
| 1888 | xmlXPathDebugObjMaxString) |
| 1889 | xmlXPathDebugObjMaxString = |
| 1890 | xmlXPathDebugObjCounterString; |
| 1891 | break; |
| 1892 | case XPATH_POINT: |
| 1893 | if (! isCached) |
| 1894 | xmlXPathDebugObjTotalPoint++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1895 | xmlXPathDebugObjCounterPoint++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1896 | if (xmlXPathDebugObjCounterPoint > |
| 1897 | xmlXPathDebugObjMaxPoint) |
| 1898 | xmlXPathDebugObjMaxPoint = |
| 1899 | xmlXPathDebugObjCounterPoint; |
| 1900 | break; |
| 1901 | case XPATH_RANGE: |
| 1902 | if (! isCached) |
| 1903 | xmlXPathDebugObjTotalRange++; |
| 1904 | xmlXPathDebugObjCounterRange++; |
| 1905 | if (xmlXPathDebugObjCounterRange > |
| 1906 | xmlXPathDebugObjMaxRange) |
| 1907 | xmlXPathDebugObjMaxRange = |
| 1908 | xmlXPathDebugObjCounterRange; |
| 1909 | break; |
| 1910 | case XPATH_LOCATIONSET: |
| 1911 | if (! isCached) |
| 1912 | xmlXPathDebugObjTotalLocset++; |
| 1913 | xmlXPathDebugObjCounterLocset++; |
| 1914 | if (xmlXPathDebugObjCounterLocset > |
| 1915 | xmlXPathDebugObjMaxLocset) |
| 1916 | xmlXPathDebugObjMaxLocset = |
| 1917 | xmlXPathDebugObjCounterLocset; |
| 1918 | break; |
| 1919 | case XPATH_USERS: |
| 1920 | if (! isCached) |
| 1921 | xmlXPathDebugObjTotalUsers++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1922 | xmlXPathDebugObjCounterUsers++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1923 | if (xmlXPathDebugObjCounterUsers > |
| 1924 | xmlXPathDebugObjMaxUsers) |
| 1925 | xmlXPathDebugObjMaxUsers = |
| 1926 | xmlXPathDebugObjCounterUsers; |
| 1927 | break; |
| 1928 | case XPATH_XSLT_TREE: |
| 1929 | if (! isCached) |
| 1930 | xmlXPathDebugObjTotalXSLTTree++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1931 | xmlXPathDebugObjCounterXSLTTree++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1932 | if (xmlXPathDebugObjCounterXSLTTree > |
| 1933 | xmlXPathDebugObjMaxXSLTTree) |
| 1934 | xmlXPathDebugObjMaxXSLTTree = |
| 1935 | xmlXPathDebugObjCounterXSLTTree; |
| 1936 | break; |
| 1937 | default: |
| 1938 | break; |
| 1939 | } |
| 1940 | if (! isCached) |
| 1941 | xmlXPathDebugObjTotalAll++; |
| 1942 | xmlXPathDebugObjCounterAll++; |
| 1943 | if (xmlXPathDebugObjCounterAll > |
| 1944 | xmlXPathDebugObjMaxAll) |
| 1945 | xmlXPathDebugObjMaxAll = |
| 1946 | xmlXPathDebugObjCounterAll; |
| 1947 | } |
| 1948 | |
| 1949 | static void |
| 1950 | xmlXPathDebugObjUsageReleased(xmlXPathContextPtr ctxt, |
| 1951 | xmlXPathObjectType objType) |
| 1952 | { |
| 1953 | int isCached = 0; |
| 1954 | |
| 1955 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1956 | if (ctxt->cache != NULL) { |
| 1957 | xmlXPathContextCachePtr cache = |
| 1958 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1959 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1960 | isCached = 1; |
| 1961 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1962 | cache->dbgCachedAll++; |
| 1963 | switch (objType) { |
| 1964 | case XPATH_UNDEFINED: |
| 1965 | cache->dbgCachedUndefined++; |
| 1966 | break; |
| 1967 | case XPATH_NODESET: |
| 1968 | cache->dbgCachedNodeset++; |
| 1969 | break; |
| 1970 | case XPATH_BOOLEAN: |
| 1971 | cache->dbgCachedBool++; |
| 1972 | break; |
| 1973 | case XPATH_NUMBER: |
| 1974 | cache->dbgCachedNumber++; |
| 1975 | break; |
| 1976 | case XPATH_STRING: |
| 1977 | cache->dbgCachedString++; |
| 1978 | break; |
| 1979 | case XPATH_POINT: |
| 1980 | cache->dbgCachedPoint++; |
| 1981 | break; |
| 1982 | case XPATH_RANGE: |
| 1983 | cache->dbgCachedRange++; |
| 1984 | break; |
| 1985 | case XPATH_LOCATIONSET: |
| 1986 | cache->dbgCachedLocset++; |
| 1987 | break; |
| 1988 | case XPATH_USERS: |
| 1989 | cache->dbgCachedUsers++; |
| 1990 | break; |
| 1991 | case XPATH_XSLT_TREE: |
| 1992 | cache->dbgCachedXSLTTree++; |
| 1993 | break; |
| 1994 | default: |
| 1995 | break; |
| 1996 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1997 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | switch (objType) { |
| 2001 | case XPATH_UNDEFINED: |
| 2002 | xmlXPathDebugObjCounterUndefined--; |
| 2003 | break; |
| 2004 | case XPATH_NODESET: |
| 2005 | xmlXPathDebugObjCounterNodeset--; |
| 2006 | break; |
| 2007 | case XPATH_BOOLEAN: |
| 2008 | xmlXPathDebugObjCounterBool--; |
| 2009 | break; |
| 2010 | case XPATH_NUMBER: |
| 2011 | xmlXPathDebugObjCounterNumber--; |
| 2012 | break; |
| 2013 | case XPATH_STRING: |
| 2014 | xmlXPathDebugObjCounterString--; |
| 2015 | break; |
| 2016 | case XPATH_POINT: |
| 2017 | xmlXPathDebugObjCounterPoint--; |
| 2018 | break; |
| 2019 | case XPATH_RANGE: |
| 2020 | xmlXPathDebugObjCounterRange--; |
| 2021 | break; |
| 2022 | case XPATH_LOCATIONSET: |
| 2023 | xmlXPathDebugObjCounterLocset--; |
| 2024 | break; |
| 2025 | case XPATH_USERS: |
| 2026 | xmlXPathDebugObjCounterUsers--; |
| 2027 | break; |
| 2028 | case XPATH_XSLT_TREE: |
| 2029 | xmlXPathDebugObjCounterXSLTTree--; |
| 2030 | break; |
| 2031 | default: |
| 2032 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2033 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2034 | xmlXPathDebugObjCounterAll--; |
| 2035 | } |
| 2036 | |
| 2037 | /* REVISIT TODO: Make this static when committing */ |
| 2038 | static void |
| 2039 | xmlXPathDebugObjUsageDisplay(xmlXPathContextPtr ctxt) |
| 2040 | { |
| 2041 | int reqAll, reqNodeset, reqString, reqBool, reqNumber, |
| 2042 | reqXSLTTree, reqUndefined; |
| 2043 | int caAll = 0, caNodeset = 0, caString = 0, caBool = 0, |
| 2044 | caNumber = 0, caXSLTTree = 0, caUndefined = 0; |
| 2045 | int reAll = 0, reNodeset = 0, reString = 0, reBool = 0, |
| 2046 | reNumber = 0, reXSLTTree = 0, reUndefined = 0; |
| 2047 | int leftObjs = xmlXPathDebugObjCounterAll; |
| 2048 | |
| 2049 | reqAll = xmlXPathDebugObjTotalAll; |
| 2050 | reqNodeset = xmlXPathDebugObjTotalNodeset; |
| 2051 | reqString = xmlXPathDebugObjTotalString; |
| 2052 | reqBool = xmlXPathDebugObjTotalBool; |
| 2053 | reqNumber = xmlXPathDebugObjTotalNumber; |
| 2054 | reqXSLTTree = xmlXPathDebugObjTotalXSLTTree; |
| 2055 | reqUndefined = xmlXPathDebugObjTotalUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2056 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2057 | printf("# XPath object usage:\n"); |
| 2058 | |
| 2059 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2060 | if (ctxt->cache != NULL) { |
| 2061 | xmlXPathContextCachePtr cache = |
| 2062 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2063 | |
| 2064 | reAll = cache->dbgReusedAll; |
| 2065 | reqAll += reAll; |
| 2066 | reNodeset = cache->dbgReusedNodeset; |
| 2067 | reqNodeset += reNodeset; |
| 2068 | reString = cache->dbgReusedString; |
| 2069 | reqString += reString; |
| 2070 | reBool = cache->dbgReusedBool; |
| 2071 | reqBool += reBool; |
| 2072 | reNumber = cache->dbgReusedNumber; |
| 2073 | reqNumber += reNumber; |
| 2074 | reXSLTTree = cache->dbgReusedXSLTTree; |
| 2075 | reqXSLTTree += reXSLTTree; |
| 2076 | reUndefined = cache->dbgReusedUndefined; |
| 2077 | reqUndefined += reUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2078 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2079 | caAll = cache->dbgCachedAll; |
| 2080 | caBool = cache->dbgCachedBool; |
| 2081 | caNodeset = cache->dbgCachedNodeset; |
| 2082 | caString = cache->dbgCachedString; |
| 2083 | caNumber = cache->dbgCachedNumber; |
| 2084 | caXSLTTree = cache->dbgCachedXSLTTree; |
| 2085 | caUndefined = cache->dbgCachedUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2086 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2087 | if (cache->nodesetObjs) |
| 2088 | leftObjs -= cache->nodesetObjs->number; |
| 2089 | if (cache->stringObjs) |
| 2090 | leftObjs -= cache->stringObjs->number; |
| 2091 | if (cache->booleanObjs) |
| 2092 | leftObjs -= cache->booleanObjs->number; |
| 2093 | if (cache->numberObjs) |
| 2094 | leftObjs -= cache->numberObjs->number; |
| 2095 | if (cache->miscObjs) |
| 2096 | leftObjs -= cache->miscObjs->number; |
| 2097 | } |
| 2098 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2099 | |
| 2100 | printf("# all\n"); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2101 | printf("# total : %d\n", reqAll); |
| 2102 | printf("# left : %d\n", leftObjs); |
| 2103 | printf("# created: %d\n", xmlXPathDebugObjTotalAll); |
| 2104 | printf("# reused : %d\n", reAll); |
| 2105 | printf("# max : %d\n", xmlXPathDebugObjMaxAll); |
| 2106 | |
| 2107 | printf("# node-sets\n"); |
| 2108 | printf("# total : %d\n", reqNodeset); |
| 2109 | printf("# created: %d\n", xmlXPathDebugObjTotalNodeset); |
| 2110 | printf("# reused : %d\n", reNodeset); |
| 2111 | printf("# max : %d\n", xmlXPathDebugObjMaxNodeset); |
| 2112 | |
| 2113 | printf("# strings\n"); |
| 2114 | printf("# total : %d\n", reqString); |
| 2115 | printf("# created: %d\n", xmlXPathDebugObjTotalString); |
| 2116 | printf("# reused : %d\n", reString); |
| 2117 | printf("# max : %d\n", xmlXPathDebugObjMaxString); |
| 2118 | |
| 2119 | printf("# booleans\n"); |
| 2120 | printf("# total : %d\n", reqBool); |
| 2121 | printf("# created: %d\n", xmlXPathDebugObjTotalBool); |
| 2122 | printf("# reused : %d\n", reBool); |
| 2123 | printf("# max : %d\n", xmlXPathDebugObjMaxBool); |
| 2124 | |
| 2125 | printf("# numbers\n"); |
| 2126 | printf("# total : %d\n", reqNumber); |
| 2127 | printf("# created: %d\n", xmlXPathDebugObjTotalNumber); |
| 2128 | printf("# reused : %d\n", reNumber); |
| 2129 | printf("# max : %d\n", xmlXPathDebugObjMaxNumber); |
| 2130 | |
| 2131 | printf("# XSLT result tree fragments\n"); |
| 2132 | printf("# total : %d\n", reqXSLTTree); |
| 2133 | printf("# created: %d\n", xmlXPathDebugObjTotalXSLTTree); |
| 2134 | printf("# reused : %d\n", reXSLTTree); |
| 2135 | printf("# max : %d\n", xmlXPathDebugObjMaxXSLTTree); |
| 2136 | |
| 2137 | printf("# undefined\n"); |
| 2138 | printf("# total : %d\n", reqUndefined); |
| 2139 | printf("# created: %d\n", xmlXPathDebugObjTotalUndefined); |
| 2140 | printf("# reused : %d\n", reUndefined); |
| 2141 | printf("# max : %d\n", xmlXPathDebugObjMaxUndefined); |
| 2142 | |
| 2143 | } |
| 2144 | |
| 2145 | #endif /* XP_DEBUG_OBJ_USAGE */ |
| 2146 | |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 2147 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2148 | |
| 2149 | /************************************************************************ |
| 2150 | * * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2151 | * XPath object caching * |
| 2152 | * * |
| 2153 | ************************************************************************/ |
| 2154 | |
| 2155 | /** |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2156 | * xmlXPathNewCache: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2157 | * |
| 2158 | * Create a new object cache |
| 2159 | * |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2160 | * Returns the xmlXPathCache just allocated. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2161 | */ |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2162 | static xmlXPathContextCachePtr |
| 2163 | xmlXPathNewCache(void) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2164 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2165 | xmlXPathContextCachePtr ret; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2166 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2167 | ret = (xmlXPathContextCachePtr) xmlMalloc(sizeof(xmlXPathContextCache)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2168 | if (ret == NULL) { |
| 2169 | xmlXPathErrMemory(NULL, "creating object cache\n"); |
| 2170 | return(NULL); |
| 2171 | } |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2172 | memset(ret, 0 , (size_t) sizeof(xmlXPathContextCache)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2173 | ret->maxNodeset = 100; |
| 2174 | ret->maxString = 100; |
| 2175 | ret->maxBoolean = 100; |
| 2176 | ret->maxNumber = 100; |
| 2177 | ret->maxMisc = 100; |
| 2178 | return(ret); |
| 2179 | } |
| 2180 | |
| 2181 | static void |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2182 | xmlXPathCacheFreeObjectList(xmlPointerListPtr list) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2183 | { |
| 2184 | int i; |
| 2185 | xmlXPathObjectPtr obj; |
| 2186 | |
| 2187 | if (list == NULL) |
| 2188 | return; |
| 2189 | |
| 2190 | for (i = 0; i < list->number; i++) { |
| 2191 | obj = list->items[i]; |
| 2192 | /* |
| 2193 | * Note that it is already assured that we don't need to |
| 2194 | * look out for namespace nodes in the node-set. |
| 2195 | */ |
| 2196 | if (obj->nodesetval != NULL) { |
| 2197 | if (obj->nodesetval->nodeTab != NULL) |
| 2198 | xmlFree(obj->nodesetval->nodeTab); |
| 2199 | xmlFree(obj->nodesetval); |
| 2200 | } |
| 2201 | xmlFree(obj); |
| 2202 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2203 | xmlXPathDebugObjCounterAll--; |
| 2204 | #endif |
| 2205 | } |
| 2206 | xmlPointerListFree(list); |
| 2207 | } |
| 2208 | |
| 2209 | static void |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2210 | xmlXPathFreeCache(xmlXPathContextCachePtr cache) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2211 | { |
| 2212 | if (cache == NULL) |
| 2213 | return; |
| 2214 | if (cache->nodesetObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2215 | xmlXPathCacheFreeObjectList(cache->nodesetObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2216 | if (cache->stringObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2217 | xmlXPathCacheFreeObjectList(cache->stringObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2218 | if (cache->booleanObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2219 | xmlXPathCacheFreeObjectList(cache->booleanObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2220 | if (cache->numberObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2221 | xmlXPathCacheFreeObjectList(cache->numberObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2222 | if (cache->miscObjs) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2223 | xmlXPathCacheFreeObjectList(cache->miscObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2224 | xmlFree(cache); |
| 2225 | } |
| 2226 | |
| 2227 | /** |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2228 | * xmlXPathContextSetCache: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2229 | * |
| 2230 | * @ctxt: the XPath context |
| 2231 | * @active: enables/disables (creates/frees) the cache |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2232 | * @value: a value with semantics dependant on @options |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2233 | * @options: options (currently only the value 0 is used) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2234 | * |
| 2235 | * Creates/frees an object cache on the XPath context. |
| 2236 | * If activates XPath objects (xmlXPathObject) will be cached internally |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 2237 | * to be reused. |
| 2238 | * @options: |
| 2239 | * 0: This will set the XPath object caching: |
| 2240 | * @value: |
| 2241 | * This will set the maximum number of XPath objects |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2242 | * to be cached per slot |
| 2243 | * There are 5 slots for: node-set, string, number, boolean, and |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 2244 | * misc objects. Use <0 for the default number (100). |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2245 | * Other values for @options have currently no effect. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2246 | * |
| 2247 | * Returns 0 if the setting succeeded, and -1 on API or internal errors. |
| 2248 | */ |
| 2249 | int |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2250 | xmlXPathContextSetCache(xmlXPathContextPtr ctxt, |
| 2251 | int active, |
| 2252 | int value, |
| 2253 | int options) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2254 | { |
| 2255 | if (ctxt == NULL) |
| 2256 | return(-1); |
| 2257 | if (active) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2258 | xmlXPathContextCachePtr cache; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2259 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2260 | if (ctxt->cache == NULL) { |
| 2261 | ctxt->cache = xmlXPathNewCache(); |
| 2262 | if (ctxt->cache == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2263 | return(-1); |
| 2264 | } |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 2265 | cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2266 | if (options == 0) { |
| 2267 | if (value < 0) |
| 2268 | value = 100; |
| 2269 | cache->maxNodeset = value; |
| 2270 | cache->maxString = value; |
| 2271 | cache->maxNumber = value; |
| 2272 | cache->maxBoolean = value; |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 2273 | cache->maxMisc = value; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2274 | } |
| 2275 | } else if (ctxt->cache != NULL) { |
| 2276 | xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache); |
| 2277 | ctxt->cache = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2278 | } |
| 2279 | return(0); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
| 2283 | * xmlXPathCacheWrapNodeSet: |
| 2284 | * @ctxt: the XPath context |
| 2285 | * @val: the NodePtr value |
| 2286 | * |
| 2287 | * This is the cached version of xmlXPathWrapNodeSet(). |
| 2288 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2289 | * |
| 2290 | * Returns the created or reused object. |
| 2291 | */ |
| 2292 | static xmlXPathObjectPtr |
| 2293 | xmlXPathCacheWrapNodeSet(xmlXPathContextPtr ctxt, xmlNodeSetPtr val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2294 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2295 | if ((ctxt != NULL) && (ctxt->cache != NULL)) { |
| 2296 | xmlXPathContextCachePtr cache = |
| 2297 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2298 | |
| 2299 | if ((cache->miscObjs != NULL) && |
| 2300 | (cache->miscObjs->number != 0)) |
| 2301 | { |
| 2302 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2303 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2304 | ret = (xmlXPathObjectPtr) |
| 2305 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2306 | ret->type = XPATH_NODESET; |
| 2307 | ret->nodesetval = val; |
| 2308 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2309 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 2310 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2311 | return(ret); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2314 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2315 | return(xmlXPathWrapNodeSet(val)); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2316 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /** |
| 2320 | * xmlXPathCacheWrapString: |
| 2321 | * @ctxt: the XPath context |
| 2322 | * @val: the xmlChar * value |
| 2323 | * |
| 2324 | * This is the cached version of xmlXPathWrapString(). |
| 2325 | * Wraps the @val string into an XPath object. |
| 2326 | * |
| 2327 | * Returns the created or reused object. |
| 2328 | */ |
| 2329 | static xmlXPathObjectPtr |
| 2330 | xmlXPathCacheWrapString(xmlXPathContextPtr ctxt, xmlChar *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2331 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2332 | if ((ctxt != NULL) && (ctxt->cache != NULL)) { |
| 2333 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2334 | |
| 2335 | if ((cache->stringObjs != NULL) && |
| 2336 | (cache->stringObjs->number != 0)) |
| 2337 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2338 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2339 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2340 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2341 | ret = (xmlXPathObjectPtr) |
| 2342 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 2343 | ret->type = XPATH_STRING; |
| 2344 | ret->stringval = val; |
| 2345 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2346 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2347 | #endif |
| 2348 | return(ret); |
| 2349 | } else if ((cache->miscObjs != NULL) && |
| 2350 | (cache->miscObjs->number != 0)) |
| 2351 | { |
| 2352 | xmlXPathObjectPtr ret; |
| 2353 | /* |
| 2354 | * Fallback to misc-cache. |
| 2355 | */ |
| 2356 | ret = (xmlXPathObjectPtr) |
| 2357 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2358 | |
| 2359 | ret->type = XPATH_STRING; |
| 2360 | ret->stringval = val; |
| 2361 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2362 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2363 | #endif |
| 2364 | return(ret); |
| 2365 | } |
| 2366 | } |
| 2367 | return(xmlXPathWrapString(val)); |
| 2368 | } |
| 2369 | |
| 2370 | /** |
| 2371 | * xmlXPathCacheNewNodeSet: |
| 2372 | * @ctxt: the XPath context |
| 2373 | * @val: the NodePtr value |
| 2374 | * |
| 2375 | * This is the cached version of xmlXPathNewNodeSet(). |
| 2376 | * Acquire an xmlXPathObjectPtr of type NodeSet and initialize |
| 2377 | * it with the single Node @val |
| 2378 | * |
| 2379 | * Returns the created or reused object. |
| 2380 | */ |
| 2381 | static xmlXPathObjectPtr |
| 2382 | xmlXPathCacheNewNodeSet(xmlXPathContextPtr ctxt, xmlNodePtr val) |
| 2383 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2384 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2385 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2386 | |
| 2387 | if ((cache->nodesetObjs != NULL) && |
| 2388 | (cache->nodesetObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2389 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2390 | xmlXPathObjectPtr ret; |
| 2391 | /* |
| 2392 | * Use the nodset-cache. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2393 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2394 | ret = (xmlXPathObjectPtr) |
| 2395 | cache->nodesetObjs->items[--cache->nodesetObjs->number]; |
| 2396 | ret->type = XPATH_NODESET; |
| 2397 | ret->boolval = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2398 | if (val) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2399 | if ((ret->nodesetval->nodeMax == 0) || |
| 2400 | (val->type == XML_NAMESPACE_DECL)) |
| 2401 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2402 | xmlXPathNodeSetAddUnique(ret->nodesetval, val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2403 | } else { |
| 2404 | ret->nodesetval->nodeTab[0] = val; |
| 2405 | ret->nodesetval->nodeNr = 1; |
| 2406 | } |
| 2407 | } |
| 2408 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2409 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 2410 | #endif |
| 2411 | return(ret); |
| 2412 | } else if ((cache->miscObjs != NULL) && |
| 2413 | (cache->miscObjs->number != 0)) |
| 2414 | { |
| 2415 | xmlXPathObjectPtr ret; |
| 2416 | /* |
| 2417 | * Fallback to misc-cache. |
| 2418 | */ |
| 2419 | |
| 2420 | ret = (xmlXPathObjectPtr) |
| 2421 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2422 | |
| 2423 | ret->type = XPATH_NODESET; |
| 2424 | ret->boolval = 0; |
| 2425 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 2426 | if (ret->nodesetval == NULL) { |
| 2427 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 2428 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 2429 | return(NULL); |
| 2430 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2431 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2432 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 2433 | #endif |
| 2434 | return(ret); |
| 2435 | } |
| 2436 | } |
| 2437 | return(xmlXPathNewNodeSet(val)); |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * xmlXPathCacheNewCString: |
| 2442 | * @ctxt: the XPath context |
| 2443 | * @val: the char * value |
| 2444 | * |
| 2445 | * This is the cached version of xmlXPathNewCString(). |
| 2446 | * Acquire an xmlXPathObjectPtr of type string and of value @val |
| 2447 | * |
| 2448 | * Returns the created or reused object. |
| 2449 | */ |
| 2450 | static xmlXPathObjectPtr |
| 2451 | xmlXPathCacheNewCString(xmlXPathContextPtr ctxt, const char *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2452 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2453 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2454 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2455 | |
| 2456 | if ((cache->stringObjs != NULL) && |
| 2457 | (cache->stringObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2458 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2459 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2460 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2461 | ret = (xmlXPathObjectPtr) |
| 2462 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 2463 | |
| 2464 | ret->type = XPATH_STRING; |
| 2465 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 2466 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2467 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2468 | #endif |
| 2469 | return(ret); |
| 2470 | } else if ((cache->miscObjs != NULL) && |
| 2471 | (cache->miscObjs->number != 0)) |
| 2472 | { |
| 2473 | xmlXPathObjectPtr ret; |
| 2474 | |
| 2475 | ret = (xmlXPathObjectPtr) |
| 2476 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2477 | |
| 2478 | ret->type = XPATH_STRING; |
| 2479 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 2480 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2481 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2482 | #endif |
| 2483 | return(ret); |
| 2484 | } |
| 2485 | } |
| 2486 | return(xmlXPathNewCString(val)); |
| 2487 | } |
| 2488 | |
| 2489 | /** |
| 2490 | * xmlXPathCacheNewString: |
| 2491 | * @ctxt: the XPath context |
| 2492 | * @val: the xmlChar * value |
| 2493 | * |
| 2494 | * This is the cached version of xmlXPathNewString(). |
| 2495 | * Acquire an xmlXPathObjectPtr of type string and of value @val |
| 2496 | * |
| 2497 | * Returns the created or reused object. |
| 2498 | */ |
| 2499 | static xmlXPathObjectPtr |
| 2500 | xmlXPathCacheNewString(xmlXPathContextPtr ctxt, const xmlChar *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2501 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2502 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2503 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2504 | |
| 2505 | if ((cache->stringObjs != NULL) && |
| 2506 | (cache->stringObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2507 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2508 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2509 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2510 | ret = (xmlXPathObjectPtr) |
| 2511 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 2512 | ret->type = XPATH_STRING; |
| 2513 | if (val != NULL) |
| 2514 | ret->stringval = xmlStrdup(val); |
| 2515 | else |
| 2516 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 2517 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2518 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2519 | #endif |
| 2520 | return(ret); |
| 2521 | } else if ((cache->miscObjs != NULL) && |
| 2522 | (cache->miscObjs->number != 0)) |
| 2523 | { |
| 2524 | xmlXPathObjectPtr ret; |
| 2525 | |
| 2526 | ret = (xmlXPathObjectPtr) |
| 2527 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2528 | |
| 2529 | ret->type = XPATH_STRING; |
| 2530 | if (val != NULL) |
| 2531 | ret->stringval = xmlStrdup(val); |
| 2532 | else |
| 2533 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 2534 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2535 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2536 | #endif |
| 2537 | return(ret); |
| 2538 | } |
| 2539 | } |
| 2540 | return(xmlXPathNewString(val)); |
| 2541 | } |
| 2542 | |
| 2543 | /** |
| 2544 | * xmlXPathCacheNewBoolean: |
| 2545 | * @ctxt: the XPath context |
| 2546 | * @val: the boolean value |
| 2547 | * |
| 2548 | * This is the cached version of xmlXPathNewBoolean(). |
| 2549 | * Acquires an xmlXPathObjectPtr of type boolean and of value @val |
| 2550 | * |
| 2551 | * Returns the created or reused object. |
| 2552 | */ |
| 2553 | static xmlXPathObjectPtr |
| 2554 | xmlXPathCacheNewBoolean(xmlXPathContextPtr ctxt, int val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2555 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2556 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2557 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2558 | |
| 2559 | if ((cache->booleanObjs != NULL) && |
| 2560 | (cache->booleanObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2561 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2562 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2563 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2564 | ret = (xmlXPathObjectPtr) |
| 2565 | cache->booleanObjs->items[--cache->booleanObjs->number]; |
| 2566 | ret->type = XPATH_BOOLEAN; |
| 2567 | ret->boolval = (val != 0); |
| 2568 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2569 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN); |
| 2570 | #endif |
| 2571 | return(ret); |
| 2572 | } else if ((cache->miscObjs != NULL) && |
| 2573 | (cache->miscObjs->number != 0)) |
| 2574 | { |
| 2575 | xmlXPathObjectPtr ret; |
| 2576 | |
| 2577 | ret = (xmlXPathObjectPtr) |
| 2578 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2579 | |
| 2580 | ret->type = XPATH_BOOLEAN; |
| 2581 | ret->boolval = (val != 0); |
| 2582 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2583 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN); |
| 2584 | #endif |
| 2585 | return(ret); |
| 2586 | } |
| 2587 | } |
| 2588 | return(xmlXPathNewBoolean(val)); |
| 2589 | } |
| 2590 | |
| 2591 | /** |
| 2592 | * xmlXPathCacheNewFloat: |
| 2593 | * @ctxt: the XPath context |
| 2594 | * @val: the double value |
| 2595 | * |
| 2596 | * This is the cached version of xmlXPathNewFloat(). |
| 2597 | * Acquires an xmlXPathObjectPtr of type double and of value @val |
| 2598 | * |
| 2599 | * Returns the created or reused object. |
| 2600 | */ |
| 2601 | static xmlXPathObjectPtr |
| 2602 | xmlXPathCacheNewFloat(xmlXPathContextPtr ctxt, double val) |
| 2603 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2604 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2605 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2606 | |
| 2607 | if ((cache->numberObjs != NULL) && |
| 2608 | (cache->numberObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2609 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2610 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2611 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2612 | ret = (xmlXPathObjectPtr) |
| 2613 | cache->numberObjs->items[--cache->numberObjs->number]; |
| 2614 | ret->type = XPATH_NUMBER; |
| 2615 | ret->floatval = val; |
| 2616 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2617 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER); |
| 2618 | #endif |
| 2619 | return(ret); |
| 2620 | } else if ((cache->miscObjs != NULL) && |
| 2621 | (cache->miscObjs->number != 0)) |
| 2622 | { |
| 2623 | xmlXPathObjectPtr ret; |
| 2624 | |
| 2625 | ret = (xmlXPathObjectPtr) |
| 2626 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2627 | |
| 2628 | ret->type = XPATH_NUMBER; |
| 2629 | ret->floatval = val; |
| 2630 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2631 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER); |
| 2632 | #endif |
| 2633 | return(ret); |
| 2634 | } |
| 2635 | } |
| 2636 | return(xmlXPathNewFloat(val)); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * xmlXPathCacheConvertString: |
| 2641 | * @ctxt: the XPath context |
| 2642 | * @val: an XPath object |
| 2643 | * |
| 2644 | * This is the cached version of xmlXPathConvertString(). |
| 2645 | * Converts an existing object to its string() equivalent |
| 2646 | * |
| 2647 | * Returns a created or reused object, the old one is freed (cached) |
| 2648 | * (or the operation is done directly on @val) |
| 2649 | */ |
| 2650 | |
| 2651 | static xmlXPathObjectPtr |
| 2652 | xmlXPathCacheConvertString(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2653 | xmlChar *res = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (val == NULL) |
| 2656 | return(xmlXPathCacheNewCString(ctxt, "")); |
| 2657 | |
| 2658 | switch (val->type) { |
| 2659 | case XPATH_UNDEFINED: |
| 2660 | #ifdef DEBUG_EXPR |
| 2661 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 2662 | #endif |
| 2663 | break; |
| 2664 | case XPATH_NODESET: |
| 2665 | case XPATH_XSLT_TREE: |
| 2666 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 2667 | break; |
| 2668 | case XPATH_STRING: |
| 2669 | return(val); |
| 2670 | case XPATH_BOOLEAN: |
| 2671 | res = xmlXPathCastBooleanToString(val->boolval); |
| 2672 | break; |
| 2673 | case XPATH_NUMBER: |
| 2674 | res = xmlXPathCastNumberToString(val->floatval); |
| 2675 | break; |
| 2676 | case XPATH_USERS: |
| 2677 | case XPATH_POINT: |
| 2678 | case XPATH_RANGE: |
| 2679 | case XPATH_LOCATIONSET: |
| 2680 | TODO; |
| 2681 | break; |
| 2682 | } |
| 2683 | xmlXPathReleaseObject(ctxt, val); |
| 2684 | if (res == NULL) |
| 2685 | return(xmlXPathCacheNewCString(ctxt, "")); |
| 2686 | return(xmlXPathCacheWrapString(ctxt, res)); |
| 2687 | } |
| 2688 | |
| 2689 | /** |
| 2690 | * xmlXPathCacheObjectCopy: |
| 2691 | * @ctxt: the XPath context |
| 2692 | * @val: the original object |
| 2693 | * |
| 2694 | * This is the cached version of xmlXPathObjectCopy(). |
| 2695 | * Acquire a copy of a given object |
| 2696 | * |
| 2697 | * Returns a created or reused created object. |
| 2698 | */ |
| 2699 | static xmlXPathObjectPtr |
| 2700 | xmlXPathCacheObjectCopy(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) |
| 2701 | { |
| 2702 | if (val == NULL) |
| 2703 | return(NULL); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2704 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2705 | if (XP_HAS_CACHE(ctxt)) { |
| 2706 | switch (val->type) { |
| 2707 | case XPATH_NODESET: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2708 | return(xmlXPathCacheWrapNodeSet(ctxt, |
| 2709 | xmlXPathNodeSetMerge(NULL, val->nodesetval))); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2710 | case XPATH_STRING: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2711 | return(xmlXPathCacheNewString(ctxt, val->stringval)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2712 | case XPATH_BOOLEAN: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2713 | return(xmlXPathCacheNewBoolean(ctxt, val->boolval)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2714 | case XPATH_NUMBER: |
| 2715 | return(xmlXPathCacheNewFloat(ctxt, val->floatval)); |
| 2716 | default: |
| 2717 | break; |
| 2718 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2719 | } |
| 2720 | return(xmlXPathObjectCopy(val)); |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * xmlXPathCacheConvertBoolean: |
| 2725 | * @ctxt: the XPath context |
| 2726 | * @val: an XPath object |
| 2727 | * |
| 2728 | * This is the cached version of xmlXPathConvertBoolean(). |
| 2729 | * Converts an existing object to its boolean() equivalent |
| 2730 | * |
| 2731 | * Returns a created or reused object, the old one is freed (or the operation |
| 2732 | * is done directly on @val) |
| 2733 | */ |
| 2734 | static xmlXPathObjectPtr |
| 2735 | xmlXPathCacheConvertBoolean(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
| 2736 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2737 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2738 | if (val == NULL) |
| 2739 | return(xmlXPathCacheNewBoolean(ctxt, 0)); |
| 2740 | if (val->type == XPATH_BOOLEAN) |
| 2741 | return(val); |
| 2742 | ret = xmlXPathCacheNewBoolean(ctxt, xmlXPathCastToBoolean(val)); |
| 2743 | xmlXPathReleaseObject(ctxt, val); |
| 2744 | return(ret); |
| 2745 | } |
| 2746 | |
| 2747 | /** |
| 2748 | * xmlXPathCacheConvertNumber: |
| 2749 | * @ctxt: the XPath context |
| 2750 | * @val: an XPath object |
| 2751 | * |
| 2752 | * This is the cached version of xmlXPathConvertNumber(). |
| 2753 | * Converts an existing object to its number() equivalent |
| 2754 | * |
| 2755 | * Returns a created or reused object, the old one is freed (or the operation |
| 2756 | * is done directly on @val) |
| 2757 | */ |
| 2758 | static xmlXPathObjectPtr |
| 2759 | xmlXPathCacheConvertNumber(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
| 2760 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2761 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2762 | if (val == NULL) |
| 2763 | return(xmlXPathCacheNewFloat(ctxt, 0.0)); |
| 2764 | if (val->type == XPATH_NUMBER) |
| 2765 | return(val); |
| 2766 | ret = xmlXPathCacheNewFloat(ctxt, xmlXPathCastToNumber(val)); |
| 2767 | xmlXPathReleaseObject(ctxt, val); |
| 2768 | return(ret); |
| 2769 | } |
| 2770 | |
| 2771 | /************************************************************************ |
| 2772 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2773 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | * * |
| 2775 | ************************************************************************/ |
| 2776 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2777 | /** |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 2778 | * xmlXPathSetFrame: |
| 2779 | * @ctxt: an XPath parser context |
| 2780 | * |
| 2781 | * Set the callee evaluation frame |
| 2782 | * |
| 2783 | * Returns the previous frame value to be restored once done |
| 2784 | */ |
| 2785 | static int |
| 2786 | xmlXPathSetFrame(xmlXPathParserContextPtr ctxt) { |
| 2787 | int ret; |
| 2788 | |
| 2789 | if (ctxt == NULL) |
| 2790 | return(0); |
| 2791 | ret = ctxt->valueFrame; |
| 2792 | ctxt->valueFrame = ctxt->valueNr; |
| 2793 | return(ret); |
| 2794 | } |
| 2795 | |
| 2796 | /** |
| 2797 | * xmlXPathPopFrame: |
| 2798 | * @ctxt: an XPath parser context |
| 2799 | * @frame: the previous frame value |
| 2800 | * |
| 2801 | * Remove the callee evaluation frame |
| 2802 | */ |
| 2803 | static void |
| 2804 | xmlXPathPopFrame(xmlXPathParserContextPtr ctxt, int frame) { |
| 2805 | if (ctxt == NULL) |
| 2806 | return; |
| 2807 | if (ctxt->valueNr < ctxt->valueFrame) { |
| 2808 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); |
| 2809 | } |
| 2810 | ctxt->valueFrame = frame; |
| 2811 | } |
| 2812 | |
| 2813 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2814 | * valuePop: |
| 2815 | * @ctxt: an XPath evaluation context |
| 2816 | * |
| 2817 | * Pops the top XPath object from the value stack |
| 2818 | * |
| 2819 | * Returns the XPath object just removed |
| 2820 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2821 | xmlXPathObjectPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2822 | valuePop(xmlXPathParserContextPtr ctxt) |
| 2823 | { |
| 2824 | xmlXPathObjectPtr ret; |
| 2825 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2826 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2827 | return (NULL); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 2828 | |
| 2829 | if (ctxt->valueNr <= ctxt->valueFrame) { |
| 2830 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); |
| 2831 | return (NULL); |
| 2832 | } |
| 2833 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2834 | ctxt->valueNr--; |
| 2835 | if (ctxt->valueNr > 0) |
| 2836 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 2837 | else |
| 2838 | ctxt->value = NULL; |
| 2839 | ret = ctxt->valueTab[ctxt->valueNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2840 | ctxt->valueTab[ctxt->valueNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2841 | return (ret); |
| 2842 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2843 | /** |
| 2844 | * valuePush: |
| 2845 | * @ctxt: an XPath evaluation context |
| 2846 | * @value: the XPath object |
| 2847 | * |
| 2848 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2849 | * |
| 2850 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2851 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2852 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2853 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 2854 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2855 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2856 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2857 | xmlXPathObjectPtr *tmp; |
| 2858 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 2859 | if (ctxt->valueMax >= XPATH_MAX_STACK_DEPTH) { |
| 2860 | xmlXPathErrMemory(NULL, "XPath stack depth limit reached\n"); |
| 2861 | ctxt->error = XPATH_MEMORY_ERROR; |
| 2862 | return (0); |
| 2863 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2864 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 2865 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2866 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2867 | if (tmp == NULL) { |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 2868 | xmlXPathErrMemory(NULL, "pushing value\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 2869 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2870 | return (0); |
| 2871 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2872 | ctxt->valueMax *= 2; |
| 2873 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2874 | } |
| 2875 | ctxt->valueTab[ctxt->valueNr] = value; |
| 2876 | ctxt->value = value; |
| 2877 | return (ctxt->valueNr++); |
| 2878 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2879 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2880 | /** |
| 2881 | * xmlXPathPopBoolean: |
| 2882 | * @ctxt: an XPath parser context |
| 2883 | * |
| 2884 | * Pops a boolean from the stack, handling conversion if needed. |
| 2885 | * Check error with #xmlXPathCheckError. |
| 2886 | * |
| 2887 | * Returns the boolean |
| 2888 | */ |
| 2889 | int |
| 2890 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 2891 | xmlXPathObjectPtr obj; |
| 2892 | int ret; |
| 2893 | |
| 2894 | obj = valuePop(ctxt); |
| 2895 | if (obj == NULL) { |
| 2896 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2897 | return(0); |
| 2898 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2899 | if (obj->type != XPATH_BOOLEAN) |
| 2900 | ret = xmlXPathCastToBoolean(obj); |
| 2901 | else |
| 2902 | ret = obj->boolval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2903 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2904 | return(ret); |
| 2905 | } |
| 2906 | |
| 2907 | /** |
| 2908 | * xmlXPathPopNumber: |
| 2909 | * @ctxt: an XPath parser context |
| 2910 | * |
| 2911 | * Pops a number from the stack, handling conversion if needed. |
| 2912 | * Check error with #xmlXPathCheckError. |
| 2913 | * |
| 2914 | * Returns the number |
| 2915 | */ |
| 2916 | double |
| 2917 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 2918 | xmlXPathObjectPtr obj; |
| 2919 | double ret; |
| 2920 | |
| 2921 | obj = valuePop(ctxt); |
| 2922 | if (obj == NULL) { |
| 2923 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2924 | return(0); |
| 2925 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2926 | if (obj->type != XPATH_NUMBER) |
| 2927 | ret = xmlXPathCastToNumber(obj); |
| 2928 | else |
| 2929 | ret = obj->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2930 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2931 | return(ret); |
| 2932 | } |
| 2933 | |
| 2934 | /** |
| 2935 | * xmlXPathPopString: |
| 2936 | * @ctxt: an XPath parser context |
| 2937 | * |
| 2938 | * Pops a string from the stack, handling conversion if needed. |
| 2939 | * Check error with #xmlXPathCheckError. |
| 2940 | * |
| 2941 | * Returns the string |
| 2942 | */ |
| 2943 | xmlChar * |
| 2944 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 2945 | xmlXPathObjectPtr obj; |
| 2946 | xmlChar * ret; |
| 2947 | |
| 2948 | obj = valuePop(ctxt); |
| 2949 | if (obj == NULL) { |
| 2950 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2951 | return(NULL); |
| 2952 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2953 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2954 | /* TODO: needs refactoring somewhere else */ |
| 2955 | if (obj->stringval == ret) |
| 2956 | obj->stringval = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2957 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2958 | return(ret); |
| 2959 | } |
| 2960 | |
| 2961 | /** |
| 2962 | * xmlXPathPopNodeSet: |
| 2963 | * @ctxt: an XPath parser context |
| 2964 | * |
| 2965 | * Pops a node-set from the stack, handling conversion if needed. |
| 2966 | * Check error with #xmlXPathCheckError. |
| 2967 | * |
| 2968 | * Returns the node-set |
| 2969 | */ |
| 2970 | xmlNodeSetPtr |
| 2971 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 2972 | xmlXPathObjectPtr obj; |
| 2973 | xmlNodeSetPtr ret; |
| 2974 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 2975 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2976 | if (ctxt->value == NULL) { |
| 2977 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2978 | return(NULL); |
| 2979 | } |
| 2980 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 2981 | xmlXPathSetTypeError(ctxt); |
| 2982 | return(NULL); |
| 2983 | } |
| 2984 | obj = valuePop(ctxt); |
| 2985 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 2986 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 2987 | /* to fix memory leak of not clearing obj->user */ |
| 2988 | if (obj->boolval && obj->user != NULL) |
| 2989 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 2990 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2991 | obj->nodesetval = NULL; |
| 2992 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2993 | return(ret); |
| 2994 | } |
| 2995 | |
| 2996 | /** |
| 2997 | * xmlXPathPopExternal: |
| 2998 | * @ctxt: an XPath parser context |
| 2999 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3000 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3001 | * Check error with #xmlXPathCheckError. |
| 3002 | * |
| 3003 | * Returns the object |
| 3004 | */ |
| 3005 | void * |
| 3006 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 3007 | xmlXPathObjectPtr obj; |
| 3008 | void * ret; |
| 3009 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3010 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3011 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 3012 | return(NULL); |
| 3013 | } |
| 3014 | if (ctxt->value->type != XPATH_USERS) { |
| 3015 | xmlXPathSetTypeError(ctxt); |
| 3016 | return(NULL); |
| 3017 | } |
| 3018 | obj = valuePop(ctxt); |
| 3019 | ret = obj->user; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3020 | obj->user = NULL; |
| 3021 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3022 | return(ret); |
| 3023 | } |
| 3024 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3025 | /* |
| 3026 | * Macros for accessing the content. Those should be used only by the parser, |
| 3027 | * and not exported. |
| 3028 | * |
| 3029 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 3030 | * |
| 3031 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 3032 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 3033 | * in ISO-Latin or UTF-8. |
| 3034 | * This should be used internally by the parser |
| 3035 | * only to compare to ASCII values otherwise it would break when |
| 3036 | * running with UTF-8 encoding. |
| 3037 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 3038 | * to compare on ASCII based substring. |
| 3039 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 3040 | * strings within the parser. |
| 3041 | * CURRENT Returns the current char value, with the full decoding of |
| 3042 | * UTF-8 if we are using this mode. It returns an int. |
| 3043 | * NEXT Skip to the next character, this does the proper decoding |
| 3044 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 3045 | * It returns the pointer to the current xmlChar. |
| 3046 | */ |
| 3047 | |
| 3048 | #define CUR (*ctxt->cur) |
| 3049 | #define SKIP(val) ctxt->cur += (val) |
| 3050 | #define NXT(val) ctxt->cur[(val)] |
| 3051 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 3052 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 3053 | |
| 3054 | #define COPY_BUF(l,b,i,v) \ |
| 3055 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 3056 | else i += xmlCopyChar(l,&b[i],v) |
| 3057 | |
| 3058 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3059 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3060 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3061 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3062 | |
| 3063 | #define CURRENT (*ctxt->cur) |
| 3064 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 3065 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3066 | |
| 3067 | #ifndef DBL_DIG |
| 3068 | #define DBL_DIG 16 |
| 3069 | #endif |
| 3070 | #ifndef DBL_EPSILON |
| 3071 | #define DBL_EPSILON 1E-9 |
| 3072 | #endif |
| 3073 | |
| 3074 | #define UPPER_DOUBLE 1E9 |
| 3075 | #define LOWER_DOUBLE 1E-5 |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 3076 | #define LOWER_DOUBLE_EXP 5 |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3077 | |
| 3078 | #define INTEGER_DIGITS DBL_DIG |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 3079 | #define FRACTION_DIGITS (DBL_DIG + 1 + (LOWER_DOUBLE_EXP)) |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3080 | #define EXPONENT_DIGITS (3 + 2) |
| 3081 | |
| 3082 | /** |
| 3083 | * xmlXPathFormatNumber: |
| 3084 | * @number: number to format |
| 3085 | * @buffer: output buffer |
| 3086 | * @buffersize: size of output buffer |
| 3087 | * |
| 3088 | * Convert the number into a string representation. |
| 3089 | */ |
| 3090 | static void |
| 3091 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 3092 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3093 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3094 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3095 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 3096 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3097 | break; |
| 3098 | case -1: |
| 3099 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 3100 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3103 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3104 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 3105 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3106 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 3107 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 3108 | } else if (number == ((int) number)) { |
| 3109 | char work[30]; |
| 3110 | char *ptr, *cur; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 3111 | int value = (int) number; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 3112 | |
| 3113 | ptr = &buffer[0]; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 3114 | if (value == 0) { |
| 3115 | *ptr++ = '0'; |
| 3116 | } else { |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 3117 | snprintf(work, 29, "%d", value); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 3118 | cur = &work[0]; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 3119 | while ((*cur) && (ptr - buffer < buffersize)) { |
| 3120 | *ptr++ = *cur++; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | if (ptr - buffer < buffersize) { |
| 3124 | *ptr = 0; |
| 3125 | } else if (buffersize > 0) { |
| 3126 | ptr--; |
| 3127 | *ptr = 0; |
| 3128 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3129 | } else { |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 3130 | /* |
| 3131 | For the dimension of work, |
| 3132 | DBL_DIG is number of significant digits |
| 3133 | EXPONENT is only needed for "scientific notation" |
| 3134 | 3 is sign, decimal point, and terminating zero |
| 3135 | LOWER_DOUBLE_EXP is max number of leading zeroes in fraction |
| 3136 | Note that this dimension is slightly (a few characters) |
| 3137 | larger than actually necessary. |
| 3138 | */ |
| 3139 | char work[DBL_DIG + EXPONENT_DIGITS + 3 + LOWER_DOUBLE_EXP]; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3140 | int integer_place, fraction_place; |
| 3141 | char *ptr; |
| 3142 | char *after_fraction; |
| 3143 | double absolute_value; |
| 3144 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3145 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3146 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3147 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3148 | /* |
| 3149 | * First choose format - scientific or regular floating point. |
| 3150 | * In either case, result is in work, and after_fraction points |
| 3151 | * just past the fractional part. |
| 3152 | */ |
| 3153 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 3154 | (absolute_value < LOWER_DOUBLE)) && |
| 3155 | (absolute_value != 0.0) ) { |
| 3156 | /* Use scientific notation */ |
| 3157 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 3158 | fraction_place = DBL_DIG - 1; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3159 | size = snprintf(work, sizeof(work),"%*.*e", |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3160 | integer_place, fraction_place, number); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3161 | while ((size > 0) && (work[size] != 'e')) size--; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3162 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3163 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3164 | else { |
| 3165 | /* Use regular notation */ |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 3166 | if (absolute_value > 0.0) { |
| 3167 | integer_place = (int)log10(absolute_value); |
| 3168 | if (integer_place > 0) |
| 3169 | fraction_place = DBL_DIG - integer_place - 1; |
| 3170 | else |
| 3171 | fraction_place = DBL_DIG - integer_place; |
| 3172 | } else { |
| 3173 | fraction_place = 1; |
| 3174 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3175 | size = snprintf(work, sizeof(work), "%0.*f", |
| 3176 | fraction_place, number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Daniel Veillard | b9e4d5b | 2013-07-22 13:21:31 +0800 | [diff] [blame] | 3179 | /* Remove leading spaces sometimes inserted by snprintf */ |
| 3180 | while (work[0] == ' ') { |
| 3181 | for (ptr = &work[0];(ptr[0] = ptr[1]);ptr++); |
| 3182 | size--; |
| 3183 | } |
| 3184 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3185 | /* Remove fractional trailing zeroes */ |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 3186 | after_fraction = work + size; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3187 | ptr = after_fraction; |
| 3188 | while (*(--ptr) == '0') |
| 3189 | ; |
| 3190 | if (*ptr != '.') |
| 3191 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 3192 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 3193 | |
| 3194 | /* Finally copy result back to caller */ |
| 3195 | size = strlen(work) + 1; |
| 3196 | if (size > buffersize) { |
| 3197 | work[buffersize - 1] = 0; |
| 3198 | size = buffersize; |
| 3199 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 3200 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3201 | } |
| 3202 | break; |
| 3203 | } |
| 3204 | } |
| 3205 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3206 | |
| 3207 | /************************************************************************ |
| 3208 | * * |
| 3209 | * Routines to handle NodeSets * |
| 3210 | * * |
| 3211 | ************************************************************************/ |
| 3212 | |
| 3213 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3214 | * xmlXPathOrderDocElems: |
| 3215 | * @doc: an input document |
| 3216 | * |
| 3217 | * Call this routine to speed up XPath computation on static documents. |
| 3218 | * This stamps all the element nodes with the document order |
| 3219 | * Like for line information, the order is kept in the element->content |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3220 | * field, the value stored is actually - the node number (starting at -1) |
| 3221 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3222 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3223 | * Returns the number of elements found in the document or -1 in case |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3224 | * of error. |
| 3225 | */ |
| 3226 | long |
| 3227 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 3228 | long count = 0; |
| 3229 | xmlNodePtr cur; |
| 3230 | |
| 3231 | if (doc == NULL) |
| 3232 | return(-1); |
| 3233 | cur = doc->children; |
| 3234 | while (cur != NULL) { |
| 3235 | if (cur->type == XML_ELEMENT_NODE) { |
| 3236 | cur->content = (void *) (-(++count)); |
| 3237 | if (cur->children != NULL) { |
| 3238 | cur = cur->children; |
| 3239 | continue; |
| 3240 | } |
| 3241 | } |
| 3242 | if (cur->next != NULL) { |
| 3243 | cur = cur->next; |
| 3244 | continue; |
| 3245 | } |
| 3246 | do { |
| 3247 | cur = cur->parent; |
| 3248 | if (cur == NULL) |
| 3249 | break; |
| 3250 | if (cur == (xmlNodePtr) doc) { |
| 3251 | cur = NULL; |
| 3252 | break; |
| 3253 | } |
| 3254 | if (cur->next != NULL) { |
| 3255 | cur = cur->next; |
| 3256 | break; |
| 3257 | } |
| 3258 | } while (cur != NULL); |
| 3259 | } |
| 3260 | return(count); |
| 3261 | } |
| 3262 | |
| 3263 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3264 | * xmlXPathCmpNodes: |
| 3265 | * @node1: the first node |
| 3266 | * @node2: the second node |
| 3267 | * |
| 3268 | * Compare two nodes w.r.t document order |
| 3269 | * |
| 3270 | * Returns -2 in case of error 1 if first point < second point, 0 if |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3271 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3272 | */ |
| 3273 | int |
| 3274 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 3275 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3276 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 3277 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3278 | xmlNodePtr cur, root; |
| 3279 | |
| 3280 | if ((node1 == NULL) || (node2 == NULL)) |
| 3281 | return(-2); |
| 3282 | /* |
| 3283 | * a couple of optimizations which will avoid computations in most cases |
| 3284 | */ |
William M. Brack | ee0b982 | 2007-03-07 08:15:01 +0000 | [diff] [blame] | 3285 | if (node1 == node2) /* trivial case */ |
| 3286 | return(0); |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3287 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 3288 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 3289 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3290 | node1 = node1->parent; |
| 3291 | } |
| 3292 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 3293 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 3294 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3295 | node2 = node2->parent; |
| 3296 | } |
| 3297 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 3298 | if (attr1 == attr2) { |
| 3299 | /* not required, but we keep attributes in order */ |
| 3300 | if (attr1 != 0) { |
| 3301 | cur = attrNode2->prev; |
| 3302 | while (cur != NULL) { |
| 3303 | if (cur == attrNode1) |
| 3304 | return (1); |
| 3305 | cur = cur->prev; |
| 3306 | } |
| 3307 | return (-1); |
| 3308 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3309 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 3310 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 3311 | if (attr2 == 1) |
| 3312 | return(1); |
| 3313 | return(-1); |
| 3314 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3315 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 3316 | (node2->type == XML_NAMESPACE_DECL)) |
| 3317 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3318 | if (node1 == node2->prev) |
| 3319 | return(1); |
| 3320 | if (node1 == node2->next) |
| 3321 | return(-1); |
| 3322 | |
| 3323 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3324 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 3325 | */ |
| 3326 | if ((node1->type == XML_ELEMENT_NODE) && |
| 3327 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3328 | (0 > (long) node1->content) && |
| 3329 | (0 > (long) node2->content) && |
| 3330 | (node1->doc == node2->doc)) { |
| 3331 | long l1, l2; |
| 3332 | |
| 3333 | l1 = -((long) node1->content); |
| 3334 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 3335 | if (l1 < l2) |
| 3336 | return(1); |
| 3337 | if (l1 > l2) |
| 3338 | return(-1); |
| 3339 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 3340 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 3341 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3342 | * compute depth to root |
| 3343 | */ |
| 3344 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 3345 | if (cur == node1) |
| 3346 | return(1); |
| 3347 | depth2++; |
| 3348 | } |
| 3349 | root = cur; |
| 3350 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 3351 | if (cur == node2) |
| 3352 | return(-1); |
| 3353 | depth1++; |
| 3354 | } |
| 3355 | /* |
| 3356 | * Distinct document (or distinct entities :-( ) case. |
| 3357 | */ |
| 3358 | if (root != cur) { |
| 3359 | return(-2); |
| 3360 | } |
| 3361 | /* |
| 3362 | * get the nearest common ancestor. |
| 3363 | */ |
| 3364 | while (depth1 > depth2) { |
| 3365 | depth1--; |
| 3366 | node1 = node1->parent; |
| 3367 | } |
| 3368 | while (depth2 > depth1) { |
| 3369 | depth2--; |
| 3370 | node2 = node2->parent; |
| 3371 | } |
| 3372 | while (node1->parent != node2->parent) { |
| 3373 | node1 = node1->parent; |
| 3374 | node2 = node2->parent; |
| 3375 | /* should not happen but just in case ... */ |
| 3376 | if ((node1 == NULL) || (node2 == NULL)) |
| 3377 | return(-2); |
| 3378 | } |
| 3379 | /* |
| 3380 | * Find who's first. |
| 3381 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 3382 | if (node1 == node2->prev) |
| 3383 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3384 | if (node1 == node2->next) |
| 3385 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 3386 | /* |
| 3387 | * Speedup using document order if availble. |
| 3388 | */ |
| 3389 | if ((node1->type == XML_ELEMENT_NODE) && |
| 3390 | (node2->type == XML_ELEMENT_NODE) && |
| 3391 | (0 > (long) node1->content) && |
| 3392 | (0 > (long) node2->content) && |
| 3393 | (node1->doc == node2->doc)) { |
| 3394 | long l1, l2; |
| 3395 | |
| 3396 | l1 = -((long) node1->content); |
| 3397 | l2 = -((long) node2->content); |
| 3398 | if (l1 < l2) |
| 3399 | return(1); |
| 3400 | if (l1 > l2) |
| 3401 | return(-1); |
| 3402 | } |
| 3403 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3404 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 3405 | if (cur == node2) |
| 3406 | return(1); |
| 3407 | return(-1); /* assume there is no sibling list corruption */ |
| 3408 | } |
| 3409 | |
| 3410 | /** |
| 3411 | * xmlXPathNodeSetSort: |
| 3412 | * @set: the node set |
| 3413 | * |
| 3414 | * Sort the node set in document order |
| 3415 | */ |
| 3416 | void |
| 3417 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 3418 | #ifndef WITH_TIM_SORT |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3419 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3420 | xmlNodePtr tmp; |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 3421 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (set == NULL) |
| 3424 | return; |
| 3425 | |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 3426 | #ifndef WITH_TIM_SORT |
| 3427 | /* |
| 3428 | * Use the old Shell's sort implementation to sort the node-set |
| 3429 | * Timsort ought to be quite faster |
| 3430 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3431 | len = set->nodeNr; |
| 3432 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 3433 | for (i = incr; i < len; i++) { |
| 3434 | j = i - incr; |
| 3435 | while (j >= 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3436 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3437 | if (xmlXPathCmpNodesExt(set->nodeTab[j], |
| 3438 | set->nodeTab[j + incr]) == -1) |
| 3439 | #else |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3440 | if (xmlXPathCmpNodes(set->nodeTab[j], |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3441 | set->nodeTab[j + incr]) == -1) |
| 3442 | #endif |
| 3443 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3444 | tmp = set->nodeTab[j]; |
| 3445 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 3446 | set->nodeTab[j + incr] = tmp; |
| 3447 | j -= incr; |
| 3448 | } else |
| 3449 | break; |
| 3450 | } |
| 3451 | } |
| 3452 | } |
Vojtech Fried | 3e031b7 | 2012-08-24 16:52:44 +0800 | [diff] [blame] | 3453 | #else /* WITH_TIM_SORT */ |
| 3454 | libxml_domnode_tim_sort(set->nodeTab, set->nodeNr); |
| 3455 | #endif /* WITH_TIM_SORT */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | #define XML_NODESET_DEFAULT 10 |
| 3459 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3460 | * xmlXPathNodeSetDupNs: |
| 3461 | * @node: the parent node of the namespace XPath node |
| 3462 | * @ns: the libxml namespace declaration node. |
| 3463 | * |
| 3464 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 3465 | * the namespace nodes are duplicated and the next pointer is set to the |
| 3466 | * parent node in the XPath semantic. |
| 3467 | * |
| 3468 | * Returns the newly created object. |
| 3469 | */ |
| 3470 | static xmlNodePtr |
| 3471 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 3472 | xmlNsPtr cur; |
| 3473 | |
| 3474 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 3475 | return(NULL); |
| 3476 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 3477 | return((xmlNodePtr) ns); |
| 3478 | |
| 3479 | /* |
| 3480 | * Allocate a new Namespace and fill the fields. |
| 3481 | */ |
| 3482 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 3483 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3484 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3485 | return(NULL); |
| 3486 | } |
| 3487 | memset(cur, 0, sizeof(xmlNs)); |
| 3488 | cur->type = XML_NAMESPACE_DECL; |
| 3489 | if (ns->href != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3490 | cur->href = xmlStrdup(ns->href); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3491 | if (ns->prefix != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3492 | cur->prefix = xmlStrdup(ns->prefix); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3493 | cur->next = (xmlNsPtr) node; |
| 3494 | return((xmlNodePtr) cur); |
| 3495 | } |
| 3496 | |
| 3497 | /** |
| 3498 | * xmlXPathNodeSetFreeNs: |
| 3499 | * @ns: the XPath namespace node found in a nodeset. |
| 3500 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3501 | * Namespace nodes in libxml don't match the XPath semantic. In a node set |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3502 | * the namespace nodes are duplicated and the next pointer is set to the |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3503 | * parent node in the XPath semantic. Check if such a node needs to be freed |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3504 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 3505 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3506 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 3507 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 3508 | return; |
| 3509 | |
| 3510 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 3511 | if (ns->href != NULL) |
| 3512 | xmlFree((xmlChar *)ns->href); |
| 3513 | if (ns->prefix != NULL) |
| 3514 | xmlFree((xmlChar *)ns->prefix); |
| 3515 | xmlFree(ns); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3520 | * xmlXPathNodeSetCreate: |
| 3521 | * @val: an initial xmlNodePtr, or NULL |
| 3522 | * |
| 3523 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 3524 | * |
| 3525 | * Returns the newly created object. |
| 3526 | */ |
| 3527 | xmlNodeSetPtr |
| 3528 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 3529 | xmlNodeSetPtr ret; |
| 3530 | |
| 3531 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 3532 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3533 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3534 | return(NULL); |
| 3535 | } |
| 3536 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 3537 | if (val != NULL) { |
| 3538 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3539 | sizeof(xmlNodePtr)); |
| 3540 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3541 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3542 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3543 | return(NULL); |
| 3544 | } |
| 3545 | memset(ret->nodeTab, 0 , |
| 3546 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3547 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3548 | if (val->type == XML_NAMESPACE_DECL) { |
| 3549 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3550 | |
| 3551 | ret->nodeTab[ret->nodeNr++] = |
| 3552 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3553 | } else |
| 3554 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3555 | } |
| 3556 | return(ret); |
| 3557 | } |
| 3558 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3559 | /** |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3560 | * xmlXPathNodeSetCreateSize: |
| 3561 | * @size: the initial size of the set |
| 3562 | * |
| 3563 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 3564 | * |
| 3565 | * Returns the newly created object. |
| 3566 | */ |
| 3567 | static xmlNodeSetPtr |
| 3568 | xmlXPathNodeSetCreateSize(int size) { |
| 3569 | xmlNodeSetPtr ret; |
| 3570 | |
| 3571 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 3572 | if (ret == NULL) { |
| 3573 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3574 | return(NULL); |
| 3575 | } |
| 3576 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 3577 | if (size < XML_NODESET_DEFAULT) |
| 3578 | size = XML_NODESET_DEFAULT; |
| 3579 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(size * sizeof(xmlNodePtr)); |
| 3580 | if (ret->nodeTab == NULL) { |
| 3581 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3582 | xmlFree(ret); |
| 3583 | return(NULL); |
| 3584 | } |
| 3585 | memset(ret->nodeTab, 0 , size * (size_t) sizeof(xmlNodePtr)); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3586 | ret->nodeMax = size; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3587 | return(ret); |
| 3588 | } |
| 3589 | |
| 3590 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3591 | * xmlXPathNodeSetContains: |
| 3592 | * @cur: the node-set |
| 3593 | * @val: the node |
| 3594 | * |
| 3595 | * checks whether @cur contains @val |
| 3596 | * |
| 3597 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 3598 | */ |
| 3599 | int |
| 3600 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 3601 | int i; |
| 3602 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3603 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3604 | if (val->type == XML_NAMESPACE_DECL) { |
| 3605 | for (i = 0; i < cur->nodeNr; i++) { |
| 3606 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 3607 | xmlNsPtr ns1, ns2; |
| 3608 | |
| 3609 | ns1 = (xmlNsPtr) val; |
| 3610 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 3611 | if (ns1 == ns2) |
| 3612 | return(1); |
| 3613 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 3614 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 3615 | return(1); |
| 3616 | } |
| 3617 | } |
| 3618 | } else { |
| 3619 | for (i = 0; i < cur->nodeNr; i++) { |
| 3620 | if (cur->nodeTab[i] == val) |
| 3621 | return(1); |
| 3622 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3623 | } |
| 3624 | return(0); |
| 3625 | } |
| 3626 | |
| 3627 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3628 | * xmlXPathNodeSetAddNs: |
| 3629 | * @cur: the initial node set |
| 3630 | * @node: the hosting node |
| 3631 | * @ns: a the namespace node |
| 3632 | * |
| 3633 | * add a new namespace node to an existing NodeSet |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3634 | * |
| 3635 | * Returns 0 in case of success and -1 in case of error |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3636 | */ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3637 | int |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3638 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 3639 | int i; |
| 3640 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3641 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3642 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 3643 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3644 | (node->type != XML_ELEMENT_NODE)) |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3645 | return(-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3646 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3647 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3648 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3649 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3650 | */ |
| 3651 | for (i = 0;i < cur->nodeNr;i++) { |
| 3652 | if ((cur->nodeTab[i] != NULL) && |
| 3653 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 3654 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3655 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3656 | return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | /* |
| 3660 | * grow the nodeTab if needed |
| 3661 | */ |
| 3662 | if (cur->nodeMax == 0) { |
| 3663 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3664 | sizeof(xmlNodePtr)); |
| 3665 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3666 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3667 | return(-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3668 | } |
| 3669 | memset(cur->nodeTab, 0 , |
| 3670 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3671 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3672 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3673 | xmlNodePtr *temp; |
| 3674 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3675 | if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 3676 | xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3677 | return(-1); |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3678 | } |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3679 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3680 | sizeof(xmlNodePtr)); |
| 3681 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3682 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3683 | return(-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3684 | } |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3685 | cur->nodeMax *= 2; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3686 | cur->nodeTab = temp; |
| 3687 | } |
| 3688 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3689 | return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3693 | * xmlXPathNodeSetAdd: |
| 3694 | * @cur: the initial node set |
| 3695 | * @val: a new xmlNodePtr |
| 3696 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3697 | * add a new xmlNodePtr to an existing NodeSet |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3698 | * |
| 3699 | * Returns 0 in case of success, and -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3700 | */ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3701 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3702 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 3703 | int i; |
| 3704 | |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3705 | if ((cur == NULL) || (val == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3706 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3707 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3708 | /* |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 3709 | * prevent duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3710 | */ |
| 3711 | for (i = 0;i < cur->nodeNr;i++) |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3712 | if (cur->nodeTab[i] == val) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* |
| 3715 | * grow the nodeTab if needed |
| 3716 | */ |
| 3717 | if (cur->nodeMax == 0) { |
| 3718 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3719 | sizeof(xmlNodePtr)); |
| 3720 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3721 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3722 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3723 | } |
| 3724 | memset(cur->nodeTab, 0 , |
| 3725 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3726 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3727 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3728 | xmlNodePtr *temp; |
| 3729 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3730 | if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 3731 | xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3732 | return(-1); |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3733 | } |
Daniel Veillard | 0cbeb50 | 2010-11-15 12:06:29 +0100 | [diff] [blame] | 3734 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3735 | sizeof(xmlNodePtr)); |
| 3736 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3737 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3738 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3739 | } |
Daniel Veillard | 0cbeb50 | 2010-11-15 12:06:29 +0100 | [diff] [blame] | 3740 | cur->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3741 | cur->nodeTab = temp; |
| 3742 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3743 | if (val->type == XML_NAMESPACE_DECL) { |
| 3744 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3745 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3746 | cur->nodeTab[cur->nodeNr++] = |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3747 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3748 | } else |
| 3749 | cur->nodeTab[cur->nodeNr++] = val; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3750 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | /** |
| 3754 | * xmlXPathNodeSetAddUnique: |
| 3755 | * @cur: the initial node set |
| 3756 | * @val: a new xmlNodePtr |
| 3757 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3758 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3759 | * when we are sure the node is not already in the set. |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3760 | * |
| 3761 | * Returns 0 in case of success and -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3762 | */ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3763 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3764 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3765 | if ((cur == NULL) || (val == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3766 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3767 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3768 | /* |
| 3769 | * grow the nodeTab if needed |
| 3770 | */ |
| 3771 | if (cur->nodeMax == 0) { |
| 3772 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3773 | sizeof(xmlNodePtr)); |
| 3774 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3775 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3776 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3777 | } |
| 3778 | memset(cur->nodeTab, 0 , |
| 3779 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3780 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3781 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3782 | xmlNodePtr *temp; |
| 3783 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3784 | if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 3785 | xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3786 | return(-1); |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3787 | } |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3788 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3789 | sizeof(xmlNodePtr)); |
| 3790 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3791 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3792 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3793 | } |
| 3794 | cur->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3795 | cur->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3796 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3797 | if (val->type == XML_NAMESPACE_DECL) { |
| 3798 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3799 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3800 | cur->nodeTab[cur->nodeNr++] = |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3801 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3802 | } else |
| 3803 | cur->nodeTab[cur->nodeNr++] = val; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 3804 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | /** |
| 3808 | * xmlXPathNodeSetMerge: |
| 3809 | * @val1: the first NodeSet or NULL |
| 3810 | * @val2: the second NodeSet |
| 3811 | * |
| 3812 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 3813 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 3814 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3815 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3816 | */ |
| 3817 | xmlNodeSetPtr |
| 3818 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3819 | int i, j, initNr, skip; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3820 | xmlNodePtr n1, n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3821 | |
| 3822 | if (val2 == NULL) return(val1); |
| 3823 | if (val1 == NULL) { |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame] | 3824 | val1 = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 3825 | if (val1 == NULL) |
| 3826 | return (NULL); |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame] | 3827 | #if 0 |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3828 | /* |
| 3829 | * TODO: The optimization won't work in every case, since |
| 3830 | * those nasty namespace nodes need to be added with |
| 3831 | * xmlXPathNodeSetDupNs() to the set; thus a pure |
| 3832 | * memcpy is not possible. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 3833 | * If there was a flag on the nodesetval, indicating that |
| 3834 | * some temporary nodes are in, that would be helpfull. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3835 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3836 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3837 | * Optimization: Create an equally sized node-set |
| 3838 | * and memcpy the content. |
| 3839 | */ |
| 3840 | val1 = xmlXPathNodeSetCreateSize(val2->nodeNr); |
| 3841 | if (val1 == NULL) |
| 3842 | return(NULL); |
| 3843 | if (val2->nodeNr != 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3844 | if (val2->nodeNr == 1) |
| 3845 | *(val1->nodeTab) = *(val2->nodeTab); |
| 3846 | else { |
| 3847 | memcpy(val1->nodeTab, val2->nodeTab, |
| 3848 | val2->nodeNr * sizeof(xmlNodePtr)); |
| 3849 | } |
| 3850 | val1->nodeNr = val2->nodeNr; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3851 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3852 | return(val1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3853 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3856 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3857 | initNr = val1->nodeNr; |
| 3858 | |
| 3859 | for (i = 0;i < val2->nodeNr;i++) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3860 | n2 = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3862 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3863 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3864 | skip = 0; |
| 3865 | for (j = 0; j < initNr; j++) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3866 | n1 = val1->nodeTab[j]; |
| 3867 | if (n1 == n2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3868 | skip = 1; |
| 3869 | break; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3870 | } else if ((n1->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3871 | (n2->type == XML_NAMESPACE_DECL)) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3872 | if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) && |
| 3873 | (xmlStrEqual(((xmlNsPtr) n1)->prefix, |
| 3874 | ((xmlNsPtr) n2)->prefix))) |
| 3875 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3876 | skip = 1; |
| 3877 | break; |
| 3878 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | if (skip) |
| 3882 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | |
| 3884 | /* |
| 3885 | * grow the nodeTab if needed |
| 3886 | */ |
| 3887 | if (val1->nodeMax == 0) { |
| 3888 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3889 | sizeof(xmlNodePtr)); |
| 3890 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3891 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3892 | return(NULL); |
| 3893 | } |
| 3894 | memset(val1->nodeTab, 0 , |
| 3895 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3896 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 3897 | } else if (val1->nodeNr == val1->nodeMax) { |
| 3898 | xmlNodePtr *temp; |
| 3899 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 3900 | if (val1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 3901 | xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); |
| 3902 | return(NULL); |
| 3903 | } |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3904 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3905 | sizeof(xmlNodePtr)); |
| 3906 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3907 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3908 | return(NULL); |
| 3909 | } |
| 3910 | val1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3911 | val1->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3912 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3913 | if (n2->type == XML_NAMESPACE_DECL) { |
| 3914 | xmlNsPtr ns = (xmlNsPtr) n2; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3915 | |
| 3916 | val1->nodeTab[val1->nodeNr++] = |
| 3917 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3918 | } else |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3919 | val1->nodeTab[val1->nodeNr++] = n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | return(val1); |
| 3923 | } |
| 3924 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3925 | |
| 3926 | /** |
| 3927 | * xmlXPathNodeSetMergeAndClear: |
| 3928 | * @set1: the first NodeSet or NULL |
| 3929 | * @set2: the second NodeSet |
| 3930 | * @hasSet2NsNodes: 1 if set2 contains namespaces nodes |
| 3931 | * |
| 3932 | * Merges two nodesets, all nodes from @set2 are added to @set1 |
| 3933 | * if @set1 is NULL, a new set is created and copied from @set2. |
| 3934 | * Checks for duplicate nodes. Clears set2. |
| 3935 | * |
| 3936 | * Returns @set1 once extended or NULL in case of error. |
| 3937 | */ |
| 3938 | static xmlNodeSetPtr |
| 3939 | xmlXPathNodeSetMergeAndClear(xmlNodeSetPtr set1, xmlNodeSetPtr set2, |
| 3940 | int hasNullEntries) |
| 3941 | { |
| 3942 | if ((set1 == NULL) && (hasNullEntries == 0)) { |
| 3943 | /* |
| 3944 | * Note that doing a memcpy of the list, namespace nodes are |
| 3945 | * just assigned to set1, since set2 is cleared anyway. |
| 3946 | */ |
| 3947 | set1 = xmlXPathNodeSetCreateSize(set2->nodeNr); |
| 3948 | if (set1 == NULL) |
| 3949 | return(NULL); |
| 3950 | if (set2->nodeNr != 0) { |
| 3951 | memcpy(set1->nodeTab, set2->nodeTab, |
| 3952 | set2->nodeNr * sizeof(xmlNodePtr)); |
| 3953 | set1->nodeNr = set2->nodeNr; |
| 3954 | } |
| 3955 | } else { |
| 3956 | int i, j, initNbSet1; |
| 3957 | xmlNodePtr n1, n2; |
| 3958 | |
| 3959 | if (set1 == NULL) |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 3960 | set1 = xmlXPathNodeSetCreate(NULL); |
| 3961 | if (set1 == NULL) |
| 3962 | return (NULL); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3963 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3964 | initNbSet1 = set1->nodeNr; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3965 | for (i = 0;i < set2->nodeNr;i++) { |
| 3966 | n2 = set2->nodeTab[i]; |
| 3967 | /* |
| 3968 | * Skip NULLed entries. |
| 3969 | */ |
| 3970 | if (n2 == NULL) |
| 3971 | continue; |
| 3972 | /* |
| 3973 | * Skip duplicates. |
| 3974 | */ |
| 3975 | for (j = 0; j < initNbSet1; j++) { |
| 3976 | n1 = set1->nodeTab[j]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3977 | if (n1 == n2) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3978 | goto skip_node; |
| 3979 | } else if ((n1->type == XML_NAMESPACE_DECL) && |
| 3980 | (n2->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3981 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3982 | if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) && |
| 3983 | (xmlStrEqual(((xmlNsPtr) n1)->prefix, |
| 3984 | ((xmlNsPtr) n2)->prefix))) |
| 3985 | { |
| 3986 | /* |
| 3987 | * Free the namespace node. |
| 3988 | */ |
| 3989 | set2->nodeTab[i] = NULL; |
| 3990 | xmlXPathNodeSetFreeNs((xmlNsPtr) n2); |
| 3991 | goto skip_node; |
| 3992 | } |
| 3993 | } |
| 3994 | } |
| 3995 | /* |
| 3996 | * grow the nodeTab if needed |
| 3997 | */ |
| 3998 | if (set1->nodeMax == 0) { |
| 3999 | set1->nodeTab = (xmlNodePtr *) xmlMalloc( |
| 4000 | XML_NODESET_DEFAULT * sizeof(xmlNodePtr)); |
| 4001 | if (set1->nodeTab == NULL) { |
| 4002 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4003 | return(NULL); |
| 4004 | } |
| 4005 | memset(set1->nodeTab, 0, |
| 4006 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 4007 | set1->nodeMax = XML_NODESET_DEFAULT; |
| 4008 | } else if (set1->nodeNr >= set1->nodeMax) { |
| 4009 | xmlNodePtr *temp; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4010 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 4011 | if (set1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 4012 | xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); |
| 4013 | return(NULL); |
| 4014 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4015 | temp = (xmlNodePtr *) xmlRealloc( |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4016 | set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4017 | if (temp == NULL) { |
| 4018 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4019 | return(NULL); |
| 4020 | } |
| 4021 | set1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4022 | set1->nodeMax *= 2; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4023 | } |
| 4024 | if (n2->type == XML_NAMESPACE_DECL) { |
| 4025 | xmlNsPtr ns = (xmlNsPtr) n2; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4026 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4027 | set1->nodeTab[set1->nodeNr++] = |
| 4028 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 4029 | } else |
| 4030 | set1->nodeTab[set1->nodeNr++] = n2; |
| 4031 | skip_node: |
| 4032 | {} |
| 4033 | } |
| 4034 | } |
| 4035 | set2->nodeNr = 0; |
| 4036 | return(set1); |
| 4037 | } |
| 4038 | |
| 4039 | /** |
| 4040 | * xmlXPathNodeSetMergeAndClearNoDupls: |
| 4041 | * @set1: the first NodeSet or NULL |
| 4042 | * @set2: the second NodeSet |
| 4043 | * @hasSet2NsNodes: 1 if set2 contains namespaces nodes |
| 4044 | * |
| 4045 | * Merges two nodesets, all nodes from @set2 are added to @set1 |
| 4046 | * if @set1 is NULL, a new set is created and copied from @set2. |
| 4047 | * Doesn't chack for duplicate nodes. Clears set2. |
| 4048 | * |
| 4049 | * Returns @set1 once extended or NULL in case of error. |
| 4050 | */ |
| 4051 | static xmlNodeSetPtr |
| 4052 | xmlXPathNodeSetMergeAndClearNoDupls(xmlNodeSetPtr set1, xmlNodeSetPtr set2, |
| 4053 | int hasNullEntries) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4054 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4055 | if (set2 == NULL) |
| 4056 | return(set1); |
| 4057 | if ((set1 == NULL) && (hasNullEntries == 0)) { |
| 4058 | /* |
| 4059 | * Note that doing a memcpy of the list, namespace nodes are |
| 4060 | * just assigned to set1, since set2 is cleared anyway. |
| 4061 | */ |
| 4062 | set1 = xmlXPathNodeSetCreateSize(set2->nodeNr); |
| 4063 | if (set1 == NULL) |
| 4064 | return(NULL); |
| 4065 | if (set2->nodeNr != 0) { |
| 4066 | memcpy(set1->nodeTab, set2->nodeTab, |
| 4067 | set2->nodeNr * sizeof(xmlNodePtr)); |
| 4068 | set1->nodeNr = set2->nodeNr; |
| 4069 | } |
| 4070 | } else { |
| 4071 | int i; |
| 4072 | xmlNodePtr n2; |
| 4073 | |
| 4074 | if (set1 == NULL) |
| 4075 | set1 = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4076 | if (set1 == NULL) |
| 4077 | return (NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4078 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4079 | for (i = 0;i < set2->nodeNr;i++) { |
| 4080 | n2 = set2->nodeTab[i]; |
| 4081 | /* |
| 4082 | * Skip NULLed entries. |
| 4083 | */ |
| 4084 | if (n2 == NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4085 | continue; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4086 | if (set1->nodeMax == 0) { |
| 4087 | set1->nodeTab = (xmlNodePtr *) xmlMalloc( |
| 4088 | XML_NODESET_DEFAULT * sizeof(xmlNodePtr)); |
| 4089 | if (set1->nodeTab == NULL) { |
| 4090 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4091 | return(NULL); |
| 4092 | } |
| 4093 | memset(set1->nodeTab, 0, |
| 4094 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 4095 | set1->nodeMax = XML_NODESET_DEFAULT; |
| 4096 | } else if (set1->nodeNr >= set1->nodeMax) { |
| 4097 | xmlNodePtr *temp; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4098 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 4099 | if (set1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { |
| 4100 | xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); |
| 4101 | return(NULL); |
| 4102 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4103 | temp = (xmlNodePtr *) xmlRealloc( |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4104 | set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4105 | if (temp == NULL) { |
| 4106 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4107 | return(NULL); |
| 4108 | } |
| 4109 | set1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4110 | set1->nodeMax *= 2; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4111 | } |
| 4112 | set1->nodeTab[set1->nodeNr++] = n2; |
| 4113 | } |
| 4114 | } |
| 4115 | set2->nodeNr = 0; |
| 4116 | return(set1); |
| 4117 | } |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 4118 | |
| 4119 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4120 | * xmlXPathNodeSetDel: |
| 4121 | * @cur: the initial node set |
| 4122 | * @val: an xmlNodePtr |
| 4123 | * |
| 4124 | * Removes an xmlNodePtr from an existing NodeSet |
| 4125 | */ |
| 4126 | void |
| 4127 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 4128 | int i; |
| 4129 | |
| 4130 | if (cur == NULL) return; |
| 4131 | if (val == NULL) return; |
| 4132 | |
| 4133 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4134 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4135 | */ |
| 4136 | for (i = 0;i < cur->nodeNr;i++) |
| 4137 | if (cur->nodeTab[i] == val) break; |
| 4138 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4139 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4140 | #ifdef DEBUG |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4141 | xmlGenericError(xmlGenericErrorContext, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4142 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 4143 | val->name); |
| 4144 | #endif |
| 4145 | return; |
| 4146 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4147 | if ((cur->nodeTab[i] != NULL) && |
| 4148 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 4149 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4150 | cur->nodeNr--; |
| 4151 | for (;i < cur->nodeNr;i++) |
| 4152 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 4153 | cur->nodeTab[cur->nodeNr] = NULL; |
| 4154 | } |
| 4155 | |
| 4156 | /** |
| 4157 | * xmlXPathNodeSetRemove: |
| 4158 | * @cur: the initial node set |
| 4159 | * @val: the index to remove |
| 4160 | * |
| 4161 | * Removes an entry from an existing NodeSet list. |
| 4162 | */ |
| 4163 | void |
| 4164 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 4165 | if (cur == NULL) return; |
| 4166 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4167 | if ((cur->nodeTab[val] != NULL) && |
| 4168 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 4169 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4170 | cur->nodeNr--; |
| 4171 | for (;val < cur->nodeNr;val++) |
| 4172 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 4173 | cur->nodeTab[cur->nodeNr] = NULL; |
| 4174 | } |
| 4175 | |
| 4176 | /** |
| 4177 | * xmlXPathFreeNodeSet: |
| 4178 | * @obj: the xmlNodeSetPtr to free |
| 4179 | * |
| 4180 | * Free the NodeSet compound (not the actual nodes !). |
| 4181 | */ |
| 4182 | void |
| 4183 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 4184 | if (obj == NULL) return; |
| 4185 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4186 | int i; |
| 4187 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4188 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4189 | for (i = 0;i < obj->nodeNr;i++) |
| 4190 | if ((obj->nodeTab[i] != NULL) && |
| 4191 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 4192 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4193 | xmlFree(obj->nodeTab); |
| 4194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4195 | xmlFree(obj); |
| 4196 | } |
| 4197 | |
| 4198 | /** |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4199 | * xmlXPathNodeSetClear: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4200 | * @set: the node set to clear |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4201 | * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4202 | * Clears the list from all temporary XPath objects (e.g. namespace nodes |
| 4203 | * are feed), but does *not* free the list itself. Sets the length of the |
| 4204 | * list to 0. |
| 4205 | */ |
| 4206 | static void |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4207 | xmlXPathNodeSetClear(xmlNodeSetPtr set, int hasNsNodes) |
| 4208 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4209 | if ((set == NULL) || (set->nodeNr <= 0)) |
| 4210 | return; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4211 | else if (hasNsNodes) { |
| 4212 | int i; |
| 4213 | xmlNodePtr node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4214 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4215 | for (i = 0; i < set->nodeNr; i++) { |
| 4216 | node = set->nodeTab[i]; |
| 4217 | if ((node != NULL) && |
| 4218 | (node->type == XML_NAMESPACE_DECL)) |
| 4219 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4220 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4221 | } |
| 4222 | set->nodeNr = 0; |
| 4223 | } |
| 4224 | |
| 4225 | /** |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4226 | * xmlXPathNodeSetClearFromPos: |
| 4227 | * @set: the node set to be cleared |
| 4228 | * @pos: the start position to clear from |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4229 | * |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4230 | * Clears the list from temporary XPath objects (e.g. namespace nodes |
| 4231 | * are feed) starting with the entry at @pos, but does *not* free the list |
| 4232 | * itself. Sets the length of the list to @pos. |
| 4233 | */ |
| 4234 | static void |
| 4235 | xmlXPathNodeSetClearFromPos(xmlNodeSetPtr set, int pos, int hasNsNodes) |
| 4236 | { |
| 4237 | if ((set == NULL) || (set->nodeNr <= 0) || (pos >= set->nodeNr)) |
| 4238 | return; |
| 4239 | else if ((hasNsNodes)) { |
| 4240 | int i; |
| 4241 | xmlNodePtr node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4242 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4243 | for (i = pos; i < set->nodeNr; i++) { |
| 4244 | node = set->nodeTab[i]; |
| 4245 | if ((node != NULL) && |
| 4246 | (node->type == XML_NAMESPACE_DECL)) |
| 4247 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4248 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4249 | } |
| 4250 | set->nodeNr = pos; |
| 4251 | } |
| 4252 | |
| 4253 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4254 | * xmlXPathFreeValueTree: |
| 4255 | * @obj: the xmlNodeSetPtr to free |
| 4256 | * |
| 4257 | * Free the NodeSet compound and the actual tree, this is different |
| 4258 | * from xmlXPathFreeNodeSet() |
| 4259 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4260 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4261 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 4262 | int i; |
| 4263 | |
| 4264 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4265 | |
| 4266 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4267 | for (i = 0;i < obj->nodeNr;i++) { |
| 4268 | if (obj->nodeTab[i] != NULL) { |
| 4269 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 4270 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 4271 | } else { |
| 4272 | xmlFreeNodeList(obj->nodeTab[i]); |
| 4273 | } |
| 4274 | } |
| 4275 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4276 | xmlFree(obj->nodeTab); |
| 4277 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4278 | xmlFree(obj); |
| 4279 | } |
| 4280 | |
| 4281 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 4282 | /** |
| 4283 | * xmlGenericErrorContextNodeSet: |
| 4284 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4285 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4286 | * |
| 4287 | * Quick display of a NodeSet |
| 4288 | */ |
| 4289 | void |
| 4290 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 4291 | int i; |
| 4292 | |
| 4293 | if (output == NULL) output = xmlGenericErrorContext; |
| 4294 | if (obj == NULL) { |
| 4295 | fprintf(output, "NodeSet == NULL !\n"); |
| 4296 | return; |
| 4297 | } |
| 4298 | if (obj->nodeNr == 0) { |
| 4299 | fprintf(output, "NodeSet is empty\n"); |
| 4300 | return; |
| 4301 | } |
| 4302 | if (obj->nodeTab == NULL) { |
| 4303 | fprintf(output, " nodeTab == NULL !\n"); |
| 4304 | return; |
| 4305 | } |
| 4306 | for (i = 0; i < obj->nodeNr; i++) { |
| 4307 | if (obj->nodeTab[i] == NULL) { |
| 4308 | fprintf(output, " NULL !\n"); |
| 4309 | return; |
| 4310 | } |
| 4311 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 4312 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 4313 | fprintf(output, " /"); |
| 4314 | else if (obj->nodeTab[i]->name == NULL) |
| 4315 | fprintf(output, " noname!"); |
| 4316 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 4317 | } |
| 4318 | fprintf(output, "\n"); |
| 4319 | } |
| 4320 | #endif |
| 4321 | |
| 4322 | /** |
| 4323 | * xmlXPathNewNodeSet: |
| 4324 | * @val: the NodePtr value |
| 4325 | * |
| 4326 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 4327 | * it with the single Node @val |
| 4328 | * |
| 4329 | * Returns the newly created object. |
| 4330 | */ |
| 4331 | xmlXPathObjectPtr |
| 4332 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 4333 | xmlXPathObjectPtr ret; |
| 4334 | |
| 4335 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4336 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4337 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4338 | return(NULL); |
| 4339 | } |
| 4340 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4341 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 4342 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4343 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4344 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4345 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4346 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET); |
| 4347 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4348 | return(ret); |
| 4349 | } |
| 4350 | |
| 4351 | /** |
| 4352 | * xmlXPathNewValueTree: |
| 4353 | * @val: the NodePtr value |
| 4354 | * |
| 4355 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 4356 | * it with the tree root @val |
| 4357 | * |
| 4358 | * Returns the newly created object. |
| 4359 | */ |
| 4360 | xmlXPathObjectPtr |
| 4361 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 4362 | xmlXPathObjectPtr ret; |
| 4363 | |
| 4364 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4365 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4366 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4367 | return(NULL); |
| 4368 | } |
| 4369 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4370 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 4371 | ret->boolval = 1; |
| 4372 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4374 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4375 | xmlXPathDebugObjUsageRequested(NULL, XPATH_XSLT_TREE); |
| 4376 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4377 | return(ret); |
| 4378 | } |
| 4379 | |
| 4380 | /** |
| 4381 | * xmlXPathNewNodeSetList: |
| 4382 | * @val: an existing NodeSet |
| 4383 | * |
| 4384 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 4385 | * it with the Nodeset @val |
| 4386 | * |
| 4387 | * Returns the newly created object. |
| 4388 | */ |
| 4389 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4390 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 4391 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4392 | xmlXPathObjectPtr ret; |
| 4393 | int i; |
| 4394 | |
| 4395 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4396 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4397 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4398 | ret = xmlXPathNewNodeSet(NULL); |
| 4399 | else { |
| 4400 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4401 | if (ret) { |
| 4402 | for (i = 1; i < val->nodeNr; ++i) { |
| 4403 | if (xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]) |
| 4404 | < 0) break; |
| 4405 | } |
| 4406 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4407 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4408 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4409 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | /** |
| 4413 | * xmlXPathWrapNodeSet: |
| 4414 | * @val: the NodePtr value |
| 4415 | * |
| 4416 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 4417 | * |
| 4418 | * Returns the newly created object. |
| 4419 | */ |
| 4420 | xmlXPathObjectPtr |
| 4421 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 4422 | xmlXPathObjectPtr ret; |
| 4423 | |
| 4424 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4425 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4426 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4427 | return(NULL); |
| 4428 | } |
| 4429 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4430 | ret->type = XPATH_NODESET; |
| 4431 | ret->nodesetval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4432 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4433 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET); |
| 4434 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4435 | return(ret); |
| 4436 | } |
| 4437 | |
| 4438 | /** |
| 4439 | * xmlXPathFreeNodeSetList: |
| 4440 | * @obj: an existing NodeSetList object |
| 4441 | * |
| 4442 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 4443 | * the list contrary to xmlXPathFreeObject(). |
| 4444 | */ |
| 4445 | void |
| 4446 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 4447 | if (obj == NULL) return; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4448 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4449 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 4450 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4451 | xmlFree(obj); |
| 4452 | } |
| 4453 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4454 | /** |
| 4455 | * xmlXPathDifference: |
| 4456 | * @nodes1: a node-set |
| 4457 | * @nodes2: a node-set |
| 4458 | * |
| 4459 | * Implements the EXSLT - Sets difference() function: |
| 4460 | * node-set set:difference (node-set, node-set) |
| 4461 | * |
| 4462 | * Returns the difference between the two node sets, or nodes1 if |
| 4463 | * nodes2 is empty |
| 4464 | */ |
| 4465 | xmlNodeSetPtr |
| 4466 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4467 | xmlNodeSetPtr ret; |
| 4468 | int i, l1; |
| 4469 | xmlNodePtr cur; |
| 4470 | |
| 4471 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4472 | return(nodes1); |
| 4473 | |
| 4474 | ret = xmlXPathNodeSetCreate(NULL); |
| 4475 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4476 | return(ret); |
| 4477 | |
| 4478 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 4479 | |
| 4480 | for (i = 0; i < l1; i++) { |
| 4481 | cur = xmlXPathNodeSetItem(nodes1, i); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4482 | if (!xmlXPathNodeSetContains(nodes2, cur)) { |
| 4483 | if (xmlXPathNodeSetAddUnique(ret, cur) < 0) |
| 4484 | break; |
| 4485 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4486 | } |
| 4487 | return(ret); |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * xmlXPathIntersection: |
| 4492 | * @nodes1: a node-set |
| 4493 | * @nodes2: a node-set |
| 4494 | * |
| 4495 | * Implements the EXSLT - Sets intersection() function: |
| 4496 | * node-set set:intersection (node-set, node-set) |
| 4497 | * |
| 4498 | * Returns a node set comprising the nodes that are within both the |
| 4499 | * node sets passed as arguments |
| 4500 | */ |
| 4501 | xmlNodeSetPtr |
| 4502 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4503 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 4504 | int i, l1; |
| 4505 | xmlNodePtr cur; |
| 4506 | |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4507 | if (ret == NULL) |
| 4508 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4509 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4510 | return(ret); |
| 4511 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4512 | return(ret); |
| 4513 | |
| 4514 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 4515 | |
| 4516 | for (i = 0; i < l1; i++) { |
| 4517 | cur = xmlXPathNodeSetItem(nodes1, i); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4518 | if (xmlXPathNodeSetContains(nodes2, cur)) { |
| 4519 | if (xmlXPathNodeSetAddUnique(ret, cur) < 0) |
| 4520 | break; |
| 4521 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4522 | } |
| 4523 | return(ret); |
| 4524 | } |
| 4525 | |
| 4526 | /** |
| 4527 | * xmlXPathDistinctSorted: |
| 4528 | * @nodes: a node-set, sorted by document order |
| 4529 | * |
| 4530 | * Implements the EXSLT - Sets distinct() function: |
| 4531 | * node-set set:distinct (node-set) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4532 | * |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4533 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 4534 | * it is empty |
| 4535 | */ |
| 4536 | xmlNodeSetPtr |
| 4537 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 4538 | xmlNodeSetPtr ret; |
| 4539 | xmlHashTablePtr hash; |
| 4540 | int i, l; |
| 4541 | xmlChar * strval; |
| 4542 | xmlNodePtr cur; |
| 4543 | |
| 4544 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 4545 | return(nodes); |
| 4546 | |
| 4547 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4548 | if (ret == NULL) |
| 4549 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4550 | l = xmlXPathNodeSetGetLength(nodes); |
| 4551 | hash = xmlHashCreate (l); |
| 4552 | for (i = 0; i < l; i++) { |
| 4553 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4554 | strval = xmlXPathCastNodeToString(cur); |
| 4555 | if (xmlHashLookup(hash, strval) == NULL) { |
| 4556 | xmlHashAddEntry(hash, strval, strval); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4557 | if (xmlXPathNodeSetAddUnique(ret, cur) < 0) |
| 4558 | break; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4559 | } else { |
| 4560 | xmlFree(strval); |
| 4561 | } |
| 4562 | } |
| 4563 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 4564 | return(ret); |
| 4565 | } |
| 4566 | |
| 4567 | /** |
| 4568 | * xmlXPathDistinct: |
| 4569 | * @nodes: a node-set |
| 4570 | * |
| 4571 | * Implements the EXSLT - Sets distinct() function: |
| 4572 | * node-set set:distinct (node-set) |
| 4573 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 4574 | * is called with the sorted node-set |
| 4575 | * |
| 4576 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 4577 | * it is empty |
| 4578 | */ |
| 4579 | xmlNodeSetPtr |
| 4580 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 4581 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 4582 | return(nodes); |
| 4583 | |
| 4584 | xmlXPathNodeSetSort(nodes); |
| 4585 | return(xmlXPathDistinctSorted(nodes)); |
| 4586 | } |
| 4587 | |
| 4588 | /** |
| 4589 | * xmlXPathHasSameNodes: |
| 4590 | * @nodes1: a node-set |
| 4591 | * @nodes2: a node-set |
| 4592 | * |
| 4593 | * Implements the EXSLT - Sets has-same-nodes function: |
| 4594 | * boolean set:has-same-node(node-set, node-set) |
| 4595 | * |
| 4596 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 4597 | * otherwise |
| 4598 | */ |
| 4599 | int |
| 4600 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4601 | int i, l; |
| 4602 | xmlNodePtr cur; |
| 4603 | |
| 4604 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 4605 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 4606 | return(0); |
| 4607 | |
| 4608 | l = xmlXPathNodeSetGetLength(nodes1); |
| 4609 | for (i = 0; i < l; i++) { |
| 4610 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 4611 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 4612 | return(1); |
| 4613 | } |
| 4614 | return(0); |
| 4615 | } |
| 4616 | |
| 4617 | /** |
| 4618 | * xmlXPathNodeLeadingSorted: |
| 4619 | * @nodes: a node-set, sorted by document order |
| 4620 | * @node: a node |
| 4621 | * |
| 4622 | * Implements the EXSLT - Sets leading() function: |
| 4623 | * node-set set:leading (node-set, node-set) |
| 4624 | * |
| 4625 | * Returns the nodes in @nodes that precede @node in document order, |
| 4626 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4627 | * doesn't contain @node |
| 4628 | */ |
| 4629 | xmlNodeSetPtr |
| 4630 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4631 | int i, l; |
| 4632 | xmlNodePtr cur; |
| 4633 | xmlNodeSetPtr ret; |
| 4634 | |
| 4635 | if (node == NULL) |
| 4636 | return(nodes); |
| 4637 | |
| 4638 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4639 | if (ret == NULL) |
| 4640 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4641 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 4642 | (!xmlXPathNodeSetContains(nodes, node))) |
| 4643 | return(ret); |
| 4644 | |
| 4645 | l = xmlXPathNodeSetGetLength(nodes); |
| 4646 | for (i = 0; i < l; i++) { |
| 4647 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4648 | if (cur == node) |
| 4649 | break; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4650 | if (xmlXPathNodeSetAddUnique(ret, cur) < 0) |
| 4651 | break; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4652 | } |
| 4653 | return(ret); |
| 4654 | } |
| 4655 | |
| 4656 | /** |
| 4657 | * xmlXPathNodeLeading: |
| 4658 | * @nodes: a node-set |
| 4659 | * @node: a node |
| 4660 | * |
| 4661 | * Implements the EXSLT - Sets leading() function: |
| 4662 | * node-set set:leading (node-set, node-set) |
| 4663 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 4664 | * is called. |
| 4665 | * |
| 4666 | * Returns the nodes in @nodes that precede @node in document order, |
| 4667 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4668 | * doesn't contain @node |
| 4669 | */ |
| 4670 | xmlNodeSetPtr |
| 4671 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4672 | xmlXPathNodeSetSort(nodes); |
| 4673 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 4674 | } |
| 4675 | |
| 4676 | /** |
| 4677 | * xmlXPathLeadingSorted: |
| 4678 | * @nodes1: a node-set, sorted by document order |
| 4679 | * @nodes2: a node-set, sorted by document order |
| 4680 | * |
| 4681 | * Implements the EXSLT - Sets leading() function: |
| 4682 | * node-set set:leading (node-set, node-set) |
| 4683 | * |
| 4684 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 4685 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4686 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4687 | */ |
| 4688 | xmlNodeSetPtr |
| 4689 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4690 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4691 | return(nodes1); |
| 4692 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 4693 | xmlXPathNodeSetItem(nodes2, 1))); |
| 4694 | } |
| 4695 | |
| 4696 | /** |
| 4697 | * xmlXPathLeading: |
| 4698 | * @nodes1: a node-set |
| 4699 | * @nodes2: a node-set |
| 4700 | * |
| 4701 | * Implements the EXSLT - Sets leading() function: |
| 4702 | * node-set set:leading (node-set, node-set) |
| 4703 | * @nodes1 and @nodes2 are sorted by document order, then |
| 4704 | * #exslSetsLeadingSorted is called. |
| 4705 | * |
| 4706 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 4707 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4708 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4709 | */ |
| 4710 | xmlNodeSetPtr |
| 4711 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4712 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4713 | return(nodes1); |
| 4714 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4715 | return(xmlXPathNodeSetCreate(NULL)); |
| 4716 | xmlXPathNodeSetSort(nodes1); |
| 4717 | xmlXPathNodeSetSort(nodes2); |
| 4718 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 4719 | xmlXPathNodeSetItem(nodes2, 1))); |
| 4720 | } |
| 4721 | |
| 4722 | /** |
| 4723 | * xmlXPathNodeTrailingSorted: |
| 4724 | * @nodes: a node-set, sorted by document order |
| 4725 | * @node: a node |
| 4726 | * |
| 4727 | * Implements the EXSLT - Sets trailing() function: |
| 4728 | * node-set set:trailing (node-set, node-set) |
| 4729 | * |
| 4730 | * Returns the nodes in @nodes that follow @node in document order, |
| 4731 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4732 | * doesn't contain @node |
| 4733 | */ |
| 4734 | xmlNodeSetPtr |
| 4735 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4736 | int i, l; |
| 4737 | xmlNodePtr cur; |
| 4738 | xmlNodeSetPtr ret; |
| 4739 | |
| 4740 | if (node == NULL) |
| 4741 | return(nodes); |
| 4742 | |
| 4743 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4744 | if (ret == NULL) |
| 4745 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4746 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 4747 | (!xmlXPathNodeSetContains(nodes, node))) |
| 4748 | return(ret); |
| 4749 | |
| 4750 | l = xmlXPathNodeSetGetLength(nodes); |
William M. Brack | 97ac819 | 2007-06-06 17:19:24 +0000 | [diff] [blame] | 4751 | for (i = l - 1; i >= 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4752 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4753 | if (cur == node) |
| 4754 | break; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 4755 | if (xmlXPathNodeSetAddUnique(ret, cur) < 0) |
| 4756 | break; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4757 | } |
William M. Brack | 97ac819 | 2007-06-06 17:19:24 +0000 | [diff] [blame] | 4758 | xmlXPathNodeSetSort(ret); /* bug 413451 */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4759 | return(ret); |
| 4760 | } |
| 4761 | |
| 4762 | /** |
| 4763 | * xmlXPathNodeTrailing: |
| 4764 | * @nodes: a node-set |
| 4765 | * @node: a node |
| 4766 | * |
| 4767 | * Implements the EXSLT - Sets trailing() function: |
| 4768 | * node-set set:trailing (node-set, node-set) |
| 4769 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 4770 | * is called. |
| 4771 | * |
| 4772 | * Returns the nodes in @nodes that follow @node in document order, |
| 4773 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4774 | * doesn't contain @node |
| 4775 | */ |
| 4776 | xmlNodeSetPtr |
| 4777 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4778 | xmlXPathNodeSetSort(nodes); |
| 4779 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 4780 | } |
| 4781 | |
| 4782 | /** |
| 4783 | * xmlXPathTrailingSorted: |
| 4784 | * @nodes1: a node-set, sorted by document order |
| 4785 | * @nodes2: a node-set, sorted by document order |
| 4786 | * |
| 4787 | * Implements the EXSLT - Sets trailing() function: |
| 4788 | * node-set set:trailing (node-set, node-set) |
| 4789 | * |
| 4790 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 4791 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4792 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4793 | */ |
| 4794 | xmlNodeSetPtr |
| 4795 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4796 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4797 | return(nodes1); |
| 4798 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 4799 | xmlXPathNodeSetItem(nodes2, 0))); |
| 4800 | } |
| 4801 | |
| 4802 | /** |
| 4803 | * xmlXPathTrailing: |
| 4804 | * @nodes1: a node-set |
| 4805 | * @nodes2: a node-set |
| 4806 | * |
| 4807 | * Implements the EXSLT - Sets trailing() function: |
| 4808 | * node-set set:trailing (node-set, node-set) |
| 4809 | * @nodes1 and @nodes2 are sorted by document order, then |
| 4810 | * #xmlXPathTrailingSorted is called. |
| 4811 | * |
| 4812 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 4813 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4814 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4815 | */ |
| 4816 | xmlNodeSetPtr |
| 4817 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4818 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4819 | return(nodes1); |
| 4820 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4821 | return(xmlXPathNodeSetCreate(NULL)); |
| 4822 | xmlXPathNodeSetSort(nodes1); |
| 4823 | xmlXPathNodeSetSort(nodes2); |
| 4824 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 4825 | xmlXPathNodeSetItem(nodes2, 0))); |
| 4826 | } |
| 4827 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4828 | /************************************************************************ |
| 4829 | * * |
| 4830 | * Routines to handle extra functions * |
| 4831 | * * |
| 4832 | ************************************************************************/ |
| 4833 | |
| 4834 | /** |
| 4835 | * xmlXPathRegisterFunc: |
| 4836 | * @ctxt: the XPath context |
| 4837 | * @name: the function name |
| 4838 | * @f: the function implementation or NULL |
| 4839 | * |
| 4840 | * Register a new function. If @f is NULL it unregisters the function |
| 4841 | * |
| 4842 | * Returns 0 in case of success, -1 in case of error |
| 4843 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4844 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4845 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4846 | xmlXPathFunction f) { |
| 4847 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 4848 | } |
| 4849 | |
| 4850 | /** |
| 4851 | * xmlXPathRegisterFuncNS: |
| 4852 | * @ctxt: the XPath context |
| 4853 | * @name: the function name |
| 4854 | * @ns_uri: the function namespace URI |
| 4855 | * @f: the function implementation or NULL |
| 4856 | * |
| 4857 | * Register a new function. If @f is NULL it unregisters the function |
| 4858 | * |
| 4859 | * Returns 0 in case of success, -1 in case of error |
| 4860 | */ |
| 4861 | int |
| 4862 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4863 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 4864 | if (ctxt == NULL) |
| 4865 | return(-1); |
| 4866 | if (name == NULL) |
| 4867 | return(-1); |
| 4868 | |
| 4869 | if (ctxt->funcHash == NULL) |
| 4870 | ctxt->funcHash = xmlHashCreate(0); |
| 4871 | if (ctxt->funcHash == NULL) |
| 4872 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 4873 | if (f == NULL) |
| 4874 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4875 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4879 | * xmlXPathRegisterFuncLookup: |
| 4880 | * @ctxt: the XPath context |
| 4881 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4882 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4883 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4884 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4885 | */ |
| 4886 | void |
| 4887 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 4888 | xmlXPathFuncLookupFunc f, |
| 4889 | void *funcCtxt) { |
| 4890 | if (ctxt == NULL) |
| 4891 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4892 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4893 | ctxt->funcLookupData = funcCtxt; |
| 4894 | } |
| 4895 | |
| 4896 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4897 | * xmlXPathFunctionLookup: |
| 4898 | * @ctxt: the XPath context |
| 4899 | * @name: the function name |
| 4900 | * |
| 4901 | * Search in the Function array of the context for the given |
| 4902 | * function. |
| 4903 | * |
| 4904 | * Returns the xmlXPathFunction or NULL if not found |
| 4905 | */ |
| 4906 | xmlXPathFunction |
| 4907 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4908 | if (ctxt == NULL) |
| 4909 | return (NULL); |
| 4910 | |
| 4911 | if (ctxt->funcLookupFunc != NULL) { |
| 4912 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 4913 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4914 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4915 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 4916 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4917 | if (ret != NULL) |
| 4918 | return(ret); |
| 4919 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4920 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 4921 | } |
| 4922 | |
| 4923 | /** |
| 4924 | * xmlXPathFunctionLookupNS: |
| 4925 | * @ctxt: the XPath context |
| 4926 | * @name: the function name |
| 4927 | * @ns_uri: the function namespace URI |
| 4928 | * |
| 4929 | * Search in the Function array of the context for the given |
| 4930 | * function. |
| 4931 | * |
| 4932 | * Returns the xmlXPathFunction or NULL if not found |
| 4933 | */ |
| 4934 | xmlXPathFunction |
| 4935 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4936 | const xmlChar *ns_uri) { |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4937 | xmlXPathFunction ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4938 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | if (ctxt == NULL) |
| 4940 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4941 | if (name == NULL) |
| 4942 | return(NULL); |
| 4943 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4944 | if (ctxt->funcLookupFunc != NULL) { |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 4945 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4946 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4947 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 4948 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4949 | if (ret != NULL) |
| 4950 | return(ret); |
| 4951 | } |
| 4952 | |
| 4953 | if (ctxt->funcHash == NULL) |
| 4954 | return(NULL); |
| 4955 | |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4956 | XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri); |
| 4957 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * xmlXPathRegisteredFuncsCleanup: |
| 4962 | * @ctxt: the XPath context |
| 4963 | * |
| 4964 | * Cleanup the XPath context data associated to registered functions |
| 4965 | */ |
| 4966 | void |
| 4967 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 4968 | if (ctxt == NULL) |
| 4969 | return; |
| 4970 | |
| 4971 | xmlHashFree(ctxt->funcHash, NULL); |
| 4972 | ctxt->funcHash = NULL; |
| 4973 | } |
| 4974 | |
| 4975 | /************************************************************************ |
| 4976 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4977 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4978 | * * |
| 4979 | ************************************************************************/ |
| 4980 | |
| 4981 | /** |
| 4982 | * xmlXPathRegisterVariable: |
| 4983 | * @ctxt: the XPath context |
| 4984 | * @name: the variable name |
| 4985 | * @value: the variable value or NULL |
| 4986 | * |
| 4987 | * Register a new variable value. If @value is NULL it unregisters |
| 4988 | * the variable |
| 4989 | * |
| 4990 | * Returns 0 in case of success, -1 in case of error |
| 4991 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4992 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4993 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4994 | xmlXPathObjectPtr value) { |
| 4995 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 4996 | } |
| 4997 | |
| 4998 | /** |
| 4999 | * xmlXPathRegisterVariableNS: |
| 5000 | * @ctxt: the XPath context |
| 5001 | * @name: the variable name |
| 5002 | * @ns_uri: the variable namespace URI |
| 5003 | * @value: the variable value or NULL |
| 5004 | * |
| 5005 | * Register a new variable value. If @value is NULL it unregisters |
| 5006 | * the variable |
| 5007 | * |
| 5008 | * Returns 0 in case of success, -1 in case of error |
| 5009 | */ |
| 5010 | int |
| 5011 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 5012 | const xmlChar *ns_uri, |
| 5013 | xmlXPathObjectPtr value) { |
| 5014 | if (ctxt == NULL) |
| 5015 | return(-1); |
| 5016 | if (name == NULL) |
| 5017 | return(-1); |
| 5018 | |
| 5019 | if (ctxt->varHash == NULL) |
| 5020 | ctxt->varHash = xmlHashCreate(0); |
| 5021 | if (ctxt->varHash == NULL) |
| 5022 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 5023 | if (value == NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5024 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 5025 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5026 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 5027 | (void *) value, |
| 5028 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 5029 | } |
| 5030 | |
| 5031 | /** |
| 5032 | * xmlXPathRegisterVariableLookup: |
| 5033 | * @ctxt: the XPath context |
| 5034 | * @f: the lookup function |
| 5035 | * @data: the lookup data |
| 5036 | * |
| 5037 | * register an external mechanism to do variable lookup |
| 5038 | */ |
| 5039 | void |
| 5040 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 5041 | xmlXPathVariableLookupFunc f, void *data) { |
| 5042 | if (ctxt == NULL) |
| 5043 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 5044 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | ctxt->varLookupData = data; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * xmlXPathVariableLookup: |
| 5050 | * @ctxt: the XPath context |
| 5051 | * @name: the variable name |
| 5052 | * |
| 5053 | * Search in the Variable array of the context for the given |
| 5054 | * variable value. |
| 5055 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 5056 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5057 | */ |
| 5058 | xmlXPathObjectPtr |
| 5059 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 5060 | if (ctxt == NULL) |
| 5061 | return(NULL); |
| 5062 | |
| 5063 | if (ctxt->varLookupFunc != NULL) { |
| 5064 | xmlXPathObjectPtr ret; |
| 5065 | |
| 5066 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 5067 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 5068 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5069 | } |
| 5070 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 5071 | } |
| 5072 | |
| 5073 | /** |
| 5074 | * xmlXPathVariableLookupNS: |
| 5075 | * @ctxt: the XPath context |
| 5076 | * @name: the variable name |
| 5077 | * @ns_uri: the variable namespace URI |
| 5078 | * |
| 5079 | * Search in the Variable array of the context for the given |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5080 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5081 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 5082 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5083 | */ |
| 5084 | xmlXPathObjectPtr |
| 5085 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 5086 | const xmlChar *ns_uri) { |
| 5087 | if (ctxt == NULL) |
| 5088 | return(NULL); |
| 5089 | |
| 5090 | if (ctxt->varLookupFunc != NULL) { |
| 5091 | xmlXPathObjectPtr ret; |
| 5092 | |
| 5093 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 5094 | (ctxt->varLookupData, name, ns_uri); |
| 5095 | if (ret != NULL) return(ret); |
| 5096 | } |
| 5097 | |
| 5098 | if (ctxt->varHash == NULL) |
| 5099 | return(NULL); |
| 5100 | if (name == NULL) |
| 5101 | return(NULL); |
| 5102 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5103 | return(xmlXPathCacheObjectCopy(ctxt, (xmlXPathObjectPtr) |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 5104 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | /** |
| 5108 | * xmlXPathRegisteredVariablesCleanup: |
| 5109 | * @ctxt: the XPath context |
| 5110 | * |
| 5111 | * Cleanup the XPath context data associated to registered variables |
| 5112 | */ |
| 5113 | void |
| 5114 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 5115 | if (ctxt == NULL) |
| 5116 | return; |
| 5117 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5118 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5119 | ctxt->varHash = NULL; |
| 5120 | } |
| 5121 | |
| 5122 | /** |
| 5123 | * xmlXPathRegisterNs: |
| 5124 | * @ctxt: the XPath context |
Daniel Veillard | db3ce96 | 2009-03-25 09:43:49 +0000 | [diff] [blame] | 5125 | * @prefix: the namespace prefix cannot be NULL or empty string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5126 | * @ns_uri: the namespace name |
| 5127 | * |
| 5128 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 5129 | * the namespace |
| 5130 | * |
| 5131 | * Returns 0 in case of success, -1 in case of error |
| 5132 | */ |
| 5133 | int |
| 5134 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 5135 | const xmlChar *ns_uri) { |
| 5136 | if (ctxt == NULL) |
| 5137 | return(-1); |
| 5138 | if (prefix == NULL) |
| 5139 | return(-1); |
Daniel Veillard | db3ce96 | 2009-03-25 09:43:49 +0000 | [diff] [blame] | 5140 | if (prefix[0] == 0) |
| 5141 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5142 | |
| 5143 | if (ctxt->nsHash == NULL) |
| 5144 | ctxt->nsHash = xmlHashCreate(10); |
| 5145 | if (ctxt->nsHash == NULL) |
| 5146 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 5147 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 5148 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 5149 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 5150 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5151 | (xmlHashDeallocator)xmlFree)); |
| 5152 | } |
| 5153 | |
| 5154 | /** |
| 5155 | * xmlXPathNsLookup: |
| 5156 | * @ctxt: the XPath context |
| 5157 | * @prefix: the namespace prefix value |
| 5158 | * |
| 5159 | * Search in the namespace declaration array of the context for the given |
| 5160 | * namespace name associated to the given prefix |
| 5161 | * |
| 5162 | * Returns the value or NULL if not found |
| 5163 | */ |
| 5164 | const xmlChar * |
| 5165 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 5166 | if (ctxt == NULL) |
| 5167 | return(NULL); |
| 5168 | if (prefix == NULL) |
| 5169 | return(NULL); |
| 5170 | |
| 5171 | #ifdef XML_XML_NAMESPACE |
| 5172 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 5173 | return(XML_XML_NAMESPACE); |
| 5174 | #endif |
| 5175 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 5176 | if (ctxt->namespaces != NULL) { |
| 5177 | int i; |
| 5178 | |
| 5179 | for (i = 0;i < ctxt->nsNr;i++) { |
| 5180 | if ((ctxt->namespaces[i] != NULL) && |
| 5181 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 5182 | return(ctxt->namespaces[i]->href); |
| 5183 | } |
| 5184 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5185 | |
| 5186 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 5187 | } |
| 5188 | |
| 5189 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5190 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5191 | * @ctxt: the XPath context |
| 5192 | * |
| 5193 | * Cleanup the XPath context data associated to registered variables |
| 5194 | */ |
| 5195 | void |
| 5196 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 5197 | if (ctxt == NULL) |
| 5198 | return; |
| 5199 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 5200 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5201 | ctxt->nsHash = NULL; |
| 5202 | } |
| 5203 | |
| 5204 | /************************************************************************ |
| 5205 | * * |
| 5206 | * Routines to handle Values * |
| 5207 | * * |
| 5208 | ************************************************************************/ |
| 5209 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5210 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5211 | |
| 5212 | /** |
| 5213 | * xmlXPathNewFloat: |
| 5214 | * @val: the double value |
| 5215 | * |
| 5216 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 5217 | * |
| 5218 | * Returns the newly created object. |
| 5219 | */ |
| 5220 | xmlXPathObjectPtr |
| 5221 | xmlXPathNewFloat(double val) { |
| 5222 | xmlXPathObjectPtr ret; |
| 5223 | |
| 5224 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5225 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5226 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5227 | return(NULL); |
| 5228 | } |
| 5229 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5230 | ret->type = XPATH_NUMBER; |
| 5231 | ret->floatval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5232 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5233 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NUMBER); |
| 5234 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5235 | return(ret); |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * xmlXPathNewBoolean: |
| 5240 | * @val: the boolean value |
| 5241 | * |
| 5242 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 5243 | * |
| 5244 | * Returns the newly created object. |
| 5245 | */ |
| 5246 | xmlXPathObjectPtr |
| 5247 | xmlXPathNewBoolean(int val) { |
| 5248 | xmlXPathObjectPtr ret; |
| 5249 | |
| 5250 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5251 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5252 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5253 | return(NULL); |
| 5254 | } |
| 5255 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5256 | ret->type = XPATH_BOOLEAN; |
| 5257 | ret->boolval = (val != 0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5258 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5259 | xmlXPathDebugObjUsageRequested(NULL, XPATH_BOOLEAN); |
| 5260 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | return(ret); |
| 5262 | } |
| 5263 | |
| 5264 | /** |
| 5265 | * xmlXPathNewString: |
| 5266 | * @val: the xmlChar * value |
| 5267 | * |
| 5268 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 5269 | * |
| 5270 | * Returns the newly created object. |
| 5271 | */ |
| 5272 | xmlXPathObjectPtr |
| 5273 | xmlXPathNewString(const xmlChar *val) { |
| 5274 | xmlXPathObjectPtr ret; |
| 5275 | |
| 5276 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5277 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5278 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5279 | return(NULL); |
| 5280 | } |
| 5281 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5282 | ret->type = XPATH_STRING; |
| 5283 | if (val != NULL) |
| 5284 | ret->stringval = xmlStrdup(val); |
| 5285 | else |
| 5286 | ret->stringval = xmlStrdup((const xmlChar *)""); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5287 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5288 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5289 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5290 | return(ret); |
| 5291 | } |
| 5292 | |
| 5293 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5294 | * xmlXPathWrapString: |
| 5295 | * @val: the xmlChar * value |
| 5296 | * |
| 5297 | * Wraps the @val string into an XPath object. |
| 5298 | * |
| 5299 | * Returns the newly created object. |
| 5300 | */ |
| 5301 | xmlXPathObjectPtr |
| 5302 | xmlXPathWrapString (xmlChar *val) { |
| 5303 | xmlXPathObjectPtr ret; |
| 5304 | |
| 5305 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5306 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5307 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5308 | return(NULL); |
| 5309 | } |
| 5310 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5311 | ret->type = XPATH_STRING; |
| 5312 | ret->stringval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5313 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5314 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5315 | #endif |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5316 | return(ret); |
| 5317 | } |
| 5318 | |
| 5319 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5320 | * xmlXPathNewCString: |
| 5321 | * @val: the char * value |
| 5322 | * |
| 5323 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 5324 | * |
| 5325 | * Returns the newly created object. |
| 5326 | */ |
| 5327 | xmlXPathObjectPtr |
| 5328 | xmlXPathNewCString(const char *val) { |
| 5329 | xmlXPathObjectPtr ret; |
| 5330 | |
| 5331 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5332 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5333 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5334 | return(NULL); |
| 5335 | } |
| 5336 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5337 | ret->type = XPATH_STRING; |
| 5338 | ret->stringval = xmlStrdup(BAD_CAST val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5339 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5340 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5341 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5342 | return(ret); |
| 5343 | } |
| 5344 | |
| 5345 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5346 | * xmlXPathWrapCString: |
| 5347 | * @val: the char * value |
| 5348 | * |
| 5349 | * Wraps a string into an XPath object. |
| 5350 | * |
| 5351 | * Returns the newly created object. |
| 5352 | */ |
| 5353 | xmlXPathObjectPtr |
| 5354 | xmlXPathWrapCString (char * val) { |
| 5355 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 5356 | } |
| 5357 | |
| 5358 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5359 | * xmlXPathWrapExternal: |
| 5360 | * @val: the user data |
| 5361 | * |
| 5362 | * Wraps the @val data into an XPath object. |
| 5363 | * |
| 5364 | * Returns the newly created object. |
| 5365 | */ |
| 5366 | xmlXPathObjectPtr |
| 5367 | xmlXPathWrapExternal (void *val) { |
| 5368 | xmlXPathObjectPtr ret; |
| 5369 | |
| 5370 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5371 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5372 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5373 | return(NULL); |
| 5374 | } |
| 5375 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5376 | ret->type = XPATH_USERS; |
| 5377 | ret->user = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5378 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5379 | xmlXPathDebugObjUsageRequested(NULL, XPATH_USERS); |
| 5380 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5381 | return(ret); |
| 5382 | } |
| 5383 | |
| 5384 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5385 | * xmlXPathObjectCopy: |
| 5386 | * @val: the original object |
| 5387 | * |
| 5388 | * allocate a new copy of a given object |
| 5389 | * |
| 5390 | * Returns the newly created object. |
| 5391 | */ |
| 5392 | xmlXPathObjectPtr |
| 5393 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 5394 | xmlXPathObjectPtr ret; |
| 5395 | |
| 5396 | if (val == NULL) |
| 5397 | return(NULL); |
| 5398 | |
| 5399 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5400 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5401 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5402 | return(NULL); |
| 5403 | } |
| 5404 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5405 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5406 | xmlXPathDebugObjUsageRequested(NULL, val->type); |
| 5407 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5408 | switch (val->type) { |
| 5409 | case XPATH_BOOLEAN: |
| 5410 | case XPATH_NUMBER: |
| 5411 | case XPATH_POINT: |
| 5412 | case XPATH_RANGE: |
| 5413 | break; |
| 5414 | case XPATH_STRING: |
| 5415 | ret->stringval = xmlStrdup(val->stringval); |
| 5416 | break; |
| 5417 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5418 | #if 0 |
| 5419 | /* |
| 5420 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 5421 | this previous handling is no longer correct, and can cause some serious |
| 5422 | problems (ref. bug 145547) |
| 5423 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5424 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5425 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5426 | xmlNodePtr cur, tmp; |
| 5427 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5428 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5429 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5430 | top = xmlNewDoc(NULL); |
| 5431 | top->name = (char *) |
| 5432 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5433 | ret->user = top; |
| 5434 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5435 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5436 | cur = val->nodesetval->nodeTab[0]->children; |
| 5437 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5438 | tmp = xmlDocCopyNode(cur, top, 1); |
| 5439 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5440 | cur = cur->next; |
| 5441 | } |
| 5442 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5443 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5444 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5445 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5446 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5447 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5448 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5449 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5450 | case XPATH_NODESET: |
| 5451 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5452 | /* Do not deallocate the copied tree value */ |
| 5453 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | case XPATH_LOCATIONSET: |
| 5456 | #ifdef LIBXML_XPTR_ENABLED |
| 5457 | { |
| 5458 | xmlLocationSetPtr loc = val->user; |
| 5459 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 5460 | break; |
| 5461 | } |
| 5462 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 5463 | case XPATH_USERS: |
| 5464 | ret->user = val->user; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5465 | break; |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 5466 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5467 | xmlGenericError(xmlGenericErrorContext, |
| 5468 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 5469 | val->type); |
| 5470 | break; |
| 5471 | } |
| 5472 | return(ret); |
| 5473 | } |
| 5474 | |
| 5475 | /** |
| 5476 | * xmlXPathFreeObject: |
| 5477 | * @obj: the object to free |
| 5478 | * |
| 5479 | * Free up an xmlXPathObjectPtr object. |
| 5480 | */ |
| 5481 | void |
| 5482 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 5483 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5484 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5485 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5486 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5487 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5488 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 5489 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5490 | } else |
| 5491 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5492 | obj->type = XPATH_XSLT_TREE; /* TODO: Just for debugging. */ |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5493 | if (obj->nodesetval != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5494 | xmlXPathFreeValueTree(obj->nodesetval); |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5495 | } else { |
| 5496 | if (obj->nodesetval != NULL) |
| 5497 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5498 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5499 | #ifdef LIBXML_XPTR_ENABLED |
| 5500 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 5501 | if (obj->user != NULL) |
| 5502 | xmlXPtrFreeLocationSet(obj->user); |
| 5503 | #endif |
| 5504 | } else if (obj->type == XPATH_STRING) { |
| 5505 | if (obj->stringval != NULL) |
| 5506 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5507 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5508 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5509 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 5510 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5511 | xmlFree(obj); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5513 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5514 | /** |
| 5515 | * xmlXPathReleaseObject: |
| 5516 | * @obj: the xmlXPathObjectPtr to free or to cache |
| 5517 | * |
| 5518 | * Depending on the state of the cache this frees the given |
| 5519 | * XPath object or stores it in the cache. |
| 5520 | */ |
| 5521 | static void |
| 5522 | xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj) |
| 5523 | { |
| 5524 | #define XP_CACHE_ADD(sl, o) if (sl == NULL) { \ |
| 5525 | sl = xmlPointerListCreate(10); if (sl == NULL) goto free_obj; } \ |
| 5526 | if (xmlPointerListAddSize(sl, obj, 0) == -1) goto free_obj; |
| 5527 | |
| 5528 | #define XP_CACHE_WANTS(sl, n) ((sl == NULL) || ((sl)->number < n)) |
| 5529 | |
| 5530 | if (obj == NULL) |
| 5531 | return; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 5532 | if ((ctxt == NULL) || (ctxt->cache == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5533 | xmlXPathFreeObject(obj); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5534 | } else { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 5535 | xmlXPathContextCachePtr cache = |
| 5536 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5537 | |
| 5538 | switch (obj->type) { |
| 5539 | case XPATH_NODESET: |
| 5540 | case XPATH_XSLT_TREE: |
| 5541 | if (obj->nodesetval != NULL) { |
| 5542 | if (obj->boolval) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5543 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5544 | * It looks like the @boolval is used for |
| 5545 | * evaluation if this an XSLT Result Tree Fragment. |
| 5546 | * TODO: Check if this assumption is correct. |
| 5547 | */ |
| 5548 | obj->type = XPATH_XSLT_TREE; /* just for debugging */ |
| 5549 | xmlXPathFreeValueTree(obj->nodesetval); |
| 5550 | obj->nodesetval = NULL; |
| 5551 | } else if ((obj->nodesetval->nodeMax <= 40) && |
| 5552 | (XP_CACHE_WANTS(cache->nodesetObjs, |
| 5553 | cache->maxNodeset))) |
| 5554 | { |
| 5555 | XP_CACHE_ADD(cache->nodesetObjs, obj); |
| 5556 | goto obj_cached; |
| 5557 | } else { |
| 5558 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5559 | obj->nodesetval = NULL; |
| 5560 | } |
| 5561 | } |
| 5562 | break; |
| 5563 | case XPATH_STRING: |
| 5564 | if (obj->stringval != NULL) |
| 5565 | xmlFree(obj->stringval); |
| 5566 | |
| 5567 | if (XP_CACHE_WANTS(cache->stringObjs, cache->maxString)) { |
| 5568 | XP_CACHE_ADD(cache->stringObjs, obj); |
| 5569 | goto obj_cached; |
| 5570 | } |
| 5571 | break; |
| 5572 | case XPATH_BOOLEAN: |
| 5573 | if (XP_CACHE_WANTS(cache->booleanObjs, cache->maxBoolean)) { |
| 5574 | XP_CACHE_ADD(cache->booleanObjs, obj); |
| 5575 | goto obj_cached; |
| 5576 | } |
| 5577 | break; |
| 5578 | case XPATH_NUMBER: |
| 5579 | if (XP_CACHE_WANTS(cache->numberObjs, cache->maxNumber)) { |
| 5580 | XP_CACHE_ADD(cache->numberObjs, obj); |
| 5581 | goto obj_cached; |
| 5582 | } |
| 5583 | break; |
| 5584 | #ifdef LIBXML_XPTR_ENABLED |
| 5585 | case XPATH_LOCATIONSET: |
| 5586 | if (obj->user != NULL) { |
| 5587 | xmlXPtrFreeLocationSet(obj->user); |
| 5588 | } |
| 5589 | goto free_obj; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5590 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5591 | default: |
| 5592 | goto free_obj; |
| 5593 | } |
| 5594 | |
| 5595 | /* |
| 5596 | * Fallback to adding to the misc-objects slot. |
| 5597 | */ |
| 5598 | if (XP_CACHE_WANTS(cache->miscObjs, cache->maxMisc)) { |
| 5599 | XP_CACHE_ADD(cache->miscObjs, obj); |
| 5600 | } else |
| 5601 | goto free_obj; |
| 5602 | |
| 5603 | obj_cached: |
| 5604 | |
| 5605 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5606 | xmlXPathDebugObjUsageReleased(ctxt, obj->type); |
| 5607 | #endif |
| 5608 | |
| 5609 | if (obj->nodesetval != NULL) { |
| 5610 | xmlNodeSetPtr tmpset = obj->nodesetval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5611 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5612 | /* |
| 5613 | * TODO: Due to those nasty ns-nodes, we need to traverse |
| 5614 | * the list and free the ns-nodes. |
| 5615 | * URGENT TODO: Check if it's actually slowing things down. |
| 5616 | * Maybe we shouldn't try to preserve the list. |
| 5617 | */ |
| 5618 | if (tmpset->nodeNr > 1) { |
| 5619 | int i; |
| 5620 | xmlNodePtr node; |
| 5621 | |
| 5622 | for (i = 0; i < tmpset->nodeNr; i++) { |
| 5623 | node = tmpset->nodeTab[i]; |
| 5624 | if ((node != NULL) && |
| 5625 | (node->type == XML_NAMESPACE_DECL)) |
| 5626 | { |
| 5627 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
| 5628 | } |
| 5629 | } |
| 5630 | } else if (tmpset->nodeNr == 1) { |
| 5631 | if ((tmpset->nodeTab[0] != NULL) && |
| 5632 | (tmpset->nodeTab[0]->type == XML_NAMESPACE_DECL)) |
| 5633 | xmlXPathNodeSetFreeNs((xmlNsPtr) tmpset->nodeTab[0]); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5634 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5635 | tmpset->nodeNr = 0; |
| 5636 | memset(obj, 0, sizeof(xmlXPathObject)); |
| 5637 | obj->nodesetval = tmpset; |
| 5638 | } else |
| 5639 | memset(obj, 0, sizeof(xmlXPathObject)); |
| 5640 | |
| 5641 | return; |
| 5642 | |
| 5643 | free_obj: |
| 5644 | /* |
| 5645 | * Cache is full; free the object. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5646 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5647 | if (obj->nodesetval != NULL) |
| 5648 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5649 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5650 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 5651 | #endif |
| 5652 | xmlFree(obj); |
| 5653 | } |
| 5654 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5657 | |
| 5658 | /************************************************************************ |
| 5659 | * * |
| 5660 | * Type Casting Routines * |
| 5661 | * * |
| 5662 | ************************************************************************/ |
| 5663 | |
| 5664 | /** |
| 5665 | * xmlXPathCastBooleanToString: |
| 5666 | * @val: a boolean |
| 5667 | * |
| 5668 | * Converts a boolean to its string value. |
| 5669 | * |
| 5670 | * Returns a newly allocated string. |
| 5671 | */ |
| 5672 | xmlChar * |
| 5673 | xmlXPathCastBooleanToString (int val) { |
| 5674 | xmlChar *ret; |
| 5675 | if (val) |
| 5676 | ret = xmlStrdup((const xmlChar *) "true"); |
| 5677 | else |
| 5678 | ret = xmlStrdup((const xmlChar *) "false"); |
| 5679 | return(ret); |
| 5680 | } |
| 5681 | |
| 5682 | /** |
| 5683 | * xmlXPathCastNumberToString: |
| 5684 | * @val: a number |
| 5685 | * |
| 5686 | * Converts a number to its string value. |
| 5687 | * |
| 5688 | * Returns a newly allocated string. |
| 5689 | */ |
| 5690 | xmlChar * |
| 5691 | xmlXPathCastNumberToString (double val) { |
| 5692 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5693 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5694 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5695 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5696 | break; |
| 5697 | case -1: |
| 5698 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 5699 | break; |
| 5700 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5701 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5702 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5703 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 5704 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5705 | } else { |
| 5706 | /* could be improved */ |
| 5707 | char buf[100]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5708 | xmlXPathFormatNumber(val, buf, 99); |
| 5709 | buf[99] = 0; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5710 | ret = xmlStrdup((const xmlChar *) buf); |
| 5711 | } |
| 5712 | } |
| 5713 | return(ret); |
| 5714 | } |
| 5715 | |
| 5716 | /** |
| 5717 | * xmlXPathCastNodeToString: |
| 5718 | * @node: a node |
| 5719 | * |
| 5720 | * Converts a node to its string value. |
| 5721 | * |
| 5722 | * Returns a newly allocated string. |
| 5723 | */ |
| 5724 | xmlChar * |
| 5725 | xmlXPathCastNodeToString (xmlNodePtr node) { |
William M. Brack | d611c88 | 2007-05-31 05:07:17 +0000 | [diff] [blame] | 5726 | xmlChar *ret; |
| 5727 | if ((ret = xmlNodeGetContent(node)) == NULL) |
| 5728 | ret = xmlStrdup((const xmlChar *) ""); |
| 5729 | return(ret); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | /** |
| 5733 | * xmlXPathCastNodeSetToString: |
| 5734 | * @ns: a node-set |
| 5735 | * |
| 5736 | * Converts a node-set to its string value. |
| 5737 | * |
| 5738 | * Returns a newly allocated string. |
| 5739 | */ |
| 5740 | xmlChar * |
| 5741 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 5742 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 5743 | return(xmlStrdup((const xmlChar *) "")); |
| 5744 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 5745 | if (ns->nodeNr > 1) |
| 5746 | xmlXPathNodeSetSort(ns); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5747 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 5748 | } |
| 5749 | |
| 5750 | /** |
| 5751 | * xmlXPathCastToString: |
| 5752 | * @val: an XPath object |
| 5753 | * |
| 5754 | * Converts an existing object to its string() equivalent |
| 5755 | * |
Kasimier T. Buchcik | 50128ad | 2006-08-15 13:04:07 +0000 | [diff] [blame] | 5756 | * Returns the allocated string value of the object, NULL in case of error. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5757 | * It's up to the caller to free the string memory with xmlFree(). |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5758 | */ |
| 5759 | xmlChar * |
| 5760 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 5761 | xmlChar *ret = NULL; |
| 5762 | |
| 5763 | if (val == NULL) |
| 5764 | return(xmlStrdup((const xmlChar *) "")); |
| 5765 | switch (val->type) { |
| 5766 | case XPATH_UNDEFINED: |
| 5767 | #ifdef DEBUG_EXPR |
| 5768 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 5769 | #endif |
| 5770 | ret = xmlStrdup((const xmlChar *) ""); |
| 5771 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5772 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5773 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5774 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 5775 | break; |
| 5776 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 5777 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5778 | case XPATH_BOOLEAN: |
| 5779 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 5780 | break; |
| 5781 | case XPATH_NUMBER: { |
| 5782 | ret = xmlXPathCastNumberToString(val->floatval); |
| 5783 | break; |
| 5784 | } |
| 5785 | case XPATH_USERS: |
| 5786 | case XPATH_POINT: |
| 5787 | case XPATH_RANGE: |
| 5788 | case XPATH_LOCATIONSET: |
| 5789 | TODO |
| 5790 | ret = xmlStrdup((const xmlChar *) ""); |
| 5791 | break; |
| 5792 | } |
| 5793 | return(ret); |
| 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * xmlXPathConvertString: |
| 5798 | * @val: an XPath object |
| 5799 | * |
| 5800 | * Converts an existing object to its string() equivalent |
| 5801 | * |
| 5802 | * Returns the new object, the old one is freed (or the operation |
| 5803 | * is done directly on @val) |
| 5804 | */ |
| 5805 | xmlXPathObjectPtr |
| 5806 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 5807 | xmlChar *res = NULL; |
| 5808 | |
| 5809 | if (val == NULL) |
| 5810 | return(xmlXPathNewCString("")); |
| 5811 | |
| 5812 | switch (val->type) { |
| 5813 | case XPATH_UNDEFINED: |
| 5814 | #ifdef DEBUG_EXPR |
| 5815 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 5816 | #endif |
| 5817 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5818 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5819 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5820 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 5821 | break; |
| 5822 | case XPATH_STRING: |
| 5823 | return(val); |
| 5824 | case XPATH_BOOLEAN: |
| 5825 | res = xmlXPathCastBooleanToString(val->boolval); |
| 5826 | break; |
| 5827 | case XPATH_NUMBER: |
| 5828 | res = xmlXPathCastNumberToString(val->floatval); |
| 5829 | break; |
| 5830 | case XPATH_USERS: |
| 5831 | case XPATH_POINT: |
| 5832 | case XPATH_RANGE: |
| 5833 | case XPATH_LOCATIONSET: |
| 5834 | TODO; |
| 5835 | break; |
| 5836 | } |
| 5837 | xmlXPathFreeObject(val); |
| 5838 | if (res == NULL) |
| 5839 | return(xmlXPathNewCString("")); |
| 5840 | return(xmlXPathWrapString(res)); |
| 5841 | } |
| 5842 | |
| 5843 | /** |
| 5844 | * xmlXPathCastBooleanToNumber: |
| 5845 | * @val: a boolean |
| 5846 | * |
| 5847 | * Converts a boolean to its number value |
| 5848 | * |
| 5849 | * Returns the number value |
| 5850 | */ |
| 5851 | double |
| 5852 | xmlXPathCastBooleanToNumber(int val) { |
| 5853 | if (val) |
| 5854 | return(1.0); |
| 5855 | return(0.0); |
| 5856 | } |
| 5857 | |
| 5858 | /** |
| 5859 | * xmlXPathCastStringToNumber: |
| 5860 | * @val: a string |
| 5861 | * |
| 5862 | * Converts a string to its number value |
| 5863 | * |
| 5864 | * Returns the number value |
| 5865 | */ |
| 5866 | double |
| 5867 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 5868 | return(xmlXPathStringEvalNumber(val)); |
| 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * xmlXPathCastNodeToNumber: |
| 5873 | * @node: a node |
| 5874 | * |
| 5875 | * Converts a node to its number value |
| 5876 | * |
| 5877 | * Returns the number value |
| 5878 | */ |
| 5879 | double |
| 5880 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 5881 | xmlChar *strval; |
| 5882 | double ret; |
| 5883 | |
| 5884 | if (node == NULL) |
| 5885 | return(xmlXPathNAN); |
| 5886 | strval = xmlXPathCastNodeToString(node); |
| 5887 | if (strval == NULL) |
| 5888 | return(xmlXPathNAN); |
| 5889 | ret = xmlXPathCastStringToNumber(strval); |
| 5890 | xmlFree(strval); |
| 5891 | |
| 5892 | return(ret); |
| 5893 | } |
| 5894 | |
| 5895 | /** |
| 5896 | * xmlXPathCastNodeSetToNumber: |
| 5897 | * @ns: a node-set |
| 5898 | * |
| 5899 | * Converts a node-set to its number value |
| 5900 | * |
| 5901 | * Returns the number value |
| 5902 | */ |
| 5903 | double |
| 5904 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 5905 | xmlChar *str; |
| 5906 | double ret; |
| 5907 | |
| 5908 | if (ns == NULL) |
| 5909 | return(xmlXPathNAN); |
| 5910 | str = xmlXPathCastNodeSetToString(ns); |
| 5911 | ret = xmlXPathCastStringToNumber(str); |
| 5912 | xmlFree(str); |
| 5913 | return(ret); |
| 5914 | } |
| 5915 | |
| 5916 | /** |
| 5917 | * xmlXPathCastToNumber: |
| 5918 | * @val: an XPath object |
| 5919 | * |
| 5920 | * Converts an XPath object to its number value |
| 5921 | * |
| 5922 | * Returns the number value |
| 5923 | */ |
| 5924 | double |
| 5925 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 5926 | double ret = 0.0; |
| 5927 | |
| 5928 | if (val == NULL) |
| 5929 | return(xmlXPathNAN); |
| 5930 | switch (val->type) { |
| 5931 | case XPATH_UNDEFINED: |
| 5932 | #ifdef DEGUB_EXPR |
| 5933 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 5934 | #endif |
| 5935 | ret = xmlXPathNAN; |
| 5936 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5937 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5938 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5939 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 5940 | break; |
| 5941 | case XPATH_STRING: |
| 5942 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 5943 | break; |
| 5944 | case XPATH_NUMBER: |
| 5945 | ret = val->floatval; |
| 5946 | break; |
| 5947 | case XPATH_BOOLEAN: |
| 5948 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 5949 | break; |
| 5950 | case XPATH_USERS: |
| 5951 | case XPATH_POINT: |
| 5952 | case XPATH_RANGE: |
| 5953 | case XPATH_LOCATIONSET: |
| 5954 | TODO; |
| 5955 | ret = xmlXPathNAN; |
| 5956 | break; |
| 5957 | } |
| 5958 | return(ret); |
| 5959 | } |
| 5960 | |
| 5961 | /** |
| 5962 | * xmlXPathConvertNumber: |
| 5963 | * @val: an XPath object |
| 5964 | * |
| 5965 | * Converts an existing object to its number() equivalent |
| 5966 | * |
| 5967 | * Returns the new object, the old one is freed (or the operation |
| 5968 | * is done directly on @val) |
| 5969 | */ |
| 5970 | xmlXPathObjectPtr |
| 5971 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 5972 | xmlXPathObjectPtr ret; |
| 5973 | |
| 5974 | if (val == NULL) |
| 5975 | return(xmlXPathNewFloat(0.0)); |
| 5976 | if (val->type == XPATH_NUMBER) |
| 5977 | return(val); |
| 5978 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 5979 | xmlXPathFreeObject(val); |
| 5980 | return(ret); |
| 5981 | } |
| 5982 | |
| 5983 | /** |
| 5984 | * xmlXPathCastNumberToBoolean: |
| 5985 | * @val: a number |
| 5986 | * |
| 5987 | * Converts a number to its boolean value |
| 5988 | * |
| 5989 | * Returns the boolean value |
| 5990 | */ |
| 5991 | int |
| 5992 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5993 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5994 | return(0); |
| 5995 | return(1); |
| 5996 | } |
| 5997 | |
| 5998 | /** |
| 5999 | * xmlXPathCastStringToBoolean: |
| 6000 | * @val: a string |
| 6001 | * |
| 6002 | * Converts a string to its boolean value |
| 6003 | * |
| 6004 | * Returns the boolean value |
| 6005 | */ |
| 6006 | int |
| 6007 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 6008 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 6009 | return(0); |
| 6010 | return(1); |
| 6011 | } |
| 6012 | |
| 6013 | /** |
| 6014 | * xmlXPathCastNodeSetToBoolean: |
| 6015 | * @ns: a node-set |
| 6016 | * |
| 6017 | * Converts a node-set to its boolean value |
| 6018 | * |
| 6019 | * Returns the boolean value |
| 6020 | */ |
| 6021 | int |
| 6022 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 6023 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 6024 | return(0); |
| 6025 | return(1); |
| 6026 | } |
| 6027 | |
| 6028 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6029 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6030 | * @val: an XPath object |
| 6031 | * |
| 6032 | * Converts an XPath object to its boolean value |
| 6033 | * |
| 6034 | * Returns the boolean value |
| 6035 | */ |
| 6036 | int |
| 6037 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 6038 | int ret = 0; |
| 6039 | |
| 6040 | if (val == NULL) |
| 6041 | return(0); |
| 6042 | switch (val->type) { |
| 6043 | case XPATH_UNDEFINED: |
| 6044 | #ifdef DEBUG_EXPR |
| 6045 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 6046 | #endif |
| 6047 | ret = 0; |
| 6048 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6049 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6050 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6051 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 6052 | break; |
| 6053 | case XPATH_STRING: |
| 6054 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 6055 | break; |
| 6056 | case XPATH_NUMBER: |
| 6057 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 6058 | break; |
| 6059 | case XPATH_BOOLEAN: |
| 6060 | ret = val->boolval; |
| 6061 | break; |
| 6062 | case XPATH_USERS: |
| 6063 | case XPATH_POINT: |
| 6064 | case XPATH_RANGE: |
| 6065 | case XPATH_LOCATIONSET: |
| 6066 | TODO; |
| 6067 | ret = 0; |
| 6068 | break; |
| 6069 | } |
| 6070 | return(ret); |
| 6071 | } |
| 6072 | |
| 6073 | |
| 6074 | /** |
| 6075 | * xmlXPathConvertBoolean: |
| 6076 | * @val: an XPath object |
| 6077 | * |
| 6078 | * Converts an existing object to its boolean() equivalent |
| 6079 | * |
| 6080 | * Returns the new object, the old one is freed (or the operation |
| 6081 | * is done directly on @val) |
| 6082 | */ |
| 6083 | xmlXPathObjectPtr |
| 6084 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 6085 | xmlXPathObjectPtr ret; |
| 6086 | |
| 6087 | if (val == NULL) |
| 6088 | return(xmlXPathNewBoolean(0)); |
| 6089 | if (val->type == XPATH_BOOLEAN) |
| 6090 | return(val); |
| 6091 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 6092 | xmlXPathFreeObject(val); |
| 6093 | return(ret); |
| 6094 | } |
| 6095 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6096 | /************************************************************************ |
| 6097 | * * |
| 6098 | * Routines to handle XPath contexts * |
| 6099 | * * |
| 6100 | ************************************************************************/ |
| 6101 | |
| 6102 | /** |
| 6103 | * xmlXPathNewContext: |
| 6104 | * @doc: the XML document |
| 6105 | * |
| 6106 | * Create a new xmlXPathContext |
| 6107 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 6108 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6109 | */ |
| 6110 | xmlXPathContextPtr |
| 6111 | xmlXPathNewContext(xmlDocPtr doc) { |
| 6112 | xmlXPathContextPtr ret; |
| 6113 | |
| 6114 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 6115 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6116 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | return(NULL); |
| 6118 | } |
| 6119 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 6120 | ret->doc = doc; |
| 6121 | ret->node = NULL; |
| 6122 | |
| 6123 | ret->varHash = NULL; |
| 6124 | |
| 6125 | ret->nb_types = 0; |
| 6126 | ret->max_types = 0; |
| 6127 | ret->types = NULL; |
| 6128 | |
| 6129 | ret->funcHash = xmlHashCreate(0); |
| 6130 | |
| 6131 | ret->nb_axis = 0; |
| 6132 | ret->max_axis = 0; |
| 6133 | ret->axis = NULL; |
| 6134 | |
| 6135 | ret->nsHash = NULL; |
| 6136 | ret->user = NULL; |
| 6137 | |
| 6138 | ret->contextSize = -1; |
| 6139 | ret->proximityPosition = -1; |
| 6140 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6141 | #ifdef XP_DEFAULT_CACHE_ON |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 6142 | if (xmlXPathContextSetCache(ret, 1, -1, 0) == -1) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6143 | xmlXPathFreeContext(ret); |
| 6144 | return(NULL); |
| 6145 | } |
| 6146 | #endif |
| 6147 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6148 | xmlXPathRegisterAllFunctions(ret); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6149 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6150 | return(ret); |
| 6151 | } |
| 6152 | |
| 6153 | /** |
| 6154 | * xmlXPathFreeContext: |
| 6155 | * @ctxt: the context to free |
| 6156 | * |
| 6157 | * Free up an xmlXPathContext |
| 6158 | */ |
| 6159 | void |
| 6160 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 6161 | if (ctxt == NULL) return; |
| 6162 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 6163 | if (ctxt->cache != NULL) |
| 6164 | xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6165 | xmlXPathRegisteredNsCleanup(ctxt); |
| 6166 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 6167 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 6168 | xmlResetError(&ctxt->lastError); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6169 | xmlFree(ctxt); |
| 6170 | } |
| 6171 | |
| 6172 | /************************************************************************ |
| 6173 | * * |
| 6174 | * Routines to handle XPath parser contexts * |
| 6175 | * * |
| 6176 | ************************************************************************/ |
| 6177 | |
| 6178 | #define CHECK_CTXT(ctxt) \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6179 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 6180 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 6181 | NULL, NULL, XML_FROM_XPATH, \ |
| 6182 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 6183 | __FILE__, __LINE__, \ |
| 6184 | NULL, NULL, NULL, 0, 0, \ |
| 6185 | "NULL context pointer\n"); \ |
| 6186 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | } \ |
| 6188 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 6189 | #define CHECK_CTXT_NEG(ctxt) \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6190 | if (ctxt == NULL) { \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 6191 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 6192 | NULL, NULL, XML_FROM_XPATH, \ |
| 6193 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 6194 | __FILE__, __LINE__, \ |
| 6195 | NULL, NULL, NULL, 0, 0, \ |
| 6196 | "NULL context pointer\n"); \ |
| 6197 | return(-1); \ |
| 6198 | } \ |
| 6199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6200 | |
| 6201 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6202 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6203 | (ctxt->doc->children == NULL)) { \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6204 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 6205 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6206 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6207 | |
| 6208 | |
| 6209 | /** |
| 6210 | * xmlXPathNewParserContext: |
| 6211 | * @str: the XPath expression |
| 6212 | * @ctxt: the XPath context |
| 6213 | * |
| 6214 | * Create a new xmlXPathParserContext |
| 6215 | * |
| 6216 | * Returns the xmlXPathParserContext just allocated. |
| 6217 | */ |
| 6218 | xmlXPathParserContextPtr |
| 6219 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 6220 | xmlXPathParserContextPtr ret; |
| 6221 | |
| 6222 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 6223 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6224 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6225 | return(NULL); |
| 6226 | } |
| 6227 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 6228 | ret->cur = ret->base = str; |
| 6229 | ret->context = ctxt; |
| 6230 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6231 | ret->comp = xmlXPathNewCompExpr(); |
| 6232 | if (ret->comp == NULL) { |
| 6233 | xmlFree(ret->valueTab); |
| 6234 | xmlFree(ret); |
| 6235 | return(NULL); |
| 6236 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 6237 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 6238 | ret->comp->dict = ctxt->dict; |
| 6239 | xmlDictReference(ret->comp->dict); |
| 6240 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6241 | |
| 6242 | return(ret); |
| 6243 | } |
| 6244 | |
| 6245 | /** |
| 6246 | * xmlXPathCompParserContext: |
| 6247 | * @comp: the XPath compiled expression |
| 6248 | * @ctxt: the XPath context |
| 6249 | * |
| 6250 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 6251 | * |
| 6252 | * Returns the xmlXPathParserContext just allocated. |
| 6253 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6254 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6255 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 6256 | xmlXPathParserContextPtr ret; |
| 6257 | |
| 6258 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 6259 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6260 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6261 | return(NULL); |
| 6262 | } |
| 6263 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 6264 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6265 | /* Allocate the value stack */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6266 | ret->valueTab = (xmlXPathObjectPtr *) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6267 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6268 | if (ret->valueTab == NULL) { |
| 6269 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6270 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6271 | return(NULL); |
| 6272 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6273 | ret->valueNr = 0; |
| 6274 | ret->valueMax = 10; |
| 6275 | ret->value = NULL; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 6276 | ret->valueFrame = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6277 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6278 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6279 | ret->comp = comp; |
| 6280 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6281 | return(ret); |
| 6282 | } |
| 6283 | |
| 6284 | /** |
| 6285 | * xmlXPathFreeParserContext: |
| 6286 | * @ctxt: the context to free |
| 6287 | * |
| 6288 | * Free up an xmlXPathParserContext |
| 6289 | */ |
| 6290 | void |
| 6291 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 6292 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | xmlFree(ctxt->valueTab); |
| 6294 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 6295 | if (ctxt->comp != NULL) { |
| 6296 | #ifdef XPATH_STREAMING |
| 6297 | if (ctxt->comp->stream != NULL) { |
| 6298 | xmlFreePatternList(ctxt->comp->stream); |
| 6299 | ctxt->comp->stream = NULL; |
| 6300 | } |
| 6301 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6302 | xmlXPathFreeCompExpr(ctxt->comp); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 6303 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6304 | xmlFree(ctxt); |
| 6305 | } |
| 6306 | |
| 6307 | /************************************************************************ |
| 6308 | * * |
| 6309 | * The implicit core function library * |
| 6310 | * * |
| 6311 | ************************************************************************/ |
| 6312 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6313 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6314 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6315 | * @node: a node pointer |
| 6316 | * |
| 6317 | * Function computing the beginning of the string value of the node, |
| 6318 | * used to speed up comparisons |
| 6319 | * |
| 6320 | * Returns an int usable as a hash |
| 6321 | */ |
| 6322 | static unsigned int |
| 6323 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 6324 | int len = 2; |
| 6325 | const xmlChar * string = NULL; |
| 6326 | xmlNodePtr tmp = NULL; |
| 6327 | unsigned int ret = 0; |
| 6328 | |
| 6329 | if (node == NULL) |
| 6330 | return(0); |
| 6331 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 6332 | if (node->type == XML_DOCUMENT_NODE) { |
| 6333 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 6334 | if (tmp == NULL) |
| 6335 | node = node->children; |
| 6336 | else |
| 6337 | node = tmp; |
| 6338 | |
| 6339 | if (node == NULL) |
| 6340 | return(0); |
| 6341 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6342 | |
| 6343 | switch (node->type) { |
| 6344 | case XML_COMMENT_NODE: |
| 6345 | case XML_PI_NODE: |
| 6346 | case XML_CDATA_SECTION_NODE: |
| 6347 | case XML_TEXT_NODE: |
| 6348 | string = node->content; |
| 6349 | if (string == NULL) |
| 6350 | return(0); |
| 6351 | if (string[0] == 0) |
| 6352 | return(0); |
| 6353 | return(((unsigned int) string[0]) + |
| 6354 | (((unsigned int) string[1]) << 8)); |
| 6355 | case XML_NAMESPACE_DECL: |
| 6356 | string = ((xmlNsPtr)node)->href; |
| 6357 | if (string == NULL) |
| 6358 | return(0); |
| 6359 | if (string[0] == 0) |
| 6360 | return(0); |
| 6361 | return(((unsigned int) string[0]) + |
| 6362 | (((unsigned int) string[1]) << 8)); |
| 6363 | case XML_ATTRIBUTE_NODE: |
| 6364 | tmp = ((xmlAttrPtr) node)->children; |
| 6365 | break; |
| 6366 | case XML_ELEMENT_NODE: |
| 6367 | tmp = node->children; |
| 6368 | break; |
| 6369 | default: |
| 6370 | return(0); |
| 6371 | } |
| 6372 | while (tmp != NULL) { |
| 6373 | switch (tmp->type) { |
| 6374 | case XML_COMMENT_NODE: |
| 6375 | case XML_PI_NODE: |
| 6376 | case XML_CDATA_SECTION_NODE: |
| 6377 | case XML_TEXT_NODE: |
| 6378 | string = tmp->content; |
| 6379 | break; |
| 6380 | case XML_NAMESPACE_DECL: |
| 6381 | string = ((xmlNsPtr)tmp)->href; |
| 6382 | break; |
| 6383 | default: |
| 6384 | break; |
| 6385 | } |
| 6386 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6387 | if (len == 1) { |
| 6388 | return(ret + (((unsigned int) string[0]) << 8)); |
| 6389 | } |
| 6390 | if (string[1] == 0) { |
| 6391 | len = 1; |
| 6392 | ret = (unsigned int) string[0]; |
| 6393 | } else { |
| 6394 | return(((unsigned int) string[0]) + |
| 6395 | (((unsigned int) string[1]) << 8)); |
| 6396 | } |
| 6397 | } |
| 6398 | /* |
| 6399 | * Skip to next node |
| 6400 | */ |
| 6401 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 6402 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 6403 | tmp = tmp->children; |
| 6404 | continue; |
| 6405 | } |
| 6406 | } |
| 6407 | if (tmp == node) |
| 6408 | break; |
| 6409 | |
| 6410 | if (tmp->next != NULL) { |
| 6411 | tmp = tmp->next; |
| 6412 | continue; |
| 6413 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6414 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6415 | do { |
| 6416 | tmp = tmp->parent; |
| 6417 | if (tmp == NULL) |
| 6418 | break; |
| 6419 | if (tmp == node) { |
| 6420 | tmp = NULL; |
| 6421 | break; |
| 6422 | } |
| 6423 | if (tmp->next != NULL) { |
| 6424 | tmp = tmp->next; |
| 6425 | break; |
| 6426 | } |
| 6427 | } while (tmp != NULL); |
| 6428 | } |
| 6429 | return(ret); |
| 6430 | } |
| 6431 | |
| 6432 | /** |
| 6433 | * xmlXPathStringHash: |
| 6434 | * @string: a string |
| 6435 | * |
| 6436 | * Function computing the beginning of the string value of the node, |
| 6437 | * used to speed up comparisons |
| 6438 | * |
| 6439 | * Returns an int usable as a hash |
| 6440 | */ |
| 6441 | static unsigned int |
| 6442 | xmlXPathStringHash(const xmlChar * string) { |
| 6443 | if (string == NULL) |
| 6444 | return((unsigned int) 0); |
| 6445 | if (string[0] == 0) |
| 6446 | return(0); |
| 6447 | return(((unsigned int) string[0]) + |
| 6448 | (((unsigned int) string[1]) << 8)); |
| 6449 | } |
| 6450 | |
| 6451 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6452 | * xmlXPathCompareNodeSetFloat: |
| 6453 | * @ctxt: the XPath Parser context |
| 6454 | * @inf: less than (1) or greater than (0) |
| 6455 | * @strict: is the comparison strict |
| 6456 | * @arg: the node set |
| 6457 | * @f: the value |
| 6458 | * |
| 6459 | * Implement the compare operation between a nodeset and a number |
| 6460 | * @ns < @val (1, 1, ... |
| 6461 | * @ns <= @val (1, 0, ... |
| 6462 | * @ns > @val (0, 1, ... |
| 6463 | * @ns >= @val (0, 0, ... |
| 6464 | * |
| 6465 | * If one object to be compared is a node-set and the other is a number, |
| 6466 | * then the comparison will be true if and only if there is a node in the |
| 6467 | * node-set such that the result of performing the comparison on the number |
| 6468 | * to be compared and on the result of converting the string-value of that |
| 6469 | * node to a number using the number function is true. |
| 6470 | * |
| 6471 | * Returns 0 or 1 depending on the results of the test. |
| 6472 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6473 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6474 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6475 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 6476 | int i, ret = 0; |
| 6477 | xmlNodeSetPtr ns; |
| 6478 | xmlChar *str2; |
| 6479 | |
| 6480 | if ((f == NULL) || (arg == NULL) || |
| 6481 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6482 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6483 | xmlXPathReleaseObject(ctxt->context, f); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6484 | return(0); |
| 6485 | } |
| 6486 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6487 | if (ns != NULL) { |
| 6488 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6489 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6490 | if (str2 != NULL) { |
| 6491 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6492 | xmlXPathCacheNewString(ctxt->context, str2)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6493 | xmlFree(str2); |
| 6494 | xmlXPathNumberFunction(ctxt, 1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6495 | valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, f)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6496 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 6497 | if (ret) |
| 6498 | break; |
| 6499 | } |
| 6500 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6501 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6502 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6503 | xmlXPathReleaseObject(ctxt->context, f); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | return(ret); |
| 6505 | } |
| 6506 | |
| 6507 | /** |
| 6508 | * xmlXPathCompareNodeSetString: |
| 6509 | * @ctxt: the XPath Parser context |
| 6510 | * @inf: less than (1) or greater than (0) |
| 6511 | * @strict: is the comparison strict |
| 6512 | * @arg: the node set |
| 6513 | * @s: the value |
| 6514 | * |
| 6515 | * Implement the compare operation between a nodeset and a string |
| 6516 | * @ns < @val (1, 1, ... |
| 6517 | * @ns <= @val (1, 0, ... |
| 6518 | * @ns > @val (0, 1, ... |
| 6519 | * @ns >= @val (0, 0, ... |
| 6520 | * |
| 6521 | * If one object to be compared is a node-set and the other is a string, |
| 6522 | * then the comparison will be true if and only if there is a node in |
| 6523 | * the node-set such that the result of performing the comparison on the |
| 6524 | * string-value of the node and the other string is true. |
| 6525 | * |
| 6526 | * Returns 0 or 1 depending on the results of the test. |
| 6527 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6528 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6529 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6530 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 6531 | int i, ret = 0; |
| 6532 | xmlNodeSetPtr ns; |
| 6533 | xmlChar *str2; |
| 6534 | |
| 6535 | if ((s == NULL) || (arg == NULL) || |
| 6536 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6537 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6538 | xmlXPathReleaseObject(ctxt->context, s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | return(0); |
| 6540 | } |
| 6541 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6542 | if (ns != NULL) { |
| 6543 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6544 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6545 | if (str2 != NULL) { |
| 6546 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6547 | xmlXPathCacheNewString(ctxt->context, str2)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6548 | xmlFree(str2); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6549 | valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, s)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6550 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 6551 | if (ret) |
| 6552 | break; |
| 6553 | } |
| 6554 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6555 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6556 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6557 | xmlXPathReleaseObject(ctxt->context, s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6558 | return(ret); |
| 6559 | } |
| 6560 | |
| 6561 | /** |
| 6562 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6563 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6564 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6565 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6566 | * @arg2: the second node set object |
| 6567 | * |
| 6568 | * Implement the compare operation on nodesets: |
| 6569 | * |
| 6570 | * If both objects to be compared are node-sets, then the comparison |
| 6571 | * will be true if and only if there is a node in the first node-set |
| 6572 | * and a node in the second node-set such that the result of performing |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6573 | * the comparison on the string-values of the two nodes is true. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6574 | * .... |
| 6575 | * When neither object to be compared is a node-set and the operator |
| 6576 | * is <=, <, >= or >, then the objects are compared by converting both |
| 6577 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 6578 | * .... |
| 6579 | * The number function converts its argument to a number as follows: |
| 6580 | * - a string that consists of optional whitespace followed by an |
| 6581 | * optional minus sign followed by a Number followed by whitespace |
| 6582 | * is converted to the IEEE 754 number that is nearest (according |
| 6583 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 6584 | * represented by the string; any other string is converted to NaN |
| 6585 | * |
| 6586 | * Conclusion all nodes need to be converted first to their string value |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6587 | * and then the comparison must be done when possible |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6589 | static int |
| 6590 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6591 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 6592 | int i, j, init = 0; |
| 6593 | double val1; |
| 6594 | double *values2; |
| 6595 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6596 | xmlNodeSetPtr ns1; |
| 6597 | xmlNodeSetPtr ns2; |
| 6598 | |
| 6599 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6600 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 6601 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6602 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6603 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6605 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 6606 | xmlXPathFreeObject(arg1); |
| 6607 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6608 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6609 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6610 | |
| 6611 | ns1 = arg1->nodesetval; |
| 6612 | ns2 = arg2->nodesetval; |
| 6613 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6614 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6615 | xmlXPathFreeObject(arg1); |
| 6616 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6617 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6618 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6619 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6620 | xmlXPathFreeObject(arg1); |
| 6621 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6622 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6623 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6624 | |
| 6625 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 6626 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6627 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6628 | xmlXPathFreeObject(arg1); |
| 6629 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6630 | return(0); |
| 6631 | } |
| 6632 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6633 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 6634 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6635 | continue; |
| 6636 | for (j = 0;j < ns2->nodeNr;j++) { |
| 6637 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6638 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6639 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 6640 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | continue; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6642 | if (inf && strict) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6643 | ret = (val1 < values2[j]); |
| 6644 | else if (inf && !strict) |
| 6645 | ret = (val1 <= values2[j]); |
| 6646 | else if (!inf && strict) |
| 6647 | ret = (val1 > values2[j]); |
| 6648 | else if (!inf && !strict) |
| 6649 | ret = (val1 >= values2[j]); |
| 6650 | if (ret) |
| 6651 | break; |
| 6652 | } |
| 6653 | if (ret) |
| 6654 | break; |
| 6655 | init = 1; |
| 6656 | } |
| 6657 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6658 | xmlXPathFreeObject(arg1); |
| 6659 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6660 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | /** |
| 6664 | * xmlXPathCompareNodeSetValue: |
| 6665 | * @ctxt: the XPath Parser context |
| 6666 | * @inf: less than (1) or greater than (0) |
| 6667 | * @strict: is the comparison strict |
| 6668 | * @arg: the node set |
| 6669 | * @val: the value |
| 6670 | * |
| 6671 | * Implement the compare operation between a nodeset and a value |
| 6672 | * @ns < @val (1, 1, ... |
| 6673 | * @ns <= @val (1, 0, ... |
| 6674 | * @ns > @val (0, 1, ... |
| 6675 | * @ns >= @val (0, 0, ... |
| 6676 | * |
| 6677 | * If one object to be compared is a node-set and the other is a boolean, |
| 6678 | * then the comparison will be true if and only if the result of performing |
| 6679 | * the comparison on the boolean and on the result of converting |
| 6680 | * the node-set to a boolean using the boolean function is true. |
| 6681 | * |
| 6682 | * Returns 0 or 1 depending on the results of the test. |
| 6683 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6684 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6685 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6686 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 6687 | if ((val == NULL) || (arg == NULL) || |
| 6688 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6689 | return(0); |
| 6690 | |
| 6691 | switch(val->type) { |
| 6692 | case XPATH_NUMBER: |
| 6693 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 6694 | case XPATH_NODESET: |
| 6695 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6696 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | case XPATH_STRING: |
| 6698 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 6699 | case XPATH_BOOLEAN: |
| 6700 | valuePush(ctxt, arg); |
| 6701 | xmlXPathBooleanFunction(ctxt, 1); |
| 6702 | valuePush(ctxt, val); |
| 6703 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 6704 | default: |
| 6705 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6706 | } |
| 6707 | return(0); |
| 6708 | } |
| 6709 | |
| 6710 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6711 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6712 | * @arg: the nodeset object argument |
| 6713 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6714 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6715 | * |
| 6716 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 6717 | * If one object to be compared is a node-set and the other is a string, |
| 6718 | * then the comparison will be true if and only if there is a node in |
| 6719 | * the node-set such that the result of performing the comparison on the |
| 6720 | * string-value of the node and the other string is true. |
| 6721 | * |
| 6722 | * Returns 0 or 1 depending on the results of the test. |
| 6723 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6724 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6725 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6726 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6727 | int i; |
| 6728 | xmlNodeSetPtr ns; |
| 6729 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6730 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6731 | |
| 6732 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6733 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6734 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6735 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 6736 | /* |
| 6737 | * A NULL nodeset compared with a string is always false |
| 6738 | * (since there is no node equal, and no node not equal) |
| 6739 | */ |
| 6740 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6741 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 6742 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6743 | for (i = 0; i < ns->nodeNr; i++) { |
| 6744 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 6745 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 6746 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 6747 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6748 | if (neq) |
| 6749 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6750 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 6751 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 6752 | if (neq) |
| 6753 | continue; |
| 6754 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6755 | } else if (neq) { |
| 6756 | if (str2 != NULL) |
| 6757 | xmlFree(str2); |
| 6758 | return (1); |
| 6759 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6760 | if (str2 != NULL) |
| 6761 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6762 | } else if (neq) |
| 6763 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6765 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6769 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6770 | * @arg: the nodeset object argument |
| 6771 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6772 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | * |
| 6774 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 6775 | * If one object to be compared is a node-set and the other is a number, |
| 6776 | * then the comparison will be true if and only if there is a node in |
| 6777 | * the node-set such that the result of performing the comparison on the |
| 6778 | * number to be compared and on the result of converting the string-value |
| 6779 | * of that node to a number using the number function is true. |
| 6780 | * |
| 6781 | * Returns 0 or 1 depending on the results of the test. |
| 6782 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6783 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6784 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 6785 | xmlXPathObjectPtr arg, double f, int neq) { |
| 6786 | int i, ret=0; |
| 6787 | xmlNodeSetPtr ns; |
| 6788 | xmlChar *str2; |
| 6789 | xmlXPathObjectPtr val; |
| 6790 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6791 | |
| 6792 | if ((arg == NULL) || |
| 6793 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6794 | return(0); |
| 6795 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6796 | ns = arg->nodesetval; |
| 6797 | if (ns != NULL) { |
| 6798 | for (i=0;i<ns->nodeNr;i++) { |
| 6799 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 6800 | if (str2 != NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6801 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, str2)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6802 | xmlFree(str2); |
| 6803 | xmlXPathNumberFunction(ctxt, 1); |
| 6804 | val = valuePop(ctxt); |
| 6805 | v = val->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6806 | xmlXPathReleaseObject(ctxt->context, val); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6807 | if (!xmlXPathIsNaN(v)) { |
| 6808 | if ((!neq) && (v==f)) { |
| 6809 | ret = 1; |
| 6810 | break; |
| 6811 | } else if ((neq) && (v!=f)) { |
| 6812 | ret = 1; |
| 6813 | break; |
| 6814 | } |
William M. Brack | 32f0f71 | 2005-07-14 07:00:33 +0000 | [diff] [blame] | 6815 | } else { /* NaN is unequal to any value */ |
| 6816 | if (neq) |
| 6817 | ret = 1; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6818 | } |
| 6819 | } |
| 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | |
| 6827 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6828 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6829 | * @arg1: first nodeset object argument |
| 6830 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6831 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6832 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6833 | * Implement the equal / not equal operation on XPath nodesets: |
| 6834 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6835 | * If both objects to be compared are node-sets, then the comparison |
| 6836 | * will be true if and only if there is a node in the first node-set and |
| 6837 | * a node in the second node-set such that the result of performing the |
| 6838 | * comparison on the string-values of the two nodes is true. |
| 6839 | * |
| 6840 | * (needless to say, this is a costly operation) |
| 6841 | * |
| 6842 | * Returns 0 or 1 depending on the results of the test. |
| 6843 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6844 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6845 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6846 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6847 | unsigned int *hashs1; |
| 6848 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6849 | xmlChar **values1; |
| 6850 | xmlChar **values2; |
| 6851 | int ret = 0; |
| 6852 | xmlNodeSetPtr ns1; |
| 6853 | xmlNodeSetPtr ns2; |
| 6854 | |
| 6855 | if ((arg1 == NULL) || |
| 6856 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 6857 | return(0); |
| 6858 | if ((arg2 == NULL) || |
| 6859 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 6860 | return(0); |
| 6861 | |
| 6862 | ns1 = arg1->nodesetval; |
| 6863 | ns2 = arg2->nodesetval; |
| 6864 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6865 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6866 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6867 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | return(0); |
| 6869 | |
| 6870 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6871 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6872 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6873 | if (neq == 0) |
| 6874 | for (i = 0;i < ns1->nodeNr;i++) |
| 6875 | for (j = 0;j < ns2->nodeNr;j++) |
| 6876 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 6877 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6878 | |
| 6879 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6880 | if (values1 == NULL) { |
| 6881 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6882 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6883 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6884 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 6885 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6886 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6887 | xmlFree(values1); |
| 6888 | return(0); |
| 6889 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6890 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 6891 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 6892 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6893 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6894 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | xmlFree(values1); |
| 6896 | return(0); |
| 6897 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6898 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 6899 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6900 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6901 | xmlFree(hashs1); |
| 6902 | xmlFree(values1); |
| 6903 | xmlFree(values2); |
| 6904 | return(0); |
| 6905 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6906 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 6907 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6908 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6909 | for (j = 0;j < ns2->nodeNr;j++) { |
| 6910 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6911 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6912 | if (hashs1[i] != hashs2[j]) { |
| 6913 | if (neq) { |
| 6914 | ret = 1; |
| 6915 | break; |
| 6916 | } |
| 6917 | } |
| 6918 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6919 | if (values1[i] == NULL) |
| 6920 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 6921 | if (values2[j] == NULL) |
| 6922 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6923 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6924 | if (ret) |
| 6925 | break; |
| 6926 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6927 | } |
| 6928 | if (ret) |
| 6929 | break; |
| 6930 | } |
| 6931 | for (i = 0;i < ns1->nodeNr;i++) |
| 6932 | if (values1[i] != NULL) |
| 6933 | xmlFree(values1[i]); |
| 6934 | for (j = 0;j < ns2->nodeNr;j++) |
| 6935 | if (values2[j] != NULL) |
| 6936 | xmlFree(values2[j]); |
| 6937 | xmlFree(values1); |
| 6938 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6939 | xmlFree(hashs1); |
| 6940 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6941 | return(ret); |
| 6942 | } |
| 6943 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6944 | static int |
| 6945 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 6946 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6947 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6948 | /* |
| 6949 | *At this point we are assured neither arg1 nor arg2 |
| 6950 | *is a nodeset, so we can just pick the appropriate routine. |
| 6951 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6952 | switch (arg1->type) { |
| 6953 | case XPATH_UNDEFINED: |
| 6954 | #ifdef DEBUG_EXPR |
| 6955 | xmlGenericError(xmlGenericErrorContext, |
| 6956 | "Equal: undefined\n"); |
| 6957 | #endif |
| 6958 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6959 | case XPATH_BOOLEAN: |
| 6960 | switch (arg2->type) { |
| 6961 | case XPATH_UNDEFINED: |
| 6962 | #ifdef DEBUG_EXPR |
| 6963 | xmlGenericError(xmlGenericErrorContext, |
| 6964 | "Equal: undefined\n"); |
| 6965 | #endif |
| 6966 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6967 | case XPATH_BOOLEAN: |
| 6968 | #ifdef DEBUG_EXPR |
| 6969 | xmlGenericError(xmlGenericErrorContext, |
| 6970 | "Equal: %d boolean %d \n", |
| 6971 | arg1->boolval, arg2->boolval); |
| 6972 | #endif |
| 6973 | ret = (arg1->boolval == arg2->boolval); |
| 6974 | break; |
| 6975 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 6976 | ret = (arg1->boolval == |
| 6977 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6978 | break; |
| 6979 | case XPATH_STRING: |
| 6980 | if ((arg2->stringval == NULL) || |
| 6981 | (arg2->stringval[0] == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6982 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6983 | ret = 1; |
| 6984 | ret = (arg1->boolval == ret); |
| 6985 | break; |
| 6986 | case XPATH_USERS: |
| 6987 | case XPATH_POINT: |
| 6988 | case XPATH_RANGE: |
| 6989 | case XPATH_LOCATIONSET: |
| 6990 | TODO |
| 6991 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6992 | case XPATH_NODESET: |
| 6993 | case XPATH_XSLT_TREE: |
| 6994 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6995 | } |
| 6996 | break; |
| 6997 | case XPATH_NUMBER: |
| 6998 | switch (arg2->type) { |
| 6999 | case XPATH_UNDEFINED: |
| 7000 | #ifdef DEBUG_EXPR |
| 7001 | xmlGenericError(xmlGenericErrorContext, |
| 7002 | "Equal: undefined\n"); |
| 7003 | #endif |
| 7004 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7005 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 7006 | ret = (arg2->boolval== |
| 7007 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7008 | break; |
| 7009 | case XPATH_STRING: |
| 7010 | valuePush(ctxt, arg2); |
| 7011 | xmlXPathNumberFunction(ctxt, 1); |
| 7012 | arg2 = valuePop(ctxt); |
| 7013 | /* no break on purpose */ |
| 7014 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7015 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7016 | if (xmlXPathIsNaN(arg1->floatval) || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7017 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7018 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7019 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 7020 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 7021 | ret = 1; |
| 7022 | else |
| 7023 | ret = 0; |
| 7024 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 7025 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 7026 | ret = 1; |
| 7027 | else |
| 7028 | ret = 0; |
| 7029 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 7030 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 7031 | ret = 1; |
| 7032 | else |
| 7033 | ret = 0; |
| 7034 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 7035 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 7036 | ret = 1; |
| 7037 | else |
| 7038 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7039 | } else { |
| 7040 | ret = (arg1->floatval == arg2->floatval); |
| 7041 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7042 | break; |
| 7043 | case XPATH_USERS: |
| 7044 | case XPATH_POINT: |
| 7045 | case XPATH_RANGE: |
| 7046 | case XPATH_LOCATIONSET: |
| 7047 | TODO |
| 7048 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7049 | case XPATH_NODESET: |
| 7050 | case XPATH_XSLT_TREE: |
| 7051 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7052 | } |
| 7053 | break; |
| 7054 | case XPATH_STRING: |
| 7055 | switch (arg2->type) { |
| 7056 | case XPATH_UNDEFINED: |
| 7057 | #ifdef DEBUG_EXPR |
| 7058 | xmlGenericError(xmlGenericErrorContext, |
| 7059 | "Equal: undefined\n"); |
| 7060 | #endif |
| 7061 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7062 | case XPATH_BOOLEAN: |
| 7063 | if ((arg1->stringval == NULL) || |
| 7064 | (arg1->stringval[0] == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7065 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7066 | ret = 1; |
| 7067 | ret = (arg2->boolval == ret); |
| 7068 | break; |
| 7069 | case XPATH_STRING: |
| 7070 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 7071 | break; |
| 7072 | case XPATH_NUMBER: |
| 7073 | valuePush(ctxt, arg1); |
| 7074 | xmlXPathNumberFunction(ctxt, 1); |
| 7075 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7076 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7077 | if (xmlXPathIsNaN(arg1->floatval) || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7078 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7079 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7080 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 7081 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 7082 | ret = 1; |
| 7083 | else |
| 7084 | ret = 0; |
| 7085 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 7086 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 7087 | ret = 1; |
| 7088 | else |
| 7089 | ret = 0; |
| 7090 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 7091 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 7092 | ret = 1; |
| 7093 | else |
| 7094 | ret = 0; |
| 7095 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 7096 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 7097 | ret = 1; |
| 7098 | else |
| 7099 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7100 | } else { |
| 7101 | ret = (arg1->floatval == arg2->floatval); |
| 7102 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7103 | break; |
| 7104 | case XPATH_USERS: |
| 7105 | case XPATH_POINT: |
| 7106 | case XPATH_RANGE: |
| 7107 | case XPATH_LOCATIONSET: |
| 7108 | TODO |
| 7109 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7110 | case XPATH_NODESET: |
| 7111 | case XPATH_XSLT_TREE: |
| 7112 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7113 | } |
| 7114 | break; |
| 7115 | case XPATH_USERS: |
| 7116 | case XPATH_POINT: |
| 7117 | case XPATH_RANGE: |
| 7118 | case XPATH_LOCATIONSET: |
| 7119 | TODO |
| 7120 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7121 | case XPATH_NODESET: |
| 7122 | case XPATH_XSLT_TREE: |
| 7123 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7124 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7125 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7126 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7127 | return(ret); |
| 7128 | } |
| 7129 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7130 | /** |
| 7131 | * xmlXPathEqualValues: |
| 7132 | * @ctxt: the XPath Parser context |
| 7133 | * |
| 7134 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 7135 | * |
| 7136 | * Returns 0 or 1 depending on the results of the test. |
| 7137 | */ |
| 7138 | int |
| 7139 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 7140 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 7141 | int ret = 0; |
| 7142 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7143 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7144 | arg2 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7145 | arg1 = valuePop(ctxt); |
| 7146 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7147 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7148 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7149 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7150 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7151 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7152 | } |
| 7153 | |
| 7154 | if (arg1 == arg2) { |
| 7155 | #ifdef DEBUG_EXPR |
| 7156 | xmlGenericError(xmlGenericErrorContext, |
| 7157 | "Equal: by pointer\n"); |
| 7158 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 7159 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7160 | return(1); |
| 7161 | } |
| 7162 | |
| 7163 | /* |
| 7164 | *If either argument is a nodeset, it's a 'special case' |
| 7165 | */ |
| 7166 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7167 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 7168 | /* |
| 7169 | *Hack it to assure arg1 is the nodeset |
| 7170 | */ |
| 7171 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 7172 | argtmp = arg2; |
| 7173 | arg2 = arg1; |
| 7174 | arg1 = argtmp; |
| 7175 | } |
| 7176 | switch (arg2->type) { |
| 7177 | case XPATH_UNDEFINED: |
| 7178 | #ifdef DEBUG_EXPR |
| 7179 | xmlGenericError(xmlGenericErrorContext, |
| 7180 | "Equal: undefined\n"); |
| 7181 | #endif |
| 7182 | break; |
| 7183 | case XPATH_NODESET: |
| 7184 | case XPATH_XSLT_TREE: |
| 7185 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 7186 | break; |
| 7187 | case XPATH_BOOLEAN: |
| 7188 | if ((arg1->nodesetval == NULL) || |
| 7189 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7190 | else |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7191 | ret = 1; |
| 7192 | ret = (ret == arg2->boolval); |
| 7193 | break; |
| 7194 | case XPATH_NUMBER: |
| 7195 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 7196 | break; |
| 7197 | case XPATH_STRING: |
| 7198 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 7199 | break; |
| 7200 | case XPATH_USERS: |
| 7201 | case XPATH_POINT: |
| 7202 | case XPATH_RANGE: |
| 7203 | case XPATH_LOCATIONSET: |
| 7204 | TODO |
| 7205 | break; |
| 7206 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7207 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7208 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7209 | return(ret); |
| 7210 | } |
| 7211 | |
| 7212 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 7213 | } |
| 7214 | |
| 7215 | /** |
| 7216 | * xmlXPathNotEqualValues: |
| 7217 | * @ctxt: the XPath Parser context |
| 7218 | * |
| 7219 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 7220 | * |
| 7221 | * Returns 0 or 1 depending on the results of the test. |
| 7222 | */ |
| 7223 | int |
| 7224 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 7225 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 7226 | int ret = 0; |
| 7227 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7228 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7229 | arg2 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7230 | arg1 = valuePop(ctxt); |
| 7231 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7232 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7233 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7234 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7235 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7236 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7237 | } |
| 7238 | |
| 7239 | if (arg1 == arg2) { |
| 7240 | #ifdef DEBUG_EXPR |
| 7241 | xmlGenericError(xmlGenericErrorContext, |
| 7242 | "NotEqual: by pointer\n"); |
| 7243 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7244 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7245 | return(0); |
| 7246 | } |
| 7247 | |
| 7248 | /* |
| 7249 | *If either argument is a nodeset, it's a 'special case' |
| 7250 | */ |
| 7251 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7252 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 7253 | /* |
| 7254 | *Hack it to assure arg1 is the nodeset |
| 7255 | */ |
| 7256 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 7257 | argtmp = arg2; |
| 7258 | arg2 = arg1; |
| 7259 | arg1 = argtmp; |
| 7260 | } |
| 7261 | switch (arg2->type) { |
| 7262 | case XPATH_UNDEFINED: |
| 7263 | #ifdef DEBUG_EXPR |
| 7264 | xmlGenericError(xmlGenericErrorContext, |
| 7265 | "NotEqual: undefined\n"); |
| 7266 | #endif |
| 7267 | break; |
| 7268 | case XPATH_NODESET: |
| 7269 | case XPATH_XSLT_TREE: |
| 7270 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 7271 | break; |
| 7272 | case XPATH_BOOLEAN: |
| 7273 | if ((arg1->nodesetval == NULL) || |
| 7274 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7275 | else |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7276 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 7277 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7278 | break; |
| 7279 | case XPATH_NUMBER: |
| 7280 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 7281 | break; |
| 7282 | case XPATH_STRING: |
| 7283 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 7284 | break; |
| 7285 | case XPATH_USERS: |
| 7286 | case XPATH_POINT: |
| 7287 | case XPATH_RANGE: |
| 7288 | case XPATH_LOCATIONSET: |
| 7289 | TODO |
| 7290 | break; |
| 7291 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7292 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7293 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7294 | return(ret); |
| 7295 | } |
| 7296 | |
| 7297 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 7298 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7299 | |
| 7300 | /** |
| 7301 | * xmlXPathCompareValues: |
| 7302 | * @ctxt: the XPath Parser context |
| 7303 | * @inf: less than (1) or greater than (0) |
| 7304 | * @strict: is the comparison strict |
| 7305 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7306 | * Implement the compare operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | * @arg1 < @arg2 (1, 1, ... |
| 7308 | * @arg1 <= @arg2 (1, 0, ... |
| 7309 | * @arg1 > @arg2 (0, 1, ... |
| 7310 | * @arg1 >= @arg2 (0, 0, ... |
| 7311 | * |
| 7312 | * When neither object to be compared is a node-set and the operator is |
| 7313 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 7314 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 7315 | * comparison will be true if and only if the first number is less than the |
| 7316 | * second number. The <= comparison will be true if and only if the first |
| 7317 | * number is less than or equal to the second number. The > comparison |
| 7318 | * will be true if and only if the first number is greater than the second |
| 7319 | * number. The >= comparison will be true if and only if the first number |
| 7320 | * is greater than or equal to the second number. |
| 7321 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7322 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7323 | */ |
| 7324 | int |
| 7325 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7326 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7327 | xmlXPathObjectPtr arg1, arg2; |
| 7328 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7329 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7330 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7331 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7332 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7333 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7334 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7335 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7336 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7337 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7338 | } |
| 7339 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7340 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7341 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 7342 | /* |
| 7343 | * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments |
| 7344 | * are not freed from within this routine; they will be freed from the |
| 7345 | * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue |
| 7346 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7347 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 7348 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7349 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7350 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7351 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 7352 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 7353 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7354 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 7355 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 7356 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7357 | } |
| 7358 | } |
| 7359 | return(ret); |
| 7360 | } |
| 7361 | |
| 7362 | if (arg1->type != XPATH_NUMBER) { |
| 7363 | valuePush(ctxt, arg1); |
| 7364 | xmlXPathNumberFunction(ctxt, 1); |
| 7365 | arg1 = valuePop(ctxt); |
| 7366 | } |
| 7367 | if (arg1->type != XPATH_NUMBER) { |
| 7368 | xmlXPathFreeObject(arg1); |
| 7369 | xmlXPathFreeObject(arg2); |
| 7370 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7371 | } |
| 7372 | if (arg2->type != XPATH_NUMBER) { |
| 7373 | valuePush(ctxt, arg2); |
| 7374 | xmlXPathNumberFunction(ctxt, 1); |
| 7375 | arg2 = valuePop(ctxt); |
| 7376 | } |
| 7377 | if (arg2->type != XPATH_NUMBER) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7378 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7379 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7380 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7381 | } |
| 7382 | /* |
| 7383 | * Add tests for infinity and nan |
| 7384 | * => feedback on 3.4 for Inf and NaN |
| 7385 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7386 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7387 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7388 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7389 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7390 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 7391 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 7392 | if (inf && strict) { |
| 7393 | if ((arg1i == -1 && arg2i != -1) || |
| 7394 | (arg2i == 1 && arg1i != 1)) { |
| 7395 | ret = 1; |
| 7396 | } else if (arg1i == 0 && arg2i == 0) { |
| 7397 | ret = (arg1->floatval < arg2->floatval); |
| 7398 | } else { |
| 7399 | ret = 0; |
| 7400 | } |
| 7401 | } |
| 7402 | else if (inf && !strict) { |
| 7403 | if (arg1i == -1 || arg2i == 1) { |
| 7404 | ret = 1; |
| 7405 | } else if (arg1i == 0 && arg2i == 0) { |
| 7406 | ret = (arg1->floatval <= arg2->floatval); |
| 7407 | } else { |
| 7408 | ret = 0; |
| 7409 | } |
| 7410 | } |
| 7411 | else if (!inf && strict) { |
| 7412 | if ((arg1i == 1 && arg2i != 1) || |
| 7413 | (arg2i == -1 && arg1i != -1)) { |
| 7414 | ret = 1; |
| 7415 | } else if (arg1i == 0 && arg2i == 0) { |
| 7416 | ret = (arg1->floatval > arg2->floatval); |
| 7417 | } else { |
| 7418 | ret = 0; |
| 7419 | } |
| 7420 | } |
| 7421 | else if (!inf && !strict) { |
| 7422 | if (arg1i == 1 || arg2i == -1) { |
| 7423 | ret = 1; |
| 7424 | } else if (arg1i == 0 && arg2i == 0) { |
| 7425 | ret = (arg1->floatval >= arg2->floatval); |
| 7426 | } else { |
| 7427 | ret = 0; |
| 7428 | } |
| 7429 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7430 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7431 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7432 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7433 | return(ret); |
| 7434 | } |
| 7435 | |
| 7436 | /** |
| 7437 | * xmlXPathValueFlipSign: |
| 7438 | * @ctxt: the XPath Parser context |
| 7439 | * |
| 7440 | * Implement the unary - operation on an XPath object |
| 7441 | * The numeric operators convert their operands to numbers as if |
| 7442 | * by calling the number function. |
| 7443 | */ |
| 7444 | void |
| 7445 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7446 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7447 | CAST_TO_NUMBER; |
| 7448 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7449 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 7450 | ctxt->value->floatval=xmlXPathNAN; |
| 7451 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 7452 | ctxt->value->floatval=xmlXPathNINF; |
| 7453 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 7454 | ctxt->value->floatval=xmlXPathPINF; |
| 7455 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7456 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 7457 | ctxt->value->floatval = xmlXPathNZERO; |
| 7458 | else |
| 7459 | ctxt->value->floatval = 0; |
| 7460 | } |
| 7461 | else |
| 7462 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7463 | } |
| 7464 | |
| 7465 | /** |
| 7466 | * xmlXPathAddValues: |
| 7467 | * @ctxt: the XPath Parser context |
| 7468 | * |
| 7469 | * Implement the add operation on XPath objects: |
| 7470 | * The numeric operators convert their operands to numbers as if |
| 7471 | * by calling the number function. |
| 7472 | */ |
| 7473 | void |
| 7474 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 7475 | xmlXPathObjectPtr arg; |
| 7476 | double val; |
| 7477 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7478 | arg = valuePop(ctxt); |
| 7479 | if (arg == NULL) |
| 7480 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7481 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7482 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7483 | CAST_TO_NUMBER; |
| 7484 | CHECK_TYPE(XPATH_NUMBER); |
| 7485 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7486 | } |
| 7487 | |
| 7488 | /** |
| 7489 | * xmlXPathSubValues: |
| 7490 | * @ctxt: the XPath Parser context |
| 7491 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7492 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7493 | * The numeric operators convert their operands to numbers as if |
| 7494 | * by calling the number function. |
| 7495 | */ |
| 7496 | void |
| 7497 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 7498 | xmlXPathObjectPtr arg; |
| 7499 | double val; |
| 7500 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7501 | arg = valuePop(ctxt); |
| 7502 | if (arg == NULL) |
| 7503 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7504 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7505 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7506 | CAST_TO_NUMBER; |
| 7507 | CHECK_TYPE(XPATH_NUMBER); |
| 7508 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7509 | } |
| 7510 | |
| 7511 | /** |
| 7512 | * xmlXPathMultValues: |
| 7513 | * @ctxt: the XPath Parser context |
| 7514 | * |
| 7515 | * Implement the multiply operation on XPath objects: |
| 7516 | * The numeric operators convert their operands to numbers as if |
| 7517 | * by calling the number function. |
| 7518 | */ |
| 7519 | void |
| 7520 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 7521 | xmlXPathObjectPtr arg; |
| 7522 | double val; |
| 7523 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7524 | arg = valuePop(ctxt); |
| 7525 | if (arg == NULL) |
| 7526 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7527 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7528 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7529 | CAST_TO_NUMBER; |
| 7530 | CHECK_TYPE(XPATH_NUMBER); |
| 7531 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | /** |
| 7535 | * xmlXPathDivValues: |
| 7536 | * @ctxt: the XPath Parser context |
| 7537 | * |
| 7538 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 7539 | * The numeric operators convert their operands to numbers as if |
| 7540 | * by calling the number function. |
| 7541 | */ |
| 7542 | void |
| 7543 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 7544 | xmlXPathObjectPtr arg; |
| 7545 | double val; |
| 7546 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7547 | arg = valuePop(ctxt); |
| 7548 | if (arg == NULL) |
| 7549 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7550 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7551 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7552 | CAST_TO_NUMBER; |
| 7553 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7554 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 7555 | ctxt->value->floatval = xmlXPathNAN; |
| 7556 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7557 | if (ctxt->value->floatval == 0) |
| 7558 | ctxt->value->floatval = xmlXPathNAN; |
| 7559 | else if (ctxt->value->floatval > 0) |
| 7560 | ctxt->value->floatval = xmlXPathNINF; |
| 7561 | else if (ctxt->value->floatval < 0) |
| 7562 | ctxt->value->floatval = xmlXPathPINF; |
| 7563 | } |
| 7564 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 7565 | if (ctxt->value->floatval == 0) |
| 7566 | ctxt->value->floatval = xmlXPathNAN; |
| 7567 | else if (ctxt->value->floatval > 0) |
| 7568 | ctxt->value->floatval = xmlXPathPINF; |
| 7569 | else if (ctxt->value->floatval < 0) |
| 7570 | ctxt->value->floatval = xmlXPathNINF; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7571 | } else |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 7572 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
| 7575 | /** |
| 7576 | * xmlXPathModValues: |
| 7577 | * @ctxt: the XPath Parser context |
| 7578 | * |
| 7579 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 7580 | * The numeric operators convert their operands to numbers as if |
| 7581 | * by calling the number function. |
| 7582 | */ |
| 7583 | void |
| 7584 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 7585 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7586 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7587 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7588 | arg = valuePop(ctxt); |
| 7589 | if (arg == NULL) |
| 7590 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7591 | arg2 = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7592 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7593 | CAST_TO_NUMBER; |
| 7594 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7595 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 7596 | if (arg2 == 0) |
| 7597 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7598 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7599 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7601 | } |
| 7602 | |
| 7603 | /************************************************************************ |
| 7604 | * * |
| 7605 | * The traversal functions * |
| 7606 | * * |
| 7607 | ************************************************************************/ |
| 7608 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7609 | /* |
| 7610 | * A traversal function enumerates nodes along an axis. |
| 7611 | * Initially it must be called with NULL, and it indicates |
| 7612 | * termination on the axis by returning NULL. |
| 7613 | */ |
| 7614 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 7615 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 7616 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7617 | /* |
| 7618 | * xmlXPathTraversalFunctionExt: |
| 7619 | * A traversal function enumerates nodes along an axis. |
| 7620 | * Initially it must be called with NULL, and it indicates |
| 7621 | * termination on the axis by returning NULL. |
| 7622 | * The context node of the traversal is specified via @contextNode. |
| 7623 | */ |
| 7624 | typedef xmlNodePtr (*xmlXPathTraversalFunctionExt) |
| 7625 | (xmlNodePtr cur, xmlNodePtr contextNode); |
| 7626 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 7627 | /* |
| 7628 | * xmlXPathNodeSetMergeFunction: |
| 7629 | * Used for merging node sets in xmlXPathCollectAndTest(). |
| 7630 | */ |
| 7631 | typedef xmlNodeSetPtr (*xmlXPathNodeSetMergeFunction) |
| 7632 | (xmlNodeSetPtr, xmlNodeSetPtr, int); |
| 7633 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7634 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7635 | /** |
| 7636 | * xmlXPathNextSelf: |
| 7637 | * @ctxt: the XPath Parser context |
| 7638 | * @cur: the current node in the traversal |
| 7639 | * |
| 7640 | * Traversal function for the "self" direction |
| 7641 | * The self axis contains just the context node itself |
| 7642 | * |
| 7643 | * Returns the next element following that axis |
| 7644 | */ |
| 7645 | xmlNodePtr |
| 7646 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7647 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7648 | if (cur == NULL) |
| 7649 | return(ctxt->context->node); |
| 7650 | return(NULL); |
| 7651 | } |
| 7652 | |
| 7653 | /** |
| 7654 | * xmlXPathNextChild: |
| 7655 | * @ctxt: the XPath Parser context |
| 7656 | * @cur: the current node in the traversal |
| 7657 | * |
| 7658 | * Traversal function for the "child" direction |
| 7659 | * The child axis contains the children of the context node in document order. |
| 7660 | * |
| 7661 | * Returns the next element following that axis |
| 7662 | */ |
| 7663 | xmlNodePtr |
| 7664 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7665 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7666 | if (cur == NULL) { |
| 7667 | if (ctxt->context->node == NULL) return(NULL); |
| 7668 | switch (ctxt->context->node->type) { |
| 7669 | case XML_ELEMENT_NODE: |
| 7670 | case XML_TEXT_NODE: |
| 7671 | case XML_CDATA_SECTION_NODE: |
| 7672 | case XML_ENTITY_REF_NODE: |
| 7673 | case XML_ENTITY_NODE: |
| 7674 | case XML_PI_NODE: |
| 7675 | case XML_COMMENT_NODE: |
| 7676 | case XML_NOTATION_NODE: |
| 7677 | case XML_DTD_NODE: |
| 7678 | return(ctxt->context->node->children); |
| 7679 | case XML_DOCUMENT_NODE: |
| 7680 | case XML_DOCUMENT_TYPE_NODE: |
| 7681 | case XML_DOCUMENT_FRAG_NODE: |
| 7682 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 7683 | #ifdef LIBXML_DOCB_ENABLED |
| 7684 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7685 | #endif |
| 7686 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 7687 | case XML_ELEMENT_DECL: |
| 7688 | case XML_ATTRIBUTE_DECL: |
| 7689 | case XML_ENTITY_DECL: |
| 7690 | case XML_ATTRIBUTE_NODE: |
| 7691 | case XML_NAMESPACE_DECL: |
| 7692 | case XML_XINCLUDE_START: |
| 7693 | case XML_XINCLUDE_END: |
| 7694 | return(NULL); |
| 7695 | } |
| 7696 | return(NULL); |
| 7697 | } |
| 7698 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 7699 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 7700 | return(NULL); |
| 7701 | return(cur->next); |
| 7702 | } |
| 7703 | |
| 7704 | /** |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 7705 | * xmlXPathNextChildElement: |
| 7706 | * @ctxt: the XPath Parser context |
| 7707 | * @cur: the current node in the traversal |
| 7708 | * |
| 7709 | * Traversal function for the "child" direction and nodes of type element. |
| 7710 | * The child axis contains the children of the context node in document order. |
| 7711 | * |
| 7712 | * Returns the next element following that axis |
| 7713 | */ |
| 7714 | static xmlNodePtr |
| 7715 | xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 7716 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
| 7717 | if (cur == NULL) { |
| 7718 | cur = ctxt->context->node; |
| 7719 | if (cur == NULL) return(NULL); |
| 7720 | /* |
| 7721 | * Get the first element child. |
| 7722 | */ |
| 7723 | switch (cur->type) { |
| 7724 | case XML_ELEMENT_NODE: |
| 7725 | case XML_DOCUMENT_FRAG_NODE: |
| 7726 | case XML_ENTITY_REF_NODE: /* URGENT TODO: entify-refs as well? */ |
| 7727 | case XML_ENTITY_NODE: |
| 7728 | cur = cur->children; |
| 7729 | if (cur != NULL) { |
| 7730 | if (cur->type == XML_ELEMENT_NODE) |
| 7731 | return(cur); |
| 7732 | do { |
| 7733 | cur = cur->next; |
| 7734 | } while ((cur != NULL) && |
| 7735 | (cur->type != XML_ELEMENT_NODE)); |
| 7736 | return(cur); |
| 7737 | } |
| 7738 | return(NULL); |
| 7739 | case XML_DOCUMENT_NODE: |
| 7740 | case XML_HTML_DOCUMENT_NODE: |
| 7741 | #ifdef LIBXML_DOCB_ENABLED |
| 7742 | case XML_DOCB_DOCUMENT_NODE: |
| 7743 | #endif |
| 7744 | return(xmlDocGetRootElement((xmlDocPtr) cur)); |
| 7745 | default: |
| 7746 | return(NULL); |
| 7747 | } |
| 7748 | return(NULL); |
| 7749 | } |
| 7750 | /* |
| 7751 | * Get the next sibling element node. |
| 7752 | */ |
| 7753 | switch (cur->type) { |
| 7754 | case XML_ELEMENT_NODE: |
| 7755 | case XML_TEXT_NODE: |
| 7756 | case XML_ENTITY_REF_NODE: |
| 7757 | case XML_ENTITY_NODE: |
| 7758 | case XML_CDATA_SECTION_NODE: |
| 7759 | case XML_PI_NODE: |
| 7760 | case XML_COMMENT_NODE: |
| 7761 | case XML_XINCLUDE_END: |
| 7762 | break; |
| 7763 | /* case XML_DTD_NODE: */ /* URGENT TODO: DTD-node as well? */ |
| 7764 | default: |
| 7765 | return(NULL); |
| 7766 | } |
| 7767 | if (cur->next != NULL) { |
| 7768 | if (cur->next->type == XML_ELEMENT_NODE) |
| 7769 | return(cur->next); |
| 7770 | cur = cur->next; |
| 7771 | do { |
| 7772 | cur = cur->next; |
| 7773 | } while ((cur != NULL) && (cur->type != XML_ELEMENT_NODE)); |
| 7774 | return(cur); |
| 7775 | } |
| 7776 | return(NULL); |
| 7777 | } |
| 7778 | |
Daniel Veillard | 7651606 | 2012-09-11 14:02:08 +0800 | [diff] [blame] | 7779 | #if 0 |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 7780 | /** |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7781 | * xmlXPathNextDescendantOrSelfElemParent: |
| 7782 | * @ctxt: the XPath Parser context |
| 7783 | * @cur: the current node in the traversal |
| 7784 | * |
| 7785 | * Traversal function for the "descendant-or-self" axis. |
| 7786 | * Additionally it returns only nodes which can be parents of |
| 7787 | * element nodes. |
| 7788 | * |
| 7789 | * |
| 7790 | * Returns the next element following that axis |
| 7791 | */ |
| 7792 | static xmlNodePtr |
| 7793 | xmlXPathNextDescendantOrSelfElemParent(xmlNodePtr cur, |
| 7794 | xmlNodePtr contextNode) |
| 7795 | { |
| 7796 | if (cur == NULL) { |
| 7797 | if (contextNode == NULL) |
| 7798 | return(NULL); |
| 7799 | switch (contextNode->type) { |
| 7800 | case XML_ELEMENT_NODE: |
| 7801 | case XML_XINCLUDE_START: |
| 7802 | case XML_DOCUMENT_FRAG_NODE: |
| 7803 | case XML_DOCUMENT_NODE: |
| 7804 | #ifdef LIBXML_DOCB_ENABLED |
| 7805 | case XML_DOCB_DOCUMENT_NODE: |
| 7806 | #endif |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7807 | case XML_HTML_DOCUMENT_NODE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7808 | return(contextNode); |
| 7809 | default: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7810 | return(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7811 | } |
| 7812 | return(NULL); |
| 7813 | } else { |
| 7814 | xmlNodePtr start = cur; |
| 7815 | |
| 7816 | while (cur != NULL) { |
| 7817 | switch (cur->type) { |
| 7818 | case XML_ELEMENT_NODE: |
| 7819 | /* TODO: OK to have XInclude here? */ |
| 7820 | case XML_XINCLUDE_START: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7821 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7822 | if (cur != start) |
| 7823 | return(cur); |
| 7824 | if (cur->children != NULL) { |
| 7825 | cur = cur->children; |
| 7826 | continue; |
| 7827 | } |
| 7828 | break; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7829 | /* Not sure if we need those here. */ |
| 7830 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 17970a7 | 2006-10-26 08:55:47 +0000 | [diff] [blame] | 7831 | #ifdef LIBXML_DOCB_ENABLED |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7832 | case XML_DOCB_DOCUMENT_NODE: |
| 7833 | #endif |
| 7834 | case XML_HTML_DOCUMENT_NODE: |
| 7835 | if (cur != start) |
| 7836 | return(cur); |
| 7837 | return(xmlDocGetRootElement((xmlDocPtr) cur)); |
| 7838 | default: |
| 7839 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7840 | } |
| 7841 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7842 | next_sibling: |
| 7843 | if ((cur == NULL) || (cur == contextNode)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7844 | return(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7845 | if (cur->next != NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7846 | cur = cur->next; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7847 | } else { |
| 7848 | cur = cur->parent; |
| 7849 | goto next_sibling; |
| 7850 | } |
| 7851 | } |
| 7852 | } |
| 7853 | return(NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7854 | } |
Daniel Veillard | 7651606 | 2012-09-11 14:02:08 +0800 | [diff] [blame] | 7855 | #endif |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7856 | |
| 7857 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7858 | * xmlXPathNextDescendant: |
| 7859 | * @ctxt: the XPath Parser context |
| 7860 | * @cur: the current node in the traversal |
| 7861 | * |
| 7862 | * Traversal function for the "descendant" direction |
| 7863 | * the descendant axis contains the descendants of the context node in document |
| 7864 | * order; a descendant is a child or a child of a child and so on. |
| 7865 | * |
| 7866 | * Returns the next element following that axis |
| 7867 | */ |
| 7868 | xmlNodePtr |
| 7869 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7870 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7871 | if (cur == NULL) { |
| 7872 | if (ctxt->context->node == NULL) |
| 7873 | return(NULL); |
| 7874 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 7875 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 7876 | return(NULL); |
| 7877 | |
| 7878 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 7879 | return(ctxt->context->doc->children); |
| 7880 | return(ctxt->context->node->children); |
| 7881 | } |
| 7882 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 7883 | if (cur->type == XML_NAMESPACE_DECL) |
| 7884 | return(NULL); |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 7885 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7886 | /* |
| 7887 | * Do not descend on entities declarations |
| 7888 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7889 | if (cur->children->type != XML_ENTITY_DECL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7890 | cur = cur->children; |
| 7891 | /* |
| 7892 | * Skip DTDs |
| 7893 | */ |
| 7894 | if (cur->type != XML_DTD_NODE) |
| 7895 | return(cur); |
| 7896 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
| 7899 | if (cur == ctxt->context->node) return(NULL); |
| 7900 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7901 | while (cur->next != NULL) { |
| 7902 | cur = cur->next; |
| 7903 | if ((cur->type != XML_ENTITY_DECL) && |
| 7904 | (cur->type != XML_DTD_NODE)) |
| 7905 | return(cur); |
| 7906 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7907 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7908 | do { |
| 7909 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 7910 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7911 | if (cur == ctxt->context->node) return(NULL); |
| 7912 | if (cur->next != NULL) { |
| 7913 | cur = cur->next; |
| 7914 | return(cur); |
| 7915 | } |
| 7916 | } while (cur != NULL); |
| 7917 | return(cur); |
| 7918 | } |
| 7919 | |
| 7920 | /** |
| 7921 | * xmlXPathNextDescendantOrSelf: |
| 7922 | * @ctxt: the XPath Parser context |
| 7923 | * @cur: the current node in the traversal |
| 7924 | * |
| 7925 | * Traversal function for the "descendant-or-self" direction |
| 7926 | * the descendant-or-self axis contains the context node and the descendants |
| 7927 | * of the context node in document order; thus the context node is the first |
| 7928 | * node on the axis, and the first child of the context node is the second node |
| 7929 | * on the axis |
| 7930 | * |
| 7931 | * Returns the next element following that axis |
| 7932 | */ |
| 7933 | xmlNodePtr |
| 7934 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7935 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Nick Wellnhofer | f6aaabc | 2015-03-08 16:05:26 +0100 | [diff] [blame] | 7936 | if (cur == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7937 | return(ctxt->context->node); |
Nick Wellnhofer | f6aaabc | 2015-03-08 16:05:26 +0100 | [diff] [blame] | 7938 | |
| 7939 | if (ctxt->context->node == NULL) |
| 7940 | return(NULL); |
| 7941 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 7942 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 7943 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7944 | |
| 7945 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 7946 | } |
| 7947 | |
| 7948 | /** |
| 7949 | * xmlXPathNextParent: |
| 7950 | * @ctxt: the XPath Parser context |
| 7951 | * @cur: the current node in the traversal |
| 7952 | * |
| 7953 | * Traversal function for the "parent" direction |
| 7954 | * The parent axis contains the parent of the context node, if there is one. |
| 7955 | * |
| 7956 | * Returns the next element following that axis |
| 7957 | */ |
| 7958 | xmlNodePtr |
| 7959 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7960 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7961 | /* |
| 7962 | * the parent of an attribute or namespace node is the element |
| 7963 | * to which the attribute or namespace node is attached |
| 7964 | * Namespace handling !!! |
| 7965 | */ |
| 7966 | if (cur == NULL) { |
| 7967 | if (ctxt->context->node == NULL) return(NULL); |
| 7968 | switch (ctxt->context->node->type) { |
| 7969 | case XML_ELEMENT_NODE: |
| 7970 | case XML_TEXT_NODE: |
| 7971 | case XML_CDATA_SECTION_NODE: |
| 7972 | case XML_ENTITY_REF_NODE: |
| 7973 | case XML_ENTITY_NODE: |
| 7974 | case XML_PI_NODE: |
| 7975 | case XML_COMMENT_NODE: |
| 7976 | case XML_NOTATION_NODE: |
| 7977 | case XML_DTD_NODE: |
| 7978 | case XML_ELEMENT_DECL: |
| 7979 | case XML_ATTRIBUTE_DECL: |
| 7980 | case XML_XINCLUDE_START: |
| 7981 | case XML_XINCLUDE_END: |
| 7982 | case XML_ENTITY_DECL: |
| 7983 | if (ctxt->context->node->parent == NULL) |
| 7984 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7985 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 7986 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 7987 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 7988 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7989 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7990 | return(ctxt->context->node->parent); |
| 7991 | case XML_ATTRIBUTE_NODE: { |
| 7992 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 7993 | |
| 7994 | return(att->parent); |
| 7995 | } |
| 7996 | case XML_DOCUMENT_NODE: |
| 7997 | case XML_DOCUMENT_TYPE_NODE: |
| 7998 | case XML_DOCUMENT_FRAG_NODE: |
| 7999 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 8000 | #ifdef LIBXML_DOCB_ENABLED |
| 8001 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8002 | #endif |
| 8003 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8004 | case XML_NAMESPACE_DECL: { |
| 8005 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8006 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8007 | if ((ns->next != NULL) && |
| 8008 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 8009 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8010 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8011 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8012 | } |
| 8013 | } |
| 8014 | return(NULL); |
| 8015 | } |
| 8016 | |
| 8017 | /** |
| 8018 | * xmlXPathNextAncestor: |
| 8019 | * @ctxt: the XPath Parser context |
| 8020 | * @cur: the current node in the traversal |
| 8021 | * |
| 8022 | * Traversal function for the "ancestor" direction |
| 8023 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 8024 | * of the context node consist of the parent of context node and the parent's |
| 8025 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 8026 | * parent is the first node on the axis, and the parent's parent is the second |
| 8027 | * node on the axis |
| 8028 | * |
| 8029 | * Returns the next element following that axis |
| 8030 | */ |
| 8031 | xmlNodePtr |
| 8032 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8033 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8034 | /* |
| 8035 | * the parent of an attribute or namespace node is the element |
| 8036 | * to which the attribute or namespace node is attached |
| 8037 | * !!!!!!!!!!!!! |
| 8038 | */ |
| 8039 | if (cur == NULL) { |
| 8040 | if (ctxt->context->node == NULL) return(NULL); |
| 8041 | switch (ctxt->context->node->type) { |
| 8042 | case XML_ELEMENT_NODE: |
| 8043 | case XML_TEXT_NODE: |
| 8044 | case XML_CDATA_SECTION_NODE: |
| 8045 | case XML_ENTITY_REF_NODE: |
| 8046 | case XML_ENTITY_NODE: |
| 8047 | case XML_PI_NODE: |
| 8048 | case XML_COMMENT_NODE: |
| 8049 | case XML_DTD_NODE: |
| 8050 | case XML_ELEMENT_DECL: |
| 8051 | case XML_ATTRIBUTE_DECL: |
| 8052 | case XML_ENTITY_DECL: |
| 8053 | case XML_NOTATION_NODE: |
| 8054 | case XML_XINCLUDE_START: |
| 8055 | case XML_XINCLUDE_END: |
| 8056 | if (ctxt->context->node->parent == NULL) |
| 8057 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8058 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8059 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 8060 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 8061 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8062 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8063 | return(ctxt->context->node->parent); |
| 8064 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8065 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8066 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8067 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8068 | } |
| 8069 | case XML_DOCUMENT_NODE: |
| 8070 | case XML_DOCUMENT_TYPE_NODE: |
| 8071 | case XML_DOCUMENT_FRAG_NODE: |
| 8072 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 8073 | #ifdef LIBXML_DOCB_ENABLED |
| 8074 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8075 | #endif |
| 8076 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8077 | case XML_NAMESPACE_DECL: { |
| 8078 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8079 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8080 | if ((ns->next != NULL) && |
| 8081 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 8082 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8083 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8084 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8086 | } |
| 8087 | return(NULL); |
| 8088 | } |
| 8089 | if (cur == ctxt->context->doc->children) |
| 8090 | return((xmlNodePtr) ctxt->context->doc); |
| 8091 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8092 | return(NULL); |
| 8093 | switch (cur->type) { |
| 8094 | case XML_ELEMENT_NODE: |
| 8095 | case XML_TEXT_NODE: |
| 8096 | case XML_CDATA_SECTION_NODE: |
| 8097 | case XML_ENTITY_REF_NODE: |
| 8098 | case XML_ENTITY_NODE: |
| 8099 | case XML_PI_NODE: |
| 8100 | case XML_COMMENT_NODE: |
| 8101 | case XML_NOTATION_NODE: |
| 8102 | case XML_DTD_NODE: |
| 8103 | case XML_ELEMENT_DECL: |
| 8104 | case XML_ATTRIBUTE_DECL: |
| 8105 | case XML_ENTITY_DECL: |
| 8106 | case XML_XINCLUDE_START: |
| 8107 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8108 | if (cur->parent == NULL) |
| 8109 | return(NULL); |
| 8110 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8111 | ((cur->parent->name[0] == ' ') || |
| 8112 | (xmlStrEqual(cur->parent->name, |
| 8113 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8114 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8115 | return(cur->parent); |
| 8116 | case XML_ATTRIBUTE_NODE: { |
| 8117 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 8118 | |
| 8119 | return(att->parent); |
| 8120 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8121 | case XML_NAMESPACE_DECL: { |
| 8122 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8123 | |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8124 | if ((ns->next != NULL) && |
| 8125 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 8126 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8127 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8128 | return(NULL); |
| 8129 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8130 | case XML_DOCUMENT_NODE: |
| 8131 | case XML_DOCUMENT_TYPE_NODE: |
| 8132 | case XML_DOCUMENT_FRAG_NODE: |
| 8133 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 8134 | #ifdef LIBXML_DOCB_ENABLED |
| 8135 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8136 | #endif |
| 8137 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8138 | } |
| 8139 | return(NULL); |
| 8140 | } |
| 8141 | |
| 8142 | /** |
| 8143 | * xmlXPathNextAncestorOrSelf: |
| 8144 | * @ctxt: the XPath Parser context |
| 8145 | * @cur: the current node in the traversal |
| 8146 | * |
| 8147 | * Traversal function for the "ancestor-or-self" direction |
| 8148 | * he ancestor-or-self axis contains the context node and ancestors of |
| 8149 | * the context node in reverse document order; thus the context node is |
| 8150 | * the first node on the axis, and the context node's parent the second; |
| 8151 | * parent here is defined the same as with the parent axis. |
| 8152 | * |
| 8153 | * Returns the next element following that axis |
| 8154 | */ |
| 8155 | xmlNodePtr |
| 8156 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8157 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8158 | if (cur == NULL) |
| 8159 | return(ctxt->context->node); |
| 8160 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 8161 | } |
| 8162 | |
| 8163 | /** |
| 8164 | * xmlXPathNextFollowingSibling: |
| 8165 | * @ctxt: the XPath Parser context |
| 8166 | * @cur: the current node in the traversal |
| 8167 | * |
| 8168 | * Traversal function for the "following-sibling" direction |
| 8169 | * The following-sibling axis contains the following siblings of the context |
| 8170 | * node in document order. |
| 8171 | * |
| 8172 | * Returns the next element following that axis |
| 8173 | */ |
| 8174 | xmlNodePtr |
| 8175 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8176 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8177 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 8178 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 8179 | return(NULL); |
| 8180 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8181 | return(NULL); |
| 8182 | if (cur == NULL) |
| 8183 | return(ctxt->context->node->next); |
| 8184 | return(cur->next); |
| 8185 | } |
| 8186 | |
| 8187 | /** |
| 8188 | * xmlXPathNextPrecedingSibling: |
| 8189 | * @ctxt: the XPath Parser context |
| 8190 | * @cur: the current node in the traversal |
| 8191 | * |
| 8192 | * Traversal function for the "preceding-sibling" direction |
| 8193 | * The preceding-sibling axis contains the preceding siblings of the context |
| 8194 | * node in reverse document order; the first preceding sibling is first on the |
| 8195 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 8196 | * |
| 8197 | * Returns the next element following that axis |
| 8198 | */ |
| 8199 | xmlNodePtr |
| 8200 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8201 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8202 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 8203 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 8204 | return(NULL); |
| 8205 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8206 | return(NULL); |
| 8207 | if (cur == NULL) |
| 8208 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8209 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 8210 | cur = cur->prev; |
| 8211 | if (cur == NULL) |
| 8212 | return(ctxt->context->node->prev); |
| 8213 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8214 | return(cur->prev); |
| 8215 | } |
| 8216 | |
| 8217 | /** |
| 8218 | * xmlXPathNextFollowing: |
| 8219 | * @ctxt: the XPath Parser context |
| 8220 | * @cur: the current node in the traversal |
| 8221 | * |
| 8222 | * Traversal function for the "following" direction |
| 8223 | * The following axis contains all nodes in the same document as the context |
| 8224 | * node that are after the context node in document order, excluding any |
| 8225 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 8226 | * are ordered in document order |
| 8227 | * |
| 8228 | * Returns the next element following that axis |
| 8229 | */ |
| 8230 | xmlNodePtr |
| 8231 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8232 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8233 | if ((cur != NULL) && (cur->type != XML_ATTRIBUTE_NODE) && |
| 8234 | (cur->type != XML_NAMESPACE_DECL) && (cur->children != NULL)) |
| 8235 | return(cur->children); |
| 8236 | |
| 8237 | if (cur == NULL) { |
| 8238 | cur = ctxt->context->node; |
| 8239 | if (cur->type == XML_NAMESPACE_DECL) |
Daniel Veillard | 91d1975 | 2010-10-15 14:30:52 +0200 | [diff] [blame] | 8240 | return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8241 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 8242 | cur = cur->parent; |
Daniel Veillard | 91d1975 | 2010-10-15 14:30:52 +0200 | [diff] [blame] | 8243 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8244 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 8245 | if (cur->next != NULL) return(cur->next) ; |
| 8246 | do { |
| 8247 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8248 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8249 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 8250 | if (cur->next != NULL) return(cur->next); |
| 8251 | } while (cur != NULL); |
| 8252 | return(cur); |
| 8253 | } |
| 8254 | |
| 8255 | /* |
| 8256 | * xmlXPathIsAncestor: |
| 8257 | * @ancestor: the ancestor node |
| 8258 | * @node: the current node |
| 8259 | * |
| 8260 | * Check that @ancestor is a @node's ancestor |
| 8261 | * |
| 8262 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 8263 | */ |
| 8264 | static int |
| 8265 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 8266 | if ((ancestor == NULL) || (node == NULL)) return(0); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8267 | if (node->type == XML_NAMESPACE_DECL) |
| 8268 | return(0); |
| 8269 | if (ancestor->type == XML_NAMESPACE_DECL) |
| 8270 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8271 | /* nodes need to be in the same document */ |
| 8272 | if (ancestor->doc != node->doc) return(0); |
| 8273 | /* avoid searching if ancestor or node is the root node */ |
| 8274 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 8275 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 8276 | while (node->parent != NULL) { |
| 8277 | if (node->parent == ancestor) |
| 8278 | return(1); |
| 8279 | node = node->parent; |
| 8280 | } |
| 8281 | return(0); |
| 8282 | } |
| 8283 | |
| 8284 | /** |
| 8285 | * xmlXPathNextPreceding: |
| 8286 | * @ctxt: the XPath Parser context |
| 8287 | * @cur: the current node in the traversal |
| 8288 | * |
| 8289 | * Traversal function for the "preceding" direction |
| 8290 | * the preceding axis contains all nodes in the same document as the context |
| 8291 | * node that are before the context node in document order, excluding any |
| 8292 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 8293 | * ordered in reverse document order |
| 8294 | * |
| 8295 | * Returns the next element following that axis |
| 8296 | */ |
| 8297 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8298 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 8299 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8300 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8301 | if (cur == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8302 | cur = ctxt->context->node; |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8303 | if (cur->type == XML_NAMESPACE_DECL) |
| 8304 | return(NULL); |
| 8305 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 8306 | return(cur->parent); |
| 8307 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8308 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8309 | return (NULL); |
| 8310 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 8311 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | do { |
| 8313 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8314 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 8315 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8319 | if (cur == NULL) |
| 8320 | return (NULL); |
| 8321 | if (cur == ctxt->context->doc->children) |
| 8322 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8323 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8324 | return (cur); |
| 8325 | } |
| 8326 | |
| 8327 | /** |
| 8328 | * xmlXPathNextPrecedingInternal: |
| 8329 | * @ctxt: the XPath Parser context |
| 8330 | * @cur: the current node in the traversal |
| 8331 | * |
| 8332 | * Traversal function for the "preceding" direction |
| 8333 | * the preceding axis contains all nodes in the same document as the context |
| 8334 | * node that are before the context node in document order, excluding any |
| 8335 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 8336 | * ordered in reverse document order |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8337 | * This is a faster implementation but internal only since it requires a |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8338 | * state kept in the parser context: ctxt->ancestor. |
| 8339 | * |
| 8340 | * Returns the next element following that axis |
| 8341 | */ |
| 8342 | static xmlNodePtr |
| 8343 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 8344 | xmlNodePtr cur) |
| 8345 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8346 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8347 | if (cur == NULL) { |
| 8348 | cur = ctxt->context->node; |
| 8349 | if (cur == NULL) |
| 8350 | return (NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8351 | if (cur->type == XML_NAMESPACE_DECL) |
| 8352 | return (NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8353 | ctxt->ancestor = cur->parent; |
| 8354 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8355 | if (cur->type == XML_NAMESPACE_DECL) |
| 8356 | return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8357 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 8358 | cur = cur->prev; |
| 8359 | while (cur->prev == NULL) { |
| 8360 | cur = cur->parent; |
| 8361 | if (cur == NULL) |
| 8362 | return (NULL); |
| 8363 | if (cur == ctxt->context->doc->children) |
| 8364 | return (NULL); |
| 8365 | if (cur != ctxt->ancestor) |
| 8366 | return (cur); |
| 8367 | ctxt->ancestor = cur->parent; |
| 8368 | } |
| 8369 | cur = cur->prev; |
| 8370 | while (cur->last != NULL) |
| 8371 | cur = cur->last; |
| 8372 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8373 | } |
| 8374 | |
| 8375 | /** |
| 8376 | * xmlXPathNextNamespace: |
| 8377 | * @ctxt: the XPath Parser context |
| 8378 | * @cur: the current attribute in the traversal |
| 8379 | * |
| 8380 | * Traversal function for the "namespace" direction |
| 8381 | * the namespace axis contains the namespace nodes of the context node; |
| 8382 | * the order of nodes on this axis is implementation-defined; the axis will |
| 8383 | * be empty unless the context node is an element |
| 8384 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 8385 | * We keep the XML namespace node at the end of the list. |
| 8386 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8387 | * Returns the next element following that axis |
| 8388 | */ |
| 8389 | xmlNodePtr |
| 8390 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8391 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8392 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 8393 | if (cur == NULL) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8394 | if (ctxt->context->tmpNsList != NULL) |
| 8395 | xmlFree(ctxt->context->tmpNsList); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8396 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8397 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8398 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8399 | if (ctxt->context->tmpNsList != NULL) { |
| 8400 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 8401 | ctxt->context->tmpNsNr++; |
| 8402 | } |
| 8403 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 8404 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8405 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8406 | if (ctxt->context->tmpNsNr > 0) { |
| 8407 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 8408 | } else { |
| 8409 | if (ctxt->context->tmpNsList != NULL) |
| 8410 | xmlFree(ctxt->context->tmpNsList); |
| 8411 | ctxt->context->tmpNsList = NULL; |
| 8412 | return(NULL); |
| 8413 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8414 | } |
| 8415 | |
| 8416 | /** |
| 8417 | * xmlXPathNextAttribute: |
| 8418 | * @ctxt: the XPath Parser context |
| 8419 | * @cur: the current attribute in the traversal |
| 8420 | * |
| 8421 | * Traversal function for the "attribute" direction |
| 8422 | * TODO: support DTD inherited default attributes |
| 8423 | * |
| 8424 | * Returns the next element following that axis |
| 8425 | */ |
| 8426 | xmlNodePtr |
| 8427 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8428 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 8429 | if (ctxt->context->node == NULL) |
| 8430 | return(NULL); |
| 8431 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 8432 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8433 | if (cur == NULL) { |
| 8434 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 8435 | return(NULL); |
| 8436 | return((xmlNodePtr)ctxt->context->node->properties); |
| 8437 | } |
| 8438 | return((xmlNodePtr)cur->next); |
| 8439 | } |
| 8440 | |
| 8441 | /************************************************************************ |
| 8442 | * * |
| 8443 | * NodeTest Functions * |
| 8444 | * * |
| 8445 | ************************************************************************/ |
| 8446 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8447 | #define IS_FUNCTION 200 |
| 8448 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8449 | |
| 8450 | /************************************************************************ |
| 8451 | * * |
| 8452 | * Implicit tree core function library * |
| 8453 | * * |
| 8454 | ************************************************************************/ |
| 8455 | |
| 8456 | /** |
| 8457 | * xmlXPathRoot: |
| 8458 | * @ctxt: the XPath Parser context |
| 8459 | * |
| 8460 | * Initialize the context to the root of the document |
| 8461 | */ |
| 8462 | void |
| 8463 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8464 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 8465 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8466 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8467 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8468 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | } |
| 8470 | |
| 8471 | /************************************************************************ |
| 8472 | * * |
| 8473 | * The explicit core function library * |
| 8474 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 8475 | * * |
| 8476 | ************************************************************************/ |
| 8477 | |
| 8478 | |
| 8479 | /** |
| 8480 | * xmlXPathLastFunction: |
| 8481 | * @ctxt: the XPath Parser context |
| 8482 | * @nargs: the number of arguments |
| 8483 | * |
| 8484 | * Implement the last() XPath function |
| 8485 | * number last() |
| 8486 | * The last function returns the number of nodes in the context node list. |
| 8487 | */ |
| 8488 | void |
| 8489 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8490 | CHECK_ARITY(0); |
| 8491 | if (ctxt->context->contextSize >= 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8492 | valuePush(ctxt, |
| 8493 | xmlXPathCacheNewFloat(ctxt->context, |
| 8494 | (double) ctxt->context->contextSize)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8495 | #ifdef DEBUG_EXPR |
| 8496 | xmlGenericError(xmlGenericErrorContext, |
| 8497 | "last() : %d\n", ctxt->context->contextSize); |
| 8498 | #endif |
| 8499 | } else { |
| 8500 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 8501 | } |
| 8502 | } |
| 8503 | |
| 8504 | /** |
| 8505 | * xmlXPathPositionFunction: |
| 8506 | * @ctxt: the XPath Parser context |
| 8507 | * @nargs: the number of arguments |
| 8508 | * |
| 8509 | * Implement the position() XPath function |
| 8510 | * number position() |
| 8511 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8512 | * context node list. The first position is 1, and so the last position |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8513 | * will be equal to last(). |
| 8514 | */ |
| 8515 | void |
| 8516 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8517 | CHECK_ARITY(0); |
| 8518 | if (ctxt->context->proximityPosition >= 0) { |
| 8519 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8520 | xmlXPathCacheNewFloat(ctxt->context, |
| 8521 | (double) ctxt->context->proximityPosition)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8522 | #ifdef DEBUG_EXPR |
| 8523 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 8524 | ctxt->context->proximityPosition); |
| 8525 | #endif |
| 8526 | } else { |
| 8527 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 8528 | } |
| 8529 | } |
| 8530 | |
| 8531 | /** |
| 8532 | * xmlXPathCountFunction: |
| 8533 | * @ctxt: the XPath Parser context |
| 8534 | * @nargs: the number of arguments |
| 8535 | * |
| 8536 | * Implement the count() XPath function |
| 8537 | * number count(node-set) |
| 8538 | */ |
| 8539 | void |
| 8540 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8541 | xmlXPathObjectPtr cur; |
| 8542 | |
| 8543 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8544 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8545 | ((ctxt->value->type != XPATH_NODESET) && |
| 8546 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8547 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8548 | cur = valuePop(ctxt); |
| 8549 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8550 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8551 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 8552 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8553 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
| 8554 | (double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8555 | } else { |
| 8556 | if ((cur->nodesetval->nodeNr != 1) || |
| 8557 | (cur->nodesetval->nodeTab == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8558 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8559 | } else { |
| 8560 | xmlNodePtr tmp; |
| 8561 | int i = 0; |
| 8562 | |
| 8563 | tmp = cur->nodesetval->nodeTab[0]; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8564 | if ((tmp != NULL) && (tmp->type != XML_NAMESPACE_DECL)) { |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8565 | tmp = tmp->children; |
| 8566 | while (tmp != NULL) { |
| 8567 | tmp = tmp->next; |
| 8568 | i++; |
| 8569 | } |
| 8570 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8571 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) i)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8572 | } |
| 8573 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8574 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8575 | } |
| 8576 | |
| 8577 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8578 | * xmlXPathGetElementsByIds: |
| 8579 | * @doc: the document |
| 8580 | * @ids: a whitespace separated list of IDs |
| 8581 | * |
| 8582 | * Selects elements by their unique ID. |
| 8583 | * |
| 8584 | * Returns a node-set of selected elements. |
| 8585 | */ |
| 8586 | static xmlNodeSetPtr |
| 8587 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 8588 | xmlNodeSetPtr ret; |
| 8589 | const xmlChar *cur = ids; |
| 8590 | xmlChar *ID; |
| 8591 | xmlAttrPtr attr; |
| 8592 | xmlNodePtr elem = NULL; |
| 8593 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 8594 | if (ids == NULL) return(NULL); |
| 8595 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8596 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 8597 | if (ret == NULL) |
| 8598 | return(ret); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8599 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8600 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8601 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8602 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8603 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8604 | |
| 8605 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8606 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 8607 | /* |
| 8608 | * We used to check the fact that the value passed |
| 8609 | * was an NCName, but this generated much troubles for |
| 8610 | * me and Aleksey Sanin, people blatantly violated that |
| 8611 | * constaint, like Visa3D spec. |
| 8612 | * if (xmlValidateNCName(ID, 1) == 0) |
| 8613 | */ |
| 8614 | attr = xmlGetID(doc, ID); |
| 8615 | if (attr != NULL) { |
| 8616 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 8617 | elem = attr->parent; |
| 8618 | else if (attr->type == XML_ELEMENT_NODE) |
| 8619 | elem = (xmlNodePtr) attr; |
| 8620 | else |
| 8621 | elem = NULL; |
| 8622 | if (elem != NULL) |
| 8623 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8624 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8625 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8626 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8627 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8628 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8629 | ids = cur; |
| 8630 | } |
| 8631 | return(ret); |
| 8632 | } |
| 8633 | |
| 8634 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8635 | * xmlXPathIdFunction: |
| 8636 | * @ctxt: the XPath Parser context |
| 8637 | * @nargs: the number of arguments |
| 8638 | * |
| 8639 | * Implement the id() XPath function |
| 8640 | * node-set id(object) |
| 8641 | * The id function selects elements by their unique ID |
| 8642 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 8643 | * then the result is the union of the result of applying id to the |
| 8644 | * string value of each of the nodes in the argument node-set. When the |
| 8645 | * argument to id is of any other type, the argument is converted to a |
| 8646 | * string as if by a call to the string function; the string is split |
| 8647 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 8648 | * of characters matching the production S); the result is a node-set |
| 8649 | * containing the elements in the same document as the context node that |
| 8650 | * have a unique ID equal to any of the tokens in the list. |
| 8651 | */ |
| 8652 | void |
| 8653 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8654 | xmlChar *tokens; |
| 8655 | xmlNodeSetPtr ret; |
| 8656 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8657 | |
| 8658 | CHECK_ARITY(1); |
| 8659 | obj = valuePop(ctxt); |
| 8660 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 8661 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8662 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8663 | int i; |
| 8664 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8665 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 8666 | /* |
| 8667 | * FIXME -- in an out-of-memory condition this will behave badly. |
| 8668 | * The solution is not clear -- we already popped an item from |
| 8669 | * ctxt, so the object is in a corrupt state. |
| 8670 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8671 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8672 | if (obj->nodesetval != NULL) { |
| 8673 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8674 | tokens = |
| 8675 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 8676 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 8677 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 8678 | xmlXPathFreeNodeSet(ns); |
| 8679 | if (tokens != NULL) |
| 8680 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8681 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8682 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8683 | xmlXPathReleaseObject(ctxt->context, obj); |
| 8684 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8685 | return; |
| 8686 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8687 | obj = xmlXPathCacheConvertString(ctxt->context, obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8688 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8689 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8690 | xmlXPathReleaseObject(ctxt->context, obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8691 | return; |
| 8692 | } |
| 8693 | |
| 8694 | /** |
| 8695 | * xmlXPathLocalNameFunction: |
| 8696 | * @ctxt: the XPath Parser context |
| 8697 | * @nargs: the number of arguments |
| 8698 | * |
| 8699 | * Implement the local-name() XPath function |
| 8700 | * string local-name(node-set?) |
| 8701 | * The local-name function returns a string containing the local part |
| 8702 | * of the name of the node in the argument node-set that is first in |
| 8703 | * document order. If the node-set is empty or the first node has no |
| 8704 | * name, an empty string is returned. If the argument is omitted it |
| 8705 | * defaults to the context node. |
| 8706 | */ |
| 8707 | void |
| 8708 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8709 | xmlXPathObjectPtr cur; |
| 8710 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8711 | if (ctxt == NULL) return; |
| 8712 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8713 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8714 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8715 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8716 | nargs = 1; |
| 8717 | } |
| 8718 | |
| 8719 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8720 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8721 | ((ctxt->value->type != XPATH_NODESET) && |
| 8722 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8723 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8724 | cur = valuePop(ctxt); |
| 8725 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8726 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8727 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8728 | } else { |
| 8729 | int i = 0; /* Should be first in document order !!!!! */ |
| 8730 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8731 | case XML_ELEMENT_NODE: |
| 8732 | case XML_ATTRIBUTE_NODE: |
| 8733 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8734 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8735 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8736 | else |
| 8737 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8738 | xmlXPathCacheNewString(ctxt->context, |
| 8739 | cur->nodesetval->nodeTab[i]->name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8740 | break; |
| 8741 | case XML_NAMESPACE_DECL: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8742 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8743 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 8744 | break; |
| 8745 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8746 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8747 | } |
| 8748 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8749 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8750 | } |
| 8751 | |
| 8752 | /** |
| 8753 | * xmlXPathNamespaceURIFunction: |
| 8754 | * @ctxt: the XPath Parser context |
| 8755 | * @nargs: the number of arguments |
| 8756 | * |
| 8757 | * Implement the namespace-uri() XPath function |
| 8758 | * string namespace-uri(node-set?) |
| 8759 | * The namespace-uri function returns a string containing the |
| 8760 | * namespace URI of the expanded name of the node in the argument |
| 8761 | * node-set that is first in document order. If the node-set is empty, |
| 8762 | * the first node has no name, or the expanded name has no namespace |
| 8763 | * URI, an empty string is returned. If the argument is omitted it |
| 8764 | * defaults to the context node. |
| 8765 | */ |
| 8766 | void |
| 8767 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8768 | xmlXPathObjectPtr cur; |
| 8769 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8770 | if (ctxt == NULL) return; |
| 8771 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8772 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8773 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8774 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8775 | nargs = 1; |
| 8776 | } |
| 8777 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8778 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8779 | ((ctxt->value->type != XPATH_NODESET) && |
| 8780 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8781 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8782 | cur = valuePop(ctxt); |
| 8783 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8784 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8785 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8786 | } else { |
| 8787 | int i = 0; /* Should be first in document order !!!!! */ |
| 8788 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8789 | case XML_ELEMENT_NODE: |
| 8790 | case XML_ATTRIBUTE_NODE: |
| 8791 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8792 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8793 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8794 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8795 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 8796 | break; |
| 8797 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8798 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8799 | } |
| 8800 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8801 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8802 | } |
| 8803 | |
| 8804 | /** |
| 8805 | * xmlXPathNameFunction: |
| 8806 | * @ctxt: the XPath Parser context |
| 8807 | * @nargs: the number of arguments |
| 8808 | * |
| 8809 | * Implement the name() XPath function |
| 8810 | * string name(node-set?) |
| 8811 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8812 | * the name of the node in the argument node-set that is first in document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8813 | * order. The QName must represent the name with respect to the namespace |
| 8814 | * declarations in effect on the node whose name is being represented. |
| 8815 | * Typically, this will be the form in which the name occurred in the XML |
| 8816 | * source. This need not be the case if there are namespace declarations |
| 8817 | * in effect on the node that associate multiple prefixes with the same |
| 8818 | * namespace. However, an implementation may include information about |
| 8819 | * the original prefix in its representation of nodes; in this case, an |
| 8820 | * implementation can ensure that the returned string is always the same |
| 8821 | * as the QName used in the XML source. If the argument it omitted it |
| 8822 | * defaults to the context node. |
| 8823 | * Libxml keep the original prefix so the "real qualified name" used is |
| 8824 | * returned. |
| 8825 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8826 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8827 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 8828 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8829 | xmlXPathObjectPtr cur; |
| 8830 | |
| 8831 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8832 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8833 | ctxt->context->node)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8834 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8835 | } |
| 8836 | |
| 8837 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8838 | if ((ctxt->value == NULL) || |
| 8839 | ((ctxt->value->type != XPATH_NODESET) && |
| 8840 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8841 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8842 | cur = valuePop(ctxt); |
| 8843 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8844 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8845 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8846 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8847 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8848 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8849 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8850 | case XML_ELEMENT_NODE: |
| 8851 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8852 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8853 | valuePush(ctxt, |
| 8854 | xmlXPathCacheNewCString(ctxt->context, "")); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8855 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 8856 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8857 | valuePush(ctxt, |
| 8858 | xmlXPathCacheNewString(ctxt->context, |
| 8859 | cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8860 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 8861 | xmlChar *fullname; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8862 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 8863 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 8864 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 8865 | NULL, 0); |
| 8866 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 8867 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 8868 | if (fullname == NULL) { |
| 8869 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 8870 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8871 | valuePush(ctxt, xmlXPathCacheWrapString( |
| 8872 | ctxt->context, fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8873 | } |
| 8874 | break; |
| 8875 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8876 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8877 | cur->nodesetval->nodeTab[i])); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8878 | xmlXPathLocalNameFunction(ctxt, 1); |
| 8879 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8880 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8881 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8882 | } |
| 8883 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8884 | |
| 8885 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8886 | * xmlXPathStringFunction: |
| 8887 | * @ctxt: the XPath Parser context |
| 8888 | * @nargs: the number of arguments |
| 8889 | * |
| 8890 | * Implement the string() XPath function |
| 8891 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8892 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8893 | * - A node-set is converted to a string by returning the value of |
| 8894 | * the node in the node-set that is first in document order. |
| 8895 | * If the node-set is empty, an empty string is returned. |
| 8896 | * - A number is converted to a string as follows |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8897 | * + NaN is converted to the string NaN |
| 8898 | * + positive zero is converted to the string 0 |
| 8899 | * + negative zero is converted to the string 0 |
| 8900 | * + positive infinity is converted to the string Infinity |
| 8901 | * + negative infinity is converted to the string -Infinity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8902 | * + if the number is an integer, the number is represented in |
| 8903 | * decimal form as a Number with no decimal point and no leading |
| 8904 | * zeros, preceded by a minus sign (-) if the number is negative |
| 8905 | * + otherwise, the number is represented in decimal form as a |
| 8906 | * Number including a decimal point with at least one digit |
| 8907 | * before the decimal point and at least one digit after the |
| 8908 | * decimal point, preceded by a minus sign (-) if the number |
| 8909 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8910 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8911 | * before the decimal point; beyond the one required digit |
| 8912 | * after the decimal point there must be as many, but only as |
| 8913 | * many, more digits as are needed to uniquely distinguish the |
| 8914 | * number from all other IEEE 754 numeric values. |
| 8915 | * - The boolean false value is converted to the string false. |
| 8916 | * The boolean true value is converted to the string true. |
| 8917 | * |
| 8918 | * If the argument is omitted, it defaults to a node-set with the |
| 8919 | * context node as its only member. |
| 8920 | */ |
| 8921 | void |
| 8922 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8923 | xmlXPathObjectPtr cur; |
| 8924 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8925 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8926 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8927 | valuePush(ctxt, |
| 8928 | xmlXPathCacheWrapString(ctxt->context, |
| 8929 | xmlXPathCastNodeToString(ctxt->context->node))); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8930 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | CHECK_ARITY(1); |
| 8934 | cur = valuePop(ctxt); |
| 8935 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8936 | valuePush(ctxt, xmlXPathCacheConvertString(ctxt->context, cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8937 | } |
| 8938 | |
| 8939 | /** |
| 8940 | * xmlXPathStringLengthFunction: |
| 8941 | * @ctxt: the XPath Parser context |
| 8942 | * @nargs: the number of arguments |
| 8943 | * |
| 8944 | * Implement the string-length() XPath function |
| 8945 | * number string-length(string?) |
| 8946 | * The string-length returns the number of characters in the string |
| 8947 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 8948 | * the context node converted to a string, in other words the value |
| 8949 | * of the context node. |
| 8950 | */ |
| 8951 | void |
| 8952 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8953 | xmlXPathObjectPtr cur; |
| 8954 | |
| 8955 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8956 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 8957 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8958 | if (ctxt->context->node == NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8959 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8960 | } else { |
| 8961 | xmlChar *content; |
| 8962 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8963 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8964 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
| 8965 | xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8966 | xmlFree(content); |
| 8967 | } |
| 8968 | return; |
| 8969 | } |
| 8970 | CHECK_ARITY(1); |
| 8971 | CAST_TO_STRING; |
| 8972 | CHECK_TYPE(XPATH_STRING); |
| 8973 | cur = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8974 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8975 | xmlUTF8Strlen(cur->stringval))); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8976 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
| 8979 | /** |
| 8980 | * xmlXPathConcatFunction: |
| 8981 | * @ctxt: the XPath Parser context |
| 8982 | * @nargs: the number of arguments |
| 8983 | * |
| 8984 | * Implement the concat() XPath function |
| 8985 | * string concat(string, string, string*) |
| 8986 | * The concat function returns the concatenation of its arguments. |
| 8987 | */ |
| 8988 | void |
| 8989 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8990 | xmlXPathObjectPtr cur, newobj; |
| 8991 | xmlChar *tmp; |
| 8992 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8993 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8994 | if (nargs < 2) { |
| 8995 | CHECK_ARITY(2); |
| 8996 | } |
| 8997 | |
| 8998 | CAST_TO_STRING; |
| 8999 | cur = valuePop(ctxt); |
| 9000 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9001 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9002 | return; |
| 9003 | } |
| 9004 | nargs--; |
| 9005 | |
| 9006 | while (nargs > 0) { |
| 9007 | CAST_TO_STRING; |
| 9008 | newobj = valuePop(ctxt); |
| 9009 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9010 | xmlXPathReleaseObject(ctxt->context, newobj); |
| 9011 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9012 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9013 | } |
| 9014 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 9015 | newobj->stringval = cur->stringval; |
| 9016 | cur->stringval = tmp; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9017 | xmlXPathReleaseObject(ctxt->context, newobj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9018 | nargs--; |
| 9019 | } |
| 9020 | valuePush(ctxt, cur); |
| 9021 | } |
| 9022 | |
| 9023 | /** |
| 9024 | * xmlXPathContainsFunction: |
| 9025 | * @ctxt: the XPath Parser context |
| 9026 | * @nargs: the number of arguments |
| 9027 | * |
| 9028 | * Implement the contains() XPath function |
| 9029 | * boolean contains(string, string) |
| 9030 | * The contains function returns true if the first argument string |
| 9031 | * contains the second argument string, and otherwise returns false. |
| 9032 | */ |
| 9033 | void |
| 9034 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9035 | xmlXPathObjectPtr hay, needle; |
| 9036 | |
| 9037 | CHECK_ARITY(2); |
| 9038 | CAST_TO_STRING; |
| 9039 | CHECK_TYPE(XPATH_STRING); |
| 9040 | needle = valuePop(ctxt); |
| 9041 | CAST_TO_STRING; |
| 9042 | hay = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9043 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9044 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9045 | xmlXPathReleaseObject(ctxt->context, hay); |
| 9046 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9047 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9048 | } |
| 9049 | if (xmlStrstr(hay->stringval, needle->stringval)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9050 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9051 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9052 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
| 9053 | xmlXPathReleaseObject(ctxt->context, hay); |
| 9054 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9055 | } |
| 9056 | |
| 9057 | /** |
| 9058 | * xmlXPathStartsWithFunction: |
| 9059 | * @ctxt: the XPath Parser context |
| 9060 | * @nargs: the number of arguments |
| 9061 | * |
| 9062 | * Implement the starts-with() XPath function |
| 9063 | * boolean starts-with(string, string) |
| 9064 | * The starts-with function returns true if the first argument string |
| 9065 | * starts with the second argument string, and otherwise returns false. |
| 9066 | */ |
| 9067 | void |
| 9068 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9069 | xmlXPathObjectPtr hay, needle; |
| 9070 | int n; |
| 9071 | |
| 9072 | CHECK_ARITY(2); |
| 9073 | CAST_TO_STRING; |
| 9074 | CHECK_TYPE(XPATH_STRING); |
| 9075 | needle = valuePop(ctxt); |
| 9076 | CAST_TO_STRING; |
| 9077 | hay = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9078 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9079 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9080 | xmlXPathReleaseObject(ctxt->context, hay); |
| 9081 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9082 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9083 | } |
| 9084 | n = xmlStrlen(needle->stringval); |
| 9085 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9086 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9087 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9088 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
| 9089 | xmlXPathReleaseObject(ctxt->context, hay); |
| 9090 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9091 | } |
| 9092 | |
| 9093 | /** |
| 9094 | * xmlXPathSubstringFunction: |
| 9095 | * @ctxt: the XPath Parser context |
| 9096 | * @nargs: the number of arguments |
| 9097 | * |
| 9098 | * Implement the substring() XPath function |
| 9099 | * string substring(string, number, number?) |
| 9100 | * The substring function returns the substring of the first argument |
| 9101 | * starting at the position specified in the second argument with |
| 9102 | * length specified in the third argument. For example, |
| 9103 | * substring("12345",2,3) returns "234". If the third argument is not |
| 9104 | * specified, it returns the substring starting at the position specified |
| 9105 | * in the second argument and continuing to the end of the string. For |
| 9106 | * example, substring("12345",2) returns "2345". More precisely, each |
| 9107 | * character in the string (see [3.6 Strings]) is considered to have a |
| 9108 | * numeric position: the position of the first character is 1, the position |
| 9109 | * of the second character is 2 and so on. The returned substring contains |
| 9110 | * those characters for which the position of the character is greater than |
| 9111 | * or equal to the second argument and, if the third argument is specified, |
| 9112 | * less than the sum of the second and third arguments; the comparisons |
| 9113 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9114 | * - substring("12345", 1.5, 2.6) returns "234" |
| 9115 | * - substring("12345", 0, 3) returns "12" |
| 9116 | * - substring("12345", 0 div 0, 3) returns "" |
| 9117 | * - substring("12345", 1, 0 div 0) returns "" |
| 9118 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 9119 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9120 | */ |
| 9121 | void |
| 9122 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9123 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9124 | double le=0, in; |
| 9125 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9126 | xmlChar *ret; |
| 9127 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9128 | if (nargs < 2) { |
| 9129 | CHECK_ARITY(2); |
| 9130 | } |
| 9131 | if (nargs > 3) { |
| 9132 | CHECK_ARITY(3); |
| 9133 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9134 | /* |
| 9135 | * take care of possible last (position) argument |
| 9136 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9137 | if (nargs == 3) { |
| 9138 | CAST_TO_NUMBER; |
| 9139 | CHECK_TYPE(XPATH_NUMBER); |
| 9140 | len = valuePop(ctxt); |
| 9141 | le = len->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9142 | xmlXPathReleaseObject(ctxt->context, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9143 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9144 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9145 | CAST_TO_NUMBER; |
| 9146 | CHECK_TYPE(XPATH_NUMBER); |
| 9147 | start = valuePop(ctxt); |
| 9148 | in = start->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9149 | xmlXPathReleaseObject(ctxt->context, start); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9150 | CAST_TO_STRING; |
| 9151 | CHECK_TYPE(XPATH_STRING); |
| 9152 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9153 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9154 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9155 | /* |
| 9156 | * If last pos not present, calculate last position |
| 9157 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9158 | if (nargs != 3) { |
| 9159 | le = (double)m; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9160 | if (in < 1.0) |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9161 | in = 1.0; |
| 9162 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9163 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9164 | /* Need to check for the special cases where either |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9165 | * the index is NaN, the length is NaN, or both |
| 9166 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9167 | */ |
Daniel Veillard | 48b3eb2 | 2009-03-25 09:51:19 +0000 | [diff] [blame] | 9168 | if (!xmlXPathIsInf(in) && !xmlXPathIsNaN(in + le)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9169 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9170 | * To meet the requirements of the spec, the arguments |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9171 | * must be converted to integer format before |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9172 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9173 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9174 | * First we go to integer form, rounding up |
| 9175 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9176 | */ |
| 9177 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9178 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9179 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9180 | if (xmlXPathIsInf(le) == 1) { |
| 9181 | l = m; |
| 9182 | if (i < 1) |
| 9183 | i = 1; |
| 9184 | } |
| 9185 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 9186 | l = 0; |
| 9187 | else { |
| 9188 | l = (int) le; |
| 9189 | if (((double)l)+0.5 <= le) l++; |
| 9190 | } |
| 9191 | |
| 9192 | /* Now we normalize inidices */ |
| 9193 | i -= 1; |
| 9194 | l += i; |
| 9195 | if (i < 0) |
| 9196 | i = 0; |
| 9197 | if (l > m) |
| 9198 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9199 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9200 | /* number of chars to copy */ |
| 9201 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9202 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9203 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 9204 | } |
| 9205 | else { |
| 9206 | ret = NULL; |
| 9207 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9208 | if (ret == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9209 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9210 | else { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9211 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9212 | xmlFree(ret); |
| 9213 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9214 | xmlXPathReleaseObject(ctxt->context, str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | /** |
| 9218 | * xmlXPathSubstringBeforeFunction: |
| 9219 | * @ctxt: the XPath Parser context |
| 9220 | * @nargs: the number of arguments |
| 9221 | * |
| 9222 | * Implement the substring-before() XPath function |
| 9223 | * string substring-before(string, string) |
| 9224 | * The substring-before function returns the substring of the first |
| 9225 | * argument string that precedes the first occurrence of the second |
| 9226 | * argument string in the first argument string, or the empty string |
| 9227 | * if the first argument string does not contain the second argument |
| 9228 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 9229 | */ |
| 9230 | void |
| 9231 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9232 | xmlXPathObjectPtr str; |
| 9233 | xmlXPathObjectPtr find; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9234 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9235 | const xmlChar *point; |
| 9236 | int offset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9237 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9238 | CHECK_ARITY(2); |
| 9239 | CAST_TO_STRING; |
| 9240 | find = valuePop(ctxt); |
| 9241 | CAST_TO_STRING; |
| 9242 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9243 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9244 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9245 | if (target) { |
| 9246 | point = xmlStrstr(str->stringval, find->stringval); |
| 9247 | if (point) { |
| 9248 | offset = (int)(point - str->stringval); |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9249 | xmlBufAdd(target, str->stringval, offset); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9250 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9251 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9252 | xmlBufContent(target))); |
| 9253 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9254 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9255 | xmlXPathReleaseObject(ctxt->context, str); |
| 9256 | xmlXPathReleaseObject(ctxt->context, find); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9257 | } |
| 9258 | |
| 9259 | /** |
| 9260 | * xmlXPathSubstringAfterFunction: |
| 9261 | * @ctxt: the XPath Parser context |
| 9262 | * @nargs: the number of arguments |
| 9263 | * |
| 9264 | * Implement the substring-after() XPath function |
| 9265 | * string substring-after(string, string) |
| 9266 | * The substring-after function returns the substring of the first |
| 9267 | * argument string that follows the first occurrence of the second |
| 9268 | * argument string in the first argument string, or the empty stringi |
| 9269 | * if the first argument string does not contain the second argument |
| 9270 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 9271 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 9272 | */ |
| 9273 | void |
| 9274 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9275 | xmlXPathObjectPtr str; |
| 9276 | xmlXPathObjectPtr find; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9277 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9278 | const xmlChar *point; |
| 9279 | int offset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9280 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9281 | CHECK_ARITY(2); |
| 9282 | CAST_TO_STRING; |
| 9283 | find = valuePop(ctxt); |
| 9284 | CAST_TO_STRING; |
| 9285 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9286 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9287 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9288 | if (target) { |
| 9289 | point = xmlStrstr(str->stringval, find->stringval); |
| 9290 | if (point) { |
| 9291 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9292 | xmlBufAdd(target, &str->stringval[offset], |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9293 | xmlStrlen(str->stringval) - offset); |
| 9294 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9295 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9296 | xmlBufContent(target))); |
| 9297 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9298 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9299 | xmlXPathReleaseObject(ctxt->context, str); |
| 9300 | xmlXPathReleaseObject(ctxt->context, find); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9301 | } |
| 9302 | |
| 9303 | /** |
| 9304 | * xmlXPathNormalizeFunction: |
| 9305 | * @ctxt: the XPath Parser context |
| 9306 | * @nargs: the number of arguments |
| 9307 | * |
| 9308 | * Implement the normalize-space() XPath function |
| 9309 | * string normalize-space(string?) |
| 9310 | * The normalize-space function returns the argument string with white |
| 9311 | * space normalized by stripping leading and trailing whitespace |
| 9312 | * and replacing sequences of whitespace characters by a single |
| 9313 | * space. Whitespace characters are the same allowed by the S production |
| 9314 | * in XML. If the argument is omitted, it defaults to the context |
| 9315 | * node converted to a string, in other words the value of the context node. |
| 9316 | */ |
| 9317 | void |
| 9318 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9319 | xmlXPathObjectPtr obj = NULL; |
| 9320 | xmlChar *source = NULL; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9321 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9322 | xmlChar blank; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9323 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9324 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9325 | if (nargs == 0) { |
| 9326 | /* Use current context node */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9327 | valuePush(ctxt, |
| 9328 | xmlXPathCacheWrapString(ctxt->context, |
| 9329 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9330 | nargs = 1; |
| 9331 | } |
| 9332 | |
| 9333 | CHECK_ARITY(1); |
| 9334 | CAST_TO_STRING; |
| 9335 | CHECK_TYPE(XPATH_STRING); |
| 9336 | obj = valuePop(ctxt); |
| 9337 | source = obj->stringval; |
| 9338 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9339 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9340 | if (target && source) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9341 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9342 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9343 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9344 | source++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9345 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9346 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 9347 | blank = 0; |
| 9348 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9349 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9350 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9351 | } else { |
| 9352 | if (blank) { |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9353 | xmlBufAdd(target, &blank, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9354 | blank = 0; |
| 9355 | } |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9356 | xmlBufAdd(target, source, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9357 | } |
| 9358 | source++; |
| 9359 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9360 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9361 | xmlBufContent(target))); |
| 9362 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9363 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9364 | xmlXPathReleaseObject(ctxt->context, obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
| 9367 | /** |
| 9368 | * xmlXPathTranslateFunction: |
| 9369 | * @ctxt: the XPath Parser context |
| 9370 | * @nargs: the number of arguments |
| 9371 | * |
| 9372 | * Implement the translate() XPath function |
| 9373 | * string translate(string, string, string) |
| 9374 | * The translate function returns the first argument string with |
| 9375 | * occurrences of characters in the second argument string replaced |
| 9376 | * by the character at the corresponding position in the third argument |
| 9377 | * string. For example, translate("bar","abc","ABC") returns the string |
| 9378 | * BAr. If there is a character in the second argument string with no |
| 9379 | * character at a corresponding position in the third argument string |
| 9380 | * (because the second argument string is longer than the third argument |
| 9381 | * string), then occurrences of that character in the first argument |
| 9382 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 9383 | * returns "AAA". If a character occurs more than once in second |
| 9384 | * argument string, then the first occurrence determines the replacement |
| 9385 | * character. If the third argument string is longer than the second |
| 9386 | * argument string, then excess characters are ignored. |
| 9387 | */ |
| 9388 | void |
| 9389 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9390 | xmlXPathObjectPtr str; |
| 9391 | xmlXPathObjectPtr from; |
| 9392 | xmlXPathObjectPtr to; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9393 | xmlBufPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9394 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9395 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 9396 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9397 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9398 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9399 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9400 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9401 | CAST_TO_STRING; |
| 9402 | to = valuePop(ctxt); |
| 9403 | CAST_TO_STRING; |
| 9404 | from = valuePop(ctxt); |
| 9405 | CAST_TO_STRING; |
| 9406 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9407 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9408 | target = xmlBufCreate(); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9409 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9410 | max = xmlUTF8Strlen(to->stringval); |
| 9411 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 9412 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 9413 | if (offset >= 0) { |
| 9414 | if (offset < max) { |
| 9415 | point = xmlUTF8Strpos(to->stringval, offset); |
| 9416 | if (point) |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9417 | xmlBufAdd(target, point, xmlUTF8Strsize(point, 1)); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9418 | } |
| 9419 | } else |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9420 | xmlBufAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9421 | |
| 9422 | /* Step to next character in input */ |
| 9423 | cptr++; |
| 9424 | if ( ch & 0x80 ) { |
| 9425 | /* if not simple ascii, verify proper format */ |
| 9426 | if ( (ch & 0xc0) != 0xc0 ) { |
| 9427 | xmlGenericError(xmlGenericErrorContext, |
| 9428 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 9429 | /* not asserting an XPath error is probably better */ |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9430 | break; |
| 9431 | } |
| 9432 | /* then skip over remaining bytes for this char */ |
| 9433 | while ( (ch <<= 1) & 0x80 ) |
| 9434 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 9435 | xmlGenericError(xmlGenericErrorContext, |
| 9436 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 9437 | /* not asserting an XPath error is probably better */ |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9438 | break; |
| 9439 | } |
| 9440 | if (ch & 0x80) /* must have had error encountered */ |
| 9441 | break; |
| 9442 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9443 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9444 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9445 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 9446 | xmlBufContent(target))); |
| 9447 | xmlBufFree(target); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9448 | xmlXPathReleaseObject(ctxt->context, str); |
| 9449 | xmlXPathReleaseObject(ctxt->context, from); |
| 9450 | xmlXPathReleaseObject(ctxt->context, to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9451 | } |
| 9452 | |
| 9453 | /** |
| 9454 | * xmlXPathBooleanFunction: |
| 9455 | * @ctxt: the XPath Parser context |
| 9456 | * @nargs: the number of arguments |
| 9457 | * |
| 9458 | * Implement the boolean() XPath function |
| 9459 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9460 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9461 | * - a number is true if and only if it is neither positive or |
| 9462 | * negative zero nor NaN |
| 9463 | * - a node-set is true if and only if it is non-empty |
| 9464 | * - a string is true if and only if its length is non-zero |
| 9465 | */ |
| 9466 | void |
| 9467 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9468 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9469 | |
| 9470 | CHECK_ARITY(1); |
| 9471 | cur = valuePop(ctxt); |
| 9472 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9473 | cur = xmlXPathCacheConvertBoolean(ctxt->context, cur); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9474 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
| 9477 | /** |
| 9478 | * xmlXPathNotFunction: |
| 9479 | * @ctxt: the XPath Parser context |
| 9480 | * @nargs: the number of arguments |
| 9481 | * |
| 9482 | * Implement the not() XPath function |
| 9483 | * boolean not(boolean) |
| 9484 | * The not function returns true if its argument is false, |
| 9485 | * and false otherwise. |
| 9486 | */ |
| 9487 | void |
| 9488 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9489 | CHECK_ARITY(1); |
| 9490 | CAST_TO_BOOLEAN; |
| 9491 | CHECK_TYPE(XPATH_BOOLEAN); |
| 9492 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 9493 | } |
| 9494 | |
| 9495 | /** |
| 9496 | * xmlXPathTrueFunction: |
| 9497 | * @ctxt: the XPath Parser context |
| 9498 | * @nargs: the number of arguments |
| 9499 | * |
| 9500 | * Implement the true() XPath function |
| 9501 | * boolean true() |
| 9502 | */ |
| 9503 | void |
| 9504 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9505 | CHECK_ARITY(0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9506 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
| 9509 | /** |
| 9510 | * xmlXPathFalseFunction: |
| 9511 | * @ctxt: the XPath Parser context |
| 9512 | * @nargs: the number of arguments |
| 9513 | * |
| 9514 | * Implement the false() XPath function |
| 9515 | * boolean false() |
| 9516 | */ |
| 9517 | void |
| 9518 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9519 | CHECK_ARITY(0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9520 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9521 | } |
| 9522 | |
| 9523 | /** |
| 9524 | * xmlXPathLangFunction: |
| 9525 | * @ctxt: the XPath Parser context |
| 9526 | * @nargs: the number of arguments |
| 9527 | * |
| 9528 | * Implement the lang() XPath function |
| 9529 | * boolean lang(string) |
| 9530 | * The lang function returns true or false depending on whether the |
| 9531 | * language of the context node as specified by xml:lang attributes |
| 9532 | * is the same as or is a sublanguage of the language specified by |
| 9533 | * the argument string. The language of the context node is determined |
| 9534 | * by the value of the xml:lang attribute on the context node, or, if |
| 9535 | * the context node has no xml:lang attribute, by the value of the |
| 9536 | * xml:lang attribute on the nearest ancestor of the context node that |
| 9537 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 9538 | * returns false. If there is such an attribute, then lang returns |
| 9539 | * true if the attribute value is equal to the argument ignoring case, |
| 9540 | * or if there is some suffix starting with - such that the attribute |
| 9541 | * value is equal to the argument ignoring that suffix of the attribute |
| 9542 | * value and ignoring case. |
| 9543 | */ |
| 9544 | void |
| 9545 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9546 | xmlXPathObjectPtr val = NULL; |
| 9547 | const xmlChar *theLang = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9548 | const xmlChar *lang; |
| 9549 | int ret = 0; |
| 9550 | int i; |
| 9551 | |
| 9552 | CHECK_ARITY(1); |
| 9553 | CAST_TO_STRING; |
| 9554 | CHECK_TYPE(XPATH_STRING); |
| 9555 | val = valuePop(ctxt); |
| 9556 | lang = val->stringval; |
| 9557 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 9558 | if ((theLang != NULL) && (lang != NULL)) { |
| 9559 | for (i = 0;lang[i] != 0;i++) |
| 9560 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 9561 | goto not_equal; |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9562 | if ((theLang[i] == 0) || (theLang[i] == '-')) |
| 9563 | ret = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9564 | } |
| 9565 | not_equal: |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9566 | if (theLang != NULL) |
| 9567 | xmlFree((void *)theLang); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9568 | |
| 9569 | xmlXPathReleaseObject(ctxt->context, val); |
| 9570 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
| 9573 | /** |
| 9574 | * xmlXPathNumberFunction: |
| 9575 | * @ctxt: the XPath Parser context |
| 9576 | * @nargs: the number of arguments |
| 9577 | * |
| 9578 | * Implement the number() XPath function |
| 9579 | * number number(object?) |
| 9580 | */ |
| 9581 | void |
| 9582 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9583 | xmlXPathObjectPtr cur; |
| 9584 | double res; |
| 9585 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9586 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9587 | if (nargs == 0) { |
| 9588 | if (ctxt->context->node == NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9589 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0.0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9590 | } else { |
| 9591 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 9592 | |
| 9593 | res = xmlXPathStringEvalNumber(content); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9594 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9595 | xmlFree(content); |
| 9596 | } |
| 9597 | return; |
| 9598 | } |
| 9599 | |
| 9600 | CHECK_ARITY(1); |
| 9601 | cur = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9602 | valuePush(ctxt, xmlXPathCacheConvertNumber(ctxt->context, cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9603 | } |
| 9604 | |
| 9605 | /** |
| 9606 | * xmlXPathSumFunction: |
| 9607 | * @ctxt: the XPath Parser context |
| 9608 | * @nargs: the number of arguments |
| 9609 | * |
| 9610 | * Implement the sum() XPath function |
| 9611 | * number sum(node-set) |
| 9612 | * The sum function returns the sum of the values of the nodes in |
| 9613 | * the argument node-set. |
| 9614 | */ |
| 9615 | void |
| 9616 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9617 | xmlXPathObjectPtr cur; |
| 9618 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 9619 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9620 | |
| 9621 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9622 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9623 | ((ctxt->value->type != XPATH_NODESET) && |
| 9624 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 9625 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9626 | cur = valuePop(ctxt); |
| 9627 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9628 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 9629 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 9630 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9631 | } |
| 9632 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9633 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res)); |
| 9634 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9637 | /* |
| 9638 | * To assure working code on multiple platforms, we want to only depend |
| 9639 | * upon the characteristic truncation of converting a floating point value |
| 9640 | * to an integer. Unfortunately, because of the different storage sizes |
| 9641 | * of our internal floating point value (double) and integer (int), we |
| 9642 | * can't directly convert (see bug 301162). This macro is a messy |
| 9643 | * 'workaround' |
| 9644 | */ |
| 9645 | #define XTRUNC(f, v) \ |
| 9646 | f = fmod((v), INT_MAX); \ |
| 9647 | f = (v) - (f) + (double)((int)(f)); |
| 9648 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9649 | /** |
| 9650 | * xmlXPathFloorFunction: |
| 9651 | * @ctxt: the XPath Parser context |
| 9652 | * @nargs: the number of arguments |
| 9653 | * |
| 9654 | * Implement the floor() XPath function |
| 9655 | * number floor(number) |
| 9656 | * The floor function returns the largest (closest to positive infinity) |
| 9657 | * number that is not greater than the argument and that is an integer. |
| 9658 | */ |
| 9659 | void |
| 9660 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9661 | double f; |
| 9662 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9663 | CHECK_ARITY(1); |
| 9664 | CAST_TO_NUMBER; |
| 9665 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9666 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9667 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9668 | if (f != ctxt->value->floatval) { |
| 9669 | if (ctxt->value->floatval > 0) |
| 9670 | ctxt->value->floatval = f; |
| 9671 | else |
| 9672 | ctxt->value->floatval = f - 1; |
| 9673 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9674 | } |
| 9675 | |
| 9676 | /** |
| 9677 | * xmlXPathCeilingFunction: |
| 9678 | * @ctxt: the XPath Parser context |
| 9679 | * @nargs: the number of arguments |
| 9680 | * |
| 9681 | * Implement the ceiling() XPath function |
| 9682 | * number ceiling(number) |
| 9683 | * The ceiling function returns the smallest (closest to negative infinity) |
| 9684 | * number that is not less than the argument and that is an integer. |
| 9685 | */ |
| 9686 | void |
| 9687 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9688 | double f; |
| 9689 | |
| 9690 | CHECK_ARITY(1); |
| 9691 | CAST_TO_NUMBER; |
| 9692 | CHECK_TYPE(XPATH_NUMBER); |
| 9693 | |
| 9694 | #if 0 |
| 9695 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 9696 | #else |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9697 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9698 | if (f != ctxt->value->floatval) { |
| 9699 | if (ctxt->value->floatval > 0) |
| 9700 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 9701 | else { |
| 9702 | if (ctxt->value->floatval < 0 && f == 0) |
| 9703 | ctxt->value->floatval = xmlXPathNZERO; |
| 9704 | else |
| 9705 | ctxt->value->floatval = f; |
| 9706 | } |
| 9707 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9709 | #endif |
| 9710 | } |
| 9711 | |
| 9712 | /** |
| 9713 | * xmlXPathRoundFunction: |
| 9714 | * @ctxt: the XPath Parser context |
| 9715 | * @nargs: the number of arguments |
| 9716 | * |
| 9717 | * Implement the round() XPath function |
| 9718 | * number round(number) |
| 9719 | * The round function returns the number that is closest to the |
| 9720 | * argument and that is an integer. If there are two such numbers, |
| 9721 | * then the one that is even is returned. |
| 9722 | */ |
| 9723 | void |
| 9724 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9725 | double f; |
| 9726 | |
| 9727 | CHECK_ARITY(1); |
| 9728 | CAST_TO_NUMBER; |
| 9729 | CHECK_TYPE(XPATH_NUMBER); |
| 9730 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 9731 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 9732 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 9733 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9734 | (ctxt->value->floatval == 0.0)) |
| 9735 | return; |
| 9736 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9737 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9738 | if (ctxt->value->floatval < 0) { |
| 9739 | if (ctxt->value->floatval < f - 0.5) |
| 9740 | ctxt->value->floatval = f - 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9741 | else |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9742 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 9743 | if (ctxt->value->floatval == 0) |
| 9744 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9745 | } else { |
| 9746 | if (ctxt->value->floatval < f + 0.5) |
| 9747 | ctxt->value->floatval = f; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9748 | else |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9749 | ctxt->value->floatval = f + 1; |
| 9750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9751 | } |
| 9752 | |
| 9753 | /************************************************************************ |
| 9754 | * * |
| 9755 | * The Parser * |
| 9756 | * * |
| 9757 | ************************************************************************/ |
| 9758 | |
| 9759 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9760 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9761 | * implementation. |
| 9762 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 9763 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9764 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9765 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9766 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9767 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 9768 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9769 | |
| 9770 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9771 | * xmlXPathCurrentChar: |
| 9772 | * @ctxt: the XPath parser context |
| 9773 | * @cur: pointer to the beginning of the char |
| 9774 | * @len: pointer to the length of the char read |
| 9775 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9776 | * The current char value, if using UTF-8 this may actually span multiple |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9777 | * bytes in the input buffer. |
| 9778 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 9779 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9780 | */ |
| 9781 | |
| 9782 | static int |
| 9783 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 9784 | unsigned char c; |
| 9785 | unsigned int val; |
| 9786 | const xmlChar *cur; |
| 9787 | |
| 9788 | if (ctxt == NULL) |
| 9789 | return(0); |
| 9790 | cur = ctxt->cur; |
| 9791 | |
| 9792 | /* |
| 9793 | * We are supposed to handle UTF8, check it's valid |
| 9794 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 9795 | * |
| 9796 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 9797 | * 0000 0000-0000 007F 0xxxxxxx |
| 9798 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9799 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9800 | * |
| 9801 | * Check for the 0x110000 limit too |
| 9802 | */ |
| 9803 | c = *cur; |
| 9804 | if (c & 0x80) { |
| 9805 | if ((cur[1] & 0xc0) != 0x80) |
| 9806 | goto encoding_error; |
| 9807 | if ((c & 0xe0) == 0xe0) { |
| 9808 | |
| 9809 | if ((cur[2] & 0xc0) != 0x80) |
| 9810 | goto encoding_error; |
| 9811 | if ((c & 0xf0) == 0xf0) { |
| 9812 | if (((c & 0xf8) != 0xf0) || |
| 9813 | ((cur[3] & 0xc0) != 0x80)) |
| 9814 | goto encoding_error; |
| 9815 | /* 4-byte code */ |
| 9816 | *len = 4; |
| 9817 | val = (cur[0] & 0x7) << 18; |
| 9818 | val |= (cur[1] & 0x3f) << 12; |
| 9819 | val |= (cur[2] & 0x3f) << 6; |
| 9820 | val |= cur[3] & 0x3f; |
| 9821 | } else { |
| 9822 | /* 3-byte code */ |
| 9823 | *len = 3; |
| 9824 | val = (cur[0] & 0xf) << 12; |
| 9825 | val |= (cur[1] & 0x3f) << 6; |
| 9826 | val |= cur[2] & 0x3f; |
| 9827 | } |
| 9828 | } else { |
| 9829 | /* 2-byte code */ |
| 9830 | *len = 2; |
| 9831 | val = (cur[0] & 0x1f) << 6; |
| 9832 | val |= cur[1] & 0x3f; |
| 9833 | } |
| 9834 | if (!IS_CHAR(val)) { |
| 9835 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9836 | } |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9837 | return(val); |
| 9838 | } else { |
| 9839 | /* 1-byte code */ |
| 9840 | *len = 1; |
| 9841 | return((int) *cur); |
| 9842 | } |
| 9843 | encoding_error: |
| 9844 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9845 | * If we detect an UTF8 error that probably means that the |
| 9846 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9847 | * declaration header. Report the error and switch the encoding |
| 9848 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 9849 | * encoding !) |
| 9850 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 9851 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9852 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9853 | } |
| 9854 | |
| 9855 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9856 | * xmlXPathParseNCName: |
| 9857 | * @ctxt: the XPath Parser context |
| 9858 | * |
| 9859 | * parse an XML namespace non qualified name. |
| 9860 | * |
| 9861 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 9862 | * |
| 9863 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 9864 | * CombiningChar | Extender |
| 9865 | * |
| 9866 | * Returns the namespace name or NULL |
| 9867 | */ |
| 9868 | |
| 9869 | xmlChar * |
| 9870 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9871 | const xmlChar *in; |
| 9872 | xmlChar *ret; |
| 9873 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9874 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9875 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9876 | /* |
| 9877 | * Accelerator for simple ASCII names |
| 9878 | */ |
| 9879 | in = ctxt->cur; |
| 9880 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9881 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9882 | (*in == '_')) { |
| 9883 | in++; |
| 9884 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9885 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9886 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 9887 | (*in == '_') || (*in == '.') || |
| 9888 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9889 | in++; |
| 9890 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 9891 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 9892 | (*in == '@') || (*in == '*')) { |
| 9893 | count = in - ctxt->cur; |
| 9894 | if (count == 0) |
| 9895 | return(NULL); |
| 9896 | ret = xmlStrndup(ctxt->cur, count); |
| 9897 | ctxt->cur = in; |
| 9898 | return(ret); |
| 9899 | } |
| 9900 | } |
| 9901 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9902 | } |
| 9903 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9904 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9905 | /** |
| 9906 | * xmlXPathParseQName: |
| 9907 | * @ctxt: the XPath Parser context |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9908 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9909 | * |
| 9910 | * parse an XML qualified name |
| 9911 | * |
| 9912 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 9913 | * |
| 9914 | * [NS 6] Prefix ::= NCName |
| 9915 | * |
| 9916 | * [NS 7] LocalPart ::= NCName |
| 9917 | * |
| 9918 | * Returns the function returns the local part, and prefix is updated |
| 9919 | * to get the Prefix if any. |
| 9920 | */ |
| 9921 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9922 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9923 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 9924 | xmlChar *ret = NULL; |
| 9925 | |
| 9926 | *prefix = NULL; |
| 9927 | ret = xmlXPathParseNCName(ctxt); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 9928 | if (ret && CUR == ':') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9929 | *prefix = ret; |
| 9930 | NEXT; |
| 9931 | ret = xmlXPathParseNCName(ctxt); |
| 9932 | } |
| 9933 | return(ret); |
| 9934 | } |
| 9935 | |
| 9936 | /** |
| 9937 | * xmlXPathParseName: |
| 9938 | * @ctxt: the XPath Parser context |
| 9939 | * |
| 9940 | * parse an XML name |
| 9941 | * |
| 9942 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 9943 | * CombiningChar | Extender |
| 9944 | * |
| 9945 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 9946 | * |
| 9947 | * Returns the namespace name or NULL |
| 9948 | */ |
| 9949 | |
| 9950 | xmlChar * |
| 9951 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9952 | const xmlChar *in; |
| 9953 | xmlChar *ret; |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 9954 | size_t count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9955 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9956 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9957 | /* |
| 9958 | * Accelerator for simple ASCII names |
| 9959 | */ |
| 9960 | in = ctxt->cur; |
| 9961 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9962 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9963 | (*in == '_') || (*in == ':')) { |
| 9964 | in++; |
| 9965 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9966 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9967 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 9968 | (*in == '_') || (*in == '-') || |
| 9969 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9970 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 9971 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9972 | count = in - ctxt->cur; |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 9973 | if (count > XML_MAX_NAME_LENGTH) { |
| 9974 | ctxt->cur = in; |
| 9975 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
| 9976 | } |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9977 | ret = xmlStrndup(ctxt->cur, count); |
| 9978 | ctxt->cur = in; |
| 9979 | return(ret); |
| 9980 | } |
| 9981 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9982 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9983 | } |
| 9984 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9985 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9986 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9987 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 9988 | int len = 0, l; |
| 9989 | int c; |
| 9990 | |
| 9991 | /* |
| 9992 | * Handler for more complex cases |
| 9993 | */ |
| 9994 | c = CUR_CHAR(l); |
| 9995 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9996 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 9997 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9998 | (!IS_LETTER(c) && (c != '_') && |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 9999 | ((!qualified) || (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10000 | return(NULL); |
| 10001 | } |
| 10002 | |
| 10003 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 10004 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 10005 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10006 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10007 | (IS_COMBINING(c)) || |
| 10008 | (IS_EXTENDER(c)))) { |
| 10009 | COPY_BUF(l,buf,len,c); |
| 10010 | NEXTL(l); |
| 10011 | c = CUR_CHAR(l); |
| 10012 | if (len >= XML_MAX_NAMELEN) { |
| 10013 | /* |
| 10014 | * Okay someone managed to make a huge name, so he's ready to pay |
| 10015 | * for the processing speed. |
| 10016 | */ |
| 10017 | xmlChar *buffer; |
| 10018 | int max = len * 2; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10019 | |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 10020 | if (len > XML_MAX_NAME_LENGTH) { |
| 10021 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
| 10022 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10023 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10024 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10025 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10026 | } |
| 10027 | memcpy(buffer, buf, len); |
| 10028 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 10029 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10030 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10031 | (IS_COMBINING(c)) || |
| 10032 | (IS_EXTENDER(c))) { |
| 10033 | if (len + 10 > max) { |
Daniel Veillard | cd852ad | 2012-07-30 10:12:18 +0800 | [diff] [blame] | 10034 | if (max > XML_MAX_NAME_LENGTH) { |
| 10035 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
| 10036 | } |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10037 | max *= 2; |
| 10038 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 10039 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10040 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10041 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10042 | } |
| 10043 | } |
| 10044 | COPY_BUF(l,buffer,len,c); |
| 10045 | NEXTL(l); |
| 10046 | c = CUR_CHAR(l); |
| 10047 | } |
| 10048 | buffer[len] = 0; |
| 10049 | return(buffer); |
| 10050 | } |
| 10051 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 10052 | if (len == 0) |
| 10053 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 10054 | return(xmlStrndup(buf, len)); |
| 10055 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10056 | |
| 10057 | #define MAX_FRAC 20 |
| 10058 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 10059 | /* |
| 10060 | * These are used as divisors for the fractional part of a number. |
| 10061 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 10062 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 10063 | */ |
| 10064 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10065 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 10066 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 10067 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 10068 | 100000000000000.0, |
| 10069 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 10070 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10071 | }; |
| 10072 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10073 | /** |
| 10074 | * xmlXPathStringEvalNumber: |
| 10075 | * @str: A string to scan |
| 10076 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10077 | * [30a] Float ::= Number ('e' Digits?)? |
| 10078 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10079 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10080 | * | '.' Digits |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10081 | * [31] Digits ::= [0-9]+ |
| 10082 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10083 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10084 | * In complement of the Number expression, this function also handles |
| 10085 | * negative values : '-' Number. |
| 10086 | * |
| 10087 | * Returns the double value. |
| 10088 | */ |
| 10089 | double |
| 10090 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 10091 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10092 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10093 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10094 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10095 | int exponent = 0; |
| 10096 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10097 | #ifdef __GNUC__ |
| 10098 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10099 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10100 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 10101 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10102 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10103 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 10104 | return(xmlXPathNAN); |
| 10105 | } |
| 10106 | if (*cur == '-') { |
| 10107 | isneg = 1; |
| 10108 | cur++; |
| 10109 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10110 | |
| 10111 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10112 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10113 | * tmp/temp is a workaround against a gcc compiler bug |
| 10114 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10115 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10116 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10117 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10118 | ret = ret * 10; |
| 10119 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10120 | ok = 1; |
| 10121 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10122 | temp = (double) tmp; |
| 10123 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10124 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10125 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10126 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10127 | while ((*cur >= '0') && (*cur <= '9')) { |
| 10128 | ret = ret * 10 + (*cur - '0'); |
| 10129 | ok = 1; |
| 10130 | cur++; |
| 10131 | } |
| 10132 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10133 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10134 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10135 | int v, frac = 0; |
| 10136 | double fraction = 0; |
| 10137 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10138 | cur++; |
| 10139 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 10140 | return(xmlXPathNAN); |
| 10141 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10142 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 10143 | v = (*cur - '0'); |
| 10144 | fraction = fraction * 10 + v; |
| 10145 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10146 | cur++; |
| 10147 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10148 | fraction /= my_pow10[frac]; |
| 10149 | ret = ret + fraction; |
| 10150 | while ((*cur >= '0') && (*cur <= '9')) |
| 10151 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10152 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10153 | if ((*cur == 'e') || (*cur == 'E')) { |
| 10154 | cur++; |
| 10155 | if (*cur == '-') { |
| 10156 | is_exponent_negative = 1; |
| 10157 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 10158 | } else if (*cur == '+') { |
| 10159 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10160 | } |
| 10161 | while ((*cur >= '0') && (*cur <= '9')) { |
| 10162 | exponent = exponent * 10 + (*cur - '0'); |
| 10163 | cur++; |
| 10164 | } |
| 10165 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10166 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10167 | if (*cur != 0) return(xmlXPathNAN); |
| 10168 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10169 | if (is_exponent_negative) exponent = -exponent; |
| 10170 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10171 | return(ret); |
| 10172 | } |
| 10173 | |
| 10174 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10175 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10176 | * @ctxt: the XPath Parser context |
| 10177 | * |
| 10178 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10179 | * | '.' Digits |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10180 | * [31] Digits ::= [0-9]+ |
| 10181 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10182 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10183 | * |
| 10184 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10185 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10186 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 10187 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10188 | double ret = 0.0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10189 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10190 | int exponent = 0; |
| 10191 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10192 | #ifdef __GNUC__ |
| 10193 | unsigned long tmp = 0; |
| 10194 | double temp; |
| 10195 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10196 | |
| 10197 | CHECK_ERROR; |
| 10198 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 10199 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 10200 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10201 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10202 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10203 | * tmp/temp is a workaround against a gcc compiler bug |
| 10204 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10205 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10206 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10207 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10208 | ret = ret * 10; |
| 10209 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10210 | ok = 1; |
| 10211 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10212 | temp = (double) tmp; |
| 10213 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10214 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10215 | #else |
| 10216 | ret = 0; |
| 10217 | while ((CUR >= '0') && (CUR <= '9')) { |
| 10218 | ret = ret * 10 + (CUR - '0'); |
| 10219 | ok = 1; |
| 10220 | NEXT; |
| 10221 | } |
| 10222 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10223 | if (CUR == '.') { |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10224 | int v, frac = 0; |
| 10225 | double fraction = 0; |
| 10226 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10227 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10228 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 10229 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 10230 | } |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10231 | while ((CUR >= '0') && (CUR <= '9') && (frac < MAX_FRAC)) { |
| 10232 | v = (CUR - '0'); |
| 10233 | fraction = fraction * 10 + v; |
| 10234 | frac = frac + 1; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10235 | NEXT; |
| 10236 | } |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10237 | fraction /= my_pow10[frac]; |
| 10238 | ret = ret + fraction; |
| 10239 | while ((CUR >= '0') && (CUR <= '9')) |
| 10240 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10241 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10242 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10243 | NEXT; |
| 10244 | if (CUR == '-') { |
| 10245 | is_exponent_negative = 1; |
| 10246 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 10247 | } else if (CUR == '+') { |
| 10248 | NEXT; |
| 10249 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10250 | while ((CUR >= '0') && (CUR <= '9')) { |
| 10251 | exponent = exponent * 10 + (CUR - '0'); |
| 10252 | NEXT; |
| 10253 | } |
| 10254 | if (is_exponent_negative) |
| 10255 | exponent = -exponent; |
| 10256 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10257 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10258 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 10259 | xmlXPathCacheNewFloat(ctxt->context, ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10260 | } |
| 10261 | |
| 10262 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10263 | * xmlXPathParseLiteral: |
| 10264 | * @ctxt: the XPath Parser context |
| 10265 | * |
| 10266 | * Parse a Literal |
| 10267 | * |
| 10268 | * [29] Literal ::= '"' [^"]* '"' |
| 10269 | * | "'" [^']* "'" |
| 10270 | * |
| 10271 | * Returns the value found or NULL in case of error |
| 10272 | */ |
| 10273 | static xmlChar * |
| 10274 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 10275 | const xmlChar *q; |
| 10276 | xmlChar *ret = NULL; |
| 10277 | |
| 10278 | if (CUR == '"') { |
| 10279 | NEXT; |
| 10280 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10281 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10282 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10283 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10284 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10285 | } else { |
| 10286 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10287 | NEXT; |
| 10288 | } |
| 10289 | } else if (CUR == '\'') { |
| 10290 | NEXT; |
| 10291 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10292 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10293 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10294 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10295 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10296 | } else { |
| 10297 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10298 | NEXT; |
| 10299 | } |
| 10300 | } else { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10301 | XP_ERRORNULL(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10302 | } |
| 10303 | return(ret); |
| 10304 | } |
| 10305 | |
| 10306 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10307 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10308 | * @ctxt: the XPath Parser context |
| 10309 | * |
| 10310 | * Parse a Literal and push it on the stack. |
| 10311 | * |
| 10312 | * [29] Literal ::= '"' [^"]* '"' |
| 10313 | * | "'" [^']* "'" |
| 10314 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10315 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10316 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10317 | static void |
| 10318 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10319 | const xmlChar *q; |
| 10320 | xmlChar *ret = NULL; |
| 10321 | |
| 10322 | if (CUR == '"') { |
| 10323 | NEXT; |
| 10324 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10325 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10326 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10327 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10328 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 10329 | } else { |
| 10330 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10331 | NEXT; |
| 10332 | } |
| 10333 | } else if (CUR == '\'') { |
| 10334 | NEXT; |
| 10335 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10336 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10337 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10338 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10339 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 10340 | } else { |
| 10341 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10342 | NEXT; |
| 10343 | } |
| 10344 | } else { |
| 10345 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 10346 | } |
| 10347 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10348 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 10349 | xmlXPathCacheNewString(ctxt->context, ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10350 | xmlFree(ret); |
| 10351 | } |
| 10352 | |
| 10353 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10354 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10355 | * @ctxt: the XPath Parser context |
| 10356 | * |
| 10357 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 10358 | * |
| 10359 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10360 | * to variable values. The value of a variable is an object, which can be |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10361 | * of any of the types that are possible for the value of an expression, |
| 10362 | * and may also be of additional types not specified here. |
| 10363 | * |
| 10364 | * Early evaluation is possible since: |
| 10365 | * The variable bindings [...] used to evaluate a subexpression are |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10366 | * always the same as those used to evaluate the containing expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10367 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10368 | * [36] VariableReference ::= '$' QName |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10369 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10370 | static void |
| 10371 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10372 | xmlChar *name; |
| 10373 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10374 | |
| 10375 | SKIP_BLANKS; |
| 10376 | if (CUR != '$') { |
| 10377 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 10378 | } |
| 10379 | NEXT; |
| 10380 | name = xmlXPathParseQName(ctxt, &prefix); |
| 10381 | if (name == NULL) { |
| 10382 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 10383 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10384 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10385 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 10386 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10387 | SKIP_BLANKS; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 10388 | if ((ctxt->context != NULL) && (ctxt->context->flags & XML_XPATH_NOVAR)) { |
Daniel Veillard | 4788128 | 2012-09-07 14:24:50 +0800 | [diff] [blame] | 10389 | XP_ERROR(XPATH_FORBID_VARIABLE_ERROR); |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 10390 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10391 | } |
| 10392 | |
| 10393 | /** |
| 10394 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10395 | * @name: a name string |
| 10396 | * |
| 10397 | * Is the name given a NodeType one. |
| 10398 | * |
| 10399 | * [38] NodeType ::= 'comment' |
| 10400 | * | 'text' |
| 10401 | * | 'processing-instruction' |
| 10402 | * | 'node' |
| 10403 | * |
| 10404 | * Returns 1 if true 0 otherwise |
| 10405 | */ |
| 10406 | int |
| 10407 | xmlXPathIsNodeType(const xmlChar *name) { |
| 10408 | if (name == NULL) |
| 10409 | return(0); |
| 10410 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10411 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10412 | return(1); |
| 10413 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 10414 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10415 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10416 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10417 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10418 | return(1); |
| 10419 | return(0); |
| 10420 | } |
| 10421 | |
| 10422 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10423 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10424 | * @ctxt: the XPath Parser context |
| 10425 | * |
| 10426 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10427 | * [17] Argument ::= Expr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10428 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10429 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10430 | * pushed on the stack |
| 10431 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10432 | static void |
| 10433 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10434 | xmlChar *name; |
| 10435 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10436 | int nbargs = 0; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10437 | int sort = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10438 | |
| 10439 | name = xmlXPathParseQName(ctxt, &prefix); |
| 10440 | if (name == NULL) { |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 10441 | xmlFree(prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10442 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10443 | } |
| 10444 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10445 | #ifdef DEBUG_EXPR |
| 10446 | if (prefix == NULL) |
| 10447 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 10448 | name); |
| 10449 | else |
| 10450 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 10451 | prefix, name); |
| 10452 | #endif |
| 10453 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10454 | if (CUR != '(') { |
| 10455 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10456 | } |
| 10457 | NEXT; |
| 10458 | SKIP_BLANKS; |
| 10459 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10460 | /* |
| 10461 | * Optimization for count(): we don't need the node-set to be sorted. |
| 10462 | */ |
| 10463 | if ((prefix == NULL) && (name[0] == 'c') && |
| 10464 | xmlStrEqual(name, BAD_CAST "count")) |
| 10465 | { |
| 10466 | sort = 0; |
| 10467 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10468 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 10469 | if (CUR != ')') { |
| 10470 | while (CUR != 0) { |
| 10471 | int op1 = ctxt->comp->last; |
| 10472 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10473 | xmlXPathCompileExpr(ctxt, sort); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 10474 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10475 | xmlFree(name); |
| 10476 | xmlFree(prefix); |
| 10477 | return; |
| 10478 | } |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 10479 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 10480 | nbargs++; |
| 10481 | if (CUR == ')') break; |
| 10482 | if (CUR != ',') { |
| 10483 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10484 | } |
| 10485 | NEXT; |
| 10486 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10487 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10488 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10489 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 10490 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10491 | NEXT; |
| 10492 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10493 | } |
| 10494 | |
| 10495 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10496 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10497 | * @ctxt: the XPath Parser context |
| 10498 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10499 | * [15] PrimaryExpr ::= VariableReference |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10500 | * | '(' Expr ')' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10501 | * | Literal |
| 10502 | * | Number |
| 10503 | * | FunctionCall |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10504 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10505 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10506 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10507 | static void |
| 10508 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10509 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10510 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10511 | else if (CUR == '(') { |
| 10512 | NEXT; |
| 10513 | SKIP_BLANKS; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10514 | xmlXPathCompileExpr(ctxt, 1); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10515 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10516 | if (CUR != ')') { |
| 10517 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10518 | } |
| 10519 | NEXT; |
| 10520 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 10521 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10522 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10523 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10524 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10525 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10526 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10527 | } |
| 10528 | SKIP_BLANKS; |
| 10529 | } |
| 10530 | |
| 10531 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10532 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10533 | * @ctxt: the XPath Parser context |
| 10534 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10535 | * [20] FilterExpr ::= PrimaryExpr |
| 10536 | * | FilterExpr Predicate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10537 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10538 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10539 | * Square brackets are used to filter expressions in the same way that |
| 10540 | * they are used in location paths. It is an error if the expression to |
| 10541 | * be filtered does not evaluate to a node-set. The context node list |
| 10542 | * used for evaluating the expression in square brackets is the node-set |
| 10543 | * to be filtered listed in document order. |
| 10544 | */ |
| 10545 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10546 | static void |
| 10547 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 10548 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10549 | CHECK_ERROR; |
| 10550 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10551 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10552 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10553 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10554 | SKIP_BLANKS; |
| 10555 | } |
| 10556 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10557 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10558 | } |
| 10559 | |
| 10560 | /** |
| 10561 | * xmlXPathScanName: |
| 10562 | * @ctxt: the XPath Parser context |
| 10563 | * |
| 10564 | * Trickery: parse an XML name but without consuming the input flow |
| 10565 | * Needed to avoid insanity in the parser state. |
| 10566 | * |
| 10567 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 10568 | * CombiningChar | Extender |
| 10569 | * |
| 10570 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 10571 | * |
| 10572 | * [6] Names ::= Name (S Name)* |
| 10573 | * |
| 10574 | * Returns the Name parsed or NULL |
| 10575 | */ |
| 10576 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10577 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10578 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10579 | int len = 0, l; |
| 10580 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10581 | const xmlChar *cur; |
| 10582 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10583 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10584 | cur = ctxt->cur; |
| 10585 | |
| 10586 | c = CUR_CHAR(l); |
| 10587 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 10588 | (!IS_LETTER(c) && (c != '_') && |
| 10589 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10590 | return(NULL); |
| 10591 | } |
| 10592 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10593 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 10594 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 10595 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10596 | (c == '_') || (c == ':') || |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10597 | (IS_COMBINING(c)) || |
| 10598 | (IS_EXTENDER(c)))) { |
| 10599 | len += l; |
| 10600 | NEXTL(l); |
| 10601 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10602 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10603 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 10604 | ctxt->cur = cur; |
| 10605 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10606 | } |
| 10607 | |
| 10608 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10609 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10610 | * @ctxt: the XPath Parser context |
| 10611 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10612 | * [19] PathExpr ::= LocationPath |
| 10613 | * | FilterExpr |
| 10614 | * | FilterExpr '/' RelativeLocationPath |
| 10615 | * | FilterExpr '//' RelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10616 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10617 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10618 | * The / operator and // operators combine an arbitrary expression |
| 10619 | * and a relative location path. It is an error if the expression |
| 10620 | * does not evaluate to a node-set. |
| 10621 | * The / operator does composition in the same way as when / is |
| 10622 | * used in a location path. As in location paths, // is short for |
| 10623 | * /descendant-or-self::node()/. |
| 10624 | */ |
| 10625 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10626 | static void |
| 10627 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10628 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 10629 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 10630 | |
| 10631 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10632 | if ((CUR == '$') || (CUR == '(') || |
| 10633 | (IS_ASCII_DIGIT(CUR)) || |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 10634 | (CUR == '\'') || (CUR == '"') || |
| 10635 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10636 | lc = 0; |
| 10637 | } else if (CUR == '*') { |
| 10638 | /* relative or absolute location path */ |
| 10639 | lc = 1; |
| 10640 | } else if (CUR == '/') { |
| 10641 | /* relative or absolute location path */ |
| 10642 | lc = 1; |
| 10643 | } else if (CUR == '@') { |
| 10644 | /* relative abbreviated attribute location path */ |
| 10645 | lc = 1; |
| 10646 | } else if (CUR == '.') { |
| 10647 | /* relative abbreviated attribute location path */ |
| 10648 | lc = 1; |
| 10649 | } else { |
| 10650 | /* |
| 10651 | * Problem is finding if we have a name here whether it's: |
| 10652 | * - a nodetype |
| 10653 | * - a function call in which case it's followed by '(' |
| 10654 | * - an axis in which case it's followed by ':' |
| 10655 | * - a element name |
| 10656 | * We do an a priori analysis here rather than having to |
| 10657 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10658 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10659 | * read/write/debug. |
| 10660 | */ |
| 10661 | SKIP_BLANKS; |
| 10662 | name = xmlXPathScanName(ctxt); |
| 10663 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 10664 | #ifdef DEBUG_STEP |
| 10665 | xmlGenericError(xmlGenericErrorContext, |
| 10666 | "PathExpr: Axis\n"); |
| 10667 | #endif |
| 10668 | lc = 1; |
| 10669 | xmlFree(name); |
| 10670 | } else if (name != NULL) { |
| 10671 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10672 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10673 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10674 | while (NXT(len) != 0) { |
| 10675 | if (NXT(len) == '/') { |
| 10676 | /* element name */ |
| 10677 | #ifdef DEBUG_STEP |
| 10678 | xmlGenericError(xmlGenericErrorContext, |
| 10679 | "PathExpr: AbbrRelLocation\n"); |
| 10680 | #endif |
| 10681 | lc = 1; |
| 10682 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10683 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 10684 | /* ignore blanks */ |
| 10685 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10686 | } else if (NXT(len) == ':') { |
| 10687 | #ifdef DEBUG_STEP |
| 10688 | xmlGenericError(xmlGenericErrorContext, |
| 10689 | "PathExpr: AbbrRelLocation\n"); |
| 10690 | #endif |
| 10691 | lc = 1; |
| 10692 | break; |
| 10693 | } else if ((NXT(len) == '(')) { |
| 10694 | /* Note Type or Function */ |
| 10695 | if (xmlXPathIsNodeType(name)) { |
| 10696 | #ifdef DEBUG_STEP |
| 10697 | xmlGenericError(xmlGenericErrorContext, |
| 10698 | "PathExpr: Type search\n"); |
| 10699 | #endif |
| 10700 | lc = 1; |
| 10701 | } else { |
| 10702 | #ifdef DEBUG_STEP |
| 10703 | xmlGenericError(xmlGenericErrorContext, |
| 10704 | "PathExpr: function call\n"); |
| 10705 | #endif |
| 10706 | lc = 0; |
| 10707 | } |
| 10708 | break; |
| 10709 | } else if ((NXT(len) == '[')) { |
| 10710 | /* element name */ |
| 10711 | #ifdef DEBUG_STEP |
| 10712 | xmlGenericError(xmlGenericErrorContext, |
| 10713 | "PathExpr: AbbrRelLocation\n"); |
| 10714 | #endif |
| 10715 | lc = 1; |
| 10716 | break; |
| 10717 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 10718 | (NXT(len) == '=')) { |
| 10719 | lc = 1; |
| 10720 | break; |
| 10721 | } else { |
| 10722 | lc = 1; |
| 10723 | break; |
| 10724 | } |
| 10725 | len++; |
| 10726 | } |
| 10727 | if (NXT(len) == 0) { |
| 10728 | #ifdef DEBUG_STEP |
| 10729 | xmlGenericError(xmlGenericErrorContext, |
| 10730 | "PathExpr: AbbrRelLocation\n"); |
| 10731 | #endif |
| 10732 | /* element name */ |
| 10733 | lc = 1; |
| 10734 | } |
| 10735 | xmlFree(name); |
| 10736 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10737 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10738 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10739 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10740 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10741 | |
| 10742 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10743 | if (CUR == '/') { |
| 10744 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 10745 | } else { |
| 10746 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10747 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10748 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10749 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10750 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10751 | CHECK_ERROR; |
| 10752 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 10753 | SKIP(2); |
| 10754 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10755 | |
| 10756 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 10757 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 10758 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 10759 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10760 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10761 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10762 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10763 | } |
| 10764 | } |
| 10765 | SKIP_BLANKS; |
| 10766 | } |
| 10767 | |
| 10768 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10769 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10770 | * @ctxt: the XPath Parser context |
| 10771 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10772 | * [18] UnionExpr ::= PathExpr |
| 10773 | * | UnionExpr '|' PathExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10774 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10775 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10776 | */ |
| 10777 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10778 | static void |
| 10779 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 10780 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10781 | CHECK_ERROR; |
| 10782 | SKIP_BLANKS; |
| 10783 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10784 | int op1 = ctxt->comp->last; |
| 10785 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10786 | |
| 10787 | NEXT; |
| 10788 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10789 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10790 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10791 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 10792 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10793 | SKIP_BLANKS; |
| 10794 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10795 | } |
| 10796 | |
| 10797 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10798 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10799 | * @ctxt: the XPath Parser context |
| 10800 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10801 | * [27] UnaryExpr ::= UnionExpr |
| 10802 | * | '-' UnaryExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10803 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10804 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10805 | */ |
| 10806 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10807 | static void |
| 10808 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10809 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10810 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10811 | |
| 10812 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 10813 | while (CUR == '-') { |
| 10814 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10815 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10816 | NEXT; |
| 10817 | SKIP_BLANKS; |
| 10818 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10819 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10820 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10821 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10822 | if (found) { |
| 10823 | if (minus) |
| 10824 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 10825 | else |
| 10826 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10827 | } |
| 10828 | } |
| 10829 | |
| 10830 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10831 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10832 | * @ctxt: the XPath Parser context |
| 10833 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10834 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 10835 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 10836 | * | MultiplicativeExpr 'div' UnaryExpr |
| 10837 | * | MultiplicativeExpr 'mod' UnaryExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10838 | * [34] MultiplyOperator ::= '*' |
| 10839 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10840 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10841 | */ |
| 10842 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10843 | static void |
| 10844 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 10845 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10846 | CHECK_ERROR; |
| 10847 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10848 | while ((CUR == '*') || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10849 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 10850 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 10851 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10852 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10853 | |
| 10854 | if (CUR == '*') { |
| 10855 | op = 0; |
| 10856 | NEXT; |
| 10857 | } else if (CUR == 'd') { |
| 10858 | op = 1; |
| 10859 | SKIP(3); |
| 10860 | } else if (CUR == 'm') { |
| 10861 | op = 2; |
| 10862 | SKIP(3); |
| 10863 | } |
| 10864 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10865 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10866 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10867 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10868 | SKIP_BLANKS; |
| 10869 | } |
| 10870 | } |
| 10871 | |
| 10872 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10873 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10874 | * @ctxt: the XPath Parser context |
| 10875 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10876 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 10877 | * | AdditiveExpr '+' MultiplicativeExpr |
| 10878 | * | AdditiveExpr '-' MultiplicativeExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10879 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10880 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10881 | */ |
| 10882 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10883 | static void |
| 10884 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10885 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10886 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10887 | CHECK_ERROR; |
| 10888 | SKIP_BLANKS; |
| 10889 | while ((CUR == '+') || (CUR == '-')) { |
| 10890 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10891 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10892 | |
| 10893 | if (CUR == '+') plus = 1; |
| 10894 | else plus = 0; |
| 10895 | NEXT; |
| 10896 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10897 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10898 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10899 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10900 | SKIP_BLANKS; |
| 10901 | } |
| 10902 | } |
| 10903 | |
| 10904 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10905 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10906 | * @ctxt: the XPath Parser context |
| 10907 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10908 | * [24] RelationalExpr ::= AdditiveExpr |
| 10909 | * | RelationalExpr '<' AdditiveExpr |
| 10910 | * | RelationalExpr '>' AdditiveExpr |
| 10911 | * | RelationalExpr '<=' AdditiveExpr |
| 10912 | * | RelationalExpr '>=' AdditiveExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10913 | * |
| 10914 | * A <= B > C is allowed ? Answer from James, yes with |
| 10915 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 10916 | * which is basically what got implemented. |
| 10917 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10918 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10919 | * on the stack |
| 10920 | */ |
| 10921 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10922 | static void |
| 10923 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 10924 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10925 | CHECK_ERROR; |
| 10926 | SKIP_BLANKS; |
| 10927 | while ((CUR == '<') || |
| 10928 | (CUR == '>') || |
| 10929 | ((CUR == '<') && (NXT(1) == '=')) || |
| 10930 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10931 | int inf, strict; |
| 10932 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10933 | |
| 10934 | if (CUR == '<') inf = 1; |
| 10935 | else inf = 0; |
| 10936 | if (NXT(1) == '=') strict = 0; |
| 10937 | else strict = 1; |
| 10938 | NEXT; |
| 10939 | if (!strict) NEXT; |
| 10940 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10941 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10942 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10943 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10944 | SKIP_BLANKS; |
| 10945 | } |
| 10946 | } |
| 10947 | |
| 10948 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10949 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10950 | * @ctxt: the XPath Parser context |
| 10951 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10952 | * [23] EqualityExpr ::= RelationalExpr |
| 10953 | * | EqualityExpr '=' RelationalExpr |
| 10954 | * | EqualityExpr '!=' RelationalExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10955 | * |
| 10956 | * A != B != C is allowed ? Answer from James, yes with |
| 10957 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 10958 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 10959 | * which is basically what got implemented. |
| 10960 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10961 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10962 | * |
| 10963 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10964 | static void |
| 10965 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 10966 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10967 | CHECK_ERROR; |
| 10968 | SKIP_BLANKS; |
| 10969 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10970 | int eq; |
| 10971 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10972 | |
| 10973 | if (CUR == '=') eq = 1; |
| 10974 | else eq = 0; |
| 10975 | NEXT; |
| 10976 | if (!eq) NEXT; |
| 10977 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10978 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10979 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10980 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10981 | SKIP_BLANKS; |
| 10982 | } |
| 10983 | } |
| 10984 | |
| 10985 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10986 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10987 | * @ctxt: the XPath Parser context |
| 10988 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10989 | * [22] AndExpr ::= EqualityExpr |
| 10990 | * | AndExpr 'and' EqualityExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10991 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10992 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10993 | * |
| 10994 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10995 | static void |
| 10996 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 10997 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10998 | CHECK_ERROR; |
| 10999 | SKIP_BLANKS; |
| 11000 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11001 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11002 | SKIP(3); |
| 11003 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11004 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11005 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11006 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11007 | SKIP_BLANKS; |
| 11008 | } |
| 11009 | } |
| 11010 | |
| 11011 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 11012 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11013 | * @ctxt: the XPath Parser context |
| 11014 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11015 | * [14] Expr ::= OrExpr |
| 11016 | * [21] OrExpr ::= AndExpr |
| 11017 | * | OrExpr 'or' AndExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11018 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11019 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11020 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11021 | static void |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11022 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11023 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11024 | CHECK_ERROR; |
| 11025 | SKIP_BLANKS; |
| 11026 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11027 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11028 | SKIP(2); |
| 11029 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11030 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11031 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11032 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11033 | SKIP_BLANKS; |
| 11034 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11035 | if ((sort) && (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE)) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11036 | /* more ops could be optimized too */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11037 | /* |
| 11038 | * This is the main place to eliminate sorting for |
| 11039 | * operations which don't require a sorted node-set. |
| 11040 | * E.g. count(). |
| 11041 | */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11042 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 11043 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11044 | } |
| 11045 | |
| 11046 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11047 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11048 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11049 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11050 | * |
| 11051 | * [8] Predicate ::= '[' PredicateExpr ']' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11052 | * [9] PredicateExpr ::= Expr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11053 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11054 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11055 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11056 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11057 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11058 | int op1 = ctxt->comp->last; |
| 11059 | |
| 11060 | SKIP_BLANKS; |
| 11061 | if (CUR != '[') { |
| 11062 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 11063 | } |
| 11064 | NEXT; |
| 11065 | SKIP_BLANKS; |
| 11066 | |
| 11067 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11068 | /* |
| 11069 | * This call to xmlXPathCompileExpr() will deactivate sorting |
| 11070 | * of the predicate result. |
| 11071 | * TODO: Sorting is still activated for filters, since I'm not |
| 11072 | * sure if needed. Normally sorting should not be needed, since |
| 11073 | * a filter can only diminish the number of items in a sequence, |
| 11074 | * but won't change its order; so if the initial sequence is sorted, |
| 11075 | * subsequent sorting is not needed. |
| 11076 | */ |
| 11077 | if (! filter) |
| 11078 | xmlXPathCompileExpr(ctxt, 0); |
| 11079 | else |
| 11080 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11081 | CHECK_ERROR; |
| 11082 | |
| 11083 | if (CUR != ']') { |
| 11084 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 11085 | } |
| 11086 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11087 | if (filter) |
| 11088 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 11089 | else |
| 11090 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11091 | |
| 11092 | NEXT; |
| 11093 | SKIP_BLANKS; |
| 11094 | } |
| 11095 | |
| 11096 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11097 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11098 | * @ctxt: the XPath Parser context |
| 11099 | * @test: pointer to a xmlXPathTestVal |
| 11100 | * @type: pointer to a xmlXPathTypeVal |
| 11101 | * @prefix: placeholder for a possible name prefix |
| 11102 | * |
| 11103 | * [7] NodeTest ::= NameTest |
| 11104 | * | NodeType '(' ')' |
| 11105 | * | 'processing-instruction' '(' Literal ')' |
| 11106 | * |
| 11107 | * [37] NameTest ::= '*' |
| 11108 | * | NCName ':' '*' |
| 11109 | * | QName |
| 11110 | * [38] NodeType ::= 'comment' |
| 11111 | * | 'text' |
| 11112 | * | 'processing-instruction' |
| 11113 | * | 'node' |
| 11114 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11115 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11116 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11117 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11118 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 11119 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 11120 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11121 | int blanks; |
| 11122 | |
| 11123 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 11124 | STRANGE; |
| 11125 | return(NULL); |
| 11126 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11127 | *type = (xmlXPathTypeVal) 0; |
| 11128 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11129 | *prefix = NULL; |
| 11130 | SKIP_BLANKS; |
| 11131 | |
| 11132 | if ((name == NULL) && (CUR == '*')) { |
| 11133 | /* |
| 11134 | * All elements |
| 11135 | */ |
| 11136 | NEXT; |
| 11137 | *test = NODE_TEST_ALL; |
| 11138 | return(NULL); |
| 11139 | } |
| 11140 | |
| 11141 | if (name == NULL) |
| 11142 | name = xmlXPathParseNCName(ctxt); |
| 11143 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11144 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11145 | } |
| 11146 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11147 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11148 | SKIP_BLANKS; |
| 11149 | if (CUR == '(') { |
| 11150 | NEXT; |
| 11151 | /* |
| 11152 | * NodeType or PI search |
| 11153 | */ |
| 11154 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 11155 | *type = NODE_TYPE_COMMENT; |
| 11156 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 11157 | *type = NODE_TYPE_NODE; |
| 11158 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 11159 | *type = NODE_TYPE_PI; |
| 11160 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 11161 | *type = NODE_TYPE_TEXT; |
| 11162 | else { |
| 11163 | if (name != NULL) |
| 11164 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11165 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11166 | } |
| 11167 | |
| 11168 | *test = NODE_TEST_TYPE; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11169 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11170 | SKIP_BLANKS; |
| 11171 | if (*type == NODE_TYPE_PI) { |
| 11172 | /* |
| 11173 | * Specific case: search a PI by name. |
| 11174 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11175 | if (name != NULL) |
| 11176 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 11177 | name = NULL; |
| 11178 | if (CUR != ')') { |
| 11179 | name = xmlXPathParseLiteral(ctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11180 | CHECK_ERROR NULL; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 11181 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 11182 | SKIP_BLANKS; |
| 11183 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11184 | } |
| 11185 | if (CUR != ')') { |
| 11186 | if (name != NULL) |
| 11187 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11188 | XP_ERRORNULL(XPATH_UNCLOSED_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11189 | } |
| 11190 | NEXT; |
| 11191 | return(name); |
| 11192 | } |
| 11193 | *test = NODE_TEST_NAME; |
| 11194 | if ((!blanks) && (CUR == ':')) { |
| 11195 | NEXT; |
| 11196 | |
| 11197 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11198 | * Since currently the parser context don't have a |
| 11199 | * namespace list associated: |
| 11200 | * The namespace name for this prefix can be computed |
| 11201 | * only at evaluation time. The compilation is done |
| 11202 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11203 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11204 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11205 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 11206 | if (name != NULL) |
| 11207 | xmlFree(name); |
| 11208 | if (*prefix == NULL) { |
| 11209 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 11210 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11211 | #else |
| 11212 | *prefix = name; |
| 11213 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11214 | |
| 11215 | if (CUR == '*') { |
| 11216 | /* |
| 11217 | * All elements |
| 11218 | */ |
| 11219 | NEXT; |
| 11220 | *test = NODE_TEST_ALL; |
| 11221 | return(NULL); |
| 11222 | } |
| 11223 | |
| 11224 | name = xmlXPathParseNCName(ctxt); |
| 11225 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11226 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11227 | } |
| 11228 | } |
| 11229 | return(name); |
| 11230 | } |
| 11231 | |
| 11232 | /** |
| 11233 | * xmlXPathIsAxisName: |
| 11234 | * @name: a preparsed name token |
| 11235 | * |
| 11236 | * [6] AxisName ::= 'ancestor' |
| 11237 | * | 'ancestor-or-self' |
| 11238 | * | 'attribute' |
| 11239 | * | 'child' |
| 11240 | * | 'descendant' |
| 11241 | * | 'descendant-or-self' |
| 11242 | * | 'following' |
| 11243 | * | 'following-sibling' |
| 11244 | * | 'namespace' |
| 11245 | * | 'parent' |
| 11246 | * | 'preceding' |
| 11247 | * | 'preceding-sibling' |
| 11248 | * | 'self' |
| 11249 | * |
| 11250 | * Returns the axis or 0 |
| 11251 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11252 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11253 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11254 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11255 | switch (name[0]) { |
| 11256 | case 'a': |
| 11257 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 11258 | ret = AXIS_ANCESTOR; |
| 11259 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 11260 | ret = AXIS_ANCESTOR_OR_SELF; |
| 11261 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 11262 | ret = AXIS_ATTRIBUTE; |
| 11263 | break; |
| 11264 | case 'c': |
| 11265 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 11266 | ret = AXIS_CHILD; |
| 11267 | break; |
| 11268 | case 'd': |
| 11269 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 11270 | ret = AXIS_DESCENDANT; |
| 11271 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 11272 | ret = AXIS_DESCENDANT_OR_SELF; |
| 11273 | break; |
| 11274 | case 'f': |
| 11275 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 11276 | ret = AXIS_FOLLOWING; |
| 11277 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 11278 | ret = AXIS_FOLLOWING_SIBLING; |
| 11279 | break; |
| 11280 | case 'n': |
| 11281 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 11282 | ret = AXIS_NAMESPACE; |
| 11283 | break; |
| 11284 | case 'p': |
| 11285 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 11286 | ret = AXIS_PARENT; |
| 11287 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 11288 | ret = AXIS_PRECEDING; |
| 11289 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 11290 | ret = AXIS_PRECEDING_SIBLING; |
| 11291 | break; |
| 11292 | case 's': |
| 11293 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 11294 | ret = AXIS_SELF; |
| 11295 | break; |
| 11296 | } |
| 11297 | return(ret); |
| 11298 | } |
| 11299 | |
| 11300 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11301 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11302 | * @ctxt: the XPath Parser context |
| 11303 | * |
| 11304 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11305 | * | AbbreviatedStep |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11306 | * |
| 11307 | * [12] AbbreviatedStep ::= '.' | '..' |
| 11308 | * |
| 11309 | * [5] AxisSpecifier ::= AxisName '::' |
| 11310 | * | AbbreviatedAxisSpecifier |
| 11311 | * |
| 11312 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 11313 | * |
| 11314 | * Modified for XPtr range support as: |
| 11315 | * |
| 11316 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 11317 | * | AbbreviatedStep |
| 11318 | * | 'range-to' '(' Expr ')' Predicate* |
| 11319 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11320 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11321 | * A location step of . is short for self::node(). This is |
| 11322 | * particularly useful in conjunction with //. For example, the |
| 11323 | * location path .//para is short for |
| 11324 | * self::node()/descendant-or-self::node()/child::para |
| 11325 | * and so will select all para descendant elements of the context |
| 11326 | * node. |
| 11327 | * Similarly, a location step of .. is short for parent::node(). |
| 11328 | * For example, ../title is short for parent::node()/child::title |
| 11329 | * and so will select the title children of the parent of the context |
| 11330 | * node. |
| 11331 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11332 | static void |
| 11333 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11334 | #ifdef LIBXML_XPTR_ENABLED |
| 11335 | int rangeto = 0; |
| 11336 | int op2 = -1; |
| 11337 | #endif |
| 11338 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11339 | SKIP_BLANKS; |
| 11340 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 11341 | SKIP(2); |
| 11342 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11343 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 11344 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11345 | } else if (CUR == '.') { |
| 11346 | NEXT; |
| 11347 | SKIP_BLANKS; |
| 11348 | } else { |
| 11349 | xmlChar *name = NULL; |
| 11350 | const xmlChar *prefix = NULL; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 11351 | xmlXPathTestVal test = (xmlXPathTestVal) 0; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11352 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 11353 | xmlXPathTypeVal type = (xmlXPathTypeVal) 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11354 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11355 | |
| 11356 | /* |
| 11357 | * The modification needed for XPointer change to the production |
| 11358 | */ |
| 11359 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11360 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11361 | name = xmlXPathParseNCName(ctxt); |
| 11362 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11363 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11364 | xmlFree(name); |
| 11365 | SKIP_BLANKS; |
| 11366 | if (CUR != '(') { |
| 11367 | XP_ERROR(XPATH_EXPR_ERROR); |
| 11368 | } |
| 11369 | NEXT; |
| 11370 | SKIP_BLANKS; |
| 11371 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11372 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11373 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11374 | CHECK_ERROR; |
| 11375 | |
| 11376 | SKIP_BLANKS; |
| 11377 | if (CUR != ')') { |
| 11378 | XP_ERROR(XPATH_EXPR_ERROR); |
| 11379 | } |
| 11380 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11381 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11382 | goto eval_predicates; |
| 11383 | } |
| 11384 | } |
| 11385 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11386 | if (CUR == '*') { |
| 11387 | axis = AXIS_CHILD; |
| 11388 | } else { |
| 11389 | if (name == NULL) |
| 11390 | name = xmlXPathParseNCName(ctxt); |
| 11391 | if (name != NULL) { |
| 11392 | axis = xmlXPathIsAxisName(name); |
| 11393 | if (axis != 0) { |
| 11394 | SKIP_BLANKS; |
| 11395 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 11396 | SKIP(2); |
| 11397 | xmlFree(name); |
| 11398 | name = NULL; |
| 11399 | } else { |
| 11400 | /* an element name can conflict with an axis one :-\ */ |
| 11401 | axis = AXIS_CHILD; |
| 11402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11403 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11404 | axis = AXIS_CHILD; |
| 11405 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11406 | } else if (CUR == '@') { |
| 11407 | NEXT; |
| 11408 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11409 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11410 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11411 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11412 | } |
| 11413 | |
Daniel Veillard | 2f3523f | 2010-10-15 18:30:29 +0200 | [diff] [blame] | 11414 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11415 | xmlFree(name); |
| 11416 | return; |
| 11417 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11418 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11419 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11420 | if (test == 0) |
| 11421 | return; |
| 11422 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 11423 | if ((prefix != NULL) && (ctxt->context != NULL) && |
| 11424 | (ctxt->context->flags & XML_XPATH_CHECKNS)) { |
| 11425 | if (xmlXPathNsLookup(ctxt->context, prefix) == NULL) { |
| 11426 | xmlXPathErr(ctxt, XPATH_UNDEF_PREFIX_ERROR); |
| 11427 | } |
| 11428 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11429 | #ifdef DEBUG_STEP |
| 11430 | xmlGenericError(xmlGenericErrorContext, |
| 11431 | "Basis : computing new set\n"); |
| 11432 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11433 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11434 | #ifdef DEBUG_STEP |
| 11435 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11436 | if (ctxt->value == NULL) |
| 11437 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 11438 | else if (ctxt->value->nodesetval == NULL) |
| 11439 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 11440 | else |
| 11441 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11442 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11443 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 11444 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11445 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 11446 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11447 | op1 = ctxt->comp->last; |
| 11448 | ctxt->comp->last = -1; |
| 11449 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11450 | SKIP_BLANKS; |
| 11451 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11452 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11453 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11454 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11455 | #ifdef LIBXML_XPTR_ENABLED |
| 11456 | if (rangeto) { |
| 11457 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 11458 | } else |
| 11459 | #endif |
| 11460 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 11461 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11462 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11463 | } |
| 11464 | #ifdef DEBUG_STEP |
| 11465 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11466 | if (ctxt->value == NULL) |
| 11467 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 11468 | else if (ctxt->value->nodesetval == NULL) |
| 11469 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 11470 | else |
| 11471 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 11472 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11473 | #endif |
| 11474 | } |
| 11475 | |
| 11476 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11477 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11478 | * @ctxt: the XPath Parser context |
| 11479 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11480 | * [3] RelativeLocationPath ::= Step |
| 11481 | * | RelativeLocationPath '/' Step |
| 11482 | * | AbbreviatedRelativeLocationPath |
| 11483 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11484 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11485 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11486 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11487 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11488 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11489 | (xmlXPathParserContextPtr ctxt) { |
| 11490 | SKIP_BLANKS; |
| 11491 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11492 | SKIP(2); |
| 11493 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11494 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 11495 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11496 | } else if (CUR == '/') { |
| 11497 | NEXT; |
| 11498 | SKIP_BLANKS; |
| 11499 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11500 | xmlXPathCompStep(ctxt); |
Martin | 729601f | 2009-10-12 22:42:26 +0200 | [diff] [blame] | 11501 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11502 | SKIP_BLANKS; |
| 11503 | while (CUR == '/') { |
| 11504 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11505 | SKIP(2); |
| 11506 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11507 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11508 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11509 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11510 | } else if (CUR == '/') { |
| 11511 | NEXT; |
| 11512 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11513 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11514 | } |
| 11515 | SKIP_BLANKS; |
| 11516 | } |
| 11517 | } |
| 11518 | |
| 11519 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11520 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11521 | * @ctxt: the XPath Parser context |
| 11522 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11523 | * [1] LocationPath ::= RelativeLocationPath |
| 11524 | * | AbsoluteLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11525 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11526 | * | AbbreviatedAbsoluteLocationPath |
| 11527 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 11528 | * '//' RelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11529 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11530 | * Compile a location path |
| 11531 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11532 | * // is short for /descendant-or-self::node()/. For example, |
| 11533 | * //para is short for /descendant-or-self::node()/child::para and |
| 11534 | * so will select any para element in the document (even a para element |
| 11535 | * that is a document element will be selected by //para since the |
| 11536 | * document element node is a child of the root node); div//para is |
| 11537 | * short for div/descendant-or-self::node()/child::para and so will |
| 11538 | * select all para descendants of div children. |
| 11539 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11540 | static void |
| 11541 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11542 | SKIP_BLANKS; |
| 11543 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11544 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11545 | } else { |
| 11546 | while (CUR == '/') { |
| 11547 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11548 | SKIP(2); |
| 11549 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11550 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 11551 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11552 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11553 | } else if (CUR == '/') { |
| 11554 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 11555 | SKIP_BLANKS; |
| 11556 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 11557 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 11558 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11559 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11560 | } |
Martin | 729601f | 2009-10-12 22:42:26 +0200 | [diff] [blame] | 11561 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11562 | } |
| 11563 | } |
| 11564 | } |
| 11565 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11566 | /************************************************************************ |
| 11567 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11568 | * XPath precompiled expression evaluation * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11569 | * * |
| 11570 | ************************************************************************/ |
| 11571 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11572 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11573 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 11574 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11575 | #ifdef DEBUG_STEP |
| 11576 | static void |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11577 | xmlXPathDebugDumpStepAxis(xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11578 | int nbNodes) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11579 | { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11580 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11581 | switch (op->value) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11582 | case AXIS_ANCESTOR: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11583 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11584 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11585 | case AXIS_ANCESTOR_OR_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11586 | xmlGenericError(xmlGenericErrorContext, |
| 11587 | "axis 'ancestors-or-self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11588 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11589 | case AXIS_ATTRIBUTE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11590 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11591 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11592 | case AXIS_CHILD: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11593 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11594 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11595 | case AXIS_DESCENDANT: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11596 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11597 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11598 | case AXIS_DESCENDANT_OR_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11599 | xmlGenericError(xmlGenericErrorContext, |
| 11600 | "axis 'descendant-or-self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11601 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11602 | case AXIS_FOLLOWING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11603 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11604 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11605 | case AXIS_FOLLOWING_SIBLING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11606 | xmlGenericError(xmlGenericErrorContext, |
| 11607 | "axis 'following-siblings' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11608 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11609 | case AXIS_NAMESPACE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11610 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11611 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11612 | case AXIS_PARENT: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11613 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11614 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11615 | case AXIS_PRECEDING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11616 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11617 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11618 | case AXIS_PRECEDING_SIBLING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11619 | xmlGenericError(xmlGenericErrorContext, |
| 11620 | "axis 'preceding-sibling' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11621 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11622 | case AXIS_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11623 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11624 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11625 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11626 | xmlGenericError(xmlGenericErrorContext, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11627 | " context contains %d nodes\n", nbNodes); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11628 | switch (op->value2) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11629 | case NODE_TEST_NONE: |
| 11630 | xmlGenericError(xmlGenericErrorContext, |
| 11631 | " searching for none !!!\n"); |
| 11632 | break; |
| 11633 | case NODE_TEST_TYPE: |
| 11634 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11635 | " searching for type %d\n", op->value3); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11636 | break; |
| 11637 | case NODE_TEST_PI: |
| 11638 | xmlGenericError(xmlGenericErrorContext, |
| 11639 | " searching for PI !!!\n"); |
| 11640 | break; |
| 11641 | case NODE_TEST_ALL: |
| 11642 | xmlGenericError(xmlGenericErrorContext, |
| 11643 | " searching for *\n"); |
| 11644 | break; |
| 11645 | case NODE_TEST_NS: |
| 11646 | xmlGenericError(xmlGenericErrorContext, |
| 11647 | " searching for namespace %s\n", |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11648 | op->value5); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11649 | break; |
| 11650 | case NODE_TEST_NAME: |
| 11651 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11652 | " searching for name %s\n", op->value5); |
| 11653 | if (op->value4) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11654 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11655 | " with namespace %s\n", op->value4); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11656 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11657 | } |
| 11658 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11659 | } |
| 11660 | #endif /* DEBUG_STEP */ |
| 11661 | |
| 11662 | static int |
| 11663 | xmlXPathCompOpEvalPredicate(xmlXPathParserContextPtr ctxt, |
| 11664 | xmlXPathStepOpPtr op, |
| 11665 | xmlNodeSetPtr set, |
| 11666 | int contextSize, |
| 11667 | int hasNsNodes) |
| 11668 | { |
| 11669 | if (op->ch1 != -1) { |
| 11670 | xmlXPathCompExprPtr comp = ctxt->comp; |
| 11671 | /* |
| 11672 | * Process inner predicates first. |
| 11673 | */ |
| 11674 | if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) { |
| 11675 | /* |
| 11676 | * TODO: raise an internal error. |
| 11677 | */ |
| 11678 | } |
| 11679 | contextSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 11680 | &comp->steps[op->ch1], set, contextSize, hasNsNodes); |
| 11681 | CHECK_ERROR0; |
| 11682 | if (contextSize <= 0) |
| 11683 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11684 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11685 | if (op->ch2 != -1) { |
| 11686 | xmlXPathContextPtr xpctxt = ctxt->context; |
| 11687 | xmlNodePtr contextNode, oldContextNode; |
| 11688 | xmlDocPtr oldContextDoc; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11689 | int i, res, contextPos = 0, newContextSize; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11690 | xmlXPathStepOpPtr exprOp; |
| 11691 | xmlXPathObjectPtr contextObj = NULL, exprRes = NULL; |
| 11692 | |
| 11693 | #ifdef LIBXML_XPTR_ENABLED |
| 11694 | /* |
| 11695 | * URGENT TODO: Check the following: |
| 11696 | * We don't expect location sets if evaluating prediates, right? |
| 11697 | * Only filters should expect location sets, right? |
| 11698 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11699 | #endif |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11700 | /* |
| 11701 | * SPEC XPath 1.0: |
| 11702 | * "For each node in the node-set to be filtered, the |
| 11703 | * PredicateExpr is evaluated with that node as the |
| 11704 | * context node, with the number of nodes in the |
| 11705 | * node-set as the context size, and with the proximity |
| 11706 | * position of the node in the node-set with respect to |
| 11707 | * the axis as the context position;" |
| 11708 | * @oldset is the node-set" to be filtered. |
| 11709 | * |
| 11710 | * SPEC XPath 1.0: |
| 11711 | * "only predicates change the context position and |
| 11712 | * context size (see [2.4 Predicates])." |
| 11713 | * Example: |
| 11714 | * node-set context pos |
| 11715 | * nA 1 |
| 11716 | * nB 2 |
| 11717 | * nC 3 |
| 11718 | * After applying predicate [position() > 1] : |
| 11719 | * node-set context pos |
| 11720 | * nB 1 |
| 11721 | * nC 2 |
| 11722 | */ |
| 11723 | oldContextNode = xpctxt->node; |
| 11724 | oldContextDoc = xpctxt->doc; |
| 11725 | /* |
| 11726 | * Get the expression of this predicate. |
| 11727 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11728 | exprOp = &ctxt->comp->steps[op->ch2]; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11729 | newContextSize = 0; |
| 11730 | for (i = 0; i < set->nodeNr; i++) { |
| 11731 | if (set->nodeTab[i] == NULL) |
| 11732 | continue; |
| 11733 | |
| 11734 | contextNode = set->nodeTab[i]; |
| 11735 | xpctxt->node = contextNode; |
| 11736 | xpctxt->contextSize = contextSize; |
| 11737 | xpctxt->proximityPosition = ++contextPos; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11738 | |
| 11739 | /* |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11740 | * Also set the xpath document in case things like |
| 11741 | * key() are evaluated in the predicate. |
| 11742 | */ |
| 11743 | if ((contextNode->type != XML_NAMESPACE_DECL) && |
| 11744 | (contextNode->doc != NULL)) |
| 11745 | xpctxt->doc = contextNode->doc; |
| 11746 | /* |
| 11747 | * Evaluate the predicate expression with 1 context node |
| 11748 | * at a time; this node is packaged into a node set; this |
| 11749 | * node set is handed over to the evaluation mechanism. |
| 11750 | */ |
| 11751 | if (contextObj == NULL) |
| 11752 | contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 11753 | else { |
| 11754 | if (xmlXPathNodeSetAddUnique(contextObj->nodesetval, |
| 11755 | contextNode) < 0) { |
| 11756 | ctxt->error = XPATH_MEMORY_ERROR; |
| 11757 | goto evaluation_exit; |
| 11758 | } |
| 11759 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11760 | |
| 11761 | valuePush(ctxt, contextObj); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11762 | |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 11763 | res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11764 | |
William M. Brack | 0bcec06 | 2007-02-14 02:15:19 +0000 | [diff] [blame] | 11765 | if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) { |
| 11766 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11767 | newContextSize = 0; |
| 11768 | goto evaluation_exit; |
| 11769 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11770 | |
| 11771 | if (res != 0) { |
| 11772 | newContextSize++; |
| 11773 | } else { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11774 | /* |
| 11775 | * Remove the entry from the initial node set. |
| 11776 | */ |
| 11777 | set->nodeTab[i] = NULL; |
| 11778 | if (contextNode->type == XML_NAMESPACE_DECL) |
| 11779 | xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11780 | } |
| 11781 | if (ctxt->value == contextObj) { |
| 11782 | /* |
| 11783 | * Don't free the temporary XPath object holding the |
| 11784 | * context node, in order to avoid massive recreation |
| 11785 | * inside this loop. |
| 11786 | */ |
| 11787 | valuePop(ctxt); |
| 11788 | xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes); |
| 11789 | } else { |
| 11790 | /* |
| 11791 | * TODO: The object was lost in the evaluation machinery. |
| 11792 | * Can this happen? Maybe in internal-error cases. |
| 11793 | */ |
| 11794 | contextObj = NULL; |
| 11795 | } |
| 11796 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11797 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11798 | if (contextObj != NULL) { |
| 11799 | if (ctxt->value == contextObj) |
| 11800 | valuePop(ctxt); |
| 11801 | xmlXPathReleaseObject(xpctxt, contextObj); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11802 | } |
William M. Brack | 0bcec06 | 2007-02-14 02:15:19 +0000 | [diff] [blame] | 11803 | evaluation_exit: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11804 | if (exprRes != NULL) |
| 11805 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 11806 | /* |
| 11807 | * Reset/invalidate the context. |
| 11808 | */ |
| 11809 | xpctxt->node = oldContextNode; |
| 11810 | xpctxt->doc = oldContextDoc; |
| 11811 | xpctxt->contextSize = -1; |
| 11812 | xpctxt->proximityPosition = -1; |
| 11813 | return(newContextSize); |
| 11814 | } |
| 11815 | return(contextSize); |
| 11816 | } |
| 11817 | |
| 11818 | static int |
| 11819 | xmlXPathCompOpEvalPositionalPredicate(xmlXPathParserContextPtr ctxt, |
| 11820 | xmlXPathStepOpPtr op, |
| 11821 | xmlNodeSetPtr set, |
| 11822 | int contextSize, |
| 11823 | int minPos, |
| 11824 | int maxPos, |
| 11825 | int hasNsNodes) |
| 11826 | { |
| 11827 | if (op->ch1 != -1) { |
| 11828 | xmlXPathCompExprPtr comp = ctxt->comp; |
| 11829 | if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) { |
| 11830 | /* |
| 11831 | * TODO: raise an internal error. |
| 11832 | */ |
| 11833 | } |
| 11834 | contextSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 11835 | &comp->steps[op->ch1], set, contextSize, hasNsNodes); |
| 11836 | CHECK_ERROR0; |
| 11837 | if (contextSize <= 0) |
| 11838 | return(0); |
| 11839 | } |
| 11840 | /* |
| 11841 | * Check if the node set contains a sufficient number of nodes for |
| 11842 | * the requested range. |
| 11843 | */ |
| 11844 | if (contextSize < minPos) { |
| 11845 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11846 | return(0); |
| 11847 | } |
| 11848 | if (op->ch2 == -1) { |
| 11849 | /* |
| 11850 | * TODO: Can this ever happen? |
| 11851 | */ |
| 11852 | return (contextSize); |
| 11853 | } else { |
| 11854 | xmlDocPtr oldContextDoc; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11855 | int i, pos = 0, newContextSize = 0, contextPos = 0, res; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11856 | xmlXPathStepOpPtr exprOp; |
| 11857 | xmlXPathObjectPtr contextObj = NULL, exprRes = NULL; |
| 11858 | xmlNodePtr oldContextNode, contextNode = NULL; |
| 11859 | xmlXPathContextPtr xpctxt = ctxt->context; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11860 | int frame; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11861 | |
| 11862 | #ifdef LIBXML_XPTR_ENABLED |
| 11863 | /* |
| 11864 | * URGENT TODO: Check the following: |
| 11865 | * We don't expect location sets if evaluating prediates, right? |
| 11866 | * Only filters should expect location sets, right? |
| 11867 | */ |
| 11868 | #endif /* LIBXML_XPTR_ENABLED */ |
| 11869 | |
| 11870 | /* |
| 11871 | * Save old context. |
| 11872 | */ |
| 11873 | oldContextNode = xpctxt->node; |
| 11874 | oldContextDoc = xpctxt->doc; |
| 11875 | /* |
| 11876 | * Get the expression of this predicate. |
| 11877 | */ |
| 11878 | exprOp = &ctxt->comp->steps[op->ch2]; |
| 11879 | for (i = 0; i < set->nodeNr; i++) { |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11880 | xmlXPathObjectPtr tmp; |
| 11881 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11882 | if (set->nodeTab[i] == NULL) |
| 11883 | continue; |
| 11884 | |
| 11885 | contextNode = set->nodeTab[i]; |
| 11886 | xpctxt->node = contextNode; |
| 11887 | xpctxt->contextSize = contextSize; |
| 11888 | xpctxt->proximityPosition = ++contextPos; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11889 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11890 | /* |
| 11891 | * Initialize the new set. |
| 11892 | * Also set the xpath document in case things like |
| 11893 | * key() evaluation are attempted on the predicate |
| 11894 | */ |
| 11895 | if ((contextNode->type != XML_NAMESPACE_DECL) && |
| 11896 | (contextNode->doc != NULL)) |
| 11897 | xpctxt->doc = contextNode->doc; |
| 11898 | /* |
| 11899 | * Evaluate the predicate expression with 1 context node |
| 11900 | * at a time; this node is packaged into a node set; this |
| 11901 | * node set is handed over to the evaluation mechanism. |
| 11902 | */ |
| 11903 | if (contextObj == NULL) |
| 11904 | contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 11905 | else { |
| 11906 | if (xmlXPathNodeSetAddUnique(contextObj->nodesetval, |
| 11907 | contextNode) < 0) { |
| 11908 | ctxt->error = XPATH_MEMORY_ERROR; |
| 11909 | goto evaluation_exit; |
| 11910 | } |
| 11911 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11912 | |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11913 | frame = xmlXPathSetFrame(ctxt); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11914 | valuePush(ctxt, contextObj); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 11915 | res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11916 | tmp = valuePop(ctxt); |
| 11917 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11918 | |
William M. Brack | f179456 | 2007-08-23 12:58:13 +0000 | [diff] [blame] | 11919 | if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) { |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11920 | while (tmp != contextObj) { |
Daniel Veillard | df83c17 | 2010-11-17 14:12:14 +0100 | [diff] [blame] | 11921 | /* |
| 11922 | * Free up the result |
| 11923 | * then pop off contextObj, which will be freed later |
| 11924 | */ |
| 11925 | xmlXPathReleaseObject(xpctxt, tmp); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11926 | tmp = valuePop(ctxt); |
Daniel Veillard | fec31bc | 2010-11-18 11:07:24 +0100 | [diff] [blame] | 11927 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11928 | goto evaluation_error; |
William M. Brack | f179456 | 2007-08-23 12:58:13 +0000 | [diff] [blame] | 11929 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11930 | /* push the result back onto the stack */ |
| 11931 | valuePush(ctxt, tmp); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11932 | |
| 11933 | if (res) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11934 | pos++; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11935 | |
| 11936 | if (res && (pos >= minPos) && (pos <= maxPos)) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11937 | /* |
| 11938 | * Fits in the requested range. |
| 11939 | */ |
| 11940 | newContextSize++; |
| 11941 | if (minPos == maxPos) { |
| 11942 | /* |
| 11943 | * Only 1 node was requested. |
| 11944 | */ |
| 11945 | if (contextNode->type == XML_NAMESPACE_DECL) { |
| 11946 | /* |
| 11947 | * As always: take care of those nasty |
| 11948 | * namespace nodes. |
| 11949 | */ |
| 11950 | set->nodeTab[i] = NULL; |
| 11951 | } |
| 11952 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11953 | set->nodeNr = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11954 | set->nodeTab[0] = contextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11955 | goto evaluation_exit; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11956 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11957 | if (pos == maxPos) { |
| 11958 | /* |
| 11959 | * We are done. |
| 11960 | */ |
| 11961 | xmlXPathNodeSetClearFromPos(set, i +1, hasNsNodes); |
| 11962 | goto evaluation_exit; |
| 11963 | } |
| 11964 | } else { |
| 11965 | /* |
| 11966 | * Remove the entry from the initial node set. |
| 11967 | */ |
| 11968 | set->nodeTab[i] = NULL; |
| 11969 | if (contextNode->type == XML_NAMESPACE_DECL) |
| 11970 | xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode); |
| 11971 | } |
| 11972 | if (exprRes != NULL) { |
| 11973 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 11974 | exprRes = NULL; |
| 11975 | } |
| 11976 | if (ctxt->value == contextObj) { |
| 11977 | /* |
| 11978 | * Don't free the temporary XPath object holding the |
| 11979 | * context node, in order to avoid massive recreation |
| 11980 | * inside this loop. |
| 11981 | */ |
| 11982 | valuePop(ctxt); |
| 11983 | xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes); |
| 11984 | } else { |
| 11985 | /* |
| 11986 | * The object was lost in the evaluation machinery. |
| 11987 | * Can this happen? Maybe in case of internal-errors. |
| 11988 | */ |
| 11989 | contextObj = NULL; |
| 11990 | } |
| 11991 | } |
| 11992 | goto evaluation_exit; |
| 11993 | |
| 11994 | evaluation_error: |
| 11995 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11996 | newContextSize = 0; |
| 11997 | |
| 11998 | evaluation_exit: |
| 11999 | if (contextObj != NULL) { |
| 12000 | if (ctxt->value == contextObj) |
| 12001 | valuePop(ctxt); |
| 12002 | xmlXPathReleaseObject(xpctxt, contextObj); |
| 12003 | } |
| 12004 | if (exprRes != NULL) |
| 12005 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 12006 | /* |
| 12007 | * Reset/invalidate the context. |
| 12008 | */ |
| 12009 | xpctxt->node = oldContextNode; |
| 12010 | xpctxt->doc = oldContextDoc; |
| 12011 | xpctxt->contextSize = -1; |
| 12012 | xpctxt->proximityPosition = -1; |
| 12013 | return(newContextSize); |
| 12014 | } |
| 12015 | return(contextSize); |
| 12016 | } |
| 12017 | |
| 12018 | static int |
| 12019 | xmlXPathIsPositionalPredicate(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12020 | xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12021 | int *maxPos) |
| 12022 | { |
| 12023 | |
| 12024 | xmlXPathStepOpPtr exprOp; |
| 12025 | |
| 12026 | /* |
| 12027 | * BIG NOTE: This is not intended for XPATH_OP_FILTER yet! |
| 12028 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12029 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12030 | /* |
| 12031 | * If not -1, then ch1 will point to: |
| 12032 | * 1) For predicates (XPATH_OP_PREDICATE): |
| 12033 | * - an inner predicate operator |
| 12034 | * 2) For filters (XPATH_OP_FILTER): |
| 12035 | * - an inner filter operater OR |
| 12036 | * - an expression selecting the node set. |
| 12037 | * E.g. "key('a', 'b')" or "(//foo | //bar)". |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12038 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12039 | if ((op->op != XPATH_OP_PREDICATE) && (op->op != XPATH_OP_FILTER)) |
| 12040 | return(0); |
| 12041 | |
| 12042 | if (op->ch2 != -1) { |
| 12043 | exprOp = &ctxt->comp->steps[op->ch2]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12044 | } else |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12045 | return(0); |
| 12046 | |
| 12047 | if ((exprOp != NULL) && |
| 12048 | (exprOp->op == XPATH_OP_VALUE) && |
| 12049 | (exprOp->value4 != NULL) && |
| 12050 | (((xmlXPathObjectPtr) exprOp->value4)->type == XPATH_NUMBER)) |
| 12051 | { |
| 12052 | /* |
| 12053 | * We have a "[n]" predicate here. |
| 12054 | * TODO: Unfortunately this simplistic test here is not |
| 12055 | * able to detect a position() predicate in compound |
| 12056 | * expressions like "[@attr = 'a" and position() = 1], |
| 12057 | * and even not the usage of position() in |
| 12058 | * "[position() = 1]"; thus - obviously - a position-range, |
| 12059 | * like it "[position() < 5]", is also not detected. |
| 12060 | * Maybe we could rewrite the AST to ease the optimization. |
| 12061 | */ |
| 12062 | *maxPos = (int) ((xmlXPathObjectPtr) exprOp->value4)->floatval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12063 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12064 | if (((xmlXPathObjectPtr) exprOp->value4)->floatval == |
| 12065 | (float) *maxPos) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12066 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12067 | return(1); |
| 12068 | } |
| 12069 | } |
| 12070 | return(0); |
| 12071 | } |
| 12072 | |
| 12073 | static int |
| 12074 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
| 12075 | xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12076 | xmlNodePtr * first, xmlNodePtr * last, |
| 12077 | int toBool) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12078 | { |
| 12079 | |
| 12080 | #define XP_TEST_HIT \ |
| 12081 | if (hasAxisRange != 0) { \ |
| 12082 | if (++pos == maxPos) { \ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12083 | if (addNode(seq, cur) < 0) \ |
| 12084 | ctxt->error = XPATH_MEMORY_ERROR; \ |
| 12085 | goto axis_range_end; } \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12086 | } else { \ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12087 | if (addNode(seq, cur) < 0) \ |
| 12088 | ctxt->error = XPATH_MEMORY_ERROR; \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12089 | if (breakOnFirstHit) goto first_hit; } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12090 | |
| 12091 | #define XP_TEST_HIT_NS \ |
| 12092 | if (hasAxisRange != 0) { \ |
| 12093 | if (++pos == maxPos) { \ |
| 12094 | hasNsNodes = 1; \ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12095 | if (xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur) < 0) \ |
| 12096 | ctxt->error = XPATH_MEMORY_ERROR; \ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12097 | goto axis_range_end; } \ |
| 12098 | } else { \ |
| 12099 | hasNsNodes = 1; \ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12100 | if (xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur) < 0) \ |
| 12101 | ctxt->error = XPATH_MEMORY_ERROR; \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12102 | if (breakOnFirstHit) goto first_hit; } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12103 | |
| 12104 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 12105 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 12106 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
| 12107 | const xmlChar *prefix = op->value4; |
| 12108 | const xmlChar *name = op->value5; |
| 12109 | const xmlChar *URI = NULL; |
| 12110 | |
| 12111 | #ifdef DEBUG_STEP |
| 12112 | int nbMatches = 0, prevMatches = 0; |
| 12113 | #endif |
| 12114 | int total = 0, hasNsNodes = 0; |
| 12115 | /* The popped object holding the context nodes */ |
| 12116 | xmlXPathObjectPtr obj; |
| 12117 | /* The set of context nodes for the node tests */ |
| 12118 | xmlNodeSetPtr contextSeq; |
| 12119 | int contextIdx; |
| 12120 | xmlNodePtr contextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12121 | /* The final resulting node set wrt to all context nodes */ |
| 12122 | xmlNodeSetPtr outSeq; |
| 12123 | /* |
| 12124 | * The temporary resulting node set wrt 1 context node. |
| 12125 | * Used to feed predicate evaluation. |
| 12126 | */ |
| 12127 | xmlNodeSetPtr seq; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12128 | xmlNodePtr cur; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12129 | /* First predicate operator */ |
| 12130 | xmlXPathStepOpPtr predOp; |
| 12131 | int maxPos; /* The requested position() (when a "[n]" predicate) */ |
| 12132 | int hasPredicateRange, hasAxisRange, pos, size, newSize; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12133 | int breakOnFirstHit; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12134 | |
| 12135 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12136 | int (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12137 | xmlXPathNodeSetMergeFunction mergeAndClear; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12138 | xmlNodePtr oldContextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12139 | xmlXPathContextPtr xpctxt = ctxt->context; |
| 12140 | |
| 12141 | |
| 12142 | CHECK_TYPE0(XPATH_NODESET); |
| 12143 | obj = valuePop(ctxt); |
| 12144 | /* |
| 12145 | * Setup namespaces. |
| 12146 | */ |
| 12147 | if (prefix != NULL) { |
| 12148 | URI = xmlXPathNsLookup(xpctxt, prefix); |
| 12149 | if (URI == NULL) { |
| 12150 | xmlXPathReleaseObject(xpctxt, obj); |
| 12151 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 12152 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12153 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12154 | /* |
| 12155 | * Setup axis. |
| 12156 | * |
| 12157 | * MAYBE FUTURE TODO: merging optimizations: |
| 12158 | * - If the nodes to be traversed wrt to the initial nodes and |
| 12159 | * the current axis cannot overlap, then we could avoid searching |
| 12160 | * for duplicates during the merge. |
| 12161 | * But the question is how/when to evaluate if they cannot overlap. |
| 12162 | * Example: if we know that for two initial nodes, the one is |
| 12163 | * not in the ancestor-or-self axis of the other, then we could safely |
| 12164 | * avoid a duplicate-aware merge, if the axis to be traversed is e.g. |
| 12165 | * the descendant-or-self axis. |
| 12166 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12167 | mergeAndClear = xmlXPathNodeSetMergeAndClear; |
| 12168 | switch (axis) { |
| 12169 | case AXIS_ANCESTOR: |
| 12170 | first = NULL; |
| 12171 | next = xmlXPathNextAncestor; |
| 12172 | break; |
| 12173 | case AXIS_ANCESTOR_OR_SELF: |
| 12174 | first = NULL; |
| 12175 | next = xmlXPathNextAncestorOrSelf; |
| 12176 | break; |
| 12177 | case AXIS_ATTRIBUTE: |
| 12178 | first = NULL; |
| 12179 | last = NULL; |
| 12180 | next = xmlXPathNextAttribute; |
| 12181 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12182 | break; |
| 12183 | case AXIS_CHILD: |
| 12184 | last = NULL; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12185 | if (((test == NODE_TEST_NAME) || (test == NODE_TEST_ALL)) && |
| 12186 | (type == NODE_TYPE_NODE)) |
| 12187 | { |
| 12188 | /* |
| 12189 | * Optimization if an element node type is 'element'. |
| 12190 | */ |
| 12191 | next = xmlXPathNextChildElement; |
| 12192 | } else |
| 12193 | next = xmlXPathNextChild; |
| 12194 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12195 | break; |
| 12196 | case AXIS_DESCENDANT: |
| 12197 | last = NULL; |
| 12198 | next = xmlXPathNextDescendant; |
| 12199 | break; |
| 12200 | case AXIS_DESCENDANT_OR_SELF: |
| 12201 | last = NULL; |
| 12202 | next = xmlXPathNextDescendantOrSelf; |
| 12203 | break; |
| 12204 | case AXIS_FOLLOWING: |
| 12205 | last = NULL; |
| 12206 | next = xmlXPathNextFollowing; |
| 12207 | break; |
| 12208 | case AXIS_FOLLOWING_SIBLING: |
| 12209 | last = NULL; |
| 12210 | next = xmlXPathNextFollowingSibling; |
| 12211 | break; |
| 12212 | case AXIS_NAMESPACE: |
| 12213 | first = NULL; |
| 12214 | last = NULL; |
| 12215 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 12216 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12217 | break; |
| 12218 | case AXIS_PARENT: |
| 12219 | first = NULL; |
| 12220 | next = xmlXPathNextParent; |
| 12221 | break; |
| 12222 | case AXIS_PRECEDING: |
| 12223 | first = NULL; |
| 12224 | next = xmlXPathNextPrecedingInternal; |
| 12225 | break; |
| 12226 | case AXIS_PRECEDING_SIBLING: |
| 12227 | first = NULL; |
| 12228 | next = xmlXPathNextPrecedingSibling; |
| 12229 | break; |
| 12230 | case AXIS_SELF: |
| 12231 | first = NULL; |
| 12232 | last = NULL; |
| 12233 | next = xmlXPathNextSelf; |
| 12234 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12235 | break; |
| 12236 | } |
| 12237 | |
| 12238 | #ifdef DEBUG_STEP |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 12239 | xmlXPathDebugDumpStepAxis(op, |
| 12240 | (obj->nodesetval != NULL) ? obj->nodesetval->nodeNr : 0); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12241 | #endif |
| 12242 | |
| 12243 | if (next == NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12244 | xmlXPathReleaseObject(xpctxt, obj); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12245 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12246 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12247 | contextSeq = obj->nodesetval; |
| 12248 | if ((contextSeq == NULL) || (contextSeq->nodeNr <= 0)) { |
| 12249 | xmlXPathReleaseObject(xpctxt, obj); |
| 12250 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, NULL)); |
| 12251 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12252 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12253 | /* |
| 12254 | * Predicate optimization --------------------------------------------- |
| 12255 | * If this step has a last predicate, which contains a position(), |
| 12256 | * then we'll optimize (although not exactly "position()", but only |
| 12257 | * the short-hand form, i.e., "[n]". |
| 12258 | * |
| 12259 | * Example - expression "/foo[parent::bar][1]": |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12260 | * |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12261 | * COLLECT 'child' 'name' 'node' foo -- op (we are here) |
| 12262 | * ROOT -- op->ch1 |
| 12263 | * PREDICATE -- op->ch2 (predOp) |
| 12264 | * PREDICATE -- predOp->ch1 = [parent::bar] |
| 12265 | * SORT |
| 12266 | * COLLECT 'parent' 'name' 'node' bar |
| 12267 | * NODE |
| 12268 | * ELEM Object is a number : 1 -- predOp->ch2 = [1] |
| 12269 | * |
| 12270 | */ |
| 12271 | maxPos = 0; |
| 12272 | predOp = NULL; |
| 12273 | hasPredicateRange = 0; |
| 12274 | hasAxisRange = 0; |
| 12275 | if (op->ch2 != -1) { |
| 12276 | /* |
| 12277 | * There's at least one predicate. 16 == XPATH_OP_PREDICATE |
| 12278 | */ |
| 12279 | predOp = &ctxt->comp->steps[op->ch2]; |
| 12280 | if (xmlXPathIsPositionalPredicate(ctxt, predOp, &maxPos)) { |
| 12281 | if (predOp->ch1 != -1) { |
| 12282 | /* |
| 12283 | * Use the next inner predicate operator. |
| 12284 | */ |
| 12285 | predOp = &ctxt->comp->steps[predOp->ch1]; |
| 12286 | hasPredicateRange = 1; |
| 12287 | } else { |
| 12288 | /* |
| 12289 | * There's no other predicate than the [n] predicate. |
| 12290 | */ |
| 12291 | predOp = NULL; |
| 12292 | hasAxisRange = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12293 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12294 | } |
| 12295 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12296 | breakOnFirstHit = ((toBool) && (predOp == NULL)) ? 1 : 0; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12297 | /* |
| 12298 | * Axis traversal ----------------------------------------------------- |
| 12299 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12300 | /* |
| 12301 | * 2.3 Node Tests |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12302 | * - For the attribute axis, the principal node type is attribute. |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12303 | * - For the namespace axis, the principal node type is namespace. |
| 12304 | * - For other axes, the principal node type is element. |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12305 | * |
| 12306 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12307 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12308 | * select all element children of the context node |
| 12309 | */ |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12310 | oldContextNode = xpctxt->node; |
| 12311 | addNode = xmlXPathNodeSetAddUnique; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12312 | outSeq = NULL; |
| 12313 | seq = NULL; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12314 | contextNode = NULL; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12315 | contextIdx = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12316 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12317 | |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12318 | while (((contextIdx < contextSeq->nodeNr) || (contextNode != NULL)) && |
| 12319 | (ctxt->error == XPATH_EXPRESSION_OK)) { |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 12320 | xpctxt->node = contextSeq->nodeTab[contextIdx++]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12321 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12322 | if (seq == NULL) { |
| 12323 | seq = xmlXPathNodeSetCreate(NULL); |
| 12324 | if (seq == NULL) { |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12325 | total = 0; |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12326 | goto error; |
| 12327 | } |
| 12328 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12329 | /* |
| 12330 | * Traverse the axis and test the nodes. |
| 12331 | */ |
| 12332 | pos = 0; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12333 | cur = NULL; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12334 | hasNsNodes = 0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12335 | do { |
| 12336 | cur = next(ctxt, cur); |
| 12337 | if (cur == NULL) |
| 12338 | break; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12339 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12340 | /* |
| 12341 | * QUESTION TODO: What does the "first" and "last" stuff do? |
| 12342 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12343 | if ((first != NULL) && (*first != NULL)) { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12344 | if (*first == cur) |
| 12345 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12346 | if (((total % 256) == 0) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12347 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12348 | (xmlXPathCmpNodesExt(*first, cur) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12349 | #else |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12350 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12351 | #endif |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12352 | { |
| 12353 | break; |
| 12354 | } |
| 12355 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12356 | if ((last != NULL) && (*last != NULL)) { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12357 | if (*last == cur) |
| 12358 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12359 | if (((total % 256) == 0) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12360 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12361 | (xmlXPathCmpNodesExt(cur, *last) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12362 | #else |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12363 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12364 | #endif |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12365 | { |
| 12366 | break; |
| 12367 | } |
| 12368 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12369 | |
| 12370 | total++; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12371 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12372 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12373 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 12374 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12375 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12376 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12377 | case NODE_TEST_NONE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12378 | total = 0; |
| 12379 | STRANGE |
| 12380 | goto error; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12381 | case NODE_TEST_TYPE: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12382 | if (type == NODE_TYPE_NODE) { |
| 12383 | switch (cur->type) { |
| 12384 | case XML_DOCUMENT_NODE: |
| 12385 | case XML_HTML_DOCUMENT_NODE: |
| 12386 | #ifdef LIBXML_DOCB_ENABLED |
| 12387 | case XML_DOCB_DOCUMENT_NODE: |
| 12388 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12389 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12390 | case XML_ATTRIBUTE_NODE: |
| 12391 | case XML_PI_NODE: |
| 12392 | case XML_COMMENT_NODE: |
| 12393 | case XML_CDATA_SECTION_NODE: |
| 12394 | case XML_TEXT_NODE: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12395 | XP_TEST_HIT |
| 12396 | break; |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 12397 | case XML_NAMESPACE_DECL: { |
| 12398 | if (axis == AXIS_NAMESPACE) { |
| 12399 | XP_TEST_HIT_NS |
| 12400 | } else { |
| 12401 | hasNsNodes = 1; |
| 12402 | XP_TEST_HIT |
| 12403 | } |
| 12404 | break; |
| 12405 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12406 | default: |
| 12407 | break; |
| 12408 | } |
| 12409 | } else if (cur->type == type) { |
Daniel Veillard | 713434d | 2012-09-26 10:21:06 +0800 | [diff] [blame] | 12410 | if (cur->type == XML_NAMESPACE_DECL) |
Kasimier T. Buchcik | 889b762 | 2006-07-03 11:44:13 +0000 | [diff] [blame] | 12411 | XP_TEST_HIT_NS |
| 12412 | else |
| 12413 | XP_TEST_HIT |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12414 | } else if ((type == NODE_TYPE_TEXT) && |
| 12415 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 12416 | { |
| 12417 | XP_TEST_HIT |
| 12418 | } |
| 12419 | break; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12420 | case NODE_TEST_PI: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12421 | if ((cur->type == XML_PI_NODE) && |
| 12422 | ((name == NULL) || xmlStrEqual(name, cur->name))) |
| 12423 | { |
| 12424 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12425 | } |
| 12426 | break; |
| 12427 | case NODE_TEST_ALL: |
| 12428 | if (axis == AXIS_ATTRIBUTE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12429 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 12430 | { |
Nick Wellnhofer | e8de99f | 2013-08-05 01:26:25 +0200 | [diff] [blame] | 12431 | if (prefix == NULL) |
| 12432 | { |
| 12433 | XP_TEST_HIT |
| 12434 | } else if ((cur->ns != NULL) && |
| 12435 | (xmlStrEqual(URI, cur->ns->href))) |
| 12436 | { |
| 12437 | XP_TEST_HIT |
| 12438 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12439 | } |
| 12440 | } else if (axis == AXIS_NAMESPACE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12441 | if (cur->type == XML_NAMESPACE_DECL) |
| 12442 | { |
| 12443 | XP_TEST_HIT_NS |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12444 | } |
| 12445 | } else { |
| 12446 | if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12447 | if (prefix == NULL) |
| 12448 | { |
| 12449 | XP_TEST_HIT |
| 12450 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12451 | } else if ((cur->ns != NULL) && |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12452 | (xmlStrEqual(URI, cur->ns->href))) |
| 12453 | { |
| 12454 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12455 | } |
| 12456 | } |
| 12457 | } |
| 12458 | break; |
| 12459 | case NODE_TEST_NS:{ |
| 12460 | TODO; |
| 12461 | break; |
| 12462 | } |
| 12463 | case NODE_TEST_NAME: |
Daniel Veillard | fe3970e | 2006-11-23 16:08:30 +0000 | [diff] [blame] | 12464 | if (axis == AXIS_ATTRIBUTE) { |
| 12465 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 12466 | break; |
| 12467 | } else if (axis == AXIS_NAMESPACE) { |
| 12468 | if (cur->type != XML_NAMESPACE_DECL) |
| 12469 | break; |
| 12470 | } else { |
| 12471 | if (cur->type != XML_ELEMENT_NODE) |
| 12472 | break; |
| 12473 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12474 | switch (cur->type) { |
| 12475 | case XML_ELEMENT_NODE: |
| 12476 | if (xmlStrEqual(name, cur->name)) { |
| 12477 | if (prefix == NULL) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12478 | if (cur->ns == NULL) |
| 12479 | { |
| 12480 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12481 | } |
| 12482 | } else { |
| 12483 | if ((cur->ns != NULL) && |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12484 | (xmlStrEqual(URI, cur->ns->href))) |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12485 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12486 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12487 | } |
| 12488 | } |
| 12489 | } |
| 12490 | break; |
| 12491 | case XML_ATTRIBUTE_NODE:{ |
| 12492 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 12493 | |
| 12494 | if (xmlStrEqual(name, attr->name)) { |
| 12495 | if (prefix == NULL) { |
| 12496 | if ((attr->ns == NULL) || |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12497 | (attr->ns->prefix == NULL)) |
| 12498 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12499 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12500 | } |
| 12501 | } else { |
| 12502 | if ((attr->ns != NULL) && |
| 12503 | (xmlStrEqual(URI, |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12504 | attr->ns->href))) |
| 12505 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12506 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12507 | } |
| 12508 | } |
| 12509 | } |
| 12510 | break; |
| 12511 | } |
| 12512 | case XML_NAMESPACE_DECL: |
| 12513 | if (cur->type == XML_NAMESPACE_DECL) { |
| 12514 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 12515 | |
| 12516 | if ((ns->prefix != NULL) && (name != NULL) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12517 | && (xmlStrEqual(ns->prefix, name))) |
| 12518 | { |
| 12519 | XP_TEST_HIT_NS |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12520 | } |
| 12521 | } |
| 12522 | break; |
| 12523 | default: |
| 12524 | break; |
| 12525 | } |
| 12526 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12527 | } /* switch(test) */ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12528 | } while ((cur != NULL) && (ctxt->error == XPATH_EXPRESSION_OK)); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12529 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12530 | goto apply_predicates; |
| 12531 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12532 | axis_range_end: /* ----------------------------------------------------- */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12533 | /* |
| 12534 | * We have a "/foo[n]", and position() = n was reached. |
| 12535 | * Note that we can have as well "/foo/::parent::foo[1]", so |
| 12536 | * a duplicate-aware merge is still needed. |
| 12537 | * Merge with the result. |
| 12538 | */ |
| 12539 | if (outSeq == NULL) { |
| 12540 | outSeq = seq; |
| 12541 | seq = NULL; |
| 12542 | } else |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12543 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12544 | /* |
| 12545 | * Break if only a true/false result was requested. |
| 12546 | */ |
| 12547 | if (toBool) |
| 12548 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12549 | continue; |
| 12550 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12551 | first_hit: /* ---------------------------------------------------------- */ |
| 12552 | /* |
| 12553 | * Break if only a true/false result was requested and |
| 12554 | * no predicates existed and a node test succeeded. |
| 12555 | */ |
| 12556 | if (outSeq == NULL) { |
| 12557 | outSeq = seq; |
| 12558 | seq = NULL; |
| 12559 | } else |
| 12560 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12561 | break; |
| 12562 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12563 | #ifdef DEBUG_STEP |
| 12564 | if (seq != NULL) |
| 12565 | nbMatches += seq->nodeNr; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12566 | #endif |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12567 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12568 | apply_predicates: /* --------------------------------------------------- */ |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 12569 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 12570 | goto error; |
| 12571 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12572 | /* |
| 12573 | * Apply predicates. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12574 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12575 | if ((predOp != NULL) && (seq->nodeNr > 0)) { |
| 12576 | /* |
| 12577 | * E.g. when we have a "/foo[some expression][n]". |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12578 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12579 | /* |
| 12580 | * QUESTION TODO: The old predicate evaluation took into |
| 12581 | * account location-sets. |
| 12582 | * (E.g. ctxt->value->type == XPATH_LOCATIONSET) |
| 12583 | * Do we expect such a set here? |
| 12584 | * All what I learned now from the evaluation semantics |
| 12585 | * does not indicate that a location-set will be processed |
| 12586 | * here, so this looks OK. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12587 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12588 | /* |
| 12589 | * Iterate over all predicates, starting with the outermost |
| 12590 | * predicate. |
| 12591 | * TODO: Problem: we cannot execute the inner predicates first |
| 12592 | * since we cannot go back *up* the operator tree! |
| 12593 | * Options we have: |
| 12594 | * 1) Use of recursive functions (like is it currently done |
| 12595 | * via xmlXPathCompOpEval()) |
| 12596 | * 2) Add a predicate evaluation information stack to the |
| 12597 | * context struct |
| 12598 | * 3) Change the way the operators are linked; we need a |
| 12599 | * "parent" field on xmlXPathStepOp |
| 12600 | * |
| 12601 | * For the moment, I'll try to solve this with a recursive |
| 12602 | * function: xmlXPathCompOpEvalPredicate(). |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12603 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12604 | size = seq->nodeNr; |
| 12605 | if (hasPredicateRange != 0) |
| 12606 | newSize = xmlXPathCompOpEvalPositionalPredicate(ctxt, |
| 12607 | predOp, seq, size, maxPos, maxPos, hasNsNodes); |
| 12608 | else |
| 12609 | newSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 12610 | predOp, seq, size, hasNsNodes); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12611 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12612 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 12613 | total = 0; |
| 12614 | goto error; |
| 12615 | } |
| 12616 | /* |
| 12617 | * Add the filtered set of nodes to the result node set. |
| 12618 | */ |
| 12619 | if (newSize == 0) { |
| 12620 | /* |
| 12621 | * The predicates filtered all nodes out. |
| 12622 | */ |
| 12623 | xmlXPathNodeSetClear(seq, hasNsNodes); |
| 12624 | } else if (seq->nodeNr > 0) { |
| 12625 | /* |
| 12626 | * Add to result set. |
| 12627 | */ |
| 12628 | if (outSeq == NULL) { |
| 12629 | if (size != newSize) { |
| 12630 | /* |
| 12631 | * We need to merge and clear here, since |
| 12632 | * the sequence will contained NULLed entries. |
| 12633 | */ |
| 12634 | outSeq = mergeAndClear(NULL, seq, 1); |
| 12635 | } else { |
| 12636 | outSeq = seq; |
| 12637 | seq = NULL; |
| 12638 | } |
| 12639 | } else |
| 12640 | outSeq = mergeAndClear(outSeq, seq, |
| 12641 | (size != newSize) ? 1: 0); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12642 | /* |
| 12643 | * Break if only a true/false result was requested. |
| 12644 | */ |
| 12645 | if (toBool) |
| 12646 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12647 | } |
| 12648 | } else if (seq->nodeNr > 0) { |
| 12649 | /* |
| 12650 | * Add to result set. |
| 12651 | */ |
| 12652 | if (outSeq == NULL) { |
| 12653 | outSeq = seq; |
| 12654 | seq = NULL; |
| 12655 | } else { |
| 12656 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12657 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12658 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12659 | } |
| 12660 | |
| 12661 | error: |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 12662 | if ((obj->boolval) && (obj->user != NULL)) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12663 | /* |
| 12664 | * QUESTION TODO: What does this do and why? |
| 12665 | * TODO: Do we have to do this also for the "error" |
| 12666 | * cleanup further down? |
| 12667 | */ |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 12668 | ctxt->value->boolval = 1; |
| 12669 | ctxt->value->user = obj->user; |
| 12670 | obj->user = NULL; |
| 12671 | obj->boolval = 0; |
| 12672 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12673 | xmlXPathReleaseObject(xpctxt, obj); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12674 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12675 | /* |
| 12676 | * Ensure we return at least an emtpy set. |
| 12677 | */ |
| 12678 | if (outSeq == NULL) { |
| 12679 | if ((seq != NULL) && (seq->nodeNr == 0)) |
| 12680 | outSeq = seq; |
| 12681 | else |
| 12682 | outSeq = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 12683 | /* XXX what if xmlXPathNodeSetCreate returned NULL here? */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12684 | } |
| 12685 | if ((seq != NULL) && (seq != outSeq)) { |
| 12686 | xmlXPathFreeNodeSet(seq); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12687 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12688 | /* |
| 12689 | * Hand over the result. Better to push the set also in |
| 12690 | * case of errors. |
| 12691 | */ |
| 12692 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, outSeq)); |
| 12693 | /* |
| 12694 | * Reset the context node. |
| 12695 | */ |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12696 | xpctxt->node = oldContextNode; |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 12697 | /* |
| 12698 | * When traversing the namespace axis in "toBool" mode, it's |
| 12699 | * possible that tmpNsList wasn't freed. |
| 12700 | */ |
| 12701 | if (xpctxt->tmpNsList != NULL) { |
| 12702 | xmlFree(xpctxt->tmpNsList); |
| 12703 | xpctxt->tmpNsList = NULL; |
| 12704 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12705 | |
| 12706 | #ifdef DEBUG_STEP |
| 12707 | xmlGenericError(xmlGenericErrorContext, |
| 12708 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 12709 | total, nbMatches); |
| 12710 | #endif |
| 12711 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12712 | return(total); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12713 | } |
| 12714 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12715 | static int |
| 12716 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 12717 | xmlXPathStepOpPtr op, xmlNodePtr * first); |
| 12718 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12719 | /** |
| 12720 | * xmlXPathCompOpEvalFirst: |
| 12721 | * @ctxt: the XPath parser context with the compiled expression |
| 12722 | * @op: an XPath compiled operation |
| 12723 | * @first: the first elem found so far |
| 12724 | * |
| 12725 | * Evaluate the Precompiled XPath operation searching only the first |
| 12726 | * element in document order |
| 12727 | * |
| 12728 | * Returns the number of examined objects. |
| 12729 | */ |
| 12730 | static int |
| 12731 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 12732 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 12733 | { |
| 12734 | int total = 0, cur; |
| 12735 | xmlXPathCompExprPtr comp; |
| 12736 | xmlXPathObjectPtr arg1, arg2; |
| 12737 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12738 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12739 | comp = ctxt->comp; |
| 12740 | switch (op->op) { |
| 12741 | case XPATH_OP_END: |
| 12742 | return (0); |
| 12743 | case XPATH_OP_UNION: |
| 12744 | total = |
| 12745 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 12746 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12747 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12748 | if ((ctxt->value != NULL) |
| 12749 | && (ctxt->value->type == XPATH_NODESET) |
| 12750 | && (ctxt->value->nodesetval != NULL) |
| 12751 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 12752 | /* |
| 12753 | * limit tree traversing to first node in the result |
| 12754 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12755 | /* |
| 12756 | * OPTIMIZE TODO: This implicitely sorts |
| 12757 | * the result, even if not needed. E.g. if the argument |
| 12758 | * of the count() function, no sorting is needed. |
| 12759 | * OPTIMIZE TODO: How do we know if the node-list wasn't |
| 12760 | * aready sorted? |
| 12761 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12762 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 12763 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12764 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 12765 | } |
| 12766 | cur = |
| 12767 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 12768 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12769 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12770 | CHECK_TYPE0(XPATH_NODESET); |
| 12771 | arg2 = valuePop(ctxt); |
| 12772 | |
| 12773 | CHECK_TYPE0(XPATH_NODESET); |
| 12774 | arg1 = valuePop(ctxt); |
| 12775 | |
| 12776 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 12777 | arg2->nodesetval); |
| 12778 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12779 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12780 | /* optimizer */ |
| 12781 | if (total > cur) |
| 12782 | xmlXPathCompSwap(op); |
| 12783 | return (total + cur); |
| 12784 | case XPATH_OP_ROOT: |
| 12785 | xmlXPathRoot(ctxt); |
| 12786 | return (0); |
| 12787 | case XPATH_OP_NODE: |
| 12788 | if (op->ch1 != -1) |
| 12789 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12790 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12791 | if (op->ch2 != -1) |
| 12792 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12793 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12794 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 12795 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12796 | return (total); |
| 12797 | case XPATH_OP_RESET: |
| 12798 | if (op->ch1 != -1) |
| 12799 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12800 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12801 | if (op->ch2 != -1) |
| 12802 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12803 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12804 | ctxt->context->node = NULL; |
| 12805 | return (total); |
| 12806 | case XPATH_OP_COLLECT:{ |
| 12807 | if (op->ch1 == -1) |
| 12808 | return (total); |
| 12809 | |
| 12810 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12811 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12812 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12813 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12814 | return (total); |
| 12815 | } |
| 12816 | case XPATH_OP_VALUE: |
| 12817 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12818 | xmlXPathCacheObjectCopy(ctxt->context, |
| 12819 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12820 | return (0); |
| 12821 | case XPATH_OP_SORT: |
| 12822 | if (op->ch1 != -1) |
| 12823 | total += |
| 12824 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 12825 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12826 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12827 | if ((ctxt->value != NULL) |
| 12828 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12829 | && (ctxt->value->nodesetval != NULL) |
| 12830 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12831 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 12832 | return (total); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12833 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 12834 | case XPATH_OP_FILTER: |
Marius Wachtler | 2ddecc2 | 2010-10-12 09:09:07 +0200 | [diff] [blame] | 12835 | total += xmlXPathCompOpEvalFilterFirst(ctxt, op, first); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12836 | return (total); |
| 12837 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12838 | default: |
| 12839 | return (xmlXPathCompOpEval(ctxt, op)); |
| 12840 | } |
| 12841 | } |
| 12842 | |
| 12843 | /** |
| 12844 | * xmlXPathCompOpEvalLast: |
| 12845 | * @ctxt: the XPath parser context with the compiled expression |
| 12846 | * @op: an XPath compiled operation |
| 12847 | * @last: the last elem found so far |
| 12848 | * |
| 12849 | * Evaluate the Precompiled XPath operation searching only the last |
| 12850 | * element in document order |
| 12851 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 12852 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12853 | */ |
| 12854 | static int |
| 12855 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 12856 | xmlNodePtr * last) |
| 12857 | { |
| 12858 | int total = 0, cur; |
| 12859 | xmlXPathCompExprPtr comp; |
| 12860 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12861 | xmlNodePtr bak; |
| 12862 | xmlDocPtr bakd; |
| 12863 | int pp; |
| 12864 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12865 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12866 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12867 | comp = ctxt->comp; |
| 12868 | switch (op->op) { |
| 12869 | case XPATH_OP_END: |
| 12870 | return (0); |
| 12871 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12872 | bakd = ctxt->context->doc; |
| 12873 | bak = ctxt->context->node; |
| 12874 | pp = ctxt->context->proximityPosition; |
| 12875 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12876 | total = |
| 12877 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12878 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12879 | if ((ctxt->value != NULL) |
| 12880 | && (ctxt->value->type == XPATH_NODESET) |
| 12881 | && (ctxt->value->nodesetval != NULL) |
| 12882 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 12883 | /* |
| 12884 | * limit tree traversing to first node in the result |
| 12885 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12886 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 12887 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12888 | *last = |
| 12889 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 12890 | nodesetval->nodeNr - |
| 12891 | 1]; |
| 12892 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12893 | ctxt->context->doc = bakd; |
| 12894 | ctxt->context->node = bak; |
| 12895 | ctxt->context->proximityPosition = pp; |
| 12896 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12897 | cur = |
| 12898 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12899 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12900 | if ((ctxt->value != NULL) |
| 12901 | && (ctxt->value->type == XPATH_NODESET) |
| 12902 | && (ctxt->value->nodesetval != NULL) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12903 | && (ctxt->value->nodesetval->nodeNr >= 1)) { /* TODO: NOP ? */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12904 | } |
| 12905 | CHECK_TYPE0(XPATH_NODESET); |
| 12906 | arg2 = valuePop(ctxt); |
| 12907 | |
| 12908 | CHECK_TYPE0(XPATH_NODESET); |
| 12909 | arg1 = valuePop(ctxt); |
| 12910 | |
| 12911 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 12912 | arg2->nodesetval); |
| 12913 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12914 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12915 | /* optimizer */ |
| 12916 | if (total > cur) |
| 12917 | xmlXPathCompSwap(op); |
| 12918 | return (total + cur); |
| 12919 | case XPATH_OP_ROOT: |
| 12920 | xmlXPathRoot(ctxt); |
| 12921 | return (0); |
| 12922 | case XPATH_OP_NODE: |
| 12923 | if (op->ch1 != -1) |
| 12924 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12925 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12926 | if (op->ch2 != -1) |
| 12927 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12928 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12929 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 12930 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12931 | return (total); |
| 12932 | case XPATH_OP_RESET: |
| 12933 | if (op->ch1 != -1) |
| 12934 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12935 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12936 | if (op->ch2 != -1) |
| 12937 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12938 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12939 | ctxt->context->node = NULL; |
| 12940 | return (total); |
| 12941 | case XPATH_OP_COLLECT:{ |
| 12942 | if (op->ch1 == -1) |
| 12943 | return (0); |
| 12944 | |
| 12945 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12946 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12947 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12948 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12949 | return (total); |
| 12950 | } |
| 12951 | case XPATH_OP_VALUE: |
| 12952 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12953 | xmlXPathCacheObjectCopy(ctxt->context, |
| 12954 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12955 | return (0); |
| 12956 | case XPATH_OP_SORT: |
| 12957 | if (op->ch1 != -1) |
| 12958 | total += |
| 12959 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 12960 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12961 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12962 | if ((ctxt->value != NULL) |
| 12963 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12964 | && (ctxt->value->nodesetval != NULL) |
| 12965 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12966 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 12967 | return (total); |
| 12968 | default: |
| 12969 | return (xmlXPathCompOpEval(ctxt, op)); |
| 12970 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12971 | } |
| 12972 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12973 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 12974 | static int |
| 12975 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 12976 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 12977 | { |
| 12978 | int total = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12979 | xmlXPathCompExprPtr comp; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12980 | xmlXPathObjectPtr res; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12981 | xmlXPathObjectPtr obj; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12982 | xmlNodeSetPtr oldset; |
| 12983 | xmlNodePtr oldnode; |
| 12984 | xmlDocPtr oldDoc; |
| 12985 | int i; |
| 12986 | |
| 12987 | CHECK_ERROR0; |
| 12988 | comp = ctxt->comp; |
| 12989 | /* |
| 12990 | * Optimization for ()[last()] selection i.e. the last elem |
| 12991 | */ |
| 12992 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 12993 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 12994 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 12995 | int f = comp->steps[op->ch2].ch1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12996 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12997 | if ((f != -1) && |
| 12998 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 12999 | (comp->steps[f].value5 == NULL) && |
| 13000 | (comp->steps[f].value == 0) && |
| 13001 | (comp->steps[f].value4 != NULL) && |
| 13002 | (xmlStrEqual |
| 13003 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 13004 | xmlNodePtr last = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13005 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13006 | total += |
| 13007 | xmlXPathCompOpEvalLast(ctxt, |
| 13008 | &comp->steps[op->ch1], |
| 13009 | &last); |
| 13010 | CHECK_ERROR0; |
| 13011 | /* |
| 13012 | * The nodeset should be in document order, |
| 13013 | * Keep only the last value |
| 13014 | */ |
| 13015 | if ((ctxt->value != NULL) && |
| 13016 | (ctxt->value->type == XPATH_NODESET) && |
| 13017 | (ctxt->value->nodesetval != NULL) && |
| 13018 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 13019 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 13020 | ctxt->value->nodesetval->nodeTab[0] = |
| 13021 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 13022 | value-> |
| 13023 | nodesetval-> |
| 13024 | nodeNr - |
| 13025 | 1]; |
| 13026 | ctxt->value->nodesetval->nodeNr = 1; |
| 13027 | *first = *(ctxt->value->nodesetval->nodeTab); |
| 13028 | } |
| 13029 | return (total); |
| 13030 | } |
| 13031 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13032 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13033 | if (op->ch1 != -1) |
| 13034 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 13035 | CHECK_ERROR0; |
| 13036 | if (op->ch2 == -1) |
| 13037 | return (total); |
| 13038 | if (ctxt->value == NULL) |
| 13039 | return (total); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13040 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13041 | #ifdef LIBXML_XPTR_ENABLED |
| 13042 | oldnode = ctxt->context->node; |
| 13043 | /* |
| 13044 | * Hum are we filtering the result of an XPointer expression |
| 13045 | */ |
| 13046 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 13047 | xmlXPathObjectPtr tmp = NULL; |
| 13048 | xmlLocationSetPtr newlocset = NULL; |
| 13049 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13050 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13051 | /* |
| 13052 | * Extract the old locset, and then evaluate the result of the |
| 13053 | * expression for all the element in the locset. use it to grow |
| 13054 | * up a new locset. |
| 13055 | */ |
| 13056 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 13057 | obj = valuePop(ctxt); |
| 13058 | oldlocset = obj->user; |
| 13059 | ctxt->context->node = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13060 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13061 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 13062 | ctxt->context->contextSize = 0; |
| 13063 | ctxt->context->proximityPosition = 0; |
| 13064 | if (op->ch2 != -1) |
| 13065 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 13066 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13067 | if (res != NULL) { |
| 13068 | xmlXPathReleaseObject(ctxt->context, res); |
| 13069 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13070 | valuePush(ctxt, obj); |
| 13071 | CHECK_ERROR0; |
| 13072 | return (total); |
| 13073 | } |
| 13074 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13075 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13076 | for (i = 0; i < oldlocset->locNr; i++) { |
| 13077 | /* |
| 13078 | * Run the evaluation with a node list made of a |
| 13079 | * single item in the nodelocset. |
| 13080 | */ |
| 13081 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 13082 | ctxt->context->contextSize = oldlocset->locNr; |
| 13083 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13084 | if (tmp == NULL) { |
| 13085 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13086 | ctxt->context->node); |
| 13087 | } else { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 13088 | if (xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 13089 | ctxt->context->node) < 0) { |
| 13090 | ctxt->error = XPATH_MEMORY_ERROR; |
| 13091 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13092 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13093 | valuePush(ctxt, tmp); |
| 13094 | if (op->ch2 != -1) |
| 13095 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 13096 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13097 | xmlXPathFreeObject(obj); |
| 13098 | return(0); |
| 13099 | } |
| 13100 | /* |
| 13101 | * The result of the evaluation need to be tested to |
| 13102 | * decided whether the filter succeeded or not |
| 13103 | */ |
| 13104 | res = valuePop(ctxt); |
| 13105 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13106 | xmlXPtrLocationSetAdd(newlocset, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13107 | xmlXPathCacheObjectCopy(ctxt->context, |
| 13108 | oldlocset->locTab[i])); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13109 | } |
| 13110 | /* |
| 13111 | * Cleanup |
| 13112 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13113 | if (res != NULL) { |
| 13114 | xmlXPathReleaseObject(ctxt->context, res); |
| 13115 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13116 | if (ctxt->value == tmp) { |
| 13117 | valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13118 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13119 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13120 | * REVISIT TODO: Don't create a temporary nodeset |
| 13121 | * for everly iteration. |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13122 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13123 | /* OLD: xmlXPathFreeObject(res); */ |
| 13124 | } else |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13125 | tmp = NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13126 | ctxt->context->node = NULL; |
| 13127 | /* |
| 13128 | * Only put the first node in the result, then leave. |
| 13129 | */ |
| 13130 | if (newlocset->locNr > 0) { |
| 13131 | *first = (xmlNodePtr) oldlocset->locTab[i]->user; |
| 13132 | break; |
| 13133 | } |
| 13134 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13135 | if (tmp != NULL) { |
| 13136 | xmlXPathReleaseObject(ctxt->context, tmp); |
| 13137 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13138 | /* |
| 13139 | * The result is used as the new evaluation locset. |
| 13140 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13141 | xmlXPathReleaseObject(ctxt->context, obj); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13142 | ctxt->context->node = NULL; |
| 13143 | ctxt->context->contextSize = -1; |
| 13144 | ctxt->context->proximityPosition = -1; |
| 13145 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 13146 | ctxt->context->node = oldnode; |
| 13147 | return (total); |
| 13148 | } |
| 13149 | #endif /* LIBXML_XPTR_ENABLED */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13150 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13151 | /* |
| 13152 | * Extract the old set, and then evaluate the result of the |
| 13153 | * expression for all the element in the set. use it to grow |
| 13154 | * up a new set. |
| 13155 | */ |
| 13156 | CHECK_TYPE0(XPATH_NODESET); |
| 13157 | obj = valuePop(ctxt); |
| 13158 | oldset = obj->nodesetval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13159 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13160 | oldnode = ctxt->context->node; |
| 13161 | oldDoc = ctxt->context->doc; |
| 13162 | ctxt->context->node = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13163 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13164 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 13165 | ctxt->context->contextSize = 0; |
| 13166 | ctxt->context->proximityPosition = 0; |
| 13167 | /* QUESTION TODO: Why was this code commented out? |
| 13168 | if (op->ch2 != -1) |
| 13169 | total += |
| 13170 | xmlXPathCompOpEval(ctxt, |
| 13171 | &comp->steps[op->ch2]); |
| 13172 | CHECK_ERROR0; |
| 13173 | res = valuePop(ctxt); |
| 13174 | if (res != NULL) |
| 13175 | xmlXPathFreeObject(res); |
| 13176 | */ |
| 13177 | valuePush(ctxt, obj); |
| 13178 | ctxt->context->node = oldnode; |
| 13179 | CHECK_ERROR0; |
| 13180 | } else { |
| 13181 | xmlNodeSetPtr newset; |
| 13182 | xmlXPathObjectPtr tmp = NULL; |
| 13183 | /* |
| 13184 | * Initialize the new set. |
| 13185 | * Also set the xpath document in case things like |
| 13186 | * key() evaluation are attempted on the predicate |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13187 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13188 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 13189 | /* XXX what if xmlXPathNodeSetCreate returned NULL? */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13190 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13191 | for (i = 0; i < oldset->nodeNr; i++) { |
| 13192 | /* |
| 13193 | * Run the evaluation with a node list made of |
| 13194 | * a single item in the nodeset. |
| 13195 | */ |
| 13196 | ctxt->context->node = oldset->nodeTab[i]; |
| 13197 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 13198 | (oldset->nodeTab[i]->doc != NULL)) |
| 13199 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13200 | if (tmp == NULL) { |
| 13201 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13202 | ctxt->context->node); |
| 13203 | } else { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 13204 | if (xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 13205 | ctxt->context->node) < 0) { |
| 13206 | ctxt->error = XPATH_MEMORY_ERROR; |
| 13207 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13208 | } |
| 13209 | valuePush(ctxt, tmp); |
| 13210 | ctxt->context->contextSize = oldset->nodeNr; |
| 13211 | ctxt->context->proximityPosition = i + 1; |
| 13212 | if (op->ch2 != -1) |
| 13213 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 13214 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13215 | xmlXPathFreeNodeSet(newset); |
| 13216 | xmlXPathFreeObject(obj); |
| 13217 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13218 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13219 | /* |
| 13220 | * The result of the evaluation needs to be tested to |
| 13221 | * decide whether the filter succeeded or not |
| 13222 | */ |
| 13223 | res = valuePop(ctxt); |
| 13224 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 13225 | if (xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]) < 0) |
| 13226 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13227 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13228 | /* |
| 13229 | * Cleanup |
| 13230 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13231 | if (res != NULL) { |
| 13232 | xmlXPathReleaseObject(ctxt->context, res); |
| 13233 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13234 | if (ctxt->value == tmp) { |
| 13235 | valuePop(ctxt); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13236 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13237 | * Don't free the temporary nodeset |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13238 | * in order to avoid massive recreation inside this |
| 13239 | * loop. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13240 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 13241 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13242 | } else |
| 13243 | tmp = NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13244 | ctxt->context->node = NULL; |
| 13245 | /* |
| 13246 | * Only put the first node in the result, then leave. |
| 13247 | */ |
| 13248 | if (newset->nodeNr > 0) { |
| 13249 | *first = *(newset->nodeTab); |
| 13250 | break; |
| 13251 | } |
| 13252 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13253 | if (tmp != NULL) { |
| 13254 | xmlXPathReleaseObject(ctxt->context, tmp); |
| 13255 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13256 | /* |
| 13257 | * The result is used as the new evaluation set. |
| 13258 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13259 | xmlXPathReleaseObject(ctxt->context, obj); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13260 | ctxt->context->node = NULL; |
| 13261 | ctxt->context->contextSize = -1; |
| 13262 | ctxt->context->proximityPosition = -1; |
| 13263 | /* may want to move this past the '}' later */ |
| 13264 | ctxt->context->doc = oldDoc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13265 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, newset)); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13266 | } |
| 13267 | ctxt->context->node = oldnode; |
| 13268 | return(total); |
| 13269 | } |
| 13270 | #endif /* XP_OPTIMIZED_FILTER_FIRST */ |
| 13271 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13272 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13273 | * xmlXPathCompOpEval: |
| 13274 | * @ctxt: the XPath parser context with the compiled expression |
| 13275 | * @op: an XPath compiled operation |
| 13276 | * |
| 13277 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13278 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13279 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13280 | static int |
| 13281 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 13282 | { |
| 13283 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13284 | int equal, ret; |
| 13285 | xmlXPathCompExprPtr comp; |
| 13286 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13287 | xmlNodePtr bak; |
| 13288 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13289 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13290 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13291 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13292 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13293 | comp = ctxt->comp; |
| 13294 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13295 | case XPATH_OP_END: |
| 13296 | return (0); |
| 13297 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13298 | bakd = ctxt->context->doc; |
| 13299 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13300 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13301 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13302 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13303 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13304 | xmlXPathBooleanFunction(ctxt, 1); |
| 13305 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 13306 | return (total); |
| 13307 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13308 | ctxt->context->doc = bakd; |
| 13309 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13310 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13311 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13312 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13313 | if (ctxt->error) { |
| 13314 | xmlXPathFreeObject(arg2); |
| 13315 | return(0); |
| 13316 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13317 | xmlXPathBooleanFunction(ctxt, 1); |
| 13318 | arg1 = valuePop(ctxt); |
| 13319 | arg1->boolval &= arg2->boolval; |
| 13320 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13321 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13322 | return (total); |
| 13323 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13324 | bakd = ctxt->context->doc; |
| 13325 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13326 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13327 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13328 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13329 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13330 | xmlXPathBooleanFunction(ctxt, 1); |
| 13331 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 13332 | return (total); |
| 13333 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13334 | ctxt->context->doc = bakd; |
| 13335 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13336 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13337 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13338 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13339 | if (ctxt->error) { |
| 13340 | xmlXPathFreeObject(arg2); |
| 13341 | return(0); |
| 13342 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13343 | xmlXPathBooleanFunction(ctxt, 1); |
| 13344 | arg1 = valuePop(ctxt); |
| 13345 | arg1->boolval |= arg2->boolval; |
| 13346 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13347 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13348 | return (total); |
| 13349 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13350 | bakd = ctxt->context->doc; |
| 13351 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13352 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13353 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13354 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13355 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13356 | ctxt->context->doc = bakd; |
| 13357 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13358 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13359 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13360 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13361 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 13362 | if (op->value) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13363 | equal = xmlXPathEqualValues(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 13364 | else |
| 13365 | equal = xmlXPathNotEqualValues(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13366 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13367 | return (total); |
| 13368 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13369 | bakd = ctxt->context->doc; |
| 13370 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13371 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13372 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13373 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13374 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13375 | ctxt->context->doc = bakd; |
| 13376 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13377 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13378 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13379 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13380 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13381 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13382 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13383 | return (total); |
| 13384 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13385 | bakd = ctxt->context->doc; |
| 13386 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13387 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13388 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13389 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13390 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13391 | if (op->ch2 != -1) { |
| 13392 | ctxt->context->doc = bakd; |
| 13393 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13394 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13395 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13396 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13397 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13398 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13399 | if (op->value == 0) |
| 13400 | xmlXPathSubValues(ctxt); |
| 13401 | else if (op->value == 1) |
| 13402 | xmlXPathAddValues(ctxt); |
| 13403 | else if (op->value == 2) |
| 13404 | xmlXPathValueFlipSign(ctxt); |
| 13405 | else if (op->value == 3) { |
| 13406 | CAST_TO_NUMBER; |
| 13407 | CHECK_TYPE0(XPATH_NUMBER); |
| 13408 | } |
| 13409 | return (total); |
| 13410 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13411 | bakd = ctxt->context->doc; |
| 13412 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13413 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13414 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13415 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13416 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13417 | ctxt->context->doc = bakd; |
| 13418 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13419 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13420 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13421 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13422 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13423 | if (op->value == 0) |
| 13424 | xmlXPathMultValues(ctxt); |
| 13425 | else if (op->value == 1) |
| 13426 | xmlXPathDivValues(ctxt); |
| 13427 | else if (op->value == 2) |
| 13428 | xmlXPathModValues(ctxt); |
| 13429 | return (total); |
| 13430 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13431 | bakd = ctxt->context->doc; |
| 13432 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13433 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13434 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13435 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13436 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13437 | ctxt->context->doc = bakd; |
| 13438 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13439 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13440 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13441 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13442 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13443 | CHECK_TYPE0(XPATH_NODESET); |
| 13444 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13445 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13446 | CHECK_TYPE0(XPATH_NODESET); |
| 13447 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13448 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 13449 | if ((arg1->nodesetval == NULL) || |
| 13450 | ((arg2->nodesetval != NULL) && |
| 13451 | (arg2->nodesetval->nodeNr != 0))) |
| 13452 | { |
| 13453 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 13454 | arg2->nodesetval); |
| 13455 | } |
| 13456 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13457 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13458 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13459 | return (total); |
| 13460 | case XPATH_OP_ROOT: |
| 13461 | xmlXPathRoot(ctxt); |
| 13462 | return (total); |
| 13463 | case XPATH_OP_NODE: |
| 13464 | if (op->ch1 != -1) |
| 13465 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13466 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13467 | if (op->ch2 != -1) |
| 13468 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13469 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13470 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 13471 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13472 | return (total); |
| 13473 | case XPATH_OP_RESET: |
| 13474 | if (op->ch1 != -1) |
| 13475 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13476 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13477 | if (op->ch2 != -1) |
| 13478 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13479 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13480 | ctxt->context->node = NULL; |
| 13481 | return (total); |
| 13482 | case XPATH_OP_COLLECT:{ |
| 13483 | if (op->ch1 == -1) |
| 13484 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 13485 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13486 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13487 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13488 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 13489 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13490 | return (total); |
| 13491 | } |
| 13492 | case XPATH_OP_VALUE: |
| 13493 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13494 | xmlXPathCacheObjectCopy(ctxt->context, |
| 13495 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13496 | return (total); |
| 13497 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13498 | xmlXPathObjectPtr val; |
| 13499 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13500 | if (op->ch1 != -1) |
| 13501 | total += |
| 13502 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13503 | if (op->value5 == NULL) { |
| 13504 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 13505 | if (val == NULL) { |
| 13506 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 13507 | return(0); |
| 13508 | } |
| 13509 | valuePush(ctxt, val); |
| 13510 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13511 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13512 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13513 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 13514 | if (URI == NULL) { |
| 13515 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13516 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
| 13517 | (char *) op->value4, (char *)op->value5); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 13518 | ctxt->error = XPATH_UNDEF_PREFIX_ERROR; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13519 | return (total); |
| 13520 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13521 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 13522 | op->value4, URI); |
| 13523 | if (val == NULL) { |
| 13524 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 13525 | return(0); |
| 13526 | } |
| 13527 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13528 | } |
| 13529 | return (total); |
| 13530 | } |
| 13531 | case XPATH_OP_FUNCTION:{ |
| 13532 | xmlXPathFunction func; |
| 13533 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13534 | int i; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13535 | int frame; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13536 | |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13537 | frame = xmlXPathSetFrame(ctxt); |
Nick Wellnhofer | 03c6723 | 2013-12-20 00:01:53 +0100 | [diff] [blame] | 13538 | if (op->ch1 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13539 | total += |
| 13540 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Nick Wellnhofer | 03c6723 | 2013-12-20 00:01:53 +0100 | [diff] [blame] | 13541 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13542 | xmlXPathPopFrame(ctxt, frame); |
| 13543 | return (total); |
| 13544 | } |
| 13545 | } |
| 13546 | if (ctxt->valueNr < ctxt->valueFrame + op->value) { |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13547 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13548 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13549 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13550 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13551 | return (total); |
| 13552 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13553 | for (i = 0; i < op->value; i++) { |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13554 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 13555 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13556 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13557 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13558 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13559 | return (total); |
| 13560 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13561 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13562 | if (op->cache != NULL) |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 13563 | XML_CAST_FPTR(func) = op->cache; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13564 | else { |
| 13565 | const xmlChar *URI = NULL; |
| 13566 | |
| 13567 | if (op->value5 == NULL) |
| 13568 | func = |
| 13569 | xmlXPathFunctionLookup(ctxt->context, |
| 13570 | op->value4); |
| 13571 | else { |
| 13572 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 13573 | if (URI == NULL) { |
| 13574 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13575 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
| 13576 | (char *)op->value4, (char *)op->value5); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13577 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 13578 | ctxt->error = XPATH_UNDEF_PREFIX_ERROR; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13579 | return (total); |
| 13580 | } |
| 13581 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 13582 | op->value4, URI); |
| 13583 | } |
| 13584 | if (func == NULL) { |
| 13585 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13586 | "xmlXPathCompOpEval: function %s not found\n", |
| 13587 | (char *)op->value4); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13588 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13589 | } |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 13590 | op->cache = XML_CAST_FPTR(func); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13591 | op->cacheURI = (void *) URI; |
| 13592 | } |
| 13593 | oldFunc = ctxt->context->function; |
| 13594 | oldFuncURI = ctxt->context->functionURI; |
| 13595 | ctxt->context->function = op->value4; |
| 13596 | ctxt->context->functionURI = op->cacheURI; |
| 13597 | func(ctxt, op->value); |
| 13598 | ctxt->context->function = oldFunc; |
| 13599 | ctxt->context->functionURI = oldFuncURI; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13600 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13601 | return (total); |
| 13602 | } |
| 13603 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 13604 | bakd = ctxt->context->doc; |
| 13605 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 13606 | pp = ctxt->context->proximityPosition; |
| 13607 | cs = ctxt->context->contextSize; |
Nick Wellnhofer | 07def30 | 2014-03-21 19:38:08 +0100 | [diff] [blame] | 13608 | if (op->ch1 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13609 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Nick Wellnhofer | 07def30 | 2014-03-21 19:38:08 +0100 | [diff] [blame] | 13610 | ctxt->context->contextSize = cs; |
| 13611 | ctxt->context->proximityPosition = pp; |
| 13612 | ctxt->context->node = bak; |
| 13613 | ctxt->context->doc = bakd; |
| 13614 | CHECK_ERROR0; |
| 13615 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13616 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13617 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Nick Wellnhofer | 07def30 | 2014-03-21 19:38:08 +0100 | [diff] [blame] | 13618 | ctxt->context->contextSize = cs; |
| 13619 | ctxt->context->proximityPosition = pp; |
| 13620 | ctxt->context->node = bak; |
| 13621 | ctxt->context->doc = bakd; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13622 | CHECK_ERROR0; |
| 13623 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13624 | return (total); |
| 13625 | case XPATH_OP_PREDICATE: |
| 13626 | case XPATH_OP_FILTER:{ |
| 13627 | xmlXPathObjectPtr res; |
| 13628 | xmlXPathObjectPtr obj, tmp; |
| 13629 | xmlNodeSetPtr newset = NULL; |
| 13630 | xmlNodeSetPtr oldset; |
| 13631 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13632 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13633 | int i; |
| 13634 | |
| 13635 | /* |
| 13636 | * Optimization for ()[1] selection i.e. the first elem |
| 13637 | */ |
| 13638 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13639 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 13640 | /* |
| 13641 | * FILTER TODO: Can we assume that the inner processing |
| 13642 | * will result in an ordered list if we have an |
| 13643 | * XPATH_OP_FILTER? |
| 13644 | * What about an additional field or flag on |
| 13645 | * xmlXPathObject like @sorted ? This way we wouln'd need |
| 13646 | * to assume anything, so it would be more robust and |
| 13647 | * easier to optimize. |
| 13648 | */ |
| 13649 | ((comp->steps[op->ch1].op == XPATH_OP_SORT) || /* 18 */ |
| 13650 | (comp->steps[op->ch1].op == XPATH_OP_FILTER)) && /* 17 */ |
| 13651 | #else |
| 13652 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 13653 | #endif |
| 13654 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { /* 12 */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13655 | xmlXPathObjectPtr val; |
| 13656 | |
| 13657 | val = comp->steps[op->ch2].value4; |
| 13658 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 13659 | (val->floatval == 1.0)) { |
| 13660 | xmlNodePtr first = NULL; |
| 13661 | |
| 13662 | total += |
| 13663 | xmlXPathCompOpEvalFirst(ctxt, |
| 13664 | &comp->steps[op->ch1], |
| 13665 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13666 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13667 | /* |
| 13668 | * The nodeset should be in document order, |
| 13669 | * Keep only the first value |
| 13670 | */ |
| 13671 | if ((ctxt->value != NULL) && |
| 13672 | (ctxt->value->type == XPATH_NODESET) && |
| 13673 | (ctxt->value->nodesetval != NULL) && |
| 13674 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 13675 | ctxt->value->nodesetval->nodeNr = 1; |
| 13676 | return (total); |
| 13677 | } |
| 13678 | } |
| 13679 | /* |
| 13680 | * Optimization for ()[last()] selection i.e. the last elem |
| 13681 | */ |
| 13682 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 13683 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 13684 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 13685 | int f = comp->steps[op->ch2].ch1; |
| 13686 | |
| 13687 | if ((f != -1) && |
| 13688 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 13689 | (comp->steps[f].value5 == NULL) && |
| 13690 | (comp->steps[f].value == 0) && |
| 13691 | (comp->steps[f].value4 != NULL) && |
| 13692 | (xmlStrEqual |
| 13693 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 13694 | xmlNodePtr last = NULL; |
| 13695 | |
| 13696 | total += |
| 13697 | xmlXPathCompOpEvalLast(ctxt, |
| 13698 | &comp->steps[op->ch1], |
| 13699 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13700 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13701 | /* |
| 13702 | * The nodeset should be in document order, |
| 13703 | * Keep only the last value |
| 13704 | */ |
| 13705 | if ((ctxt->value != NULL) && |
| 13706 | (ctxt->value->type == XPATH_NODESET) && |
| 13707 | (ctxt->value->nodesetval != NULL) && |
| 13708 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 13709 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 13710 | ctxt->value->nodesetval->nodeTab[0] = |
| 13711 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 13712 | value-> |
| 13713 | nodesetval-> |
| 13714 | nodeNr - |
| 13715 | 1]; |
| 13716 | ctxt->value->nodesetval->nodeNr = 1; |
| 13717 | } |
| 13718 | return (total); |
| 13719 | } |
| 13720 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13721 | /* |
| 13722 | * Process inner predicates first. |
| 13723 | * Example "index[parent::book][1]": |
| 13724 | * ... |
| 13725 | * PREDICATE <-- we are here "[1]" |
| 13726 | * PREDICATE <-- process "[parent::book]" first |
| 13727 | * SORT |
| 13728 | * COLLECT 'parent' 'name' 'node' book |
| 13729 | * NODE |
| 13730 | * ELEM Object is a number : 1 |
| 13731 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13732 | if (op->ch1 != -1) |
| 13733 | total += |
| 13734 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13735 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13736 | if (op->ch2 == -1) |
| 13737 | return (total); |
| 13738 | if (ctxt->value == NULL) |
| 13739 | return (total); |
| 13740 | |
| 13741 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13742 | |
| 13743 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13744 | /* |
| 13745 | * Hum are we filtering the result of an XPointer expression |
| 13746 | */ |
| 13747 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 13748 | xmlLocationSetPtr newlocset = NULL; |
| 13749 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13750 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13751 | /* |
| 13752 | * Extract the old locset, and then evaluate the result of the |
| 13753 | * expression for all the element in the locset. use it to grow |
| 13754 | * up a new locset. |
| 13755 | */ |
| 13756 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 13757 | obj = valuePop(ctxt); |
| 13758 | oldlocset = obj->user; |
| 13759 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13760 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13761 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 13762 | ctxt->context->contextSize = 0; |
| 13763 | ctxt->context->proximityPosition = 0; |
| 13764 | if (op->ch2 != -1) |
| 13765 | total += |
| 13766 | xmlXPathCompOpEval(ctxt, |
| 13767 | &comp->steps[op->ch2]); |
| 13768 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13769 | if (res != NULL) { |
| 13770 | xmlXPathReleaseObject(ctxt->context, res); |
| 13771 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13772 | valuePush(ctxt, obj); |
| 13773 | CHECK_ERROR0; |
| 13774 | return (total); |
| 13775 | } |
| 13776 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13777 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13778 | for (i = 0; i < oldlocset->locNr; i++) { |
| 13779 | /* |
| 13780 | * Run the evaluation with a node list made of a |
| 13781 | * single item in the nodelocset. |
| 13782 | */ |
| 13783 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13784 | ctxt->context->contextSize = oldlocset->locNr; |
| 13785 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13786 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13787 | ctxt->context->node); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 13788 | valuePush(ctxt, tmp); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13789 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13790 | if (op->ch2 != -1) |
| 13791 | total += |
| 13792 | xmlXPathCompOpEval(ctxt, |
| 13793 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 13794 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13795 | xmlXPathFreeObject(obj); |
| 13796 | return(0); |
| 13797 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13798 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13799 | /* |
| 13800 | * The result of the evaluation need to be tested to |
| 13801 | * decided whether the filter succeeded or not |
| 13802 | */ |
| 13803 | res = valuePop(ctxt); |
| 13804 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13805 | xmlXPtrLocationSetAdd(newlocset, |
| 13806 | xmlXPathObjectCopy |
| 13807 | (oldlocset->locTab[i])); |
| 13808 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13809 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13810 | /* |
| 13811 | * Cleanup |
| 13812 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13813 | if (res != NULL) { |
| 13814 | xmlXPathReleaseObject(ctxt->context, res); |
| 13815 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13816 | if (ctxt->value == tmp) { |
| 13817 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13818 | xmlXPathReleaseObject(ctxt->context, res); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13819 | } |
| 13820 | |
| 13821 | ctxt->context->node = NULL; |
| 13822 | } |
| 13823 | |
| 13824 | /* |
| 13825 | * The result is used as the new evaluation locset. |
| 13826 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13827 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13828 | ctxt->context->node = NULL; |
| 13829 | ctxt->context->contextSize = -1; |
| 13830 | ctxt->context->proximityPosition = -1; |
| 13831 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 13832 | ctxt->context->node = oldnode; |
| 13833 | return (total); |
| 13834 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13835 | #endif /* LIBXML_XPTR_ENABLED */ |
| 13836 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13837 | /* |
| 13838 | * Extract the old set, and then evaluate the result of the |
| 13839 | * expression for all the element in the set. use it to grow |
| 13840 | * up a new set. |
| 13841 | */ |
| 13842 | CHECK_TYPE0(XPATH_NODESET); |
| 13843 | obj = valuePop(ctxt); |
| 13844 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 13845 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13846 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13847 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13848 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13849 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13850 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 13851 | ctxt->context->contextSize = 0; |
| 13852 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 13853 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13854 | if (op->ch2 != -1) |
| 13855 | total += |
| 13856 | xmlXPathCompOpEval(ctxt, |
| 13857 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13858 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13859 | res = valuePop(ctxt); |
| 13860 | if (res != NULL) |
| 13861 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 13862 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13863 | valuePush(ctxt, obj); |
| 13864 | ctxt->context->node = oldnode; |
| 13865 | CHECK_ERROR0; |
| 13866 | } else { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13867 | tmp = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13868 | /* |
| 13869 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13870 | * Also set the xpath document in case things like |
| 13871 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13872 | */ |
| 13873 | newset = xmlXPathNodeSetCreate(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13874 | /* |
| 13875 | * SPEC XPath 1.0: |
| 13876 | * "For each node in the node-set to be filtered, the |
| 13877 | * PredicateExpr is evaluated with that node as the |
| 13878 | * context node, with the number of nodes in the |
| 13879 | * node-set as the context size, and with the proximity |
| 13880 | * position of the node in the node-set with respect to |
| 13881 | * the axis as the context position;" |
| 13882 | * @oldset is the node-set" to be filtered. |
| 13883 | * |
| 13884 | * SPEC XPath 1.0: |
| 13885 | * "only predicates change the context position and |
| 13886 | * context size (see [2.4 Predicates])." |
| 13887 | * Example: |
| 13888 | * node-set context pos |
| 13889 | * nA 1 |
| 13890 | * nB 2 |
| 13891 | * nC 3 |
| 13892 | * After applying predicate [position() > 1] : |
| 13893 | * node-set context pos |
| 13894 | * nB 1 |
| 13895 | * nC 2 |
| 13896 | * |
| 13897 | * removed the first node in the node-set, then |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13898 | * the context position of the |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13899 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13900 | for (i = 0; i < oldset->nodeNr; i++) { |
| 13901 | /* |
| 13902 | * Run the evaluation with a node list made of |
| 13903 | * a single item in the nodeset. |
| 13904 | */ |
| 13905 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13906 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 13907 | (oldset->nodeTab[i]->doc != NULL)) |
| 13908 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13909 | if (tmp == NULL) { |
| 13910 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13911 | ctxt->context->node); |
| 13912 | } else { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 13913 | if (xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 13914 | ctxt->context->node) < 0) { |
| 13915 | ctxt->error = XPATH_MEMORY_ERROR; |
| 13916 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13917 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13918 | valuePush(ctxt, tmp); |
| 13919 | ctxt->context->contextSize = oldset->nodeNr; |
| 13920 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13921 | /* |
| 13922 | * Evaluate the predicate against the context node. |
| 13923 | * Can/should we optimize position() predicates |
| 13924 | * here (e.g. "[1]")? |
| 13925 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13926 | if (op->ch2 != -1) |
| 13927 | total += |
| 13928 | xmlXPathCompOpEval(ctxt, |
| 13929 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 13930 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13931 | xmlXPathFreeNodeSet(newset); |
| 13932 | xmlXPathFreeObject(obj); |
| 13933 | return(0); |
| 13934 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13935 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13936 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13937 | * The result of the evaluation needs to be tested to |
| 13938 | * decide whether the filter succeeded or not |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13939 | */ |
| 13940 | /* |
| 13941 | * OPTIMIZE TODO: Can we use |
| 13942 | * xmlXPathNodeSetAdd*Unique()* instead? |
| 13943 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13944 | res = valuePop(ctxt); |
| 13945 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 13946 | if (xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]) |
| 13947 | < 0) |
| 13948 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13949 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13950 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13951 | /* |
| 13952 | * Cleanup |
| 13953 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13954 | if (res != NULL) { |
| 13955 | xmlXPathReleaseObject(ctxt->context, res); |
| 13956 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13957 | if (ctxt->value == tmp) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13958 | valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13959 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13960 | /* |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13961 | * Don't free the temporary nodeset |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13962 | * in order to avoid massive recreation inside this |
| 13963 | * loop. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13964 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13965 | } else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13966 | tmp = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13967 | ctxt->context->node = NULL; |
| 13968 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13969 | if (tmp != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13970 | xmlXPathReleaseObject(ctxt->context, tmp); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13971 | /* |
| 13972 | * The result is used as the new evaluation set. |
| 13973 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13974 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13975 | ctxt->context->node = NULL; |
| 13976 | ctxt->context->contextSize = -1; |
| 13977 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13978 | /* may want to move this past the '}' later */ |
| 13979 | ctxt->context->doc = oldDoc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13980 | valuePush(ctxt, |
| 13981 | xmlXPathCacheWrapNodeSet(ctxt->context, newset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13982 | } |
| 13983 | ctxt->context->node = oldnode; |
| 13984 | return (total); |
| 13985 | } |
| 13986 | case XPATH_OP_SORT: |
| 13987 | if (op->ch1 != -1) |
| 13988 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13989 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13990 | if ((ctxt->value != NULL) && |
| 13991 | (ctxt->value->type == XPATH_NODESET) && |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 13992 | (ctxt->value->nodesetval != NULL) && |
| 13993 | (ctxt->value->nodesetval->nodeNr > 1)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13994 | { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13995 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13996 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13997 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13998 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13999 | case XPATH_OP_RANGETO:{ |
| 14000 | xmlXPathObjectPtr range; |
| 14001 | xmlXPathObjectPtr res, obj; |
| 14002 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14003 | xmlLocationSetPtr newlocset = NULL; |
| 14004 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14005 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14006 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14007 | |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 14008 | if (op->ch1 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14009 | total += |
| 14010 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 14011 | CHECK_ERROR0; |
| 14012 | } |
| 14013 | if (ctxt->value == NULL) { |
| 14014 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 14015 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14016 | if (op->ch2 == -1) |
| 14017 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14018 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14019 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 14020 | /* |
| 14021 | * Extract the old locset, and then evaluate the result of the |
| 14022 | * expression for all the element in the locset. use it to grow |
| 14023 | * up a new locset. |
| 14024 | */ |
| 14025 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 14026 | obj = valuePop(ctxt); |
| 14027 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14028 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14029 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14030 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14031 | ctxt->context->contextSize = 0; |
| 14032 | ctxt->context->proximityPosition = 0; |
| 14033 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 14034 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14035 | if (res != NULL) { |
| 14036 | xmlXPathReleaseObject(ctxt->context, res); |
| 14037 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14038 | valuePush(ctxt, obj); |
| 14039 | CHECK_ERROR0; |
| 14040 | return (total); |
| 14041 | } |
| 14042 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14043 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14044 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14045 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14046 | * Run the evaluation with a node list made of a |
| 14047 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14048 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 14049 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 14050 | ctxt->context->contextSize = oldlocset->locNr; |
| 14051 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14052 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 14053 | ctxt->context->node); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14054 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14055 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14056 | if (op->ch2 != -1) |
| 14057 | total += |
| 14058 | xmlXPathCompOpEval(ctxt, |
| 14059 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 14060 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 14061 | xmlXPathFreeObject(obj); |
| 14062 | return(0); |
| 14063 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14064 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14065 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14066 | if (res->type == XPATH_LOCATIONSET) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14067 | xmlLocationSetPtr rloc = |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14068 | (xmlLocationSetPtr)res->user; |
| 14069 | for (j=0; j<rloc->locNr; j++) { |
| 14070 | range = xmlXPtrNewRange( |
| 14071 | oldlocset->locTab[i]->user, |
| 14072 | oldlocset->locTab[i]->index, |
| 14073 | rloc->locTab[j]->user2, |
| 14074 | rloc->locTab[j]->index2); |
| 14075 | if (range != NULL) { |
| 14076 | xmlXPtrLocationSetAdd(newlocset, range); |
| 14077 | } |
| 14078 | } |
| 14079 | } else { |
| 14080 | range = xmlXPtrNewRangeNodeObject( |
| 14081 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 14082 | if (range != NULL) { |
| 14083 | xmlXPtrLocationSetAdd(newlocset,range); |
| 14084 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14085 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14086 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14087 | /* |
| 14088 | * Cleanup |
| 14089 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14090 | if (res != NULL) { |
| 14091 | xmlXPathReleaseObject(ctxt->context, res); |
| 14092 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14093 | if (ctxt->value == tmp) { |
| 14094 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14095 | xmlXPathReleaseObject(ctxt->context, res); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14096 | } |
| 14097 | |
| 14098 | ctxt->context->node = NULL; |
| 14099 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14100 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14101 | CHECK_TYPE0(XPATH_NODESET); |
| 14102 | obj = valuePop(ctxt); |
| 14103 | oldset = obj->nodesetval; |
| 14104 | ctxt->context->node = NULL; |
| 14105 | |
| 14106 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 14107 | |
| 14108 | if (oldset != NULL) { |
| 14109 | for (i = 0; i < oldset->nodeNr; i++) { |
| 14110 | /* |
| 14111 | * Run the evaluation with a node list made of a single item |
| 14112 | * in the nodeset. |
| 14113 | */ |
| 14114 | ctxt->context->node = oldset->nodeTab[i]; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14115 | /* |
| 14116 | * OPTIMIZE TODO: Avoid recreation for every iteration. |
| 14117 | */ |
| 14118 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 14119 | ctxt->context->node); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14120 | valuePush(ctxt, tmp); |
| 14121 | |
| 14122 | if (op->ch2 != -1) |
| 14123 | total += |
| 14124 | xmlXPathCompOpEval(ctxt, |
| 14125 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 14126 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 14127 | xmlXPathFreeObject(obj); |
| 14128 | return(0); |
| 14129 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14130 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14131 | res = valuePop(ctxt); |
| 14132 | range = |
| 14133 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 14134 | res); |
| 14135 | if (range != NULL) { |
| 14136 | xmlXPtrLocationSetAdd(newlocset, range); |
| 14137 | } |
| 14138 | |
| 14139 | /* |
| 14140 | * Cleanup |
| 14141 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14142 | if (res != NULL) { |
| 14143 | xmlXPathReleaseObject(ctxt->context, res); |
| 14144 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14145 | if (ctxt->value == tmp) { |
| 14146 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14147 | xmlXPathReleaseObject(ctxt->context, res); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14148 | } |
| 14149 | |
| 14150 | ctxt->context->node = NULL; |
| 14151 | } |
| 14152 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14153 | } |
| 14154 | |
| 14155 | /* |
| 14156 | * The result is used as the new evaluation set. |
| 14157 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14158 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14159 | ctxt->context->node = NULL; |
| 14160 | ctxt->context->contextSize = -1; |
| 14161 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14162 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14163 | return (total); |
| 14164 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14165 | #endif /* LIBXML_XPTR_ENABLED */ |
| 14166 | } |
| 14167 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14168 | "XPath: unknown precompiled operation %d\n", op->op); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 14169 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14170 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14171 | } |
| 14172 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14173 | /** |
| 14174 | * xmlXPathCompOpEvalToBoolean: |
| 14175 | * @ctxt: the XPath parser context |
| 14176 | * |
| 14177 | * Evaluates if the expression evaluates to true. |
| 14178 | * |
| 14179 | * Returns 1 if true, 0 if false and -1 on API or internal errors. |
| 14180 | */ |
| 14181 | static int |
| 14182 | xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt, |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14183 | xmlXPathStepOpPtr op, |
| 14184 | int isPredicate) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14185 | { |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14186 | xmlXPathObjectPtr resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14187 | |
| 14188 | start: |
| 14189 | /* comp = ctxt->comp; */ |
| 14190 | switch (op->op) { |
| 14191 | case XPATH_OP_END: |
| 14192 | return (0); |
| 14193 | case XPATH_OP_VALUE: |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14194 | resObj = (xmlXPathObjectPtr) op->value4; |
Kasimier T. Buchcik | 2bdb12f | 2006-06-29 10:49:59 +0000 | [diff] [blame] | 14195 | if (isPredicate) |
| 14196 | return(xmlXPathEvaluatePredicateResult(ctxt, resObj)); |
| 14197 | return(xmlXPathCastToBoolean(resObj)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14198 | case XPATH_OP_SORT: |
| 14199 | /* |
| 14200 | * We don't need sorting for boolean results. Skip this one. |
| 14201 | */ |
| 14202 | if (op->ch1 != -1) { |
| 14203 | op = &ctxt->comp->steps[op->ch1]; |
| 14204 | goto start; |
| 14205 | } |
| 14206 | return(0); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14207 | case XPATH_OP_COLLECT: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14208 | if (op->ch1 == -1) |
| 14209 | return(0); |
| 14210 | |
| 14211 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch1]); |
| 14212 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14213 | return(-1); |
| 14214 | |
| 14215 | xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 1); |
| 14216 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14217 | return(-1); |
| 14218 | |
| 14219 | resObj = valuePop(ctxt); |
| 14220 | if (resObj == NULL) |
| 14221 | return(-1); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14222 | break; |
| 14223 | default: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14224 | /* |
| 14225 | * Fallback to call xmlXPathCompOpEval(). |
| 14226 | */ |
| 14227 | xmlXPathCompOpEval(ctxt, op); |
| 14228 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14229 | return(-1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14230 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14231 | resObj = valuePop(ctxt); |
| 14232 | if (resObj == NULL) |
Kasimier T. Buchcik | 2bdb12f | 2006-06-29 10:49:59 +0000 | [diff] [blame] | 14233 | return(-1); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14234 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14235 | } |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14236 | |
| 14237 | if (resObj) { |
| 14238 | int res; |
| 14239 | |
| 14240 | if (resObj->type == XPATH_BOOLEAN) { |
| 14241 | res = resObj->boolval; |
| 14242 | } else if (isPredicate) { |
| 14243 | /* |
| 14244 | * For predicates a result of type "number" is handled |
| 14245 | * differently: |
| 14246 | * SPEC XPath 1.0: |
| 14247 | * "If the result is a number, the result will be converted |
| 14248 | * to true if the number is equal to the context position |
| 14249 | * and will be converted to false otherwise;" |
| 14250 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14251 | res = xmlXPathEvaluatePredicateResult(ctxt, resObj); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14252 | } else { |
| 14253 | res = xmlXPathCastToBoolean(resObj); |
| 14254 | } |
| 14255 | xmlXPathReleaseObject(ctxt->context, resObj); |
| 14256 | return(res); |
| 14257 | } |
| 14258 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14259 | return(0); |
| 14260 | } |
| 14261 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14262 | #ifdef XPATH_STREAMING |
| 14263 | /** |
| 14264 | * xmlXPathRunStreamEval: |
| 14265 | * @ctxt: the XPath parser context with the compiled expression |
| 14266 | * |
| 14267 | * Evaluate the Precompiled Streamable XPath expression in the given context. |
| 14268 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14269 | static int |
| 14270 | xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp, |
| 14271 | xmlXPathObjectPtr *resultSeq, int toBool) |
| 14272 | { |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14273 | int max_depth, min_depth; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14274 | int from_root; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14275 | int ret, depth; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14276 | int eval_all_nodes; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 14277 | xmlNodePtr cur = NULL, limit = NULL; |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14278 | xmlStreamCtxtPtr patstream = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14279 | |
| 14280 | int nb_nodes = 0; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14281 | |
| 14282 | if ((ctxt == NULL) || (comp == NULL)) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14283 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14284 | max_depth = xmlPatternMaxDepth(comp); |
| 14285 | if (max_depth == -1) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14286 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14287 | if (max_depth == -2) |
| 14288 | max_depth = 10000; |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14289 | min_depth = xmlPatternMinDepth(comp); |
| 14290 | if (min_depth == -1) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14291 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14292 | from_root = xmlPatternFromRoot(comp); |
| 14293 | if (from_root < 0) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14294 | return(-1); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14295 | #if 0 |
| 14296 | printf("stream eval: depth %d from root %d\n", max_depth, from_root); |
| 14297 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14298 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14299 | if (! toBool) { |
| 14300 | if (resultSeq == NULL) |
| 14301 | return(-1); |
| 14302 | *resultSeq = xmlXPathCacheNewNodeSet(ctxt, NULL); |
| 14303 | if (*resultSeq == NULL) |
| 14304 | return(-1); |
| 14305 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14306 | |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14307 | /* |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14308 | * handle the special cases of "/" amd "." being matched |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14309 | */ |
| 14310 | if (min_depth == 0) { |
| 14311 | if (from_root) { |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14312 | /* Select "/" */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14313 | if (toBool) |
| 14314 | return(1); |
| 14315 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 14316 | (xmlNodePtr) ctxt->doc); |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14317 | } else { |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14318 | /* Select "self::node()" */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14319 | if (toBool) |
| 14320 | return(1); |
| 14321 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, ctxt->node); |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14322 | } |
| 14323 | } |
| 14324 | if (max_depth == 0) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14325 | return(0); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14326 | } |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14327 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14328 | if (from_root) { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 14329 | cur = (xmlNodePtr)ctxt->doc; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14330 | } else if (ctxt->node != NULL) { |
| 14331 | switch (ctxt->node->type) { |
| 14332 | case XML_ELEMENT_NODE: |
| 14333 | case XML_DOCUMENT_NODE: |
| 14334 | case XML_DOCUMENT_FRAG_NODE: |
| 14335 | case XML_HTML_DOCUMENT_NODE: |
| 14336 | #ifdef LIBXML_DOCB_ENABLED |
| 14337 | case XML_DOCB_DOCUMENT_NODE: |
| 14338 | #endif |
| 14339 | cur = ctxt->node; |
| 14340 | break; |
| 14341 | case XML_ATTRIBUTE_NODE: |
| 14342 | case XML_TEXT_NODE: |
| 14343 | case XML_CDATA_SECTION_NODE: |
| 14344 | case XML_ENTITY_REF_NODE: |
| 14345 | case XML_ENTITY_NODE: |
| 14346 | case XML_PI_NODE: |
| 14347 | case XML_COMMENT_NODE: |
| 14348 | case XML_NOTATION_NODE: |
| 14349 | case XML_DTD_NODE: |
| 14350 | case XML_DOCUMENT_TYPE_NODE: |
| 14351 | case XML_ELEMENT_DECL: |
| 14352 | case XML_ATTRIBUTE_DECL: |
| 14353 | case XML_ENTITY_DECL: |
| 14354 | case XML_NAMESPACE_DECL: |
| 14355 | case XML_XINCLUDE_START: |
| 14356 | case XML_XINCLUDE_END: |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14357 | break; |
| 14358 | } |
| 14359 | limit = cur; |
| 14360 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14361 | if (cur == NULL) { |
| 14362 | return(0); |
| 14363 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14364 | |
| 14365 | patstream = xmlPatternGetStreamCtxt(comp); |
| 14366 | if (patstream == NULL) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14367 | /* |
| 14368 | * QUESTION TODO: Is this an error? |
| 14369 | */ |
| 14370 | return(0); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14371 | } |
| 14372 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14373 | eval_all_nodes = xmlStreamWantsAnyNode(patstream); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14374 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14375 | if (from_root) { |
| 14376 | ret = xmlStreamPush(patstream, NULL, NULL); |
| 14377 | if (ret < 0) { |
| 14378 | } else if (ret == 1) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14379 | if (toBool) |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14380 | goto return_1; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14381 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14382 | } |
| 14383 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14384 | depth = 0; |
| 14385 | goto scan_children; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14386 | next_node: |
Daniel Veillard | d3ff7ef | 2006-02-27 19:43:17 +0000 | [diff] [blame] | 14387 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14388 | nb_nodes++; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14389 | |
| 14390 | switch (cur->type) { |
| 14391 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14392 | case XML_TEXT_NODE: |
| 14393 | case XML_CDATA_SECTION_NODE: |
| 14394 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14395 | case XML_PI_NODE: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14396 | if (cur->type == XML_ELEMENT_NODE) { |
| 14397 | ret = xmlStreamPush(patstream, cur->name, |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14398 | (cur->ns ? cur->ns->href : NULL)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14399 | } else if (eval_all_nodes) |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14400 | ret = xmlStreamPushNode(patstream, NULL, NULL, cur->type); |
| 14401 | else |
| 14402 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14403 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14404 | if (ret < 0) { |
| 14405 | /* NOP. */ |
| 14406 | } else if (ret == 1) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14407 | if (toBool) |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14408 | goto return_1; |
Daniel Veillard | 1bd45d1 | 2012-09-05 15:35:19 +0800 | [diff] [blame] | 14409 | if (xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur) |
| 14410 | < 0) { |
| 14411 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 14412 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 14413 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14414 | } |
| 14415 | if ((cur->children == NULL) || (depth >= max_depth)) { |
| 14416 | ret = xmlStreamPop(patstream); |
| 14417 | while (cur->next != NULL) { |
| 14418 | cur = cur->next; |
| 14419 | if ((cur->type != XML_ENTITY_DECL) && |
| 14420 | (cur->type != XML_DTD_NODE)) |
| 14421 | goto next_node; |
| 14422 | } |
| 14423 | } |
| 14424 | default: |
| 14425 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14426 | } |
| 14427 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14428 | scan_children: |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 14429 | if (cur->type == XML_NAMESPACE_DECL) break; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14430 | if ((cur->children != NULL) && (depth < max_depth)) { |
| 14431 | /* |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14432 | * Do not descend on entities declarations |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14433 | */ |
| 14434 | if (cur->children->type != XML_ENTITY_DECL) { |
| 14435 | cur = cur->children; |
| 14436 | depth++; |
| 14437 | /* |
| 14438 | * Skip DTDs |
| 14439 | */ |
| 14440 | if (cur->type != XML_DTD_NODE) |
| 14441 | continue; |
| 14442 | } |
| 14443 | } |
| 14444 | |
| 14445 | if (cur == limit) |
| 14446 | break; |
| 14447 | |
| 14448 | while (cur->next != NULL) { |
| 14449 | cur = cur->next; |
| 14450 | if ((cur->type != XML_ENTITY_DECL) && |
| 14451 | (cur->type != XML_DTD_NODE)) |
| 14452 | goto next_node; |
| 14453 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14454 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14455 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14456 | cur = cur->parent; |
| 14457 | depth--; |
| 14458 | if ((cur == NULL) || (cur == limit)) |
| 14459 | goto done; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14460 | if (cur->type == XML_ELEMENT_NODE) { |
| 14461 | ret = xmlStreamPop(patstream); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14462 | } else if ((eval_all_nodes) && |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14463 | ((cur->type == XML_TEXT_NODE) || |
| 14464 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 14465 | (cur->type == XML_COMMENT_NODE) || |
| 14466 | (cur->type == XML_PI_NODE))) |
| 14467 | { |
| 14468 | ret = xmlStreamPop(patstream); |
| 14469 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14470 | if (cur->next != NULL) { |
| 14471 | cur = cur->next; |
| 14472 | break; |
| 14473 | } |
| 14474 | } while (cur != NULL); |
| 14475 | |
| 14476 | } while ((cur != NULL) && (depth >= 0)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14477 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14478 | done: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14479 | |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14480 | #if 0 |
| 14481 | printf("stream eval: checked %d nodes selected %d\n", |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14482 | nb_nodes, retObj->nodesetval->nodeNr); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14483 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14484 | |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14485 | if (patstream) |
| 14486 | xmlFreeStreamCtxt(patstream); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14487 | return(0); |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14488 | |
| 14489 | return_1: |
| 14490 | if (patstream) |
| 14491 | xmlFreeStreamCtxt(patstream); |
| 14492 | return(1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14493 | } |
| 14494 | #endif /* XPATH_STREAMING */ |
| 14495 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14496 | /** |
| 14497 | * xmlXPathRunEval: |
| 14498 | * @ctxt: the XPath parser context with the compiled expression |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14499 | * @toBool: evaluate to a boolean result |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14500 | * |
| 14501 | * Evaluate the Precompiled XPath expression in the given context. |
| 14502 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14503 | static int |
| 14504 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt, int toBool) |
| 14505 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14506 | xmlXPathCompExprPtr comp; |
| 14507 | |
| 14508 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14509 | return(-1); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14510 | |
| 14511 | if (ctxt->valueTab == NULL) { |
| 14512 | /* Allocate the value stack */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14513 | ctxt->valueTab = (xmlXPathObjectPtr *) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14514 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 14515 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 14516 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14517 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14518 | } |
| 14519 | ctxt->valueNr = 0; |
| 14520 | ctxt->valueMax = 10; |
| 14521 | ctxt->value = NULL; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 14522 | ctxt->valueFrame = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14523 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14524 | #ifdef XPATH_STREAMING |
| 14525 | if (ctxt->comp->stream) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14526 | int res; |
| 14527 | |
| 14528 | if (toBool) { |
| 14529 | /* |
| 14530 | * Evaluation to boolean result. |
| 14531 | */ |
| 14532 | res = xmlXPathRunStreamEval(ctxt->context, |
| 14533 | ctxt->comp->stream, NULL, 1); |
| 14534 | if (res != -1) |
| 14535 | return(res); |
| 14536 | } else { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14537 | xmlXPathObjectPtr resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14538 | |
| 14539 | /* |
| 14540 | * Evaluation to a sequence. |
| 14541 | */ |
| 14542 | res = xmlXPathRunStreamEval(ctxt->context, |
| 14543 | ctxt->comp->stream, &resObj, 0); |
| 14544 | |
| 14545 | if ((res != -1) && (resObj != NULL)) { |
| 14546 | valuePush(ctxt, resObj); |
| 14547 | return(0); |
| 14548 | } |
| 14549 | if (resObj != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14550 | xmlXPathReleaseObject(ctxt->context, resObj); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14551 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14552 | /* |
| 14553 | * QUESTION TODO: This falls back to normal XPath evaluation |
| 14554 | * if res == -1. Is this intended? |
| 14555 | */ |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14556 | } |
| 14557 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14558 | comp = ctxt->comp; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14559 | if (comp->last < 0) { |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 14560 | xmlGenericError(xmlGenericErrorContext, |
| 14561 | "xmlXPathRunEval: last is less than zero\n"); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14562 | return(-1); |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 14563 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14564 | if (toBool) |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14565 | return(xmlXPathCompOpEvalToBoolean(ctxt, |
| 14566 | &comp->steps[comp->last], 0)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14567 | else |
| 14568 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 14569 | |
| 14570 | return(0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14571 | } |
| 14572 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14573 | /************************************************************************ |
| 14574 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14575 | * Public interfaces * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14576 | * * |
| 14577 | ************************************************************************/ |
| 14578 | |
| 14579 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14580 | * xmlXPathEvalPredicate: |
| 14581 | * @ctxt: the XPath context |
| 14582 | * @res: the Predicate Expression evaluation result |
| 14583 | * |
| 14584 | * Evaluate a predicate result for the current node. |
| 14585 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 14586 | * the result to a boolean. If the result is a number, the result will |
| 14587 | * be converted to true if the number is equal to the position of the |
| 14588 | * context node in the context node list (as returned by the position |
| 14589 | * function) and will be converted to false otherwise; if the result |
| 14590 | * is not a number, then the result will be converted as if by a call |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14591 | * to the boolean function. |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14592 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14593 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14594 | */ |
| 14595 | int |
| 14596 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 14597 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14598 | switch (res->type) { |
| 14599 | case XPATH_BOOLEAN: |
| 14600 | return(res->boolval); |
| 14601 | case XPATH_NUMBER: |
| 14602 | return(res->floatval == ctxt->proximityPosition); |
| 14603 | case XPATH_NODESET: |
| 14604 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 14605 | if (res->nodesetval == NULL) |
| 14606 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14607 | return(res->nodesetval->nodeNr != 0); |
| 14608 | case XPATH_STRING: |
| 14609 | return((res->stringval != NULL) && |
| 14610 | (xmlStrlen(res->stringval) != 0)); |
| 14611 | default: |
| 14612 | STRANGE |
| 14613 | } |
| 14614 | return(0); |
| 14615 | } |
| 14616 | |
| 14617 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14618 | * xmlXPathEvaluatePredicateResult: |
| 14619 | * @ctxt: the XPath Parser context |
| 14620 | * @res: the Predicate Expression evaluation result |
| 14621 | * |
| 14622 | * Evaluate a predicate result for the current node. |
| 14623 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 14624 | * the result to a boolean. If the result is a number, the result will |
| 14625 | * be converted to true if the number is equal to the position of the |
| 14626 | * context node in the context node list (as returned by the position |
| 14627 | * function) and will be converted to false otherwise; if the result |
| 14628 | * is not a number, then the result will be converted as if by a call |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14629 | * to the boolean function. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14630 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14631 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14632 | */ |
| 14633 | int |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14634 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14635 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 14636 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14637 | switch (res->type) { |
| 14638 | case XPATH_BOOLEAN: |
| 14639 | return(res->boolval); |
| 14640 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 14641 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 14642 | return((res->floatval == ctxt->context->proximityPosition) && |
| 14643 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 14644 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14645 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 14646 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14647 | case XPATH_NODESET: |
| 14648 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 14649 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 14650 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14651 | return(res->nodesetval->nodeNr != 0); |
| 14652 | case XPATH_STRING: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14653 | return((res->stringval != NULL) && (res->stringval[0] != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14654 | #ifdef LIBXML_XPTR_ENABLED |
| 14655 | case XPATH_LOCATIONSET:{ |
| 14656 | xmlLocationSetPtr ptr = res->user; |
| 14657 | if (ptr == NULL) |
| 14658 | return(0); |
| 14659 | return (ptr->locNr != 0); |
| 14660 | } |
| 14661 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14662 | default: |
| 14663 | STRANGE |
| 14664 | } |
| 14665 | return(0); |
| 14666 | } |
| 14667 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14668 | #ifdef XPATH_STREAMING |
| 14669 | /** |
| 14670 | * xmlXPathTryStreamCompile: |
| 14671 | * @ctxt: an XPath context |
| 14672 | * @str: the XPath expression |
| 14673 | * |
| 14674 | * Try to compile the XPath expression as a streamable subset. |
| 14675 | * |
| 14676 | * Returns the compiled expression or NULL if failed to compile. |
| 14677 | */ |
| 14678 | static xmlXPathCompExprPtr |
| 14679 | xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 14680 | /* |
| 14681 | * Optimization: use streaming patterns when the XPath expression can |
| 14682 | * be compiled to a stream lookup |
| 14683 | */ |
| 14684 | xmlPatternPtr stream; |
| 14685 | xmlXPathCompExprPtr comp; |
| 14686 | xmlDictPtr dict = NULL; |
| 14687 | const xmlChar **namespaces = NULL; |
| 14688 | xmlNsPtr ns; |
| 14689 | int i, j; |
| 14690 | |
| 14691 | if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) && |
| 14692 | (!xmlStrchr(str, '@'))) { |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14693 | const xmlChar *tmp; |
| 14694 | |
| 14695 | /* |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14696 | * We don't try to handle expressions using the verbose axis |
| 14697 | * specifiers ("::"), just the simplied form at this point. |
| 14698 | * Additionally, if there is no list of namespaces available and |
| 14699 | * there's a ":" in the expression, indicating a prefixed QName, |
| 14700 | * then we won't try to compile either. xmlPatterncompile() needs |
| 14701 | * to have a list of namespaces at compilation time in order to |
| 14702 | * compile prefixed name tests. |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14703 | */ |
| 14704 | tmp = xmlStrchr(str, ':'); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14705 | if ((tmp != NULL) && |
| 14706 | ((ctxt == NULL) || (ctxt->nsNr == 0) || (tmp[1] == ':'))) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14707 | return(NULL); |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14708 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14709 | if (ctxt != NULL) { |
| 14710 | dict = ctxt->dict; |
| 14711 | if (ctxt->nsNr > 0) { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 14712 | namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*)); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14713 | if (namespaces == NULL) { |
| 14714 | xmlXPathErrMemory(ctxt, "allocating namespaces array\n"); |
| 14715 | return(NULL); |
| 14716 | } |
| 14717 | for (i = 0, j = 0; (j < ctxt->nsNr); j++) { |
| 14718 | ns = ctxt->namespaces[j]; |
| 14719 | namespaces[i++] = ns->href; |
| 14720 | namespaces[i++] = ns->prefix; |
| 14721 | } |
| 14722 | namespaces[i++] = NULL; |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 14723 | namespaces[i] = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14724 | } |
| 14725 | } |
| 14726 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 14727 | stream = xmlPatterncompile(str, dict, XML_PATTERN_XPATH, |
| 14728 | &namespaces[0]); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14729 | if (namespaces != NULL) { |
| 14730 | xmlFree((xmlChar **)namespaces); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14731 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14732 | if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) { |
| 14733 | comp = xmlXPathNewCompExpr(); |
| 14734 | if (comp == NULL) { |
| 14735 | xmlXPathErrMemory(ctxt, "allocating streamable expression\n"); |
| 14736 | return(NULL); |
| 14737 | } |
| 14738 | comp->stream = stream; |
| 14739 | comp->dict = dict; |
| 14740 | if (comp->dict) |
| 14741 | xmlDictReference(comp->dict); |
| 14742 | return(comp); |
| 14743 | } |
| 14744 | xmlFreePattern(stream); |
| 14745 | } |
| 14746 | return(NULL); |
| 14747 | } |
| 14748 | #endif /* XPATH_STREAMING */ |
| 14749 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14750 | static void |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14751 | xmlXPathOptimizeExpression(xmlXPathCompExprPtr comp, xmlXPathStepOpPtr op) |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14752 | { |
| 14753 | /* |
| 14754 | * Try to rewrite "descendant-or-self::node()/foo" to an optimized |
| 14755 | * internal representation. |
| 14756 | */ |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14757 | |
Nick Wellnhofer | b4bcba2 | 2013-08-05 00:15:11 +0200 | [diff] [blame] | 14758 | if ((op->op == XPATH_OP_COLLECT /* 11 */) && |
| 14759 | (op->ch1 != -1) && |
| 14760 | (op->ch2 == -1 /* no predicate */)) |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14761 | { |
| 14762 | xmlXPathStepOpPtr prevop = &comp->steps[op->ch1]; |
| 14763 | |
| 14764 | if ((prevop->op == XPATH_OP_COLLECT /* 11 */) && |
| 14765 | ((xmlXPathAxisVal) prevop->value == |
| 14766 | AXIS_DESCENDANT_OR_SELF) && |
| 14767 | (prevop->ch2 == -1) && |
| 14768 | ((xmlXPathTestVal) prevop->value2 == NODE_TEST_TYPE) && |
| 14769 | ((xmlXPathTypeVal) prevop->value3 == NODE_TYPE_NODE)) |
| 14770 | { |
| 14771 | /* |
| 14772 | * This is a "descendant-or-self::node()" without predicates. |
| 14773 | * Try to eliminate it. |
| 14774 | */ |
| 14775 | |
| 14776 | switch ((xmlXPathAxisVal) op->value) { |
| 14777 | case AXIS_CHILD: |
| 14778 | case AXIS_DESCENDANT: |
| 14779 | /* |
| 14780 | * Convert "descendant-or-self::node()/child::" or |
| 14781 | * "descendant-or-self::node()/descendant::" to |
| 14782 | * "descendant::" |
| 14783 | */ |
| 14784 | op->ch1 = prevop->ch1; |
| 14785 | op->value = AXIS_DESCENDANT; |
| 14786 | break; |
| 14787 | case AXIS_SELF: |
| 14788 | case AXIS_DESCENDANT_OR_SELF: |
| 14789 | /* |
| 14790 | * Convert "descendant-or-self::node()/self::" or |
| 14791 | * "descendant-or-self::node()/descendant-or-self::" to |
| 14792 | * to "descendant-or-self::" |
| 14793 | */ |
| 14794 | op->ch1 = prevop->ch1; |
| 14795 | op->value = AXIS_DESCENDANT_OR_SELF; |
| 14796 | break; |
| 14797 | default: |
| 14798 | break; |
| 14799 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14800 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14801 | } |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14802 | |
Xin Li | a136fc2 | 2016-07-26 14:22:54 -0700 | [diff] [blame] | 14803 | /* OP_VALUE has invalid ch1. */ |
| 14804 | if (op->op == XPATH_OP_VALUE) |
| 14805 | return; |
| 14806 | |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14807 | /* Recurse */ |
| 14808 | if (op->ch1 != -1) |
| 14809 | xmlXPathOptimizeExpression(comp, &comp->steps[op->ch1]); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14810 | if (op->ch2 != -1) |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14811 | xmlXPathOptimizeExpression(comp, &comp->steps[op->ch2]); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14812 | } |
| 14813 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14814 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14815 | * xmlXPathCtxtCompile: |
| 14816 | * @ctxt: an XPath context |
| 14817 | * @str: the XPath expression |
| 14818 | * |
| 14819 | * Compile an XPath expression |
| 14820 | * |
| 14821 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 14822 | * the caller has to free the object. |
| 14823 | */ |
| 14824 | xmlXPathCompExprPtr |
| 14825 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 14826 | xmlXPathParserContextPtr pctxt; |
| 14827 | xmlXPathCompExprPtr comp; |
| 14828 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14829 | #ifdef XPATH_STREAMING |
| 14830 | comp = xmlXPathTryStreamCompile(ctxt, str); |
| 14831 | if (comp != NULL) |
| 14832 | return(comp); |
| 14833 | #endif |
| 14834 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14835 | xmlXPathInit(); |
| 14836 | |
| 14837 | pctxt = xmlXPathNewParserContext(str, ctxt); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 14838 | if (pctxt == NULL) |
| 14839 | return NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 14840 | xmlXPathCompileExpr(pctxt, 1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14841 | |
| 14842 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 14843 | { |
| 14844 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 14845 | return(NULL); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14846 | } |
| 14847 | |
| 14848 | if (*pctxt->cur != 0) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14849 | /* |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14850 | * aleksey: in some cases this line prints *second* error message |
| 14851 | * (see bug #78858) and probably this should be fixed. |
| 14852 | * However, we are not sure that all error messages are printed |
| 14853 | * out in other places. It's not critical so we leave it as-is for now |
| 14854 | */ |
| 14855 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 14856 | comp = NULL; |
| 14857 | } else { |
| 14858 | comp = pctxt->comp; |
| 14859 | pctxt->comp = NULL; |
| 14860 | } |
| 14861 | xmlXPathFreeParserContext(pctxt); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14862 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14863 | if (comp != NULL) { |
| 14864 | comp->expr = xmlStrdup(str); |
| 14865 | #ifdef DEBUG_EVAL_COUNTS |
| 14866 | comp->string = xmlStrdup(str); |
| 14867 | comp->nb = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14868 | #endif |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 14869 | if ((comp->nbStep > 1) && (comp->last >= 0)) { |
| 14870 | xmlXPathOptimizeExpression(comp, &comp->steps[comp->last]); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 14871 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14872 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14873 | return(comp); |
| 14874 | } |
| 14875 | |
| 14876 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14877 | * xmlXPathCompile: |
| 14878 | * @str: the XPath expression |
| 14879 | * |
| 14880 | * Compile an XPath expression |
| 14881 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 14882 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14883 | * the caller has to free the object. |
| 14884 | */ |
| 14885 | xmlXPathCompExprPtr |
| 14886 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14887 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14888 | } |
| 14889 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14890 | /** |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14891 | * xmlXPathCompiledEvalInternal: |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14892 | * @comp: the compiled XPath expression |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14893 | * @ctxt: the XPath context |
| 14894 | * @resObj: the resulting XPath object or NULL |
| 14895 | * @toBool: 1 if only a boolean result is requested |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14896 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14897 | * Evaluate the Precompiled XPath expression in the given context. |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14898 | * The caller has to free @resObj. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14899 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14900 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14901 | * the caller has to free the object. |
| 14902 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14903 | static int |
| 14904 | xmlXPathCompiledEvalInternal(xmlXPathCompExprPtr comp, |
| 14905 | xmlXPathContextPtr ctxt, |
| 14906 | xmlXPathObjectPtr *resObj, |
| 14907 | int toBool) |
| 14908 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14909 | xmlXPathParserContextPtr pctxt; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14910 | #ifndef LIBXML_THREAD_ENABLED |
| 14911 | static int reentance = 0; |
| 14912 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14913 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14914 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14915 | CHECK_CTXT_NEG(ctxt) |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 14916 | |
| 14917 | if (comp == NULL) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14918 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14919 | xmlXPathInit(); |
| 14920 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14921 | #ifndef LIBXML_THREAD_ENABLED |
| 14922 | reentance++; |
| 14923 | if (reentance > 1) |
| 14924 | xmlXPathDisableOptimizer = 1; |
| 14925 | #endif |
| 14926 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14927 | #ifdef DEBUG_EVAL_COUNTS |
| 14928 | comp->nb++; |
| 14929 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 14930 | fprintf(stderr, "100 x %s\n", comp->string); |
| 14931 | comp->nb = 0; |
| 14932 | } |
| 14933 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14934 | pctxt = xmlXPathCompParserContext(comp, ctxt); |
| 14935 | res = xmlXPathRunEval(pctxt, toBool); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14936 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14937 | if (resObj) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14938 | if (pctxt->value == NULL) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14939 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14940 | "xmlXPathCompiledEval: evaluation failed\n"); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14941 | *resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14942 | } else { |
| 14943 | *resObj = valuePop(pctxt); |
| 14944 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14945 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14946 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14947 | /* |
| 14948 | * Pop all remaining objects from the stack. |
| 14949 | */ |
| 14950 | if (pctxt->valueNr > 0) { |
| 14951 | xmlXPathObjectPtr tmp; |
| 14952 | int stack = 0; |
| 14953 | |
| 14954 | do { |
| 14955 | tmp = valuePop(pctxt); |
| 14956 | if (tmp != NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14957 | stack++; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14958 | xmlXPathReleaseObject(ctxt, tmp); |
| 14959 | } |
| 14960 | } while (tmp != NULL); |
| 14961 | if ((stack != 0) && |
| 14962 | ((toBool) || ((resObj) && (*resObj)))) |
| 14963 | { |
| 14964 | xmlGenericError(xmlGenericErrorContext, |
| 14965 | "xmlXPathCompiledEval: %d objects left on the stack.\n", |
| 14966 | stack); |
| 14967 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14968 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14969 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14970 | if ((pctxt->error != XPATH_EXPRESSION_OK) && (resObj) && (*resObj)) { |
| 14971 | xmlXPathFreeObject(*resObj); |
| 14972 | *resObj = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14973 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14974 | pctxt->comp = NULL; |
| 14975 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14976 | #ifndef LIBXML_THREAD_ENABLED |
| 14977 | reentance--; |
| 14978 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14979 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14980 | return(res); |
| 14981 | } |
| 14982 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14983 | /** |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14984 | * xmlXPathCompiledEval: |
| 14985 | * @comp: the compiled XPath expression |
| 14986 | * @ctx: the XPath context |
| 14987 | * |
| 14988 | * Evaluate the Precompiled XPath expression in the given context. |
| 14989 | * |
| 14990 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 14991 | * the caller has to free the object. |
| 14992 | */ |
| 14993 | xmlXPathObjectPtr |
| 14994 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) |
| 14995 | { |
| 14996 | xmlXPathObjectPtr res = NULL; |
| 14997 | |
| 14998 | xmlXPathCompiledEvalInternal(comp, ctx, &res, 0); |
| 14999 | return(res); |
| 15000 | } |
| 15001 | |
| 15002 | /** |
| 15003 | * xmlXPathCompiledEvalToBoolean: |
| 15004 | * @comp: the compiled XPath expression |
| 15005 | * @ctxt: the XPath context |
| 15006 | * |
| 15007 | * Applies the XPath boolean() function on the result of the given |
| 15008 | * compiled expression. |
| 15009 | * |
| 15010 | * Returns 1 if the expression evaluated to true, 0 if to false and |
| 15011 | * -1 in API and internal errors. |
| 15012 | */ |
| 15013 | int |
| 15014 | xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp, |
| 15015 | xmlXPathContextPtr ctxt) |
| 15016 | { |
| 15017 | return(xmlXPathCompiledEvalInternal(comp, ctxt, NULL, 1)); |
| 15018 | } |
| 15019 | |
| 15020 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 15021 | * xmlXPathEvalExpr: |
| 15022 | * @ctxt: the XPath Parser context |
| 15023 | * |
| 15024 | * Parse and evaluate an XPath expression in the given context, |
| 15025 | * then push the result on the context stack |
| 15026 | */ |
| 15027 | void |
| 15028 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 15029 | #ifdef XPATH_STREAMING |
| 15030 | xmlXPathCompExprPtr comp; |
| 15031 | #endif |
| 15032 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 15033 | if (ctxt == NULL) return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15034 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 15035 | #ifdef XPATH_STREAMING |
| 15036 | comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base); |
| 15037 | if (comp != NULL) { |
| 15038 | if (ctxt->comp != NULL) |
| 15039 | xmlXPathFreeCompExpr(ctxt->comp); |
| 15040 | ctxt->comp = comp; |
| 15041 | if (ctxt->cur != NULL) |
| 15042 | while (*ctxt->cur != 0) ctxt->cur++; |
| 15043 | } else |
| 15044 | #endif |
| 15045 | { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 15046 | xmlXPathCompileExpr(ctxt, 1); |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 15047 | if ((ctxt->error == XPATH_EXPRESSION_OK) && |
| 15048 | (ctxt->comp != NULL) && |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 15049 | (ctxt->comp->nbStep > 1) && |
| 15050 | (ctxt->comp->last >= 0)) |
Kasimier T. Buchcik | 080152c | 2006-06-06 09:42:15 +0000 | [diff] [blame] | 15051 | { |
Nick Wellnhofer | 6227053 | 2012-08-19 19:42:38 +0200 | [diff] [blame] | 15052 | xmlXPathOptimizeExpression(ctxt->comp, |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 15053 | &ctxt->comp->steps[ctxt->comp->last]); |
Kasimier T. Buchcik | 080152c | 2006-06-06 09:42:15 +0000 | [diff] [blame] | 15054 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 15055 | } |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 15056 | CHECK_ERROR; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15057 | xmlXPathRunEval(ctxt, 0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 15058 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15059 | |
| 15060 | /** |
| 15061 | * xmlXPathEval: |
| 15062 | * @str: the XPath expression |
| 15063 | * @ctx: the XPath context |
| 15064 | * |
| 15065 | * Evaluate the XPath Location Path in the given context. |
| 15066 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 15067 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15068 | * the caller has to free the object. |
| 15069 | */ |
| 15070 | xmlXPathObjectPtr |
| 15071 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 15072 | xmlXPathParserContextPtr ctxt; |
| 15073 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 15074 | int stack = 0; |
| 15075 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15076 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15077 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15078 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15079 | |
| 15080 | ctxt = xmlXPathNewParserContext(str, ctx); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 15081 | if (ctxt == NULL) |
| 15082 | return NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15083 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15084 | |
| 15085 | if (ctxt->value == NULL) { |
| 15086 | xmlGenericError(xmlGenericErrorContext, |
| 15087 | "xmlXPathEval: evaluation failed\n"); |
| 15088 | res = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15089 | } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL) |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 15090 | #ifdef XPATH_STREAMING |
| 15091 | && (ctxt->comp->stream == NULL) |
| 15092 | #endif |
| 15093 | ) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15094 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 15095 | res = NULL; |
| 15096 | } else { |
| 15097 | res = valuePop(ctxt); |
| 15098 | } |
| 15099 | |
| 15100 | do { |
| 15101 | tmp = valuePop(ctxt); |
| 15102 | if (tmp != NULL) { |
| 15103 | if (tmp != init) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15104 | stack++; |
| 15105 | xmlXPathReleaseObject(ctx, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 15106 | } |
| 15107 | } while (tmp != NULL); |
| 15108 | if ((stack != 0) && (res != NULL)) { |
| 15109 | xmlGenericError(xmlGenericErrorContext, |
| 15110 | "xmlXPathEval: %d object left on the stack\n", |
| 15111 | stack); |
| 15112 | } |
| 15113 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 15114 | xmlXPathFreeObject(res); |
| 15115 | res = NULL; |
| 15116 | } |
| 15117 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15118 | xmlXPathFreeParserContext(ctxt); |
| 15119 | return(res); |
| 15120 | } |
| 15121 | |
| 15122 | /** |
Alex Bligh | 28876af | 2013-03-23 17:23:27 +0000 | [diff] [blame] | 15123 | * xmlXPathSetContextNode: |
| 15124 | * @node: the node to to use as the context node |
| 15125 | * @ctx: the XPath context |
| 15126 | * |
| 15127 | * Sets 'node' as the context node. The node must be in the same |
| 15128 | * document as that associated with the context. |
| 15129 | * |
| 15130 | * Returns -1 in case of error or 0 if successful |
| 15131 | */ |
| 15132 | int |
| 15133 | xmlXPathSetContextNode(xmlNodePtr node, xmlXPathContextPtr ctx) { |
| 15134 | if ((node == NULL) || (ctx == NULL)) |
| 15135 | return(-1); |
| 15136 | |
| 15137 | if (node->doc == ctx->doc) { |
| 15138 | ctx->node = node; |
| 15139 | return(0); |
| 15140 | } |
| 15141 | return(-1); |
| 15142 | } |
| 15143 | |
| 15144 | /** |
| 15145 | * xmlXPathNodeEval: |
| 15146 | * @node: the node to to use as the context node |
| 15147 | * @str: the XPath expression |
| 15148 | * @ctx: the XPath context |
| 15149 | * |
| 15150 | * Evaluate the XPath Location Path in the given context. The node 'node' |
| 15151 | * is set as the context node. The context node is not restored. |
| 15152 | * |
| 15153 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 15154 | * the caller has to free the object. |
| 15155 | */ |
| 15156 | xmlXPathObjectPtr |
| 15157 | xmlXPathNodeEval(xmlNodePtr node, const xmlChar *str, xmlXPathContextPtr ctx) { |
| 15158 | if (str == NULL) |
| 15159 | return(NULL); |
| 15160 | if (xmlXPathSetContextNode(node, ctx) < 0) |
| 15161 | return(NULL); |
| 15162 | return(xmlXPathEval(str, ctx)); |
| 15163 | } |
| 15164 | |
| 15165 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15166 | * xmlXPathEvalExpression: |
| 15167 | * @str: the XPath expression |
| 15168 | * @ctxt: the XPath context |
| 15169 | * |
| 15170 | * Evaluate the XPath expression in the given context. |
| 15171 | * |
| 15172 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 15173 | * the caller has to free the object. |
| 15174 | */ |
| 15175 | xmlXPathObjectPtr |
| 15176 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 15177 | xmlXPathParserContextPtr pctxt; |
| 15178 | xmlXPathObjectPtr res, tmp; |
| 15179 | int stack = 0; |
| 15180 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15181 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15182 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15183 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15184 | |
| 15185 | pctxt = xmlXPathNewParserContext(str, ctxt); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 15186 | if (pctxt == NULL) |
| 15187 | return NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15188 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15189 | |
Daniel Veillard | c465ffc | 2006-10-17 19:39:33 +0000 | [diff] [blame] | 15190 | if ((*pctxt->cur != 0) || (pctxt->error != XPATH_EXPRESSION_OK)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15191 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 15192 | res = NULL; |
| 15193 | } else { |
| 15194 | res = valuePop(pctxt); |
| 15195 | } |
| 15196 | do { |
| 15197 | tmp = valuePop(pctxt); |
| 15198 | if (tmp != NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15199 | xmlXPathReleaseObject(ctxt, tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15200 | stack++; |
| 15201 | } |
| 15202 | } while (tmp != NULL); |
| 15203 | if ((stack != 0) && (res != NULL)) { |
| 15204 | xmlGenericError(xmlGenericErrorContext, |
| 15205 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 15206 | stack); |
| 15207 | } |
| 15208 | xmlXPathFreeParserContext(pctxt); |
| 15209 | return(res); |
| 15210 | } |
| 15211 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15212 | /************************************************************************ |
| 15213 | * * |
| 15214 | * Extra functions not pertaining to the XPath spec * |
| 15215 | * * |
| 15216 | ************************************************************************/ |
| 15217 | /** |
| 15218 | * xmlXPathEscapeUriFunction: |
| 15219 | * @ctxt: the XPath Parser context |
| 15220 | * @nargs: the number of arguments |
| 15221 | * |
| 15222 | * Implement the escape-uri() XPath function |
| 15223 | * string escape-uri(string $str, bool $escape-reserved) |
| 15224 | * |
| 15225 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 15226 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 15227 | * or part of a URI. The effect of the function is to replace any special |
| 15228 | * character in the string by an escape sequence of the form %xx%yy..., |
| 15229 | * where xxyy... is the hexadecimal representation of the octets used to |
| 15230 | * represent the character in UTF-8. |
| 15231 | * |
| 15232 | * The set of characters that are escaped depends on the setting of the |
| 15233 | * boolean argument $escape-reserved. |
| 15234 | * |
| 15235 | * If $escape-reserved is true, all characters are escaped other than lower |
| 15236 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 15237 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 15238 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 15239 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 15240 | * A-F). |
| 15241 | * |
| 15242 | * If $escape-reserved is false, the behavior differs in that characters |
| 15243 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 15244 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15245 | * |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15246 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 15247 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 15248 | * compared using string comparison functions, this function must always use |
| 15249 | * the upper-case letters A-F. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15250 | * |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15251 | * Generally, $escape-reserved should be set to true when escaping a string |
| 15252 | * that is to form a single part of a URI, and to false when escaping an |
| 15253 | * entire URI or URI reference. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15254 | * |
| 15255 | * In the case of non-ascii characters, the string is encoded according to |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15256 | * utf-8 and then converted according to RFC 2396. |
| 15257 | * |
| 15258 | * Examples |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15259 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15260 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 15261 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 15262 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 15263 | * |
| 15264 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 15265 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15266 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 15267 | xmlXPathObjectPtr str; |
| 15268 | int escape_reserved; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 15269 | xmlBufPtr target; |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15270 | xmlChar *cptr; |
| 15271 | xmlChar escape[4]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15272 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15273 | CHECK_ARITY(2); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15274 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15275 | escape_reserved = xmlXPathPopBoolean(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15276 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15277 | CAST_TO_STRING; |
| 15278 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15279 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 15280 | target = xmlBufCreate(); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15281 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15282 | escape[0] = '%'; |
| 15283 | escape[3] = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15284 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15285 | if (target) { |
| 15286 | for (cptr = str->stringval; *cptr; cptr++) { |
| 15287 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 15288 | (*cptr >= 'a' && *cptr <= 'z') || |
| 15289 | (*cptr >= '0' && *cptr <= '9') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15290 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15291 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 15292 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15293 | (*cptr == '%' && |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15294 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 15295 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 15296 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 15297 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 15298 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 15299 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 15300 | (!escape_reserved && |
| 15301 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 15302 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 15303 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 15304 | *cptr == ','))) { |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 15305 | xmlBufAdd(target, cptr, 1); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15306 | } else { |
| 15307 | if ((*cptr >> 4) < 10) |
| 15308 | escape[1] = '0' + (*cptr >> 4); |
| 15309 | else |
| 15310 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 15311 | if ((*cptr & 0xF) < 10) |
| 15312 | escape[2] = '0' + (*cptr & 0xF); |
| 15313 | else |
| 15314 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15315 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 15316 | xmlBufAdd(target, &escape[0], 3); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15317 | } |
| 15318 | } |
| 15319 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15320 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame] | 15321 | xmlBufContent(target))); |
| 15322 | xmlBufFree(target); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15323 | xmlXPathReleaseObject(ctxt->context, str); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15324 | } |
| 15325 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15326 | /** |
| 15327 | * xmlXPathRegisterAllFunctions: |
| 15328 | * @ctxt: the XPath context |
| 15329 | * |
| 15330 | * Registers all default XPath functions in this context |
| 15331 | */ |
| 15332 | void |
| 15333 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 15334 | { |
| 15335 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 15336 | xmlXPathBooleanFunction); |
| 15337 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 15338 | xmlXPathCeilingFunction); |
| 15339 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 15340 | xmlXPathCountFunction); |
| 15341 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 15342 | xmlXPathConcatFunction); |
| 15343 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 15344 | xmlXPathContainsFunction); |
| 15345 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 15346 | xmlXPathIdFunction); |
| 15347 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 15348 | xmlXPathFalseFunction); |
| 15349 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 15350 | xmlXPathFloorFunction); |
| 15351 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 15352 | xmlXPathLastFunction); |
| 15353 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 15354 | xmlXPathLangFunction); |
| 15355 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 15356 | xmlXPathLocalNameFunction); |
| 15357 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 15358 | xmlXPathNotFunction); |
| 15359 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 15360 | xmlXPathNameFunction); |
| 15361 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 15362 | xmlXPathNamespaceURIFunction); |
| 15363 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 15364 | xmlXPathNormalizeFunction); |
| 15365 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 15366 | xmlXPathNumberFunction); |
| 15367 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 15368 | xmlXPathPositionFunction); |
| 15369 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 15370 | xmlXPathRoundFunction); |
| 15371 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 15372 | xmlXPathStringFunction); |
| 15373 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 15374 | xmlXPathStringLengthFunction); |
| 15375 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 15376 | xmlXPathStartsWithFunction); |
| 15377 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 15378 | xmlXPathSubstringFunction); |
| 15379 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 15380 | xmlXPathSubstringBeforeFunction); |
| 15381 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 15382 | xmlXPathSubstringAfterFunction); |
| 15383 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 15384 | xmlXPathSumFunction); |
| 15385 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 15386 | xmlXPathTrueFunction); |
| 15387 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 15388 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15389 | |
| 15390 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 15391 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 15392 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15393 | } |
| 15394 | |
| 15395 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15396 | #define bottom_xpath |
| 15397 | #include "elfgcchack.h" |