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 |
| 5 | * |
| 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 | * |
| 15 | * 14 Nov 2000 ht - truncated declaration of xmlXPathEvalRelativeLocationPath |
| 16 | * for VMS |
| 17 | */ |
| 18 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 19 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 20 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 21 | #ifdef LIBXML_XPATH_ENABLED |
| 22 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | #ifdef HAVE_SYS_TYPES_H |
| 26 | #include <sys/types.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_MATH_H |
| 29 | #include <math.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_FLOAT_H |
| 32 | #include <float.h> |
| 33 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 34 | #ifdef HAVE_CTYPE_H |
| 35 | #include <ctype.h> |
| 36 | #endif |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 37 | #ifdef HAVE_SIGNAL_H |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 38 | #include <signal.h> |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 39 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 40 | |
| 41 | #include <libxml/xmlmemory.h> |
| 42 | #include <libxml/tree.h> |
| 43 | #include <libxml/valid.h> |
| 44 | #include <libxml/xpath.h> |
| 45 | #include <libxml/xpathInternals.h> |
| 46 | #include <libxml/parserInternals.h> |
| 47 | #include <libxml/hash.h> |
| 48 | #ifdef LIBXML_XPTR_ENABLED |
| 49 | #include <libxml/xpointer.h> |
| 50 | #endif |
| 51 | #ifdef LIBXML_DEBUG_ENABLED |
| 52 | #include <libxml/debugXML.h> |
| 53 | #endif |
| 54 | #include <libxml/xmlerror.h> |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 55 | #include <libxml/threads.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 56 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 57 | |
| 58 | /* #define DEBUG */ |
| 59 | /* #define DEBUG_STEP */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 60 | /* #define DEBUG_STEP_NTH */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 61 | /* #define DEBUG_EXPR */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 62 | /* #define DEBUG_EVAL_COUNTS */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 64 | double xmlXPathDivideBy(double f, double fzero); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 66 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 67 | NULL, |
| 68 | XML_NAMESPACE_DECL, |
| 69 | XML_XML_NAMESPACE, |
| 70 | BAD_CAST "xml" |
| 71 | }; |
| 72 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
Daniel Veillard | da423da | 2002-04-10 19:25:38 +0000 | [diff] [blame] | 73 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Optimizer is disabled only when threaded apps are detected while |
| 76 | * the library ain't compiled for thread safety. |
| 77 | */ |
| 78 | static int xmlXPathDisableOptimizer = 0; |
| 79 | #endif |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 80 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 81 | /************************************************************************ |
| 82 | * * |
| 83 | * Floating point stuff * |
| 84 | * * |
| 85 | ************************************************************************/ |
| 86 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 87 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 88 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 89 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 90 | #include "trionan.c" |
| 91 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 93 | * The lack of portability of this section of the libc is annoying ! |
| 94 | */ |
| 95 | double xmlXPathNAN = 0; |
| 96 | double xmlXPathPINF = 1; |
| 97 | double xmlXPathNINF = -1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 98 | double xmlXPathNZERO = 0; |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 99 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 100 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 101 | /** |
| 102 | * xmlXPathInit: |
| 103 | * |
| 104 | * Initialize the XPath environment |
| 105 | */ |
| 106 | void |
| 107 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 108 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 110 | xmlXPathPINF = trio_pinf(); |
| 111 | xmlXPathNINF = trio_ninf(); |
| 112 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 113 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 114 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 115 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 118 | /** |
| 119 | * xmlXPathIsNaN: |
| 120 | * @val: a double value |
| 121 | * |
| 122 | * Provides a portable isnan() function to detect whether a double |
| 123 | * is a NotaNumber. Based on trio code |
| 124 | * http://sourceforge.net/projects/ctrio/ |
| 125 | * |
| 126 | * Returns 1 if the value is a NaN, 0 otherwise |
| 127 | */ |
| 128 | int |
| 129 | xmlXPathIsNaN(double val) { |
| 130 | return(trio_isnan(val)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * xmlXPathIsInf: |
| 135 | * @val: a double value |
| 136 | * |
| 137 | * Provides a portable isinf() function to detect whether a double |
| 138 | * is a +Infinite or -Infinite. Based on trio code |
| 139 | * http://sourceforge.net/projects/ctrio/ |
| 140 | * |
| 141 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 142 | */ |
| 143 | int |
| 144 | xmlXPathIsInf(double val) { |
| 145 | return(trio_isinf(val)); |
| 146 | } |
| 147 | |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 148 | /** |
| 149 | * xmlXPathGetSign: |
| 150 | * @val: a double value |
| 151 | * |
| 152 | * Provides a portable function to detect the sign of a double |
| 153 | * Modified from trio code |
| 154 | * http://sourceforge.net/projects/ctrio/ |
| 155 | * |
| 156 | * Returns 1 if the value is Negative, 0 if positive |
| 157 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 158 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 159 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 160 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 164 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 165 | * * |
| 166 | * Parser Types * |
| 167 | * * |
| 168 | ************************************************************************/ |
| 169 | |
| 170 | /* |
| 171 | * Types are private: |
| 172 | */ |
| 173 | |
| 174 | typedef enum { |
| 175 | XPATH_OP_END=0, |
| 176 | XPATH_OP_AND, |
| 177 | XPATH_OP_OR, |
| 178 | XPATH_OP_EQUAL, |
| 179 | XPATH_OP_CMP, |
| 180 | XPATH_OP_PLUS, |
| 181 | XPATH_OP_MULT, |
| 182 | XPATH_OP_UNION, |
| 183 | XPATH_OP_ROOT, |
| 184 | XPATH_OP_NODE, |
| 185 | XPATH_OP_RESET, |
| 186 | XPATH_OP_COLLECT, |
| 187 | XPATH_OP_VALUE, |
| 188 | XPATH_OP_VARIABLE, |
| 189 | XPATH_OP_FUNCTION, |
| 190 | XPATH_OP_ARG, |
| 191 | XPATH_OP_PREDICATE, |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 192 | XPATH_OP_FILTER, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 193 | XPATH_OP_SORT |
| 194 | #ifdef LIBXML_XPTR_ENABLED |
| 195 | ,XPATH_OP_RANGETO |
| 196 | #endif |
| 197 | } xmlXPathOp; |
| 198 | |
| 199 | typedef enum { |
| 200 | AXIS_ANCESTOR = 1, |
| 201 | AXIS_ANCESTOR_OR_SELF, |
| 202 | AXIS_ATTRIBUTE, |
| 203 | AXIS_CHILD, |
| 204 | AXIS_DESCENDANT, |
| 205 | AXIS_DESCENDANT_OR_SELF, |
| 206 | AXIS_FOLLOWING, |
| 207 | AXIS_FOLLOWING_SIBLING, |
| 208 | AXIS_NAMESPACE, |
| 209 | AXIS_PARENT, |
| 210 | AXIS_PRECEDING, |
| 211 | AXIS_PRECEDING_SIBLING, |
| 212 | AXIS_SELF |
| 213 | } xmlXPathAxisVal; |
| 214 | |
| 215 | typedef enum { |
| 216 | NODE_TEST_NONE = 0, |
| 217 | NODE_TEST_TYPE = 1, |
| 218 | NODE_TEST_PI = 2, |
| 219 | NODE_TEST_ALL = 3, |
| 220 | NODE_TEST_NS = 4, |
| 221 | NODE_TEST_NAME = 5 |
| 222 | } xmlXPathTestVal; |
| 223 | |
| 224 | typedef enum { |
| 225 | NODE_TYPE_NODE = 0, |
| 226 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 227 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 228 | NODE_TYPE_PI = XML_PI_NODE |
| 229 | } xmlXPathTypeVal; |
| 230 | |
| 231 | |
| 232 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 233 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 234 | struct _xmlXPathStepOp { |
| 235 | xmlXPathOp op; |
| 236 | int ch1; |
| 237 | int ch2; |
| 238 | int value; |
| 239 | int value2; |
| 240 | int value3; |
| 241 | void *value4; |
| 242 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 243 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 244 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | struct _xmlXPathCompExpr { |
| 248 | int nbStep; |
| 249 | int maxStep; |
| 250 | xmlXPathStepOp *steps; /* ops for computation */ |
| 251 | int last; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 252 | #ifdef DEBUG_EVAL_COUNTS |
| 253 | int nb; |
| 254 | xmlChar *string; |
| 255 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | /************************************************************************ |
| 259 | * * |
| 260 | * Parser Type functions * |
| 261 | * * |
| 262 | ************************************************************************/ |
| 263 | |
| 264 | /** |
| 265 | * xmlXPathNewCompExpr: |
| 266 | * |
| 267 | * Create a new Xpath component |
| 268 | * |
| 269 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 270 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 271 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 272 | xmlXPathNewCompExpr(void) { |
| 273 | xmlXPathCompExprPtr cur; |
| 274 | |
| 275 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 276 | if (cur == NULL) { |
| 277 | xmlGenericError(xmlGenericErrorContext, |
| 278 | "xmlXPathNewCompExpr : malloc failed\n"); |
| 279 | return(NULL); |
| 280 | } |
| 281 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 282 | cur->maxStep = 10; |
| 283 | cur->nbStep = 0; |
| 284 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 285 | sizeof(xmlXPathStepOp)); |
| 286 | if (cur->steps == NULL) { |
| 287 | xmlGenericError(xmlGenericErrorContext, |
| 288 | "xmlXPathNewCompExpr : malloc failed\n"); |
| 289 | xmlFree(cur); |
| 290 | return(NULL); |
| 291 | } |
| 292 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 293 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 294 | #ifdef DEBUG_EVAL_COUNTS |
| 295 | cur->nb = 0; |
| 296 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 297 | return(cur); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * xmlXPathFreeCompExpr: |
| 302 | * @comp: an XPATH comp |
| 303 | * |
| 304 | * Free up the memory allocated by @comp |
| 305 | */ |
| 306 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 307 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 308 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 309 | xmlXPathStepOpPtr op; |
| 310 | int i; |
| 311 | |
| 312 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 313 | return; |
| 314 | for (i = 0; i < comp->nbStep; i++) { |
| 315 | op = &comp->steps[i]; |
| 316 | if (op->value4 != NULL) { |
| 317 | if (op->op == XPATH_OP_VALUE) |
| 318 | xmlXPathFreeObject(op->value4); |
| 319 | else |
| 320 | xmlFree(op->value4); |
| 321 | } |
| 322 | if (op->value5 != NULL) |
| 323 | xmlFree(op->value5); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 324 | } |
| 325 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 326 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 327 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 328 | #ifdef DEBUG_EVAL_COUNTS |
| 329 | if (comp->string != NULL) { |
| 330 | xmlFree(comp->string); |
| 331 | } |
| 332 | #endif |
| 333 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 334 | xmlFree(comp); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * xmlXPathCompExprAdd: |
| 339 | * @comp: the compiled expression |
| 340 | * @ch1: first child index |
| 341 | * @ch2: second child index |
| 342 | * @op: an op |
| 343 | * @value: the first int value |
| 344 | * @value2: the second int value |
| 345 | * @value3: the third int value |
| 346 | * @value4: the first string value |
| 347 | * @value5: the second string value |
| 348 | * |
| 349 | * Add an step to an XPath Compiled Expression |
| 350 | * |
| 351 | * Returns -1 in case of failure, the index otherwise |
| 352 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 353 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 354 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 355 | xmlXPathOp op, int value, |
| 356 | int value2, int value3, void *value4, void *value5) { |
| 357 | if (comp->nbStep >= comp->maxStep) { |
| 358 | xmlXPathStepOp *real; |
| 359 | |
| 360 | comp->maxStep *= 2; |
| 361 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 362 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 363 | if (real == NULL) { |
| 364 | comp->maxStep /= 2; |
| 365 | xmlGenericError(xmlGenericErrorContext, |
| 366 | "xmlXPathCompExprAdd : realloc failed\n"); |
| 367 | return(-1); |
| 368 | } |
| 369 | comp->steps = real; |
| 370 | } |
| 371 | comp->last = comp->nbStep; |
| 372 | comp->steps[comp->nbStep].ch1 = ch1; |
| 373 | comp->steps[comp->nbStep].ch2 = ch2; |
| 374 | comp->steps[comp->nbStep].op = op; |
| 375 | comp->steps[comp->nbStep].value = value; |
| 376 | comp->steps[comp->nbStep].value2 = value2; |
| 377 | comp->steps[comp->nbStep].value3 = value3; |
| 378 | comp->steps[comp->nbStep].value4 = value4; |
| 379 | comp->steps[comp->nbStep].value5 = value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 380 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 381 | return(comp->nbStep++); |
| 382 | } |
| 383 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 384 | /** |
| 385 | * xmlXPathCompSwap: |
| 386 | * @comp: the compiled expression |
| 387 | * @op: operation index |
| 388 | * |
| 389 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 390 | */ |
| 391 | static void |
| 392 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 393 | int tmp; |
| 394 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 395 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 396 | /* |
| 397 | * Since this manipulates possibly shared variables, this is |
| 398 | * disable if one detects that the library is used in a multithreaded |
| 399 | * application |
| 400 | */ |
| 401 | if (xmlXPathDisableOptimizer) |
| 402 | return; |
| 403 | #endif |
| 404 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 405 | tmp = op->ch1; |
| 406 | op->ch1 = op->ch2; |
| 407 | op->ch2 = tmp; |
| 408 | } |
| 409 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 410 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 411 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 412 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 413 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 414 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 415 | (op), (val), (val2), (val3), (val4), (val5)) |
| 416 | |
| 417 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 418 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 419 | |
| 420 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 421 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 422 | |
| 423 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
| 424 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), (val), (val2), 0 ,NULL ,NULL) |
| 425 | |
| 426 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | * * |
| 428 | * Debugging related functions * |
| 429 | * * |
| 430 | ************************************************************************/ |
| 431 | |
| 432 | #define TODO \ |
| 433 | xmlGenericError(xmlGenericErrorContext, \ |
| 434 | "Unimplemented block at %s:%d\n", \ |
| 435 | __FILE__, __LINE__); |
| 436 | |
| 437 | #define STRANGE \ |
| 438 | xmlGenericError(xmlGenericErrorContext, \ |
| 439 | "Internal error at %s:%d\n", \ |
| 440 | __FILE__, __LINE__); |
| 441 | |
| 442 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 443 | static void |
| 444 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 445 | int i; |
| 446 | char shift[100]; |
| 447 | |
| 448 | for (i = 0;((i < depth) && (i < 25));i++) |
| 449 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 450 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 451 | if (cur == NULL) { |
| 452 | fprintf(output, shift); |
| 453 | fprintf(output, "Node is NULL !\n"); |
| 454 | return; |
| 455 | |
| 456 | } |
| 457 | |
| 458 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 459 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 460 | fprintf(output, shift); |
| 461 | fprintf(output, " /\n"); |
| 462 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 463 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 464 | else |
| 465 | xmlDebugDumpOneNode(output, cur, depth); |
| 466 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 467 | static void |
| 468 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 469 | xmlNodePtr tmp; |
| 470 | int i; |
| 471 | char shift[100]; |
| 472 | |
| 473 | for (i = 0;((i < depth) && (i < 25));i++) |
| 474 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 475 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 476 | if (cur == NULL) { |
| 477 | fprintf(output, shift); |
| 478 | fprintf(output, "Node is NULL !\n"); |
| 479 | return; |
| 480 | |
| 481 | } |
| 482 | |
| 483 | while (cur != NULL) { |
| 484 | tmp = cur; |
| 485 | cur = cur->next; |
| 486 | xmlDebugDumpOneNode(output, tmp, depth); |
| 487 | } |
| 488 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 489 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 490 | static void |
| 491 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 492 | int i; |
| 493 | char shift[100]; |
| 494 | |
| 495 | for (i = 0;((i < depth) && (i < 25));i++) |
| 496 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 497 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 498 | |
| 499 | if (cur == NULL) { |
| 500 | fprintf(output, shift); |
| 501 | fprintf(output, "NodeSet is NULL !\n"); |
| 502 | return; |
| 503 | |
| 504 | } |
| 505 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 506 | if (cur != NULL) { |
| 507 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 508 | for (i = 0;i < cur->nodeNr;i++) { |
| 509 | fprintf(output, shift); |
| 510 | fprintf(output, "%d", i + 1); |
| 511 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 516 | static void |
| 517 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 518 | int i; |
| 519 | char shift[100]; |
| 520 | |
| 521 | for (i = 0;((i < depth) && (i < 25));i++) |
| 522 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 523 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 524 | |
| 525 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 526 | fprintf(output, shift); |
| 527 | fprintf(output, "Value Tree is NULL !\n"); |
| 528 | return; |
| 529 | |
| 530 | } |
| 531 | |
| 532 | fprintf(output, shift); |
| 533 | fprintf(output, "%d", i + 1); |
| 534 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 535 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 536 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 537 | static void |
| 538 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 539 | int i; |
| 540 | char shift[100]; |
| 541 | |
| 542 | for (i = 0;((i < depth) && (i < 25));i++) |
| 543 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 544 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 545 | |
| 546 | if (cur == NULL) { |
| 547 | fprintf(output, shift); |
| 548 | fprintf(output, "LocationSet is NULL !\n"); |
| 549 | return; |
| 550 | |
| 551 | } |
| 552 | |
| 553 | for (i = 0;i < cur->locNr;i++) { |
| 554 | fprintf(output, shift); |
| 555 | fprintf(output, "%d : ", i + 1); |
| 556 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 557 | } |
| 558 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 559 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 560 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 561 | /** |
| 562 | * xmlXPathDebugDumpObject: |
| 563 | * @output: the FILE * to dump the output |
| 564 | * @cur: the object to inspect |
| 565 | * @depth: indentation level |
| 566 | * |
| 567 | * Dump the content of the object for debugging purposes |
| 568 | */ |
| 569 | void |
| 570 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 571 | int i; |
| 572 | char shift[100]; |
| 573 | |
| 574 | for (i = 0;((i < depth) && (i < 25));i++) |
| 575 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 576 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 577 | |
| 578 | fprintf(output, shift); |
| 579 | |
| 580 | if (cur == NULL) { |
| 581 | fprintf(output, "Object is empty (NULL)\n"); |
| 582 | return; |
| 583 | } |
| 584 | switch(cur->type) { |
| 585 | case XPATH_UNDEFINED: |
| 586 | fprintf(output, "Object is uninitialized\n"); |
| 587 | break; |
| 588 | case XPATH_NODESET: |
| 589 | fprintf(output, "Object is a Node Set :\n"); |
| 590 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 591 | break; |
| 592 | case XPATH_XSLT_TREE: |
| 593 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 594 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 595 | break; |
| 596 | case XPATH_BOOLEAN: |
| 597 | fprintf(output, "Object is a Boolean : "); |
| 598 | if (cur->boolval) fprintf(output, "true\n"); |
| 599 | else fprintf(output, "false\n"); |
| 600 | break; |
| 601 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 602 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 603 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 604 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 605 | break; |
| 606 | case -1: |
| 607 | fprintf(output, "Object is a number : -Infinity\n"); |
| 608 | break; |
| 609 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 610 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 611 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 612 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 613 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 614 | } else { |
| 615 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 616 | } |
| 617 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 618 | break; |
| 619 | case XPATH_STRING: |
| 620 | fprintf(output, "Object is a string : "); |
| 621 | xmlDebugDumpString(output, cur->stringval); |
| 622 | fprintf(output, "\n"); |
| 623 | break; |
| 624 | case XPATH_POINT: |
| 625 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 626 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 627 | fprintf(output, "\n"); |
| 628 | break; |
| 629 | case XPATH_RANGE: |
| 630 | if ((cur->user2 == NULL) || |
| 631 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 632 | fprintf(output, "Object is a collapsed range :\n"); |
| 633 | fprintf(output, shift); |
| 634 | if (cur->index >= 0) |
| 635 | fprintf(output, "index %d in ", cur->index); |
| 636 | fprintf(output, "node\n"); |
| 637 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 638 | depth + 1); |
| 639 | } else { |
| 640 | fprintf(output, "Object is a range :\n"); |
| 641 | fprintf(output, shift); |
| 642 | fprintf(output, "From "); |
| 643 | if (cur->index >= 0) |
| 644 | fprintf(output, "index %d in ", cur->index); |
| 645 | fprintf(output, "node\n"); |
| 646 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 647 | depth + 1); |
| 648 | fprintf(output, shift); |
| 649 | fprintf(output, "To "); |
| 650 | if (cur->index2 >= 0) |
| 651 | fprintf(output, "index %d in ", cur->index2); |
| 652 | fprintf(output, "node\n"); |
| 653 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 654 | depth + 1); |
| 655 | fprintf(output, "\n"); |
| 656 | } |
| 657 | break; |
| 658 | case XPATH_LOCATIONSET: |
| 659 | #if defined(LIBXML_XPTR_ENABLED) |
| 660 | fprintf(output, "Object is a Location Set:\n"); |
| 661 | xmlXPathDebugDumpLocationSet(output, |
| 662 | (xmlLocationSetPtr) cur->user, depth); |
| 663 | #endif |
| 664 | break; |
| 665 | case XPATH_USERS: |
| 666 | fprintf(output, "Object is user defined\n"); |
| 667 | break; |
| 668 | } |
| 669 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 670 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 671 | static void |
| 672 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 673 | xmlXPathStepOpPtr op, int depth) { |
| 674 | int i; |
| 675 | char shift[100]; |
| 676 | |
| 677 | for (i = 0;((i < depth) && (i < 25));i++) |
| 678 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 679 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 680 | |
| 681 | fprintf(output, shift); |
| 682 | if (op == NULL) { |
| 683 | fprintf(output, "Step is NULL\n"); |
| 684 | return; |
| 685 | } |
| 686 | switch (op->op) { |
| 687 | case XPATH_OP_END: |
| 688 | fprintf(output, "END"); break; |
| 689 | case XPATH_OP_AND: |
| 690 | fprintf(output, "AND"); break; |
| 691 | case XPATH_OP_OR: |
| 692 | fprintf(output, "OR"); break; |
| 693 | case XPATH_OP_EQUAL: |
| 694 | if (op->value) |
| 695 | fprintf(output, "EQUAL ="); |
| 696 | else |
| 697 | fprintf(output, "EQUAL !="); |
| 698 | break; |
| 699 | case XPATH_OP_CMP: |
| 700 | if (op->value) |
| 701 | fprintf(output, "CMP <"); |
| 702 | else |
| 703 | fprintf(output, "CMP >"); |
| 704 | if (!op->value2) |
| 705 | fprintf(output, "="); |
| 706 | break; |
| 707 | case XPATH_OP_PLUS: |
| 708 | if (op->value == 0) |
| 709 | fprintf(output, "PLUS -"); |
| 710 | else if (op->value == 1) |
| 711 | fprintf(output, "PLUS +"); |
| 712 | else if (op->value == 2) |
| 713 | fprintf(output, "PLUS unary -"); |
| 714 | else if (op->value == 3) |
| 715 | fprintf(output, "PLUS unary - -"); |
| 716 | break; |
| 717 | case XPATH_OP_MULT: |
| 718 | if (op->value == 0) |
| 719 | fprintf(output, "MULT *"); |
| 720 | else if (op->value == 1) |
| 721 | fprintf(output, "MULT div"); |
| 722 | else |
| 723 | fprintf(output, "MULT mod"); |
| 724 | break; |
| 725 | case XPATH_OP_UNION: |
| 726 | fprintf(output, "UNION"); break; |
| 727 | case XPATH_OP_ROOT: |
| 728 | fprintf(output, "ROOT"); break; |
| 729 | case XPATH_OP_NODE: |
| 730 | fprintf(output, "NODE"); break; |
| 731 | case XPATH_OP_RESET: |
| 732 | fprintf(output, "RESET"); break; |
| 733 | case XPATH_OP_SORT: |
| 734 | fprintf(output, "SORT"); break; |
| 735 | case XPATH_OP_COLLECT: { |
| 736 | xmlXPathAxisVal axis = op->value; |
| 737 | xmlXPathTestVal test = op->value2; |
| 738 | xmlXPathTypeVal type = op->value3; |
| 739 | const xmlChar *prefix = op->value4; |
| 740 | const xmlChar *name = op->value5; |
| 741 | |
| 742 | fprintf(output, "COLLECT "); |
| 743 | switch (axis) { |
| 744 | case AXIS_ANCESTOR: |
| 745 | fprintf(output, " 'ancestors' "); break; |
| 746 | case AXIS_ANCESTOR_OR_SELF: |
| 747 | fprintf(output, " 'ancestors-or-self' "); break; |
| 748 | case AXIS_ATTRIBUTE: |
| 749 | fprintf(output, " 'attributes' "); break; |
| 750 | case AXIS_CHILD: |
| 751 | fprintf(output, " 'child' "); break; |
| 752 | case AXIS_DESCENDANT: |
| 753 | fprintf(output, " 'descendant' "); break; |
| 754 | case AXIS_DESCENDANT_OR_SELF: |
| 755 | fprintf(output, " 'descendant-or-self' "); break; |
| 756 | case AXIS_FOLLOWING: |
| 757 | fprintf(output, " 'following' "); break; |
| 758 | case AXIS_FOLLOWING_SIBLING: |
| 759 | fprintf(output, " 'following-siblings' "); break; |
| 760 | case AXIS_NAMESPACE: |
| 761 | fprintf(output, " 'namespace' "); break; |
| 762 | case AXIS_PARENT: |
| 763 | fprintf(output, " 'parent' "); break; |
| 764 | case AXIS_PRECEDING: |
| 765 | fprintf(output, " 'preceding' "); break; |
| 766 | case AXIS_PRECEDING_SIBLING: |
| 767 | fprintf(output, " 'preceding-sibling' "); break; |
| 768 | case AXIS_SELF: |
| 769 | fprintf(output, " 'self' "); break; |
| 770 | } |
| 771 | switch (test) { |
| 772 | case NODE_TEST_NONE: |
| 773 | fprintf(output, "'none' "); break; |
| 774 | case NODE_TEST_TYPE: |
| 775 | fprintf(output, "'type' "); break; |
| 776 | case NODE_TEST_PI: |
| 777 | fprintf(output, "'PI' "); break; |
| 778 | case NODE_TEST_ALL: |
| 779 | fprintf(output, "'all' "); break; |
| 780 | case NODE_TEST_NS: |
| 781 | fprintf(output, "'namespace' "); break; |
| 782 | case NODE_TEST_NAME: |
| 783 | fprintf(output, "'name' "); break; |
| 784 | } |
| 785 | switch (type) { |
| 786 | case NODE_TYPE_NODE: |
| 787 | fprintf(output, "'node' "); break; |
| 788 | case NODE_TYPE_COMMENT: |
| 789 | fprintf(output, "'comment' "); break; |
| 790 | case NODE_TYPE_TEXT: |
| 791 | fprintf(output, "'text' "); break; |
| 792 | case NODE_TYPE_PI: |
| 793 | fprintf(output, "'PI' "); break; |
| 794 | } |
| 795 | if (prefix != NULL) |
| 796 | fprintf(output, "%s:", prefix); |
| 797 | if (name != NULL) |
| 798 | fprintf(output, "%s", name); |
| 799 | break; |
| 800 | |
| 801 | } |
| 802 | case XPATH_OP_VALUE: { |
| 803 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 804 | |
| 805 | fprintf(output, "ELEM "); |
| 806 | xmlXPathDebugDumpObject(output, object, 0); |
| 807 | goto finish; |
| 808 | } |
| 809 | case XPATH_OP_VARIABLE: { |
| 810 | const xmlChar *prefix = op->value5; |
| 811 | const xmlChar *name = op->value4; |
| 812 | |
| 813 | if (prefix != NULL) |
| 814 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 815 | else |
| 816 | fprintf(output, "VARIABLE %s", name); |
| 817 | break; |
| 818 | } |
| 819 | case XPATH_OP_FUNCTION: { |
| 820 | int nbargs = op->value; |
| 821 | const xmlChar *prefix = op->value5; |
| 822 | const xmlChar *name = op->value4; |
| 823 | |
| 824 | if (prefix != NULL) |
| 825 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 826 | prefix, name, nbargs); |
| 827 | else |
| 828 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 829 | break; |
| 830 | } |
| 831 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 832 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 833 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 834 | #ifdef LIBXML_XPTR_ENABLED |
| 835 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 836 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 837 | default: |
| 838 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 839 | } |
| 840 | fprintf(output, "\n"); |
| 841 | finish: |
| 842 | if (op->ch1 >= 0) |
| 843 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 844 | if (op->ch2 >= 0) |
| 845 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 846 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 847 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 848 | /** |
| 849 | * xmlXPathDebugDumpCompExpr: |
| 850 | * @output: the FILE * for the output |
| 851 | * @comp: the precompiled XPath expression |
| 852 | * @depth: the indentation level. |
| 853 | * |
| 854 | * Dumps the tree of the compiled XPath expression. |
| 855 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 856 | void |
| 857 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 858 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 859 | int i; |
| 860 | char shift[100]; |
| 861 | |
| 862 | for (i = 0;((i < depth) && (i < 25));i++) |
| 863 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 864 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 865 | |
| 866 | fprintf(output, shift); |
| 867 | |
| 868 | if (comp == NULL) { |
| 869 | fprintf(output, "Compiled Expression is NULL\n"); |
| 870 | return; |
| 871 | } |
| 872 | fprintf(output, "Compiled Expression : %d elements\n", |
| 873 | comp->nbStep); |
| 874 | i = comp->last; |
| 875 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 876 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 877 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 878 | |
| 879 | /************************************************************************ |
| 880 | * * |
| 881 | * Parser stacks related functions and macros * |
| 882 | * * |
| 883 | ************************************************************************/ |
| 884 | |
| 885 | /* |
| 886 | * Generic function for accessing stacks in the Parser Context |
| 887 | */ |
| 888 | |
| 889 | #define PUSH_AND_POP(type, name) \ |
| 890 | extern int name##Push(xmlXPathParserContextPtr ctxt, type value) { \ |
| 891 | if (ctxt->name##Nr >= ctxt->name##Max) { \ |
| 892 | ctxt->name##Max *= 2; \ |
| 893 | ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ |
| 894 | ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ |
| 895 | if (ctxt->name##Tab == NULL) { \ |
| 896 | xmlGenericError(xmlGenericErrorContext, \ |
| 897 | "realloc failed !\n"); \ |
| 898 | return(0); \ |
| 899 | } \ |
| 900 | } \ |
| 901 | ctxt->name##Tab[ctxt->name##Nr] = value; \ |
| 902 | ctxt->name = value; \ |
| 903 | return(ctxt->name##Nr++); \ |
| 904 | } \ |
| 905 | extern type name##Pop(xmlXPathParserContextPtr ctxt) { \ |
| 906 | type ret; \ |
| 907 | if (ctxt->name##Nr <= 0) return(0); \ |
| 908 | ctxt->name##Nr--; \ |
| 909 | if (ctxt->name##Nr > 0) \ |
| 910 | ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ |
| 911 | else \ |
| 912 | ctxt->name = NULL; \ |
| 913 | ret = ctxt->name##Tab[ctxt->name##Nr]; \ |
| 914 | ctxt->name##Tab[ctxt->name##Nr] = 0; \ |
| 915 | return(ret); \ |
| 916 | } \ |
| 917 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 918 | /** |
| 919 | * valuePop: |
| 920 | * @ctxt: an XPath evaluation context |
| 921 | * |
| 922 | * Pops the top XPath object from the value stack |
| 923 | * |
| 924 | * Returns the XPath object just removed |
| 925 | */ |
| 926 | /** |
| 927 | * valuePush: |
| 928 | * @ctxt: an XPath evaluation context |
| 929 | * @value: the XPath object |
| 930 | * |
| 931 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 932 | * |
| 933 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 934 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | PUSH_AND_POP(xmlXPathObjectPtr, value) |
| 936 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 937 | /** |
| 938 | * xmlXPathPopBoolean: |
| 939 | * @ctxt: an XPath parser context |
| 940 | * |
| 941 | * Pops a boolean from the stack, handling conversion if needed. |
| 942 | * Check error with #xmlXPathCheckError. |
| 943 | * |
| 944 | * Returns the boolean |
| 945 | */ |
| 946 | int |
| 947 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 948 | xmlXPathObjectPtr obj; |
| 949 | int ret; |
| 950 | |
| 951 | obj = valuePop(ctxt); |
| 952 | if (obj == NULL) { |
| 953 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 954 | return(0); |
| 955 | } |
| 956 | ret = xmlXPathCastToBoolean(obj); |
| 957 | xmlXPathFreeObject(obj); |
| 958 | return(ret); |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * xmlXPathPopNumber: |
| 963 | * @ctxt: an XPath parser context |
| 964 | * |
| 965 | * Pops a number from the stack, handling conversion if needed. |
| 966 | * Check error with #xmlXPathCheckError. |
| 967 | * |
| 968 | * Returns the number |
| 969 | */ |
| 970 | double |
| 971 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 972 | xmlXPathObjectPtr obj; |
| 973 | double ret; |
| 974 | |
| 975 | obj = valuePop(ctxt); |
| 976 | if (obj == NULL) { |
| 977 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 978 | return(0); |
| 979 | } |
| 980 | ret = xmlXPathCastToNumber(obj); |
| 981 | xmlXPathFreeObject(obj); |
| 982 | return(ret); |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * xmlXPathPopString: |
| 987 | * @ctxt: an XPath parser context |
| 988 | * |
| 989 | * Pops a string from the stack, handling conversion if needed. |
| 990 | * Check error with #xmlXPathCheckError. |
| 991 | * |
| 992 | * Returns the string |
| 993 | */ |
| 994 | xmlChar * |
| 995 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 996 | xmlXPathObjectPtr obj; |
| 997 | xmlChar * ret; |
| 998 | |
| 999 | obj = valuePop(ctxt); |
| 1000 | if (obj == NULL) { |
| 1001 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1002 | return(NULL); |
| 1003 | } |
| 1004 | ret = xmlXPathCastToString(obj); |
| 1005 | /* TODO: needs refactoring somewhere else */ |
| 1006 | if (obj->stringval == ret) |
| 1007 | obj->stringval = NULL; |
| 1008 | xmlXPathFreeObject(obj); |
| 1009 | return(ret); |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * xmlXPathPopNodeSet: |
| 1014 | * @ctxt: an XPath parser context |
| 1015 | * |
| 1016 | * Pops a node-set from the stack, handling conversion if needed. |
| 1017 | * Check error with #xmlXPathCheckError. |
| 1018 | * |
| 1019 | * Returns the node-set |
| 1020 | */ |
| 1021 | xmlNodeSetPtr |
| 1022 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1023 | xmlXPathObjectPtr obj; |
| 1024 | xmlNodeSetPtr ret; |
| 1025 | |
| 1026 | if (ctxt->value == NULL) { |
| 1027 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1028 | return(NULL); |
| 1029 | } |
| 1030 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1031 | xmlXPathSetTypeError(ctxt); |
| 1032 | return(NULL); |
| 1033 | } |
| 1034 | obj = valuePop(ctxt); |
| 1035 | ret = obj->nodesetval; |
| 1036 | xmlXPathFreeNodeSetList(obj); |
| 1037 | return(ret); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * xmlXPathPopExternal: |
| 1042 | * @ctxt: an XPath parser context |
| 1043 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1044 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1045 | * Check error with #xmlXPathCheckError. |
| 1046 | * |
| 1047 | * Returns the object |
| 1048 | */ |
| 1049 | void * |
| 1050 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1051 | xmlXPathObjectPtr obj; |
| 1052 | void * ret; |
| 1053 | |
| 1054 | if (ctxt->value == NULL) { |
| 1055 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1056 | return(NULL); |
| 1057 | } |
| 1058 | if (ctxt->value->type != XPATH_USERS) { |
| 1059 | xmlXPathSetTypeError(ctxt); |
| 1060 | return(NULL); |
| 1061 | } |
| 1062 | obj = valuePop(ctxt); |
| 1063 | ret = obj->user; |
| 1064 | xmlXPathFreeObject(obj); |
| 1065 | return(ret); |
| 1066 | } |
| 1067 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | /* |
| 1069 | * Macros for accessing the content. Those should be used only by the parser, |
| 1070 | * and not exported. |
| 1071 | * |
| 1072 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1073 | * |
| 1074 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1075 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1076 | * in ISO-Latin or UTF-8. |
| 1077 | * This should be used internally by the parser |
| 1078 | * only to compare to ASCII values otherwise it would break when |
| 1079 | * running with UTF-8 encoding. |
| 1080 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1081 | * to compare on ASCII based substring. |
| 1082 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1083 | * strings within the parser. |
| 1084 | * CURRENT Returns the current char value, with the full decoding of |
| 1085 | * UTF-8 if we are using this mode. It returns an int. |
| 1086 | * NEXT Skip to the next character, this does the proper decoding |
| 1087 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1088 | * It returns the pointer to the current xmlChar. |
| 1089 | */ |
| 1090 | |
| 1091 | #define CUR (*ctxt->cur) |
| 1092 | #define SKIP(val) ctxt->cur += (val) |
| 1093 | #define NXT(val) ctxt->cur[(val)] |
| 1094 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1095 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1096 | |
| 1097 | #define COPY_BUF(l,b,i,v) \ |
| 1098 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1099 | else i += xmlCopyChar(l,&b[i],v) |
| 1100 | |
| 1101 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1102 | |
| 1103 | #define SKIP_BLANKS \ |
| 1104 | while (IS_BLANK(*(ctxt->cur))) NEXT |
| 1105 | |
| 1106 | #define CURRENT (*ctxt->cur) |
| 1107 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1108 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1109 | |
| 1110 | #ifndef DBL_DIG |
| 1111 | #define DBL_DIG 16 |
| 1112 | #endif |
| 1113 | #ifndef DBL_EPSILON |
| 1114 | #define DBL_EPSILON 1E-9 |
| 1115 | #endif |
| 1116 | |
| 1117 | #define UPPER_DOUBLE 1E9 |
| 1118 | #define LOWER_DOUBLE 1E-5 |
| 1119 | |
| 1120 | #define INTEGER_DIGITS DBL_DIG |
| 1121 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1122 | #define EXPONENT_DIGITS (3 + 2) |
| 1123 | |
| 1124 | /** |
| 1125 | * xmlXPathFormatNumber: |
| 1126 | * @number: number to format |
| 1127 | * @buffer: output buffer |
| 1128 | * @buffersize: size of output buffer |
| 1129 | * |
| 1130 | * Convert the number into a string representation. |
| 1131 | */ |
| 1132 | static void |
| 1133 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1134 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1135 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1136 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1137 | if (buffersize > (int)sizeof("Infinity")) |
| 1138 | sprintf(buffer, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | case -1: |
| 1141 | if (buffersize > (int)sizeof("-Infinity")) |
| 1142 | sprintf(buffer, "-Infinity"); |
| 1143 | break; |
| 1144 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1145 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1146 | if (buffersize > (int)sizeof("NaN")) |
| 1147 | sprintf(buffer, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1148 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
| 1149 | sprintf(buffer, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1150 | } else if (number == ((int) number)) { |
| 1151 | char work[30]; |
| 1152 | char *ptr, *cur; |
| 1153 | int res, value = (int) number; |
| 1154 | |
| 1155 | ptr = &buffer[0]; |
| 1156 | if (value < 0) { |
| 1157 | *ptr++ = '-'; |
| 1158 | value = -value; |
| 1159 | } |
| 1160 | if (value == 0) { |
| 1161 | *ptr++ = '0'; |
| 1162 | } else { |
| 1163 | cur = &work[0]; |
| 1164 | while (value != 0) { |
| 1165 | res = value % 10; |
| 1166 | value = value / 10; |
| 1167 | *cur++ = '0' + res; |
| 1168 | } |
| 1169 | cur--; |
| 1170 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1171 | *ptr++ = *cur--; |
| 1172 | } |
| 1173 | } |
| 1174 | if (ptr - buffer < buffersize) { |
| 1175 | *ptr = 0; |
| 1176 | } else if (buffersize > 0) { |
| 1177 | ptr--; |
| 1178 | *ptr = 0; |
| 1179 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1180 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1181 | /* 3 is sign, decimal point, and terminating zero */ |
| 1182 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1183 | int integer_place, fraction_place; |
| 1184 | char *ptr; |
| 1185 | char *after_fraction; |
| 1186 | double absolute_value; |
| 1187 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1188 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1189 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1190 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1191 | /* |
| 1192 | * First choose format - scientific or regular floating point. |
| 1193 | * In either case, result is in work, and after_fraction points |
| 1194 | * just past the fractional part. |
| 1195 | */ |
| 1196 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1197 | (absolute_value < LOWER_DOUBLE)) && |
| 1198 | (absolute_value != 0.0) ) { |
| 1199 | /* Use scientific notation */ |
| 1200 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1201 | fraction_place = DBL_DIG - 1; |
| 1202 | snprintf(work, sizeof(work),"%*.*e", |
| 1203 | integer_place, fraction_place, number); |
| 1204 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1205 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1206 | else { |
| 1207 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1208 | if (absolute_value > 0.0) |
| 1209 | integer_place = 1 + (int)log10(absolute_value); |
| 1210 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1211 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1212 | fraction_place = (integer_place > 0) |
| 1213 | ? DBL_DIG - integer_place |
| 1214 | : DBL_DIG; |
| 1215 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1216 | fraction_place, number); |
| 1217 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1220 | /* Remove fractional trailing zeroes */ |
| 1221 | ptr = after_fraction; |
| 1222 | while (*(--ptr) == '0') |
| 1223 | ; |
| 1224 | if (*ptr != '.') |
| 1225 | ptr++; |
| 1226 | strcpy(ptr, after_fraction); |
| 1227 | |
| 1228 | /* Finally copy result back to caller */ |
| 1229 | size = strlen(work) + 1; |
| 1230 | if (size > buffersize) { |
| 1231 | work[buffersize - 1] = 0; |
| 1232 | size = buffersize; |
| 1233 | } |
| 1234 | memcpy(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1235 | } |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
| 1239 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1240 | /************************************************************************ |
| 1241 | * * |
| 1242 | * Error handling routines * |
| 1243 | * * |
| 1244 | ************************************************************************/ |
| 1245 | |
| 1246 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 1247 | static const char *xmlXPathErrorMessages[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1248 | "Ok", |
| 1249 | "Number encoding", |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1250 | "Unfinished literal", |
| 1251 | "Start of literal", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1252 | "Expected $ for variable reference", |
| 1253 | "Undefined variable", |
| 1254 | "Invalid predicate", |
| 1255 | "Invalid expression", |
| 1256 | "Missing closing curly brace", |
| 1257 | "Unregistered function", |
| 1258 | "Invalid operand", |
| 1259 | "Invalid type", |
| 1260 | "Invalid number of arguments", |
| 1261 | "Invalid context size", |
| 1262 | "Invalid context position", |
| 1263 | "Memory allocation error", |
| 1264 | "Syntax error", |
| 1265 | "Resource error", |
| 1266 | "Sub resource error", |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1267 | "Undefined namespace prefix", |
| 1268 | "Encoding error", |
| 1269 | "Char out of XML range" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1270 | }; |
| 1271 | |
| 1272 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1273 | * xmlXPatherror: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1274 | * @ctxt: the XPath Parser context |
| 1275 | * @file: the file name |
| 1276 | * @line: the line number |
| 1277 | * @no: the error number |
| 1278 | * |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1279 | * Formats an error message. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1280 | */ |
| 1281 | void |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 1282 | xmlXPatherror(xmlXPathParserContextPtr ctxt, ATTRIBUTE_UNUSED const char *file, |
| 1283 | ATTRIBUTE_UNUSED int line, int no) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1284 | int n; |
| 1285 | const xmlChar *cur; |
| 1286 | const xmlChar *base; |
| 1287 | |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1288 | /* xmlGenericError(xmlGenericErrorContext, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1289 | "Error %s:%d: %s\n", file, line, |
| 1290 | xmlXPathErrorMessages[no]); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1291 | */ |
| 1292 | xmlGenericError(xmlGenericErrorContext, |
| 1293 | "Error %s\n", xmlXPathErrorMessages[no]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | |
| 1295 | cur = ctxt->cur; |
| 1296 | base = ctxt->base; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1297 | if ((cur == NULL) || (base == NULL)) |
| 1298 | return; |
| 1299 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 1301 | cur--; |
| 1302 | } |
| 1303 | n = 0; |
| 1304 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 1305 | cur--; |
| 1306 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 1307 | base = cur; |
| 1308 | n = 0; |
| 1309 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
| 1310 | xmlGenericError(xmlGenericErrorContext, "%c", (unsigned char) *cur++); |
| 1311 | n++; |
| 1312 | } |
| 1313 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 1314 | cur = ctxt->cur; |
| 1315 | while ((*cur == '\n') || (*cur == '\r')) |
| 1316 | cur--; |
| 1317 | n = 0; |
| 1318 | while ((cur != base) && (n++ < 80)) { |
| 1319 | xmlGenericError(xmlGenericErrorContext, " "); |
| 1320 | base++; |
| 1321 | } |
| 1322 | xmlGenericError(xmlGenericErrorContext,"^\n"); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | /************************************************************************ |
| 1327 | * * |
| 1328 | * Routines to handle NodeSets * |
| 1329 | * * |
| 1330 | ************************************************************************/ |
| 1331 | |
| 1332 | /** |
| 1333 | * xmlXPathCmpNodes: |
| 1334 | * @node1: the first node |
| 1335 | * @node2: the second node |
| 1336 | * |
| 1337 | * Compare two nodes w.r.t document order |
| 1338 | * |
| 1339 | * Returns -2 in case of error 1 if first point < second point, 0 if |
| 1340 | * that's the same node, -1 otherwise |
| 1341 | */ |
| 1342 | int |
| 1343 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1344 | int depth1, depth2; |
| 1345 | xmlNodePtr cur, root; |
| 1346 | |
| 1347 | if ((node1 == NULL) || (node2 == NULL)) |
| 1348 | return(-2); |
| 1349 | /* |
| 1350 | * a couple of optimizations which will avoid computations in most cases |
| 1351 | */ |
| 1352 | if (node1 == node2) |
| 1353 | return(0); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1354 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1355 | (node2->type == XML_NAMESPACE_DECL)) |
| 1356 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | if (node1 == node2->prev) |
| 1358 | return(1); |
| 1359 | if (node1 == node2->next) |
| 1360 | return(-1); |
| 1361 | |
| 1362 | /* |
| 1363 | * compute depth to root |
| 1364 | */ |
| 1365 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1366 | if (cur == node1) |
| 1367 | return(1); |
| 1368 | depth2++; |
| 1369 | } |
| 1370 | root = cur; |
| 1371 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1372 | if (cur == node2) |
| 1373 | return(-1); |
| 1374 | depth1++; |
| 1375 | } |
| 1376 | /* |
| 1377 | * Distinct document (or distinct entities :-( ) case. |
| 1378 | */ |
| 1379 | if (root != cur) { |
| 1380 | return(-2); |
| 1381 | } |
| 1382 | /* |
| 1383 | * get the nearest common ancestor. |
| 1384 | */ |
| 1385 | while (depth1 > depth2) { |
| 1386 | depth1--; |
| 1387 | node1 = node1->parent; |
| 1388 | } |
| 1389 | while (depth2 > depth1) { |
| 1390 | depth2--; |
| 1391 | node2 = node2->parent; |
| 1392 | } |
| 1393 | while (node1->parent != node2->parent) { |
| 1394 | node1 = node1->parent; |
| 1395 | node2 = node2->parent; |
| 1396 | /* should not happen but just in case ... */ |
| 1397 | if ((node1 == NULL) || (node2 == NULL)) |
| 1398 | return(-2); |
| 1399 | } |
| 1400 | /* |
| 1401 | * Find who's first. |
| 1402 | */ |
| 1403 | if (node1 == node2->next) |
| 1404 | return(-1); |
| 1405 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1406 | if (cur == node2) |
| 1407 | return(1); |
| 1408 | return(-1); /* assume there is no sibling list corruption */ |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * xmlXPathNodeSetSort: |
| 1413 | * @set: the node set |
| 1414 | * |
| 1415 | * Sort the node set in document order |
| 1416 | */ |
| 1417 | void |
| 1418 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1419 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1420 | xmlNodePtr tmp; |
| 1421 | |
| 1422 | if (set == NULL) |
| 1423 | return; |
| 1424 | |
| 1425 | /* Use Shell's sort to sort the node-set */ |
| 1426 | len = set->nodeNr; |
| 1427 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1428 | for (i = incr; i < len; i++) { |
| 1429 | j = i - incr; |
| 1430 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1431 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1432 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | tmp = set->nodeTab[j]; |
| 1434 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1435 | set->nodeTab[j + incr] = tmp; |
| 1436 | j -= incr; |
| 1437 | } else |
| 1438 | break; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | #define XML_NODESET_DEFAULT 10 |
| 1445 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1446 | * xmlXPathNodeSetDupNs: |
| 1447 | * @node: the parent node of the namespace XPath node |
| 1448 | * @ns: the libxml namespace declaration node. |
| 1449 | * |
| 1450 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1451 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1452 | * parent node in the XPath semantic. |
| 1453 | * |
| 1454 | * Returns the newly created object. |
| 1455 | */ |
| 1456 | static xmlNodePtr |
| 1457 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1458 | xmlNsPtr cur; |
| 1459 | |
| 1460 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1461 | return(NULL); |
| 1462 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1463 | return((xmlNodePtr) ns); |
| 1464 | |
| 1465 | /* |
| 1466 | * Allocate a new Namespace and fill the fields. |
| 1467 | */ |
| 1468 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1469 | if (cur == NULL) { |
| 1470 | xmlGenericError(xmlGenericErrorContext, |
| 1471 | "xmlXPathNodeSetDupNs : malloc failed\n"); |
| 1472 | return(NULL); |
| 1473 | } |
| 1474 | memset(cur, 0, sizeof(xmlNs)); |
| 1475 | cur->type = XML_NAMESPACE_DECL; |
| 1476 | if (ns->href != NULL) |
| 1477 | cur->href = xmlStrdup(ns->href); |
| 1478 | if (ns->prefix != NULL) |
| 1479 | cur->prefix = xmlStrdup(ns->prefix); |
| 1480 | cur->next = (xmlNsPtr) node; |
| 1481 | return((xmlNodePtr) cur); |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * xmlXPathNodeSetFreeNs: |
| 1486 | * @ns: the XPath namespace node found in a nodeset. |
| 1487 | * |
| 1488 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1489 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1490 | * parent node in the XPath semantic. Check if such a node need to be freed |
| 1491 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1492 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1493 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1494 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1495 | return; |
| 1496 | |
| 1497 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1498 | if (ns->href != NULL) |
| 1499 | xmlFree((xmlChar *)ns->href); |
| 1500 | if (ns->prefix != NULL) |
| 1501 | xmlFree((xmlChar *)ns->prefix); |
| 1502 | xmlFree(ns); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1507 | * xmlXPathNodeSetCreate: |
| 1508 | * @val: an initial xmlNodePtr, or NULL |
| 1509 | * |
| 1510 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1511 | * |
| 1512 | * Returns the newly created object. |
| 1513 | */ |
| 1514 | xmlNodeSetPtr |
| 1515 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1516 | xmlNodeSetPtr ret; |
| 1517 | |
| 1518 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1519 | if (ret == NULL) { |
| 1520 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1521 | "xmlXPathNodeSetCreate: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1522 | return(NULL); |
| 1523 | } |
| 1524 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1525 | if (val != NULL) { |
| 1526 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1527 | sizeof(xmlNodePtr)); |
| 1528 | if (ret->nodeTab == NULL) { |
| 1529 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1530 | "xmlXPathNodeSetCreate: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1531 | return(NULL); |
| 1532 | } |
| 1533 | memset(ret->nodeTab, 0 , |
| 1534 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1535 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1536 | if (val->type == XML_NAMESPACE_DECL) { |
| 1537 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1538 | |
| 1539 | ret->nodeTab[ret->nodeNr++] = |
| 1540 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1541 | } else |
| 1542 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1543 | } |
| 1544 | return(ret); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1548 | * xmlXPathNodeSetContains: |
| 1549 | * @cur: the node-set |
| 1550 | * @val: the node |
| 1551 | * |
| 1552 | * checks whether @cur contains @val |
| 1553 | * |
| 1554 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1555 | */ |
| 1556 | int |
| 1557 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1558 | int i; |
| 1559 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1560 | if (val->type == XML_NAMESPACE_DECL) { |
| 1561 | for (i = 0; i < cur->nodeNr; i++) { |
| 1562 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1563 | xmlNsPtr ns1, ns2; |
| 1564 | |
| 1565 | ns1 = (xmlNsPtr) val; |
| 1566 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1567 | if (ns1 == ns2) |
| 1568 | return(1); |
| 1569 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1570 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1571 | return(1); |
| 1572 | } |
| 1573 | } |
| 1574 | } else { |
| 1575 | for (i = 0; i < cur->nodeNr; i++) { |
| 1576 | if (cur->nodeTab[i] == val) |
| 1577 | return(1); |
| 1578 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1579 | } |
| 1580 | return(0); |
| 1581 | } |
| 1582 | |
| 1583 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1584 | * xmlXPathNodeSetAddNs: |
| 1585 | * @cur: the initial node set |
| 1586 | * @node: the hosting node |
| 1587 | * @ns: a the namespace node |
| 1588 | * |
| 1589 | * add a new namespace node to an existing NodeSet |
| 1590 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1591 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1592 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1593 | int i; |
| 1594 | |
| 1595 | if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) || |
| 1596 | (node->type != XML_ELEMENT_NODE)) |
| 1597 | return; |
| 1598 | |
| 1599 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
| 1600 | /* |
| 1601 | * check against doublons |
| 1602 | */ |
| 1603 | for (i = 0;i < cur->nodeNr;i++) { |
| 1604 | if ((cur->nodeTab[i] != NULL) && |
| 1605 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1606 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1607 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1608 | return; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * grow the nodeTab if needed |
| 1613 | */ |
| 1614 | if (cur->nodeMax == 0) { |
| 1615 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1616 | sizeof(xmlNodePtr)); |
| 1617 | if (cur->nodeTab == NULL) { |
| 1618 | xmlGenericError(xmlGenericErrorContext, |
| 1619 | "xmlXPathNodeSetAdd: out of memory\n"); |
| 1620 | return; |
| 1621 | } |
| 1622 | memset(cur->nodeTab, 0 , |
| 1623 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1624 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1625 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1626 | xmlNodePtr *temp; |
| 1627 | |
| 1628 | cur->nodeMax *= 2; |
| 1629 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1630 | sizeof(xmlNodePtr)); |
| 1631 | if (temp == NULL) { |
| 1632 | xmlGenericError(xmlGenericErrorContext, |
| 1633 | "xmlXPathNodeSetAdd: out of memory\n"); |
| 1634 | return; |
| 1635 | } |
| 1636 | cur->nodeTab = temp; |
| 1637 | } |
| 1638 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1639 | } |
| 1640 | |
| 1641 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1642 | * xmlXPathNodeSetAdd: |
| 1643 | * @cur: the initial node set |
| 1644 | * @val: a new xmlNodePtr |
| 1645 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1646 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1647 | */ |
| 1648 | void |
| 1649 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1650 | int i; |
| 1651 | |
| 1652 | if (val == NULL) return; |
| 1653 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1654 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1655 | /* |
| 1656 | * check against doublons |
| 1657 | */ |
| 1658 | for (i = 0;i < cur->nodeNr;i++) |
| 1659 | if (cur->nodeTab[i] == val) return; |
| 1660 | |
| 1661 | /* |
| 1662 | * grow the nodeTab if needed |
| 1663 | */ |
| 1664 | if (cur->nodeMax == 0) { |
| 1665 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1666 | sizeof(xmlNodePtr)); |
| 1667 | if (cur->nodeTab == NULL) { |
| 1668 | xmlGenericError(xmlGenericErrorContext, |
| 1669 | "xmlXPathNodeSetAdd: out of memory\n"); |
| 1670 | return; |
| 1671 | } |
| 1672 | memset(cur->nodeTab, 0 , |
| 1673 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1674 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1675 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1676 | xmlNodePtr *temp; |
| 1677 | |
| 1678 | cur->nodeMax *= 2; |
| 1679 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1680 | sizeof(xmlNodePtr)); |
| 1681 | if (temp == NULL) { |
| 1682 | xmlGenericError(xmlGenericErrorContext, |
| 1683 | "xmlXPathNodeSetAdd: out of memory\n"); |
| 1684 | return; |
| 1685 | } |
| 1686 | cur->nodeTab = temp; |
| 1687 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1688 | if (val->type == XML_NAMESPACE_DECL) { |
| 1689 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1690 | |
| 1691 | cur->nodeTab[cur->nodeNr++] = |
| 1692 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1693 | } else |
| 1694 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * xmlXPathNodeSetAddUnique: |
| 1699 | * @cur: the initial node set |
| 1700 | * @val: a new xmlNodePtr |
| 1701 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1702 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1703 | * when we are sure the node is not already in the set. |
| 1704 | */ |
| 1705 | void |
| 1706 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1707 | if (val == NULL) return; |
| 1708 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1709 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1710 | /* |
| 1711 | * grow the nodeTab if needed |
| 1712 | */ |
| 1713 | if (cur->nodeMax == 0) { |
| 1714 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1715 | sizeof(xmlNodePtr)); |
| 1716 | if (cur->nodeTab == NULL) { |
| 1717 | xmlGenericError(xmlGenericErrorContext, |
| 1718 | "xmlXPathNodeSetAddUnique: out of memory\n"); |
| 1719 | return; |
| 1720 | } |
| 1721 | memset(cur->nodeTab, 0 , |
| 1722 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1723 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1724 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1725 | xmlNodePtr *temp; |
| 1726 | |
| 1727 | cur->nodeMax *= 2; |
| 1728 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1729 | sizeof(xmlNodePtr)); |
| 1730 | if (temp == NULL) { |
| 1731 | xmlGenericError(xmlGenericErrorContext, |
| 1732 | "xmlXPathNodeSetAddUnique: out of memory\n"); |
| 1733 | return; |
| 1734 | } |
| 1735 | cur->nodeTab = temp; |
| 1736 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1737 | if (val->type == XML_NAMESPACE_DECL) { |
| 1738 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1739 | |
| 1740 | cur->nodeTab[cur->nodeNr++] = |
| 1741 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1742 | } else |
| 1743 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * xmlXPathNodeSetMerge: |
| 1748 | * @val1: the first NodeSet or NULL |
| 1749 | * @val2: the second NodeSet |
| 1750 | * |
| 1751 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 1752 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 1753 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1754 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1755 | */ |
| 1756 | xmlNodeSetPtr |
| 1757 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1758 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1759 | |
| 1760 | if (val2 == NULL) return(val1); |
| 1761 | if (val1 == NULL) { |
| 1762 | val1 = xmlXPathNodeSetCreate(NULL); |
| 1763 | } |
| 1764 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1765 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | initNr = val1->nodeNr; |
| 1767 | |
| 1768 | for (i = 0;i < val2->nodeNr;i++) { |
| 1769 | /* |
| 1770 | * check against doublons |
| 1771 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1772 | skip = 0; |
| 1773 | for (j = 0; j < initNr; j++) { |
| 1774 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 1775 | skip = 1; |
| 1776 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1777 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 1778 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 1779 | xmlNsPtr ns1, ns2; |
| 1780 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 1781 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 1782 | if ((ns1->next == ns2->next) && |
| 1783 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 1784 | skip = 1; |
| 1785 | break; |
| 1786 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | if (skip) |
| 1790 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1791 | |
| 1792 | /* |
| 1793 | * grow the nodeTab if needed |
| 1794 | */ |
| 1795 | if (val1->nodeMax == 0) { |
| 1796 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1797 | sizeof(xmlNodePtr)); |
| 1798 | if (val1->nodeTab == NULL) { |
| 1799 | xmlGenericError(xmlGenericErrorContext, |
| 1800 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 1801 | return(NULL); |
| 1802 | } |
| 1803 | memset(val1->nodeTab, 0 , |
| 1804 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1805 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 1806 | } else if (val1->nodeNr == val1->nodeMax) { |
| 1807 | xmlNodePtr *temp; |
| 1808 | |
| 1809 | val1->nodeMax *= 2; |
| 1810 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 1811 | sizeof(xmlNodePtr)); |
| 1812 | if (temp == NULL) { |
| 1813 | xmlGenericError(xmlGenericErrorContext, |
| 1814 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 1815 | return(NULL); |
| 1816 | } |
| 1817 | val1->nodeTab = temp; |
| 1818 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1819 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1820 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 1821 | |
| 1822 | val1->nodeTab[val1->nodeNr++] = |
| 1823 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1824 | } else |
| 1825 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | return(val1); |
| 1829 | } |
| 1830 | |
| 1831 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 1832 | * xmlXPathNodeSetMergeUnique: |
| 1833 | * @val1: the first NodeSet or NULL |
| 1834 | * @val2: the second NodeSet |
| 1835 | * |
| 1836 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 1837 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 1838 | * |
| 1839 | * Returns @val1 once extended or NULL in case of error. |
| 1840 | */ |
| 1841 | static xmlNodeSetPtr |
| 1842 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
| 1843 | int i, initNr; |
| 1844 | |
| 1845 | if (val2 == NULL) return(val1); |
| 1846 | if (val1 == NULL) { |
| 1847 | val1 = xmlXPathNodeSetCreate(NULL); |
| 1848 | } |
| 1849 | |
| 1850 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
| 1851 | initNr = val1->nodeNr; |
| 1852 | |
| 1853 | for (i = 0;i < val2->nodeNr;i++) { |
| 1854 | /* |
| 1855 | * grow the nodeTab if needed |
| 1856 | */ |
| 1857 | if (val1->nodeMax == 0) { |
| 1858 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1859 | sizeof(xmlNodePtr)); |
| 1860 | if (val1->nodeTab == NULL) { |
| 1861 | xmlGenericError(xmlGenericErrorContext, |
| 1862 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 1863 | return(NULL); |
| 1864 | } |
| 1865 | memset(val1->nodeTab, 0 , |
| 1866 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1867 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 1868 | } else if (val1->nodeNr == val1->nodeMax) { |
| 1869 | xmlNodePtr *temp; |
| 1870 | |
| 1871 | val1->nodeMax *= 2; |
| 1872 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 1873 | sizeof(xmlNodePtr)); |
| 1874 | if (temp == NULL) { |
| 1875 | xmlGenericError(xmlGenericErrorContext, |
| 1876 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 1877 | return(NULL); |
| 1878 | } |
| 1879 | val1->nodeTab = temp; |
| 1880 | } |
| 1881 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1882 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 1883 | |
| 1884 | val1->nodeTab[val1->nodeNr++] = |
| 1885 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1886 | } else |
| 1887 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 1888 | } |
| 1889 | |
| 1890 | return(val1); |
| 1891 | } |
| 1892 | |
| 1893 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | * xmlXPathNodeSetDel: |
| 1895 | * @cur: the initial node set |
| 1896 | * @val: an xmlNodePtr |
| 1897 | * |
| 1898 | * Removes an xmlNodePtr from an existing NodeSet |
| 1899 | */ |
| 1900 | void |
| 1901 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1902 | int i; |
| 1903 | |
| 1904 | if (cur == NULL) return; |
| 1905 | if (val == NULL) return; |
| 1906 | |
| 1907 | /* |
| 1908 | * check against doublons |
| 1909 | */ |
| 1910 | for (i = 0;i < cur->nodeNr;i++) |
| 1911 | if (cur->nodeTab[i] == val) break; |
| 1912 | |
| 1913 | if (i >= cur->nodeNr) { |
| 1914 | #ifdef DEBUG |
| 1915 | xmlGenericError(xmlGenericErrorContext, |
| 1916 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 1917 | val->name); |
| 1918 | #endif |
| 1919 | return; |
| 1920 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1921 | if ((cur->nodeTab[i] != NULL) && |
| 1922 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 1923 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1924 | cur->nodeNr--; |
| 1925 | for (;i < cur->nodeNr;i++) |
| 1926 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 1927 | cur->nodeTab[cur->nodeNr] = NULL; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
| 1931 | * xmlXPathNodeSetRemove: |
| 1932 | * @cur: the initial node set |
| 1933 | * @val: the index to remove |
| 1934 | * |
| 1935 | * Removes an entry from an existing NodeSet list. |
| 1936 | */ |
| 1937 | void |
| 1938 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 1939 | if (cur == NULL) return; |
| 1940 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1941 | if ((cur->nodeTab[val] != NULL) && |
| 1942 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 1943 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | cur->nodeNr--; |
| 1945 | for (;val < cur->nodeNr;val++) |
| 1946 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 1947 | cur->nodeTab[cur->nodeNr] = NULL; |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * xmlXPathFreeNodeSet: |
| 1952 | * @obj: the xmlNodeSetPtr to free |
| 1953 | * |
| 1954 | * Free the NodeSet compound (not the actual nodes !). |
| 1955 | */ |
| 1956 | void |
| 1957 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 1958 | if (obj == NULL) return; |
| 1959 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1960 | int i; |
| 1961 | |
| 1962 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
| 1963 | for (i = 0;i < obj->nodeNr;i++) |
| 1964 | if ((obj->nodeTab[i] != NULL) && |
| 1965 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 1966 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1967 | xmlFree(obj->nodeTab); |
| 1968 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1969 | xmlFree(obj); |
| 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * xmlXPathFreeValueTree: |
| 1974 | * @obj: the xmlNodeSetPtr to free |
| 1975 | * |
| 1976 | * Free the NodeSet compound and the actual tree, this is different |
| 1977 | * from xmlXPathFreeNodeSet() |
| 1978 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1979 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 1981 | int i; |
| 1982 | |
| 1983 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1984 | |
| 1985 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1986 | for (i = 0;i < obj->nodeNr;i++) { |
| 1987 | if (obj->nodeTab[i] != NULL) { |
| 1988 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1989 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 1990 | } else { |
| 1991 | xmlFreeNodeList(obj->nodeTab[i]); |
| 1992 | } |
| 1993 | } |
| 1994 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1995 | xmlFree(obj->nodeTab); |
| 1996 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1997 | xmlFree(obj); |
| 1998 | } |
| 1999 | |
| 2000 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2001 | /** |
| 2002 | * xmlGenericErrorContextNodeSet: |
| 2003 | * @output: a FILE * for the output |
| 2004 | * @obj: the xmlNodeSetPtr to free |
| 2005 | * |
| 2006 | * Quick display of a NodeSet |
| 2007 | */ |
| 2008 | void |
| 2009 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2010 | int i; |
| 2011 | |
| 2012 | if (output == NULL) output = xmlGenericErrorContext; |
| 2013 | if (obj == NULL) { |
| 2014 | fprintf(output, "NodeSet == NULL !\n"); |
| 2015 | return; |
| 2016 | } |
| 2017 | if (obj->nodeNr == 0) { |
| 2018 | fprintf(output, "NodeSet is empty\n"); |
| 2019 | return; |
| 2020 | } |
| 2021 | if (obj->nodeTab == NULL) { |
| 2022 | fprintf(output, " nodeTab == NULL !\n"); |
| 2023 | return; |
| 2024 | } |
| 2025 | for (i = 0; i < obj->nodeNr; i++) { |
| 2026 | if (obj->nodeTab[i] == NULL) { |
| 2027 | fprintf(output, " NULL !\n"); |
| 2028 | return; |
| 2029 | } |
| 2030 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2031 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2032 | fprintf(output, " /"); |
| 2033 | else if (obj->nodeTab[i]->name == NULL) |
| 2034 | fprintf(output, " noname!"); |
| 2035 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2036 | } |
| 2037 | fprintf(output, "\n"); |
| 2038 | } |
| 2039 | #endif |
| 2040 | |
| 2041 | /** |
| 2042 | * xmlXPathNewNodeSet: |
| 2043 | * @val: the NodePtr value |
| 2044 | * |
| 2045 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2046 | * it with the single Node @val |
| 2047 | * |
| 2048 | * Returns the newly created object. |
| 2049 | */ |
| 2050 | xmlXPathObjectPtr |
| 2051 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2052 | xmlXPathObjectPtr ret; |
| 2053 | |
| 2054 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2055 | if (ret == NULL) { |
| 2056 | xmlGenericError(xmlGenericErrorContext, |
| 2057 | "xmlXPathNewNodeSet: out of memory\n"); |
| 2058 | return(NULL); |
| 2059 | } |
| 2060 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2061 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2062 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2063 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2064 | /* @@ with_ns to check wether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2065 | return(ret); |
| 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * xmlXPathNewValueTree: |
| 2070 | * @val: the NodePtr value |
| 2071 | * |
| 2072 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2073 | * it with the tree root @val |
| 2074 | * |
| 2075 | * Returns the newly created object. |
| 2076 | */ |
| 2077 | xmlXPathObjectPtr |
| 2078 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2079 | xmlXPathObjectPtr ret; |
| 2080 | |
| 2081 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2082 | if (ret == NULL) { |
| 2083 | xmlGenericError(xmlGenericErrorContext, |
| 2084 | "xmlXPathNewNodeSet: out of memory\n"); |
| 2085 | return(NULL); |
| 2086 | } |
| 2087 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2088 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2089 | ret->boolval = 1; |
| 2090 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2091 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2092 | return(ret); |
| 2093 | } |
| 2094 | |
| 2095 | /** |
| 2096 | * xmlXPathNewNodeSetList: |
| 2097 | * @val: an existing NodeSet |
| 2098 | * |
| 2099 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2100 | * it with the Nodeset @val |
| 2101 | * |
| 2102 | * Returns the newly created object. |
| 2103 | */ |
| 2104 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2105 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2106 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2107 | xmlXPathObjectPtr ret; |
| 2108 | int i; |
| 2109 | |
| 2110 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2111 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2112 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2113 | ret = xmlXPathNewNodeSet(NULL); |
| 2114 | else { |
| 2115 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2116 | for (i = 1; i < val->nodeNr; ++i) |
| 2117 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2118 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2119 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2120 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | * xmlXPathWrapNodeSet: |
| 2125 | * @val: the NodePtr value |
| 2126 | * |
| 2127 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2128 | * |
| 2129 | * Returns the newly created object. |
| 2130 | */ |
| 2131 | xmlXPathObjectPtr |
| 2132 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2133 | xmlXPathObjectPtr ret; |
| 2134 | |
| 2135 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2136 | if (ret == NULL) { |
| 2137 | xmlGenericError(xmlGenericErrorContext, |
| 2138 | "xmlXPathWrapNodeSet: out of memory\n"); |
| 2139 | return(NULL); |
| 2140 | } |
| 2141 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2142 | ret->type = XPATH_NODESET; |
| 2143 | ret->nodesetval = val; |
| 2144 | return(ret); |
| 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * xmlXPathFreeNodeSetList: |
| 2149 | * @obj: an existing NodeSetList object |
| 2150 | * |
| 2151 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2152 | * the list contrary to xmlXPathFreeObject(). |
| 2153 | */ |
| 2154 | void |
| 2155 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2156 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2157 | xmlFree(obj); |
| 2158 | } |
| 2159 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2160 | /** |
| 2161 | * xmlXPathDifference: |
| 2162 | * @nodes1: a node-set |
| 2163 | * @nodes2: a node-set |
| 2164 | * |
| 2165 | * Implements the EXSLT - Sets difference() function: |
| 2166 | * node-set set:difference (node-set, node-set) |
| 2167 | * |
| 2168 | * Returns the difference between the two node sets, or nodes1 if |
| 2169 | * nodes2 is empty |
| 2170 | */ |
| 2171 | xmlNodeSetPtr |
| 2172 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2173 | xmlNodeSetPtr ret; |
| 2174 | int i, l1; |
| 2175 | xmlNodePtr cur; |
| 2176 | |
| 2177 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2178 | return(nodes1); |
| 2179 | |
| 2180 | ret = xmlXPathNodeSetCreate(NULL); |
| 2181 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2182 | return(ret); |
| 2183 | |
| 2184 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2185 | |
| 2186 | for (i = 0; i < l1; i++) { |
| 2187 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2188 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2189 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2190 | } |
| 2191 | return(ret); |
| 2192 | } |
| 2193 | |
| 2194 | /** |
| 2195 | * xmlXPathIntersection: |
| 2196 | * @nodes1: a node-set |
| 2197 | * @nodes2: a node-set |
| 2198 | * |
| 2199 | * Implements the EXSLT - Sets intersection() function: |
| 2200 | * node-set set:intersection (node-set, node-set) |
| 2201 | * |
| 2202 | * Returns a node set comprising the nodes that are within both the |
| 2203 | * node sets passed as arguments |
| 2204 | */ |
| 2205 | xmlNodeSetPtr |
| 2206 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2207 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2208 | int i, l1; |
| 2209 | xmlNodePtr cur; |
| 2210 | |
| 2211 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2212 | return(ret); |
| 2213 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2214 | return(ret); |
| 2215 | |
| 2216 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2217 | |
| 2218 | for (i = 0; i < l1; i++) { |
| 2219 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2220 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2221 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2222 | } |
| 2223 | return(ret); |
| 2224 | } |
| 2225 | |
| 2226 | /** |
| 2227 | * xmlXPathDistinctSorted: |
| 2228 | * @nodes: a node-set, sorted by document order |
| 2229 | * |
| 2230 | * Implements the EXSLT - Sets distinct() function: |
| 2231 | * node-set set:distinct (node-set) |
| 2232 | * |
| 2233 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2234 | * it is empty |
| 2235 | */ |
| 2236 | xmlNodeSetPtr |
| 2237 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2238 | xmlNodeSetPtr ret; |
| 2239 | xmlHashTablePtr hash; |
| 2240 | int i, l; |
| 2241 | xmlChar * strval; |
| 2242 | xmlNodePtr cur; |
| 2243 | |
| 2244 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2245 | return(nodes); |
| 2246 | |
| 2247 | ret = xmlXPathNodeSetCreate(NULL); |
| 2248 | l = xmlXPathNodeSetGetLength(nodes); |
| 2249 | hash = xmlHashCreate (l); |
| 2250 | for (i = 0; i < l; i++) { |
| 2251 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2252 | strval = xmlXPathCastNodeToString(cur); |
| 2253 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2254 | xmlHashAddEntry(hash, strval, strval); |
| 2255 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2256 | } else { |
| 2257 | xmlFree(strval); |
| 2258 | } |
| 2259 | } |
| 2260 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2261 | return(ret); |
| 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * xmlXPathDistinct: |
| 2266 | * @nodes: a node-set |
| 2267 | * |
| 2268 | * Implements the EXSLT - Sets distinct() function: |
| 2269 | * node-set set:distinct (node-set) |
| 2270 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2271 | * is called with the sorted node-set |
| 2272 | * |
| 2273 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2274 | * it is empty |
| 2275 | */ |
| 2276 | xmlNodeSetPtr |
| 2277 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2278 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2279 | return(nodes); |
| 2280 | |
| 2281 | xmlXPathNodeSetSort(nodes); |
| 2282 | return(xmlXPathDistinctSorted(nodes)); |
| 2283 | } |
| 2284 | |
| 2285 | /** |
| 2286 | * xmlXPathHasSameNodes: |
| 2287 | * @nodes1: a node-set |
| 2288 | * @nodes2: a node-set |
| 2289 | * |
| 2290 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2291 | * boolean set:has-same-node(node-set, node-set) |
| 2292 | * |
| 2293 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2294 | * otherwise |
| 2295 | */ |
| 2296 | int |
| 2297 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2298 | int i, l; |
| 2299 | xmlNodePtr cur; |
| 2300 | |
| 2301 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2302 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2303 | return(0); |
| 2304 | |
| 2305 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2306 | for (i = 0; i < l; i++) { |
| 2307 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2308 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2309 | return(1); |
| 2310 | } |
| 2311 | return(0); |
| 2312 | } |
| 2313 | |
| 2314 | /** |
| 2315 | * xmlXPathNodeLeadingSorted: |
| 2316 | * @nodes: a node-set, sorted by document order |
| 2317 | * @node: a node |
| 2318 | * |
| 2319 | * Implements the EXSLT - Sets leading() function: |
| 2320 | * node-set set:leading (node-set, node-set) |
| 2321 | * |
| 2322 | * Returns the nodes in @nodes that precede @node in document order, |
| 2323 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2324 | * doesn't contain @node |
| 2325 | */ |
| 2326 | xmlNodeSetPtr |
| 2327 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2328 | int i, l; |
| 2329 | xmlNodePtr cur; |
| 2330 | xmlNodeSetPtr ret; |
| 2331 | |
| 2332 | if (node == NULL) |
| 2333 | return(nodes); |
| 2334 | |
| 2335 | ret = xmlXPathNodeSetCreate(NULL); |
| 2336 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2337 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2338 | return(ret); |
| 2339 | |
| 2340 | l = xmlXPathNodeSetGetLength(nodes); |
| 2341 | for (i = 0; i < l; i++) { |
| 2342 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2343 | if (cur == node) |
| 2344 | break; |
| 2345 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2346 | } |
| 2347 | return(ret); |
| 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * xmlXPathNodeLeading: |
| 2352 | * @nodes: a node-set |
| 2353 | * @node: a node |
| 2354 | * |
| 2355 | * Implements the EXSLT - Sets leading() function: |
| 2356 | * node-set set:leading (node-set, node-set) |
| 2357 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2358 | * is called. |
| 2359 | * |
| 2360 | * Returns the nodes in @nodes that precede @node in document order, |
| 2361 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2362 | * doesn't contain @node |
| 2363 | */ |
| 2364 | xmlNodeSetPtr |
| 2365 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2366 | xmlXPathNodeSetSort(nodes); |
| 2367 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2368 | } |
| 2369 | |
| 2370 | /** |
| 2371 | * xmlXPathLeadingSorted: |
| 2372 | * @nodes1: a node-set, sorted by document order |
| 2373 | * @nodes2: a node-set, sorted by document order |
| 2374 | * |
| 2375 | * Implements the EXSLT - Sets leading() function: |
| 2376 | * node-set set:leading (node-set, node-set) |
| 2377 | * |
| 2378 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2379 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2380 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2381 | */ |
| 2382 | xmlNodeSetPtr |
| 2383 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2384 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2385 | return(nodes1); |
| 2386 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2387 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2388 | } |
| 2389 | |
| 2390 | /** |
| 2391 | * xmlXPathLeading: |
| 2392 | * @nodes1: a node-set |
| 2393 | * @nodes2: a node-set |
| 2394 | * |
| 2395 | * Implements the EXSLT - Sets leading() function: |
| 2396 | * node-set set:leading (node-set, node-set) |
| 2397 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2398 | * #exslSetsLeadingSorted is called. |
| 2399 | * |
| 2400 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2401 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2402 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2403 | */ |
| 2404 | xmlNodeSetPtr |
| 2405 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2406 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2407 | return(nodes1); |
| 2408 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2409 | return(xmlXPathNodeSetCreate(NULL)); |
| 2410 | xmlXPathNodeSetSort(nodes1); |
| 2411 | xmlXPathNodeSetSort(nodes2); |
| 2412 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2413 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2414 | } |
| 2415 | |
| 2416 | /** |
| 2417 | * xmlXPathNodeTrailingSorted: |
| 2418 | * @nodes: a node-set, sorted by document order |
| 2419 | * @node: a node |
| 2420 | * |
| 2421 | * Implements the EXSLT - Sets trailing() function: |
| 2422 | * node-set set:trailing (node-set, node-set) |
| 2423 | * |
| 2424 | * Returns the nodes in @nodes that follow @node in document order, |
| 2425 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2426 | * doesn't contain @node |
| 2427 | */ |
| 2428 | xmlNodeSetPtr |
| 2429 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2430 | int i, l; |
| 2431 | xmlNodePtr cur; |
| 2432 | xmlNodeSetPtr ret; |
| 2433 | |
| 2434 | if (node == NULL) |
| 2435 | return(nodes); |
| 2436 | |
| 2437 | ret = xmlXPathNodeSetCreate(NULL); |
| 2438 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2439 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2440 | return(ret); |
| 2441 | |
| 2442 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2443 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2444 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2445 | if (cur == node) |
| 2446 | break; |
| 2447 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2448 | } |
| 2449 | return(ret); |
| 2450 | } |
| 2451 | |
| 2452 | /** |
| 2453 | * xmlXPathNodeTrailing: |
| 2454 | * @nodes: a node-set |
| 2455 | * @node: a node |
| 2456 | * |
| 2457 | * Implements the EXSLT - Sets trailing() function: |
| 2458 | * node-set set:trailing (node-set, node-set) |
| 2459 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2460 | * is called. |
| 2461 | * |
| 2462 | * Returns the nodes in @nodes that follow @node in document order, |
| 2463 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2464 | * doesn't contain @node |
| 2465 | */ |
| 2466 | xmlNodeSetPtr |
| 2467 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2468 | xmlXPathNodeSetSort(nodes); |
| 2469 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * xmlXPathTrailingSorted: |
| 2474 | * @nodes1: a node-set, sorted by document order |
| 2475 | * @nodes2: a node-set, sorted by document order |
| 2476 | * |
| 2477 | * Implements the EXSLT - Sets trailing() function: |
| 2478 | * node-set set:trailing (node-set, node-set) |
| 2479 | * |
| 2480 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2481 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2482 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2483 | */ |
| 2484 | xmlNodeSetPtr |
| 2485 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2486 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2487 | return(nodes1); |
| 2488 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2489 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * xmlXPathTrailing: |
| 2494 | * @nodes1: a node-set |
| 2495 | * @nodes2: a node-set |
| 2496 | * |
| 2497 | * Implements the EXSLT - Sets trailing() function: |
| 2498 | * node-set set:trailing (node-set, node-set) |
| 2499 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2500 | * #xmlXPathTrailingSorted is called. |
| 2501 | * |
| 2502 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2503 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2504 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2505 | */ |
| 2506 | xmlNodeSetPtr |
| 2507 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2508 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2509 | return(nodes1); |
| 2510 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2511 | return(xmlXPathNodeSetCreate(NULL)); |
| 2512 | xmlXPathNodeSetSort(nodes1); |
| 2513 | xmlXPathNodeSetSort(nodes2); |
| 2514 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2515 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2516 | } |
| 2517 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2518 | /************************************************************************ |
| 2519 | * * |
| 2520 | * Routines to handle extra functions * |
| 2521 | * * |
| 2522 | ************************************************************************/ |
| 2523 | |
| 2524 | /** |
| 2525 | * xmlXPathRegisterFunc: |
| 2526 | * @ctxt: the XPath context |
| 2527 | * @name: the function name |
| 2528 | * @f: the function implementation or NULL |
| 2529 | * |
| 2530 | * Register a new function. If @f is NULL it unregisters the function |
| 2531 | * |
| 2532 | * Returns 0 in case of success, -1 in case of error |
| 2533 | */ |
| 2534 | int |
| 2535 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2536 | xmlXPathFunction f) { |
| 2537 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2538 | } |
| 2539 | |
| 2540 | /** |
| 2541 | * xmlXPathRegisterFuncNS: |
| 2542 | * @ctxt: the XPath context |
| 2543 | * @name: the function name |
| 2544 | * @ns_uri: the function namespace URI |
| 2545 | * @f: the function implementation or NULL |
| 2546 | * |
| 2547 | * Register a new function. If @f is NULL it unregisters the function |
| 2548 | * |
| 2549 | * Returns 0 in case of success, -1 in case of error |
| 2550 | */ |
| 2551 | int |
| 2552 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2553 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2554 | if (ctxt == NULL) |
| 2555 | return(-1); |
| 2556 | if (name == NULL) |
| 2557 | return(-1); |
| 2558 | |
| 2559 | if (ctxt->funcHash == NULL) |
| 2560 | ctxt->funcHash = xmlHashCreate(0); |
| 2561 | if (ctxt->funcHash == NULL) |
| 2562 | return(-1); |
| 2563 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
| 2564 | } |
| 2565 | |
| 2566 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2567 | * xmlXPathRegisterFuncLookup: |
| 2568 | * @ctxt: the XPath context |
| 2569 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2570 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2571 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2572 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2573 | */ |
| 2574 | void |
| 2575 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2576 | xmlXPathFuncLookupFunc f, |
| 2577 | void *funcCtxt) { |
| 2578 | if (ctxt == NULL) |
| 2579 | return; |
| 2580 | ctxt->funcLookupFunc = (void *) f; |
| 2581 | ctxt->funcLookupData = funcCtxt; |
| 2582 | } |
| 2583 | |
| 2584 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | * xmlXPathFunctionLookup: |
| 2586 | * @ctxt: the XPath context |
| 2587 | * @name: the function name |
| 2588 | * |
| 2589 | * Search in the Function array of the context for the given |
| 2590 | * function. |
| 2591 | * |
| 2592 | * Returns the xmlXPathFunction or NULL if not found |
| 2593 | */ |
| 2594 | xmlXPathFunction |
| 2595 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2596 | if (ctxt == NULL) |
| 2597 | return (NULL); |
| 2598 | |
| 2599 | if (ctxt->funcLookupFunc != NULL) { |
| 2600 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2601 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2602 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2603 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2604 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2605 | if (ret != NULL) |
| 2606 | return(ret); |
| 2607 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2608 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2609 | } |
| 2610 | |
| 2611 | /** |
| 2612 | * xmlXPathFunctionLookupNS: |
| 2613 | * @ctxt: the XPath context |
| 2614 | * @name: the function name |
| 2615 | * @ns_uri: the function namespace URI |
| 2616 | * |
| 2617 | * Search in the Function array of the context for the given |
| 2618 | * function. |
| 2619 | * |
| 2620 | * Returns the xmlXPathFunction or NULL if not found |
| 2621 | */ |
| 2622 | xmlXPathFunction |
| 2623 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2624 | const xmlChar *ns_uri) { |
| 2625 | if (ctxt == NULL) |
| 2626 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2627 | if (name == NULL) |
| 2628 | return(NULL); |
| 2629 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2630 | if (ctxt->funcLookupFunc != NULL) { |
| 2631 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2632 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2633 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2634 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2635 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2636 | if (ret != NULL) |
| 2637 | return(ret); |
| 2638 | } |
| 2639 | |
| 2640 | if (ctxt->funcHash == NULL) |
| 2641 | return(NULL); |
| 2642 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
| 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * xmlXPathRegisteredFuncsCleanup: |
| 2648 | * @ctxt: the XPath context |
| 2649 | * |
| 2650 | * Cleanup the XPath context data associated to registered functions |
| 2651 | */ |
| 2652 | void |
| 2653 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2654 | if (ctxt == NULL) |
| 2655 | return; |
| 2656 | |
| 2657 | xmlHashFree(ctxt->funcHash, NULL); |
| 2658 | ctxt->funcHash = NULL; |
| 2659 | } |
| 2660 | |
| 2661 | /************************************************************************ |
| 2662 | * * |
| 2663 | * Routines to handle Variable * |
| 2664 | * * |
| 2665 | ************************************************************************/ |
| 2666 | |
| 2667 | /** |
| 2668 | * xmlXPathRegisterVariable: |
| 2669 | * @ctxt: the XPath context |
| 2670 | * @name: the variable name |
| 2671 | * @value: the variable value or NULL |
| 2672 | * |
| 2673 | * Register a new variable value. If @value is NULL it unregisters |
| 2674 | * the variable |
| 2675 | * |
| 2676 | * Returns 0 in case of success, -1 in case of error |
| 2677 | */ |
| 2678 | int |
| 2679 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2680 | xmlXPathObjectPtr value) { |
| 2681 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2682 | } |
| 2683 | |
| 2684 | /** |
| 2685 | * xmlXPathRegisterVariableNS: |
| 2686 | * @ctxt: the XPath context |
| 2687 | * @name: the variable name |
| 2688 | * @ns_uri: the variable namespace URI |
| 2689 | * @value: the variable value or NULL |
| 2690 | * |
| 2691 | * Register a new variable value. If @value is NULL it unregisters |
| 2692 | * the variable |
| 2693 | * |
| 2694 | * Returns 0 in case of success, -1 in case of error |
| 2695 | */ |
| 2696 | int |
| 2697 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2698 | const xmlChar *ns_uri, |
| 2699 | xmlXPathObjectPtr value) { |
| 2700 | if (ctxt == NULL) |
| 2701 | return(-1); |
| 2702 | if (name == NULL) |
| 2703 | return(-1); |
| 2704 | |
| 2705 | if (ctxt->varHash == NULL) |
| 2706 | ctxt->varHash = xmlHashCreate(0); |
| 2707 | if (ctxt->varHash == NULL) |
| 2708 | return(-1); |
| 2709 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2710 | (void *) value, |
| 2711 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2712 | } |
| 2713 | |
| 2714 | /** |
| 2715 | * xmlXPathRegisterVariableLookup: |
| 2716 | * @ctxt: the XPath context |
| 2717 | * @f: the lookup function |
| 2718 | * @data: the lookup data |
| 2719 | * |
| 2720 | * register an external mechanism to do variable lookup |
| 2721 | */ |
| 2722 | void |
| 2723 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 2724 | xmlXPathVariableLookupFunc f, void *data) { |
| 2725 | if (ctxt == NULL) |
| 2726 | return; |
| 2727 | ctxt->varLookupFunc = (void *) f; |
| 2728 | ctxt->varLookupData = data; |
| 2729 | } |
| 2730 | |
| 2731 | /** |
| 2732 | * xmlXPathVariableLookup: |
| 2733 | * @ctxt: the XPath context |
| 2734 | * @name: the variable name |
| 2735 | * |
| 2736 | * Search in the Variable array of the context for the given |
| 2737 | * variable value. |
| 2738 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2739 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | */ |
| 2741 | xmlXPathObjectPtr |
| 2742 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 2743 | if (ctxt == NULL) |
| 2744 | return(NULL); |
| 2745 | |
| 2746 | if (ctxt->varLookupFunc != NULL) { |
| 2747 | xmlXPathObjectPtr ret; |
| 2748 | |
| 2749 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 2750 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 2751 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 | } |
| 2753 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 2754 | } |
| 2755 | |
| 2756 | /** |
| 2757 | * xmlXPathVariableLookupNS: |
| 2758 | * @ctxt: the XPath context |
| 2759 | * @name: the variable name |
| 2760 | * @ns_uri: the variable namespace URI |
| 2761 | * |
| 2762 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2763 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2764 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2765 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2766 | */ |
| 2767 | xmlXPathObjectPtr |
| 2768 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2769 | const xmlChar *ns_uri) { |
| 2770 | if (ctxt == NULL) |
| 2771 | return(NULL); |
| 2772 | |
| 2773 | if (ctxt->varLookupFunc != NULL) { |
| 2774 | xmlXPathObjectPtr ret; |
| 2775 | |
| 2776 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 2777 | (ctxt->varLookupData, name, ns_uri); |
| 2778 | if (ret != NULL) return(ret); |
| 2779 | } |
| 2780 | |
| 2781 | if (ctxt->varHash == NULL) |
| 2782 | return(NULL); |
| 2783 | if (name == NULL) |
| 2784 | return(NULL); |
| 2785 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 2786 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 2787 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * xmlXPathRegisteredVariablesCleanup: |
| 2792 | * @ctxt: the XPath context |
| 2793 | * |
| 2794 | * Cleanup the XPath context data associated to registered variables |
| 2795 | */ |
| 2796 | void |
| 2797 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 2798 | if (ctxt == NULL) |
| 2799 | return; |
| 2800 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2801 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2802 | ctxt->varHash = NULL; |
| 2803 | } |
| 2804 | |
| 2805 | /** |
| 2806 | * xmlXPathRegisterNs: |
| 2807 | * @ctxt: the XPath context |
| 2808 | * @prefix: the namespace prefix |
| 2809 | * @ns_uri: the namespace name |
| 2810 | * |
| 2811 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 2812 | * the namespace |
| 2813 | * |
| 2814 | * Returns 0 in case of success, -1 in case of error |
| 2815 | */ |
| 2816 | int |
| 2817 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 2818 | const xmlChar *ns_uri) { |
| 2819 | if (ctxt == NULL) |
| 2820 | return(-1); |
| 2821 | if (prefix == NULL) |
| 2822 | return(-1); |
| 2823 | |
| 2824 | if (ctxt->nsHash == NULL) |
| 2825 | ctxt->nsHash = xmlHashCreate(10); |
| 2826 | if (ctxt->nsHash == NULL) |
| 2827 | return(-1); |
| 2828 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) ns_uri, |
| 2829 | (xmlHashDeallocator)xmlFree)); |
| 2830 | } |
| 2831 | |
| 2832 | /** |
| 2833 | * xmlXPathNsLookup: |
| 2834 | * @ctxt: the XPath context |
| 2835 | * @prefix: the namespace prefix value |
| 2836 | * |
| 2837 | * Search in the namespace declaration array of the context for the given |
| 2838 | * namespace name associated to the given prefix |
| 2839 | * |
| 2840 | * Returns the value or NULL if not found |
| 2841 | */ |
| 2842 | const xmlChar * |
| 2843 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 2844 | if (ctxt == NULL) |
| 2845 | return(NULL); |
| 2846 | if (prefix == NULL) |
| 2847 | return(NULL); |
| 2848 | |
| 2849 | #ifdef XML_XML_NAMESPACE |
| 2850 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 2851 | return(XML_XML_NAMESPACE); |
| 2852 | #endif |
| 2853 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 2854 | if (ctxt->namespaces != NULL) { |
| 2855 | int i; |
| 2856 | |
| 2857 | for (i = 0;i < ctxt->nsNr;i++) { |
| 2858 | if ((ctxt->namespaces[i] != NULL) && |
| 2859 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 2860 | return(ctxt->namespaces[i]->href); |
| 2861 | } |
| 2862 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2863 | |
| 2864 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 2865 | } |
| 2866 | |
| 2867 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2868 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2869 | * @ctxt: the XPath context |
| 2870 | * |
| 2871 | * Cleanup the XPath context data associated to registered variables |
| 2872 | */ |
| 2873 | void |
| 2874 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 2875 | if (ctxt == NULL) |
| 2876 | return; |
| 2877 | |
| 2878 | xmlHashFree(ctxt->nsHash, NULL); |
| 2879 | ctxt->nsHash = NULL; |
| 2880 | } |
| 2881 | |
| 2882 | /************************************************************************ |
| 2883 | * * |
| 2884 | * Routines to handle Values * |
| 2885 | * * |
| 2886 | ************************************************************************/ |
| 2887 | |
| 2888 | /* Allocations are terrible, one need to optimize all this !!! */ |
| 2889 | |
| 2890 | /** |
| 2891 | * xmlXPathNewFloat: |
| 2892 | * @val: the double value |
| 2893 | * |
| 2894 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 2895 | * |
| 2896 | * Returns the newly created object. |
| 2897 | */ |
| 2898 | xmlXPathObjectPtr |
| 2899 | xmlXPathNewFloat(double val) { |
| 2900 | xmlXPathObjectPtr ret; |
| 2901 | |
| 2902 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2903 | if (ret == NULL) { |
| 2904 | xmlGenericError(xmlGenericErrorContext, |
| 2905 | "xmlXPathNewFloat: out of memory\n"); |
| 2906 | return(NULL); |
| 2907 | } |
| 2908 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2909 | ret->type = XPATH_NUMBER; |
| 2910 | ret->floatval = val; |
| 2911 | return(ret); |
| 2912 | } |
| 2913 | |
| 2914 | /** |
| 2915 | * xmlXPathNewBoolean: |
| 2916 | * @val: the boolean value |
| 2917 | * |
| 2918 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 2919 | * |
| 2920 | * Returns the newly created object. |
| 2921 | */ |
| 2922 | xmlXPathObjectPtr |
| 2923 | xmlXPathNewBoolean(int val) { |
| 2924 | xmlXPathObjectPtr ret; |
| 2925 | |
| 2926 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2927 | if (ret == NULL) { |
| 2928 | xmlGenericError(xmlGenericErrorContext, |
| 2929 | "xmlXPathNewBoolean: out of memory\n"); |
| 2930 | return(NULL); |
| 2931 | } |
| 2932 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2933 | ret->type = XPATH_BOOLEAN; |
| 2934 | ret->boolval = (val != 0); |
| 2935 | return(ret); |
| 2936 | } |
| 2937 | |
| 2938 | /** |
| 2939 | * xmlXPathNewString: |
| 2940 | * @val: the xmlChar * value |
| 2941 | * |
| 2942 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 2943 | * |
| 2944 | * Returns the newly created object. |
| 2945 | */ |
| 2946 | xmlXPathObjectPtr |
| 2947 | xmlXPathNewString(const xmlChar *val) { |
| 2948 | xmlXPathObjectPtr ret; |
| 2949 | |
| 2950 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2951 | if (ret == NULL) { |
| 2952 | xmlGenericError(xmlGenericErrorContext, |
| 2953 | "xmlXPathNewString: out of memory\n"); |
| 2954 | return(NULL); |
| 2955 | } |
| 2956 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2957 | ret->type = XPATH_STRING; |
| 2958 | if (val != NULL) |
| 2959 | ret->stringval = xmlStrdup(val); |
| 2960 | else |
| 2961 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 2962 | return(ret); |
| 2963 | } |
| 2964 | |
| 2965 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 2966 | * xmlXPathWrapString: |
| 2967 | * @val: the xmlChar * value |
| 2968 | * |
| 2969 | * Wraps the @val string into an XPath object. |
| 2970 | * |
| 2971 | * Returns the newly created object. |
| 2972 | */ |
| 2973 | xmlXPathObjectPtr |
| 2974 | xmlXPathWrapString (xmlChar *val) { |
| 2975 | xmlXPathObjectPtr ret; |
| 2976 | |
| 2977 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2978 | if (ret == NULL) { |
| 2979 | xmlGenericError(xmlGenericErrorContext, |
| 2980 | "xmlXPathWrapString: out of memory\n"); |
| 2981 | return(NULL); |
| 2982 | } |
| 2983 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2984 | ret->type = XPATH_STRING; |
| 2985 | ret->stringval = val; |
| 2986 | return(ret); |
| 2987 | } |
| 2988 | |
| 2989 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2990 | * xmlXPathNewCString: |
| 2991 | * @val: the char * value |
| 2992 | * |
| 2993 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 2994 | * |
| 2995 | * Returns the newly created object. |
| 2996 | */ |
| 2997 | xmlXPathObjectPtr |
| 2998 | xmlXPathNewCString(const char *val) { |
| 2999 | xmlXPathObjectPtr ret; |
| 3000 | |
| 3001 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3002 | if (ret == NULL) { |
| 3003 | xmlGenericError(xmlGenericErrorContext, |
| 3004 | "xmlXPathNewCString: out of memory\n"); |
| 3005 | return(NULL); |
| 3006 | } |
| 3007 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3008 | ret->type = XPATH_STRING; |
| 3009 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3010 | return(ret); |
| 3011 | } |
| 3012 | |
| 3013 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3014 | * xmlXPathWrapCString: |
| 3015 | * @val: the char * value |
| 3016 | * |
| 3017 | * Wraps a string into an XPath object. |
| 3018 | * |
| 3019 | * Returns the newly created object. |
| 3020 | */ |
| 3021 | xmlXPathObjectPtr |
| 3022 | xmlXPathWrapCString (char * val) { |
| 3023 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3024 | } |
| 3025 | |
| 3026 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3027 | * xmlXPathWrapExternal: |
| 3028 | * @val: the user data |
| 3029 | * |
| 3030 | * Wraps the @val data into an XPath object. |
| 3031 | * |
| 3032 | * Returns the newly created object. |
| 3033 | */ |
| 3034 | xmlXPathObjectPtr |
| 3035 | xmlXPathWrapExternal (void *val) { |
| 3036 | xmlXPathObjectPtr ret; |
| 3037 | |
| 3038 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3039 | if (ret == NULL) { |
| 3040 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3041 | "xmlXPathWrapExternal: out of memory\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3042 | return(NULL); |
| 3043 | } |
| 3044 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3045 | ret->type = XPATH_USERS; |
| 3046 | ret->user = val; |
| 3047 | return(ret); |
| 3048 | } |
| 3049 | |
| 3050 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | * xmlXPathObjectCopy: |
| 3052 | * @val: the original object |
| 3053 | * |
| 3054 | * allocate a new copy of a given object |
| 3055 | * |
| 3056 | * Returns the newly created object. |
| 3057 | */ |
| 3058 | xmlXPathObjectPtr |
| 3059 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3060 | xmlXPathObjectPtr ret; |
| 3061 | |
| 3062 | if (val == NULL) |
| 3063 | return(NULL); |
| 3064 | |
| 3065 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3066 | if (ret == NULL) { |
| 3067 | xmlGenericError(xmlGenericErrorContext, |
| 3068 | "xmlXPathObjectCopy: out of memory\n"); |
| 3069 | return(NULL); |
| 3070 | } |
| 3071 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3072 | switch (val->type) { |
| 3073 | case XPATH_BOOLEAN: |
| 3074 | case XPATH_NUMBER: |
| 3075 | case XPATH_POINT: |
| 3076 | case XPATH_RANGE: |
| 3077 | break; |
| 3078 | case XPATH_STRING: |
| 3079 | ret->stringval = xmlStrdup(val->stringval); |
| 3080 | break; |
| 3081 | case XPATH_XSLT_TREE: |
| 3082 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3083 | (val->nodesetval->nodeTab != NULL)) { |
| 3084 | ret->boolval = 1; |
Daniel Veillard | 6ab3838 | 2001-10-06 13:08:27 +0000 | [diff] [blame] | 3085 | ret->user = xmlDocCopyNode(val->nodesetval->nodeTab[0], |
| 3086 | val->nodesetval->nodeTab[0]->doc, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3087 | ret->nodesetval = xmlXPathNodeSetCreate( |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3088 | (xmlNodePtr) ret->user); |
| 3089 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3090 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3091 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | case XPATH_NODESET: |
| 3094 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3095 | /* Do not deallocate the copied tree value */ |
| 3096 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3097 | break; |
| 3098 | case XPATH_LOCATIONSET: |
| 3099 | #ifdef LIBXML_XPTR_ENABLED |
| 3100 | { |
| 3101 | xmlLocationSetPtr loc = val->user; |
| 3102 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3103 | break; |
| 3104 | } |
| 3105 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3106 | case XPATH_USERS: |
| 3107 | ret->user = val->user; |
| 3108 | break; |
| 3109 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3110 | xmlGenericError(xmlGenericErrorContext, |
| 3111 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3112 | val->type); |
| 3113 | break; |
| 3114 | } |
| 3115 | return(ret); |
| 3116 | } |
| 3117 | |
| 3118 | /** |
| 3119 | * xmlXPathFreeObject: |
| 3120 | * @obj: the object to free |
| 3121 | * |
| 3122 | * Free up an xmlXPathObjectPtr object. |
| 3123 | */ |
| 3124 | void |
| 3125 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3126 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3127 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3128 | if (obj->boolval) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3129 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3130 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3131 | xmlFreeNodeList((xmlNodePtr) obj->user); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3132 | } else if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3133 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3134 | } else { |
| 3135 | if (obj->nodesetval != NULL) |
| 3136 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3137 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3138 | #ifdef LIBXML_XPTR_ENABLED |
| 3139 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3140 | if (obj->user != NULL) |
| 3141 | xmlXPtrFreeLocationSet(obj->user); |
| 3142 | #endif |
| 3143 | } else if (obj->type == XPATH_STRING) { |
| 3144 | if (obj->stringval != NULL) |
| 3145 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3148 | xmlFree(obj); |
| 3149 | } |
| 3150 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3151 | |
| 3152 | /************************************************************************ |
| 3153 | * * |
| 3154 | * Type Casting Routines * |
| 3155 | * * |
| 3156 | ************************************************************************/ |
| 3157 | |
| 3158 | /** |
| 3159 | * xmlXPathCastBooleanToString: |
| 3160 | * @val: a boolean |
| 3161 | * |
| 3162 | * Converts a boolean to its string value. |
| 3163 | * |
| 3164 | * Returns a newly allocated string. |
| 3165 | */ |
| 3166 | xmlChar * |
| 3167 | xmlXPathCastBooleanToString (int val) { |
| 3168 | xmlChar *ret; |
| 3169 | if (val) |
| 3170 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3171 | else |
| 3172 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3173 | return(ret); |
| 3174 | } |
| 3175 | |
| 3176 | /** |
| 3177 | * xmlXPathCastNumberToString: |
| 3178 | * @val: a number |
| 3179 | * |
| 3180 | * Converts a number to its string value. |
| 3181 | * |
| 3182 | * Returns a newly allocated string. |
| 3183 | */ |
| 3184 | xmlChar * |
| 3185 | xmlXPathCastNumberToString (double val) { |
| 3186 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3187 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3188 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3189 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3190 | break; |
| 3191 | case -1: |
| 3192 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3193 | break; |
| 3194 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3195 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3196 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3197 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3198 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3199 | } else { |
| 3200 | /* could be improved */ |
| 3201 | char buf[100]; |
| 3202 | xmlXPathFormatNumber(val, buf, 100); |
| 3203 | ret = xmlStrdup((const xmlChar *) buf); |
| 3204 | } |
| 3205 | } |
| 3206 | return(ret); |
| 3207 | } |
| 3208 | |
| 3209 | /** |
| 3210 | * xmlXPathCastNodeToString: |
| 3211 | * @node: a node |
| 3212 | * |
| 3213 | * Converts a node to its string value. |
| 3214 | * |
| 3215 | * Returns a newly allocated string. |
| 3216 | */ |
| 3217 | xmlChar * |
| 3218 | xmlXPathCastNodeToString (xmlNodePtr node) { |
Daniel Veillard | 23b1f37 | 2002-04-18 15:50:05 +0000 | [diff] [blame] | 3219 | if ((node != NULL) && (node->type == XML_DOCUMENT_NODE)) |
| 3220 | node = xmlDocGetRootElement((xmlDocPtr) node); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3221 | return(xmlNodeGetContent(node)); |
| 3222 | } |
| 3223 | |
| 3224 | /** |
| 3225 | * xmlXPathCastNodeSetToString: |
| 3226 | * @ns: a node-set |
| 3227 | * |
| 3228 | * Converts a node-set to its string value. |
| 3229 | * |
| 3230 | * Returns a newly allocated string. |
| 3231 | */ |
| 3232 | xmlChar * |
| 3233 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3234 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3235 | return(xmlStrdup((const xmlChar *) "")); |
| 3236 | |
| 3237 | xmlXPathNodeSetSort(ns); |
| 3238 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3239 | } |
| 3240 | |
| 3241 | /** |
| 3242 | * xmlXPathCastToString: |
| 3243 | * @val: an XPath object |
| 3244 | * |
| 3245 | * Converts an existing object to its string() equivalent |
| 3246 | * |
| 3247 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3248 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3249 | * string object). |
| 3250 | */ |
| 3251 | xmlChar * |
| 3252 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3253 | xmlChar *ret = NULL; |
| 3254 | |
| 3255 | if (val == NULL) |
| 3256 | return(xmlStrdup((const xmlChar *) "")); |
| 3257 | switch (val->type) { |
| 3258 | case XPATH_UNDEFINED: |
| 3259 | #ifdef DEBUG_EXPR |
| 3260 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3261 | #endif |
| 3262 | ret = xmlStrdup((const xmlChar *) ""); |
| 3263 | break; |
| 3264 | case XPATH_XSLT_TREE: |
| 3265 | case XPATH_NODESET: |
| 3266 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3267 | break; |
| 3268 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3269 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3270 | case XPATH_BOOLEAN: |
| 3271 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3272 | break; |
| 3273 | case XPATH_NUMBER: { |
| 3274 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3275 | break; |
| 3276 | } |
| 3277 | case XPATH_USERS: |
| 3278 | case XPATH_POINT: |
| 3279 | case XPATH_RANGE: |
| 3280 | case XPATH_LOCATIONSET: |
| 3281 | TODO |
| 3282 | ret = xmlStrdup((const xmlChar *) ""); |
| 3283 | break; |
| 3284 | } |
| 3285 | return(ret); |
| 3286 | } |
| 3287 | |
| 3288 | /** |
| 3289 | * xmlXPathConvertString: |
| 3290 | * @val: an XPath object |
| 3291 | * |
| 3292 | * Converts an existing object to its string() equivalent |
| 3293 | * |
| 3294 | * Returns the new object, the old one is freed (or the operation |
| 3295 | * is done directly on @val) |
| 3296 | */ |
| 3297 | xmlXPathObjectPtr |
| 3298 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3299 | xmlChar *res = NULL; |
| 3300 | |
| 3301 | if (val == NULL) |
| 3302 | return(xmlXPathNewCString("")); |
| 3303 | |
| 3304 | switch (val->type) { |
| 3305 | case XPATH_UNDEFINED: |
| 3306 | #ifdef DEBUG_EXPR |
| 3307 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3308 | #endif |
| 3309 | break; |
| 3310 | case XPATH_XSLT_TREE: |
| 3311 | case XPATH_NODESET: |
| 3312 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3313 | break; |
| 3314 | case XPATH_STRING: |
| 3315 | return(val); |
| 3316 | case XPATH_BOOLEAN: |
| 3317 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3318 | break; |
| 3319 | case XPATH_NUMBER: |
| 3320 | res = xmlXPathCastNumberToString(val->floatval); |
| 3321 | break; |
| 3322 | case XPATH_USERS: |
| 3323 | case XPATH_POINT: |
| 3324 | case XPATH_RANGE: |
| 3325 | case XPATH_LOCATIONSET: |
| 3326 | TODO; |
| 3327 | break; |
| 3328 | } |
| 3329 | xmlXPathFreeObject(val); |
| 3330 | if (res == NULL) |
| 3331 | return(xmlXPathNewCString("")); |
| 3332 | return(xmlXPathWrapString(res)); |
| 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * xmlXPathCastBooleanToNumber: |
| 3337 | * @val: a boolean |
| 3338 | * |
| 3339 | * Converts a boolean to its number value |
| 3340 | * |
| 3341 | * Returns the number value |
| 3342 | */ |
| 3343 | double |
| 3344 | xmlXPathCastBooleanToNumber(int val) { |
| 3345 | if (val) |
| 3346 | return(1.0); |
| 3347 | return(0.0); |
| 3348 | } |
| 3349 | |
| 3350 | /** |
| 3351 | * xmlXPathCastStringToNumber: |
| 3352 | * @val: a string |
| 3353 | * |
| 3354 | * Converts a string to its number value |
| 3355 | * |
| 3356 | * Returns the number value |
| 3357 | */ |
| 3358 | double |
| 3359 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3360 | return(xmlXPathStringEvalNumber(val)); |
| 3361 | } |
| 3362 | |
| 3363 | /** |
| 3364 | * xmlXPathCastNodeToNumber: |
| 3365 | * @node: a node |
| 3366 | * |
| 3367 | * Converts a node to its number value |
| 3368 | * |
| 3369 | * Returns the number value |
| 3370 | */ |
| 3371 | double |
| 3372 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3373 | xmlChar *strval; |
| 3374 | double ret; |
| 3375 | |
| 3376 | if (node == NULL) |
| 3377 | return(xmlXPathNAN); |
| 3378 | strval = xmlXPathCastNodeToString(node); |
| 3379 | if (strval == NULL) |
| 3380 | return(xmlXPathNAN); |
| 3381 | ret = xmlXPathCastStringToNumber(strval); |
| 3382 | xmlFree(strval); |
| 3383 | |
| 3384 | return(ret); |
| 3385 | } |
| 3386 | |
| 3387 | /** |
| 3388 | * xmlXPathCastNodeSetToNumber: |
| 3389 | * @ns: a node-set |
| 3390 | * |
| 3391 | * Converts a node-set to its number value |
| 3392 | * |
| 3393 | * Returns the number value |
| 3394 | */ |
| 3395 | double |
| 3396 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3397 | xmlChar *str; |
| 3398 | double ret; |
| 3399 | |
| 3400 | if (ns == NULL) |
| 3401 | return(xmlXPathNAN); |
| 3402 | str = xmlXPathCastNodeSetToString(ns); |
| 3403 | ret = xmlXPathCastStringToNumber(str); |
| 3404 | xmlFree(str); |
| 3405 | return(ret); |
| 3406 | } |
| 3407 | |
| 3408 | /** |
| 3409 | * xmlXPathCastToNumber: |
| 3410 | * @val: an XPath object |
| 3411 | * |
| 3412 | * Converts an XPath object to its number value |
| 3413 | * |
| 3414 | * Returns the number value |
| 3415 | */ |
| 3416 | double |
| 3417 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3418 | double ret = 0.0; |
| 3419 | |
| 3420 | if (val == NULL) |
| 3421 | return(xmlXPathNAN); |
| 3422 | switch (val->type) { |
| 3423 | case XPATH_UNDEFINED: |
| 3424 | #ifdef DEGUB_EXPR |
| 3425 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3426 | #endif |
| 3427 | ret = xmlXPathNAN; |
| 3428 | break; |
| 3429 | case XPATH_XSLT_TREE: |
| 3430 | case XPATH_NODESET: |
| 3431 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3432 | break; |
| 3433 | case XPATH_STRING: |
| 3434 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3435 | break; |
| 3436 | case XPATH_NUMBER: |
| 3437 | ret = val->floatval; |
| 3438 | break; |
| 3439 | case XPATH_BOOLEAN: |
| 3440 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3441 | break; |
| 3442 | case XPATH_USERS: |
| 3443 | case XPATH_POINT: |
| 3444 | case XPATH_RANGE: |
| 3445 | case XPATH_LOCATIONSET: |
| 3446 | TODO; |
| 3447 | ret = xmlXPathNAN; |
| 3448 | break; |
| 3449 | } |
| 3450 | return(ret); |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * xmlXPathConvertNumber: |
| 3455 | * @val: an XPath object |
| 3456 | * |
| 3457 | * Converts an existing object to its number() equivalent |
| 3458 | * |
| 3459 | * Returns the new object, the old one is freed (or the operation |
| 3460 | * is done directly on @val) |
| 3461 | */ |
| 3462 | xmlXPathObjectPtr |
| 3463 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3464 | xmlXPathObjectPtr ret; |
| 3465 | |
| 3466 | if (val == NULL) |
| 3467 | return(xmlXPathNewFloat(0.0)); |
| 3468 | if (val->type == XPATH_NUMBER) |
| 3469 | return(val); |
| 3470 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3471 | xmlXPathFreeObject(val); |
| 3472 | return(ret); |
| 3473 | } |
| 3474 | |
| 3475 | /** |
| 3476 | * xmlXPathCastNumberToBoolean: |
| 3477 | * @val: a number |
| 3478 | * |
| 3479 | * Converts a number to its boolean value |
| 3480 | * |
| 3481 | * Returns the boolean value |
| 3482 | */ |
| 3483 | int |
| 3484 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3485 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3486 | return(0); |
| 3487 | return(1); |
| 3488 | } |
| 3489 | |
| 3490 | /** |
| 3491 | * xmlXPathCastStringToBoolean: |
| 3492 | * @val: a string |
| 3493 | * |
| 3494 | * Converts a string to its boolean value |
| 3495 | * |
| 3496 | * Returns the boolean value |
| 3497 | */ |
| 3498 | int |
| 3499 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3500 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3501 | return(0); |
| 3502 | return(1); |
| 3503 | } |
| 3504 | |
| 3505 | /** |
| 3506 | * xmlXPathCastNodeSetToBoolean: |
| 3507 | * @ns: a node-set |
| 3508 | * |
| 3509 | * Converts a node-set to its boolean value |
| 3510 | * |
| 3511 | * Returns the boolean value |
| 3512 | */ |
| 3513 | int |
| 3514 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3515 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3516 | return(0); |
| 3517 | return(1); |
| 3518 | } |
| 3519 | |
| 3520 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3521 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3522 | * @val: an XPath object |
| 3523 | * |
| 3524 | * Converts an XPath object to its boolean value |
| 3525 | * |
| 3526 | * Returns the boolean value |
| 3527 | */ |
| 3528 | int |
| 3529 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3530 | int ret = 0; |
| 3531 | |
| 3532 | if (val == NULL) |
| 3533 | return(0); |
| 3534 | switch (val->type) { |
| 3535 | case XPATH_UNDEFINED: |
| 3536 | #ifdef DEBUG_EXPR |
| 3537 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3538 | #endif |
| 3539 | ret = 0; |
| 3540 | break; |
| 3541 | case XPATH_XSLT_TREE: |
| 3542 | case XPATH_NODESET: |
| 3543 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3544 | break; |
| 3545 | case XPATH_STRING: |
| 3546 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3547 | break; |
| 3548 | case XPATH_NUMBER: |
| 3549 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3550 | break; |
| 3551 | case XPATH_BOOLEAN: |
| 3552 | ret = val->boolval; |
| 3553 | break; |
| 3554 | case XPATH_USERS: |
| 3555 | case XPATH_POINT: |
| 3556 | case XPATH_RANGE: |
| 3557 | case XPATH_LOCATIONSET: |
| 3558 | TODO; |
| 3559 | ret = 0; |
| 3560 | break; |
| 3561 | } |
| 3562 | return(ret); |
| 3563 | } |
| 3564 | |
| 3565 | |
| 3566 | /** |
| 3567 | * xmlXPathConvertBoolean: |
| 3568 | * @val: an XPath object |
| 3569 | * |
| 3570 | * Converts an existing object to its boolean() equivalent |
| 3571 | * |
| 3572 | * Returns the new object, the old one is freed (or the operation |
| 3573 | * is done directly on @val) |
| 3574 | */ |
| 3575 | xmlXPathObjectPtr |
| 3576 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3577 | xmlXPathObjectPtr ret; |
| 3578 | |
| 3579 | if (val == NULL) |
| 3580 | return(xmlXPathNewBoolean(0)); |
| 3581 | if (val->type == XPATH_BOOLEAN) |
| 3582 | return(val); |
| 3583 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3584 | xmlXPathFreeObject(val); |
| 3585 | return(ret); |
| 3586 | } |
| 3587 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3588 | /************************************************************************ |
| 3589 | * * |
| 3590 | * Routines to handle XPath contexts * |
| 3591 | * * |
| 3592 | ************************************************************************/ |
| 3593 | |
| 3594 | /** |
| 3595 | * xmlXPathNewContext: |
| 3596 | * @doc: the XML document |
| 3597 | * |
| 3598 | * Create a new xmlXPathContext |
| 3599 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3600 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3601 | */ |
| 3602 | xmlXPathContextPtr |
| 3603 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3604 | xmlXPathContextPtr ret; |
| 3605 | |
| 3606 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3607 | if (ret == NULL) { |
| 3608 | xmlGenericError(xmlGenericErrorContext, |
| 3609 | "xmlXPathNewContext: out of memory\n"); |
| 3610 | return(NULL); |
| 3611 | } |
| 3612 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3613 | ret->doc = doc; |
| 3614 | ret->node = NULL; |
| 3615 | |
| 3616 | ret->varHash = NULL; |
| 3617 | |
| 3618 | ret->nb_types = 0; |
| 3619 | ret->max_types = 0; |
| 3620 | ret->types = NULL; |
| 3621 | |
| 3622 | ret->funcHash = xmlHashCreate(0); |
| 3623 | |
| 3624 | ret->nb_axis = 0; |
| 3625 | ret->max_axis = 0; |
| 3626 | ret->axis = NULL; |
| 3627 | |
| 3628 | ret->nsHash = NULL; |
| 3629 | ret->user = NULL; |
| 3630 | |
| 3631 | ret->contextSize = -1; |
| 3632 | ret->proximityPosition = -1; |
| 3633 | |
| 3634 | xmlXPathRegisterAllFunctions(ret); |
| 3635 | |
| 3636 | return(ret); |
| 3637 | } |
| 3638 | |
| 3639 | /** |
| 3640 | * xmlXPathFreeContext: |
| 3641 | * @ctxt: the context to free |
| 3642 | * |
| 3643 | * Free up an xmlXPathContext |
| 3644 | */ |
| 3645 | void |
| 3646 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
| 3647 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3648 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3649 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3650 | xmlFree(ctxt); |
| 3651 | } |
| 3652 | |
| 3653 | /************************************************************************ |
| 3654 | * * |
| 3655 | * Routines to handle XPath parser contexts * |
| 3656 | * * |
| 3657 | ************************************************************************/ |
| 3658 | |
| 3659 | #define CHECK_CTXT(ctxt) \ |
| 3660 | if (ctxt == NULL) { \ |
| 3661 | xmlGenericError(xmlGenericErrorContext, \ |
| 3662 | "%s:%d Internal error: ctxt == NULL\n", \ |
| 3663 | __FILE__, __LINE__); \ |
| 3664 | } \ |
| 3665 | |
| 3666 | |
| 3667 | #define CHECK_CONTEXT(ctxt) \ |
| 3668 | if (ctxt == NULL) { \ |
| 3669 | xmlGenericError(xmlGenericErrorContext, \ |
| 3670 | "%s:%d Internal error: no context\n", \ |
| 3671 | __FILE__, __LINE__); \ |
| 3672 | } \ |
| 3673 | else if (ctxt->doc == NULL) { \ |
| 3674 | xmlGenericError(xmlGenericErrorContext, \ |
| 3675 | "%s:%d Internal error: no document\n", \ |
| 3676 | __FILE__, __LINE__); \ |
| 3677 | } \ |
| 3678 | else if (ctxt->doc->children == NULL) { \ |
| 3679 | xmlGenericError(xmlGenericErrorContext, \ |
| 3680 | "%s:%d Internal error: document without root\n", \ |
| 3681 | __FILE__, __LINE__); \ |
| 3682 | } \ |
| 3683 | |
| 3684 | |
| 3685 | /** |
| 3686 | * xmlXPathNewParserContext: |
| 3687 | * @str: the XPath expression |
| 3688 | * @ctxt: the XPath context |
| 3689 | * |
| 3690 | * Create a new xmlXPathParserContext |
| 3691 | * |
| 3692 | * Returns the xmlXPathParserContext just allocated. |
| 3693 | */ |
| 3694 | xmlXPathParserContextPtr |
| 3695 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3696 | xmlXPathParserContextPtr ret; |
| 3697 | |
| 3698 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3699 | if (ret == NULL) { |
| 3700 | xmlGenericError(xmlGenericErrorContext, |
| 3701 | "xmlXPathNewParserContext: out of memory\n"); |
| 3702 | return(NULL); |
| 3703 | } |
| 3704 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3705 | ret->cur = ret->base = str; |
| 3706 | ret->context = ctxt; |
| 3707 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3708 | ret->comp = xmlXPathNewCompExpr(); |
| 3709 | if (ret->comp == NULL) { |
| 3710 | xmlFree(ret->valueTab); |
| 3711 | xmlFree(ret); |
| 3712 | return(NULL); |
| 3713 | } |
| 3714 | |
| 3715 | return(ret); |
| 3716 | } |
| 3717 | |
| 3718 | /** |
| 3719 | * xmlXPathCompParserContext: |
| 3720 | * @comp: the XPath compiled expression |
| 3721 | * @ctxt: the XPath context |
| 3722 | * |
| 3723 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 3724 | * |
| 3725 | * Returns the xmlXPathParserContext just allocated. |
| 3726 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3727 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3728 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 3729 | xmlXPathParserContextPtr ret; |
| 3730 | |
| 3731 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3732 | if (ret == NULL) { |
| 3733 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3734 | "xmlXPathCompParserContext: out of memory\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3735 | return(NULL); |
| 3736 | } |
| 3737 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3738 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3739 | /* Allocate the value stack */ |
| 3740 | ret->valueTab = (xmlXPathObjectPtr *) |
| 3741 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3742 | if (ret->valueTab == NULL) { |
| 3743 | xmlFree(ret); |
| 3744 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3745 | "xmlXPathCompParserContext: out of memory\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3746 | return(NULL); |
| 3747 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3748 | ret->valueNr = 0; |
| 3749 | ret->valueMax = 10; |
| 3750 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3751 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 3752 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3753 | ret->comp = comp; |
| 3754 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3755 | return(ret); |
| 3756 | } |
| 3757 | |
| 3758 | /** |
| 3759 | * xmlXPathFreeParserContext: |
| 3760 | * @ctxt: the context to free |
| 3761 | * |
| 3762 | * Free up an xmlXPathParserContext |
| 3763 | */ |
| 3764 | void |
| 3765 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 3766 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3767 | xmlFree(ctxt->valueTab); |
| 3768 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3769 | if (ctxt->comp) |
| 3770 | xmlXPathFreeCompExpr(ctxt->comp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3771 | xmlFree(ctxt); |
| 3772 | } |
| 3773 | |
| 3774 | /************************************************************************ |
| 3775 | * * |
| 3776 | * The implicit core function library * |
| 3777 | * * |
| 3778 | ************************************************************************/ |
| 3779 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3780 | /** |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 3781 | * xmlXPathNodeStringHash: |
| 3782 | * @node: a node pointer |
| 3783 | * |
| 3784 | * Function computing the beginning of the string value of the node, |
| 3785 | * used to speed up comparisons |
| 3786 | * |
| 3787 | * Returns an int usable as a hash |
| 3788 | */ |
| 3789 | static unsigned int |
| 3790 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 3791 | int len = 2; |
| 3792 | const xmlChar * string = NULL; |
| 3793 | xmlNodePtr tmp = NULL; |
| 3794 | unsigned int ret = 0; |
| 3795 | |
| 3796 | if (node == NULL) |
| 3797 | return(0); |
| 3798 | |
| 3799 | |
| 3800 | switch (node->type) { |
| 3801 | case XML_COMMENT_NODE: |
| 3802 | case XML_PI_NODE: |
| 3803 | case XML_CDATA_SECTION_NODE: |
| 3804 | case XML_TEXT_NODE: |
| 3805 | string = node->content; |
| 3806 | if (string == NULL) |
| 3807 | return(0); |
| 3808 | if (string[0] == 0) |
| 3809 | return(0); |
| 3810 | return(((unsigned int) string[0]) + |
| 3811 | (((unsigned int) string[1]) << 8)); |
| 3812 | case XML_NAMESPACE_DECL: |
| 3813 | string = ((xmlNsPtr)node)->href; |
| 3814 | if (string == NULL) |
| 3815 | return(0); |
| 3816 | if (string[0] == 0) |
| 3817 | return(0); |
| 3818 | return(((unsigned int) string[0]) + |
| 3819 | (((unsigned int) string[1]) << 8)); |
| 3820 | case XML_ATTRIBUTE_NODE: |
| 3821 | tmp = ((xmlAttrPtr) node)->children; |
| 3822 | break; |
| 3823 | case XML_ELEMENT_NODE: |
| 3824 | tmp = node->children; |
| 3825 | break; |
| 3826 | default: |
| 3827 | return(0); |
| 3828 | } |
| 3829 | while (tmp != NULL) { |
| 3830 | switch (tmp->type) { |
| 3831 | case XML_COMMENT_NODE: |
| 3832 | case XML_PI_NODE: |
| 3833 | case XML_CDATA_SECTION_NODE: |
| 3834 | case XML_TEXT_NODE: |
| 3835 | string = tmp->content; |
| 3836 | break; |
| 3837 | case XML_NAMESPACE_DECL: |
| 3838 | string = ((xmlNsPtr)tmp)->href; |
| 3839 | break; |
| 3840 | default: |
| 3841 | break; |
| 3842 | } |
| 3843 | if ((string != NULL) && (string[0] != 0)) { |
| 3844 | if (string[0] == 0) |
| 3845 | return(0); |
| 3846 | if (len == 1) { |
| 3847 | return(ret + (((unsigned int) string[0]) << 8)); |
| 3848 | } |
| 3849 | if (string[1] == 0) { |
| 3850 | len = 1; |
| 3851 | ret = (unsigned int) string[0]; |
| 3852 | } else { |
| 3853 | return(((unsigned int) string[0]) + |
| 3854 | (((unsigned int) string[1]) << 8)); |
| 3855 | } |
| 3856 | } |
| 3857 | /* |
| 3858 | * Skip to next node |
| 3859 | */ |
| 3860 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 3861 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 3862 | tmp = tmp->children; |
| 3863 | continue; |
| 3864 | } |
| 3865 | } |
| 3866 | if (tmp == node) |
| 3867 | break; |
| 3868 | |
| 3869 | if (tmp->next != NULL) { |
| 3870 | tmp = tmp->next; |
| 3871 | continue; |
| 3872 | } |
| 3873 | |
| 3874 | do { |
| 3875 | tmp = tmp->parent; |
| 3876 | if (tmp == NULL) |
| 3877 | break; |
| 3878 | if (tmp == node) { |
| 3879 | tmp = NULL; |
| 3880 | break; |
| 3881 | } |
| 3882 | if (tmp->next != NULL) { |
| 3883 | tmp = tmp->next; |
| 3884 | break; |
| 3885 | } |
| 3886 | } while (tmp != NULL); |
| 3887 | } |
| 3888 | return(ret); |
| 3889 | } |
| 3890 | |
| 3891 | /** |
| 3892 | * xmlXPathStringHash: |
| 3893 | * @string: a string |
| 3894 | * |
| 3895 | * Function computing the beginning of the string value of the node, |
| 3896 | * used to speed up comparisons |
| 3897 | * |
| 3898 | * Returns an int usable as a hash |
| 3899 | */ |
| 3900 | static unsigned int |
| 3901 | xmlXPathStringHash(const xmlChar * string) { |
| 3902 | if (string == NULL) |
| 3903 | return((unsigned int) 0); |
| 3904 | if (string[0] == 0) |
| 3905 | return(0); |
| 3906 | return(((unsigned int) string[0]) + |
| 3907 | (((unsigned int) string[1]) << 8)); |
| 3908 | } |
| 3909 | |
| 3910 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3911 | * xmlXPathCompareNodeSetFloat: |
| 3912 | * @ctxt: the XPath Parser context |
| 3913 | * @inf: less than (1) or greater than (0) |
| 3914 | * @strict: is the comparison strict |
| 3915 | * @arg: the node set |
| 3916 | * @f: the value |
| 3917 | * |
| 3918 | * Implement the compare operation between a nodeset and a number |
| 3919 | * @ns < @val (1, 1, ... |
| 3920 | * @ns <= @val (1, 0, ... |
| 3921 | * @ns > @val (0, 1, ... |
| 3922 | * @ns >= @val (0, 0, ... |
| 3923 | * |
| 3924 | * If one object to be compared is a node-set and the other is a number, |
| 3925 | * then the comparison will be true if and only if there is a node in the |
| 3926 | * node-set such that the result of performing the comparison on the number |
| 3927 | * to be compared and on the result of converting the string-value of that |
| 3928 | * node to a number using the number function is true. |
| 3929 | * |
| 3930 | * Returns 0 or 1 depending on the results of the test. |
| 3931 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3932 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3933 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 3934 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 3935 | int i, ret = 0; |
| 3936 | xmlNodeSetPtr ns; |
| 3937 | xmlChar *str2; |
| 3938 | |
| 3939 | if ((f == NULL) || (arg == NULL) || |
| 3940 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 3941 | xmlXPathFreeObject(arg); |
| 3942 | xmlXPathFreeObject(f); |
| 3943 | return(0); |
| 3944 | } |
| 3945 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 3946 | if (ns != NULL) { |
| 3947 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3948 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 3949 | if (str2 != NULL) { |
| 3950 | valuePush(ctxt, |
| 3951 | xmlXPathNewString(str2)); |
| 3952 | xmlFree(str2); |
| 3953 | xmlXPathNumberFunction(ctxt, 1); |
| 3954 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 3955 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 3956 | if (ret) |
| 3957 | break; |
| 3958 | } |
| 3959 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3960 | } |
| 3961 | xmlXPathFreeObject(arg); |
| 3962 | xmlXPathFreeObject(f); |
| 3963 | return(ret); |
| 3964 | } |
| 3965 | |
| 3966 | /** |
| 3967 | * xmlXPathCompareNodeSetString: |
| 3968 | * @ctxt: the XPath Parser context |
| 3969 | * @inf: less than (1) or greater than (0) |
| 3970 | * @strict: is the comparison strict |
| 3971 | * @arg: the node set |
| 3972 | * @s: the value |
| 3973 | * |
| 3974 | * Implement the compare operation between a nodeset and a string |
| 3975 | * @ns < @val (1, 1, ... |
| 3976 | * @ns <= @val (1, 0, ... |
| 3977 | * @ns > @val (0, 1, ... |
| 3978 | * @ns >= @val (0, 0, ... |
| 3979 | * |
| 3980 | * If one object to be compared is a node-set and the other is a string, |
| 3981 | * then the comparison will be true if and only if there is a node in |
| 3982 | * the node-set such that the result of performing the comparison on the |
| 3983 | * string-value of the node and the other string is true. |
| 3984 | * |
| 3985 | * Returns 0 or 1 depending on the results of the test. |
| 3986 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3987 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3988 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 3989 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 3990 | int i, ret = 0; |
| 3991 | xmlNodeSetPtr ns; |
| 3992 | xmlChar *str2; |
| 3993 | |
| 3994 | if ((s == NULL) || (arg == NULL) || |
| 3995 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 3996 | xmlXPathFreeObject(arg); |
| 3997 | xmlXPathFreeObject(s); |
| 3998 | return(0); |
| 3999 | } |
| 4000 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4001 | if (ns != NULL) { |
| 4002 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4003 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4004 | if (str2 != NULL) { |
| 4005 | valuePush(ctxt, |
| 4006 | xmlXPathNewString(str2)); |
| 4007 | xmlFree(str2); |
| 4008 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4009 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4010 | if (ret) |
| 4011 | break; |
| 4012 | } |
| 4013 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | } |
| 4015 | xmlXPathFreeObject(arg); |
| 4016 | xmlXPathFreeObject(s); |
| 4017 | return(ret); |
| 4018 | } |
| 4019 | |
| 4020 | /** |
| 4021 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4022 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4023 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4024 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4025 | * @arg2: the second node set object |
| 4026 | * |
| 4027 | * Implement the compare operation on nodesets: |
| 4028 | * |
| 4029 | * If both objects to be compared are node-sets, then the comparison |
| 4030 | * will be true if and only if there is a node in the first node-set |
| 4031 | * and a node in the second node-set such that the result of performing |
| 4032 | * the comparison on the string-values of the two nodes is true. |
| 4033 | * .... |
| 4034 | * When neither object to be compared is a node-set and the operator |
| 4035 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4036 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4037 | * .... |
| 4038 | * The number function converts its argument to a number as follows: |
| 4039 | * - a string that consists of optional whitespace followed by an |
| 4040 | * optional minus sign followed by a Number followed by whitespace |
| 4041 | * is converted to the IEEE 754 number that is nearest (according |
| 4042 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4043 | * represented by the string; any other string is converted to NaN |
| 4044 | * |
| 4045 | * Conclusion all nodes need to be converted first to their string value |
| 4046 | * and then the comparison must be done when possible |
| 4047 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4048 | static int |
| 4049 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4050 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4051 | int i, j, init = 0; |
| 4052 | double val1; |
| 4053 | double *values2; |
| 4054 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4055 | xmlNodeSetPtr ns1; |
| 4056 | xmlNodeSetPtr ns2; |
| 4057 | |
| 4058 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4059 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4060 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4061 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4062 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4063 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4064 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4065 | xmlXPathFreeObject(arg1); |
| 4066 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4067 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4068 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4069 | |
| 4070 | ns1 = arg1->nodesetval; |
| 4071 | ns2 = arg2->nodesetval; |
| 4072 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4073 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4074 | xmlXPathFreeObject(arg1); |
| 4075 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4076 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4077 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4078 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4079 | xmlXPathFreeObject(arg1); |
| 4080 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4081 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4082 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4083 | |
| 4084 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4085 | if (values2 == NULL) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4086 | xmlXPathFreeObject(arg1); |
| 4087 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4088 | return(0); |
| 4089 | } |
| 4090 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4091 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4092 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4093 | continue; |
| 4094 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4095 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4096 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4097 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4098 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4099 | continue; |
| 4100 | if (inf && strict) |
| 4101 | ret = (val1 < values2[j]); |
| 4102 | else if (inf && !strict) |
| 4103 | ret = (val1 <= values2[j]); |
| 4104 | else if (!inf && strict) |
| 4105 | ret = (val1 > values2[j]); |
| 4106 | else if (!inf && !strict) |
| 4107 | ret = (val1 >= values2[j]); |
| 4108 | if (ret) |
| 4109 | break; |
| 4110 | } |
| 4111 | if (ret) |
| 4112 | break; |
| 4113 | init = 1; |
| 4114 | } |
| 4115 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4116 | xmlXPathFreeObject(arg1); |
| 4117 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4118 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /** |
| 4122 | * xmlXPathCompareNodeSetValue: |
| 4123 | * @ctxt: the XPath Parser context |
| 4124 | * @inf: less than (1) or greater than (0) |
| 4125 | * @strict: is the comparison strict |
| 4126 | * @arg: the node set |
| 4127 | * @val: the value |
| 4128 | * |
| 4129 | * Implement the compare operation between a nodeset and a value |
| 4130 | * @ns < @val (1, 1, ... |
| 4131 | * @ns <= @val (1, 0, ... |
| 4132 | * @ns > @val (0, 1, ... |
| 4133 | * @ns >= @val (0, 0, ... |
| 4134 | * |
| 4135 | * If one object to be compared is a node-set and the other is a boolean, |
| 4136 | * then the comparison will be true if and only if the result of performing |
| 4137 | * the comparison on the boolean and on the result of converting |
| 4138 | * the node-set to a boolean using the boolean function is true. |
| 4139 | * |
| 4140 | * Returns 0 or 1 depending on the results of the test. |
| 4141 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4142 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4143 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4144 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4145 | if ((val == NULL) || (arg == NULL) || |
| 4146 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4147 | return(0); |
| 4148 | |
| 4149 | switch(val->type) { |
| 4150 | case XPATH_NUMBER: |
| 4151 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4152 | case XPATH_NODESET: |
| 4153 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4154 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4155 | case XPATH_STRING: |
| 4156 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4157 | case XPATH_BOOLEAN: |
| 4158 | valuePush(ctxt, arg); |
| 4159 | xmlXPathBooleanFunction(ctxt, 1); |
| 4160 | valuePush(ctxt, val); |
| 4161 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4162 | default: |
| 4163 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4164 | } |
| 4165 | return(0); |
| 4166 | } |
| 4167 | |
| 4168 | /** |
| 4169 | * xmlXPathEqualNodeSetString |
| 4170 | * @arg: the nodeset object argument |
| 4171 | * @str: the string to compare to. |
| 4172 | * |
| 4173 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4174 | * If one object to be compared is a node-set and the other is a string, |
| 4175 | * then the comparison will be true if and only if there is a node in |
| 4176 | * the node-set such that the result of performing the comparison on the |
| 4177 | * string-value of the node and the other string is true. |
| 4178 | * |
| 4179 | * Returns 0 or 1 depending on the results of the test. |
| 4180 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4181 | static int |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4182 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str) |
| 4183 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4184 | int i; |
| 4185 | xmlNodeSetPtr ns; |
| 4186 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4187 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4188 | |
| 4189 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4190 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4191 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4192 | ns = arg->nodesetval; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4193 | hash = xmlXPathStringHash(str); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4194 | if (ns == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4195 | return (0); |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 4196 | if (ns->nodeNr <= 0) { |
| 4197 | if (hash == 0) |
| 4198 | return(1); |
| 4199 | return(0); |
| 4200 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4201 | for (i = 0; i < ns->nodeNr; i++) { |
| 4202 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4203 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4204 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4205 | xmlFree(str2); |
| 4206 | return (1); |
| 4207 | } |
| 4208 | if (str2 != NULL) |
| 4209 | xmlFree(str2); |
| 4210 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4211 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4212 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | /** |
| 4216 | * xmlXPathEqualNodeSetFloat |
| 4217 | * @arg: the nodeset object argument |
| 4218 | * @f: the float to compare to |
| 4219 | * |
| 4220 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4221 | * If one object to be compared is a node-set and the other is a number, |
| 4222 | * then the comparison will be true if and only if there is a node in |
| 4223 | * the node-set such that the result of performing the comparison on the |
| 4224 | * number to be compared and on the result of converting the string-value |
| 4225 | * of that node to a number using the number function is true. |
| 4226 | * |
| 4227 | * Returns 0 or 1 depending on the results of the test. |
| 4228 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4229 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4230 | xmlXPathEqualNodeSetFloat(xmlXPathObjectPtr arg, double f) { |
| 4231 | char buf[100] = ""; |
| 4232 | |
| 4233 | if ((arg == NULL) || |
| 4234 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4235 | return(0); |
| 4236 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 4237 | xmlXPathFormatNumber(f, buf, sizeof(buf)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4238 | return(xmlXPathEqualNodeSetString(arg, BAD_CAST buf)); |
| 4239 | } |
| 4240 | |
| 4241 | |
| 4242 | /** |
| 4243 | * xmlXPathEqualNodeSets |
| 4244 | * @arg1: first nodeset object argument |
| 4245 | * @arg2: second nodeset object argument |
| 4246 | * |
| 4247 | * Implement the equal operation on XPath nodesets: @arg1 == @arg2 |
| 4248 | * If both objects to be compared are node-sets, then the comparison |
| 4249 | * will be true if and only if there is a node in the first node-set and |
| 4250 | * a node in the second node-set such that the result of performing the |
| 4251 | * comparison on the string-values of the two nodes is true. |
| 4252 | * |
| 4253 | * (needless to say, this is a costly operation) |
| 4254 | * |
| 4255 | * Returns 0 or 1 depending on the results of the test. |
| 4256 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4257 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4258 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4259 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4260 | unsigned int *hashs1; |
| 4261 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4262 | xmlChar **values1; |
| 4263 | xmlChar **values2; |
| 4264 | int ret = 0; |
| 4265 | xmlNodeSetPtr ns1; |
| 4266 | xmlNodeSetPtr ns2; |
| 4267 | |
| 4268 | if ((arg1 == NULL) || |
| 4269 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4270 | return(0); |
| 4271 | if ((arg2 == NULL) || |
| 4272 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4273 | return(0); |
| 4274 | |
| 4275 | ns1 = arg1->nodesetval; |
| 4276 | ns2 = arg2->nodesetval; |
| 4277 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4278 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4279 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4280 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4281 | return(0); |
| 4282 | |
| 4283 | /* |
| 4284 | * check if there is a node pertaining to both sets |
| 4285 | */ |
| 4286 | for (i = 0;i < ns1->nodeNr;i++) |
| 4287 | for (j = 0;j < ns2->nodeNr;j++) |
| 4288 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4289 | return(1); |
| 4290 | |
| 4291 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
| 4292 | if (values1 == NULL) |
| 4293 | return(0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4294 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4295 | if (hashs1 == NULL) { |
| 4296 | xmlFree(values1); |
| 4297 | return(0); |
| 4298 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4299 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4300 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4301 | if (values2 == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4302 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4303 | xmlFree(values1); |
| 4304 | return(0); |
| 4305 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4306 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4307 | if (hashs2 == NULL) { |
| 4308 | xmlFree(hashs1); |
| 4309 | xmlFree(values1); |
| 4310 | xmlFree(values2); |
| 4311 | return(0); |
| 4312 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4313 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4314 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4315 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4316 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4317 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4318 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
| 4319 | if (hashs1[i] == hashs2[j]) { |
| 4320 | if (values1[i] == NULL) |
| 4321 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4322 | if (values2[j] == NULL) |
| 4323 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
| 4324 | ret = xmlStrEqual(values1[i], values2[j]); |
| 4325 | if (ret) |
| 4326 | break; |
| 4327 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4328 | } |
| 4329 | if (ret) |
| 4330 | break; |
| 4331 | } |
| 4332 | for (i = 0;i < ns1->nodeNr;i++) |
| 4333 | if (values1[i] != NULL) |
| 4334 | xmlFree(values1[i]); |
| 4335 | for (j = 0;j < ns2->nodeNr;j++) |
| 4336 | if (values2[j] != NULL) |
| 4337 | xmlFree(values2[j]); |
| 4338 | xmlFree(values1); |
| 4339 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4340 | xmlFree(hashs1); |
| 4341 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | return(ret); |
| 4343 | } |
| 4344 | |
| 4345 | /** |
| 4346 | * xmlXPathEqualValues: |
| 4347 | * @ctxt: the XPath Parser context |
| 4348 | * |
| 4349 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4350 | * |
| 4351 | * Returns 0 or 1 depending on the results of the test. |
| 4352 | */ |
| 4353 | int |
| 4354 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4355 | xmlXPathObjectPtr arg1, arg2; |
| 4356 | int ret = 0; |
| 4357 | |
| 4358 | arg1 = valuePop(ctxt); |
| 4359 | if (arg1 == NULL) |
| 4360 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4361 | |
| 4362 | arg2 = valuePop(ctxt); |
| 4363 | if (arg2 == NULL) { |
| 4364 | xmlXPathFreeObject(arg1); |
| 4365 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4366 | } |
| 4367 | |
| 4368 | if (arg1 == arg2) { |
| 4369 | #ifdef DEBUG_EXPR |
| 4370 | xmlGenericError(xmlGenericErrorContext, |
| 4371 | "Equal: by pointer\n"); |
| 4372 | #endif |
| 4373 | return(1); |
| 4374 | } |
| 4375 | |
| 4376 | switch (arg1->type) { |
| 4377 | case XPATH_UNDEFINED: |
| 4378 | #ifdef DEBUG_EXPR |
| 4379 | xmlGenericError(xmlGenericErrorContext, |
| 4380 | "Equal: undefined\n"); |
| 4381 | #endif |
| 4382 | break; |
| 4383 | case XPATH_XSLT_TREE: |
| 4384 | case XPATH_NODESET: |
| 4385 | switch (arg2->type) { |
| 4386 | case XPATH_UNDEFINED: |
| 4387 | #ifdef DEBUG_EXPR |
| 4388 | xmlGenericError(xmlGenericErrorContext, |
| 4389 | "Equal: undefined\n"); |
| 4390 | #endif |
| 4391 | break; |
| 4392 | case XPATH_XSLT_TREE: |
| 4393 | case XPATH_NODESET: |
| 4394 | ret = xmlXPathEqualNodeSets(arg1, arg2); |
| 4395 | break; |
| 4396 | case XPATH_BOOLEAN: |
| 4397 | if ((arg1->nodesetval == NULL) || |
| 4398 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4399 | else |
| 4400 | ret = 1; |
| 4401 | ret = (ret == arg2->boolval); |
| 4402 | break; |
| 4403 | case XPATH_NUMBER: |
| 4404 | ret = xmlXPathEqualNodeSetFloat(arg1, arg2->floatval); |
| 4405 | break; |
| 4406 | case XPATH_STRING: |
| 4407 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval); |
| 4408 | break; |
| 4409 | case XPATH_USERS: |
| 4410 | case XPATH_POINT: |
| 4411 | case XPATH_RANGE: |
| 4412 | case XPATH_LOCATIONSET: |
| 4413 | TODO |
| 4414 | break; |
| 4415 | } |
| 4416 | break; |
| 4417 | case XPATH_BOOLEAN: |
| 4418 | switch (arg2->type) { |
| 4419 | case XPATH_UNDEFINED: |
| 4420 | #ifdef DEBUG_EXPR |
| 4421 | xmlGenericError(xmlGenericErrorContext, |
| 4422 | "Equal: undefined\n"); |
| 4423 | #endif |
| 4424 | break; |
| 4425 | case XPATH_NODESET: |
| 4426 | case XPATH_XSLT_TREE: |
| 4427 | if ((arg2->nodesetval == NULL) || |
Daniel Veillard | bd6e631 | 2002-04-01 08:04:14 +0000 | [diff] [blame] | 4428 | (arg2->nodesetval->nodeNr == 0)) |
| 4429 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4430 | else |
| 4431 | ret = 1; |
Daniel Veillard | bd6e631 | 2002-04-01 08:04:14 +0000 | [diff] [blame] | 4432 | ret = (ret == arg1->boolval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4433 | break; |
| 4434 | case XPATH_BOOLEAN: |
| 4435 | #ifdef DEBUG_EXPR |
| 4436 | xmlGenericError(xmlGenericErrorContext, |
| 4437 | "Equal: %d boolean %d \n", |
| 4438 | arg1->boolval, arg2->boolval); |
| 4439 | #endif |
| 4440 | ret = (arg1->boolval == arg2->boolval); |
| 4441 | break; |
| 4442 | case XPATH_NUMBER: |
| 4443 | if (arg2->floatval) ret = 1; |
| 4444 | else ret = 0; |
| 4445 | ret = (arg1->boolval == ret); |
| 4446 | break; |
| 4447 | case XPATH_STRING: |
| 4448 | if ((arg2->stringval == NULL) || |
| 4449 | (arg2->stringval[0] == 0)) ret = 0; |
| 4450 | else |
| 4451 | ret = 1; |
| 4452 | ret = (arg1->boolval == ret); |
| 4453 | break; |
| 4454 | case XPATH_USERS: |
| 4455 | case XPATH_POINT: |
| 4456 | case XPATH_RANGE: |
| 4457 | case XPATH_LOCATIONSET: |
| 4458 | TODO |
| 4459 | break; |
| 4460 | } |
| 4461 | break; |
| 4462 | case XPATH_NUMBER: |
| 4463 | switch (arg2->type) { |
| 4464 | case XPATH_UNDEFINED: |
| 4465 | #ifdef DEBUG_EXPR |
| 4466 | xmlGenericError(xmlGenericErrorContext, |
| 4467 | "Equal: undefined\n"); |
| 4468 | #endif |
| 4469 | break; |
| 4470 | case XPATH_NODESET: |
| 4471 | case XPATH_XSLT_TREE: |
| 4472 | ret = xmlXPathEqualNodeSetFloat(arg2, arg1->floatval); |
| 4473 | break; |
| 4474 | case XPATH_BOOLEAN: |
| 4475 | if (arg1->floatval) ret = 1; |
| 4476 | else ret = 0; |
| 4477 | ret = (arg2->boolval == ret); |
| 4478 | break; |
| 4479 | case XPATH_STRING: |
| 4480 | valuePush(ctxt, arg2); |
| 4481 | xmlXPathNumberFunction(ctxt, 1); |
| 4482 | arg2 = valuePop(ctxt); |
| 4483 | /* no break on purpose */ |
| 4484 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4485 | /* Hand check NaN and Infinity equalities */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4486 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
| 4487 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4488 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4489 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4490 | ret = 1; |
| 4491 | else |
| 4492 | ret = 0; |
| 4493 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4494 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4495 | ret = 1; |
| 4496 | else |
| 4497 | ret = 0; |
| 4498 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4499 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4500 | ret = 1; |
| 4501 | else |
| 4502 | ret = 0; |
| 4503 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4504 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4505 | ret = 1; |
| 4506 | else |
| 4507 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4508 | } else { |
| 4509 | ret = (arg1->floatval == arg2->floatval); |
| 4510 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4511 | break; |
| 4512 | case XPATH_USERS: |
| 4513 | case XPATH_POINT: |
| 4514 | case XPATH_RANGE: |
| 4515 | case XPATH_LOCATIONSET: |
| 4516 | TODO |
| 4517 | break; |
| 4518 | } |
| 4519 | break; |
| 4520 | case XPATH_STRING: |
| 4521 | switch (arg2->type) { |
| 4522 | case XPATH_UNDEFINED: |
| 4523 | #ifdef DEBUG_EXPR |
| 4524 | xmlGenericError(xmlGenericErrorContext, |
| 4525 | "Equal: undefined\n"); |
| 4526 | #endif |
| 4527 | break; |
| 4528 | case XPATH_NODESET: |
| 4529 | case XPATH_XSLT_TREE: |
| 4530 | ret = xmlXPathEqualNodeSetString(arg2, arg1->stringval); |
| 4531 | break; |
| 4532 | case XPATH_BOOLEAN: |
| 4533 | if ((arg1->stringval == NULL) || |
| 4534 | (arg1->stringval[0] == 0)) ret = 0; |
| 4535 | else |
| 4536 | ret = 1; |
| 4537 | ret = (arg2->boolval == ret); |
| 4538 | break; |
| 4539 | case XPATH_STRING: |
| 4540 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4541 | break; |
| 4542 | case XPATH_NUMBER: |
| 4543 | valuePush(ctxt, arg1); |
| 4544 | xmlXPathNumberFunction(ctxt, 1); |
| 4545 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4546 | /* Hand check NaN and Infinity equalities */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4547 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
| 4548 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4549 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4550 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4551 | ret = 1; |
| 4552 | else |
| 4553 | ret = 0; |
| 4554 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4555 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4556 | ret = 1; |
| 4557 | else |
| 4558 | ret = 0; |
| 4559 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4560 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4561 | ret = 1; |
| 4562 | else |
| 4563 | ret = 0; |
| 4564 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4565 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4566 | ret = 1; |
| 4567 | else |
| 4568 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4569 | } else { |
| 4570 | ret = (arg1->floatval == arg2->floatval); |
| 4571 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4572 | break; |
| 4573 | case XPATH_USERS: |
| 4574 | case XPATH_POINT: |
| 4575 | case XPATH_RANGE: |
| 4576 | case XPATH_LOCATIONSET: |
| 4577 | TODO |
| 4578 | break; |
| 4579 | } |
| 4580 | break; |
| 4581 | case XPATH_USERS: |
| 4582 | case XPATH_POINT: |
| 4583 | case XPATH_RANGE: |
| 4584 | case XPATH_LOCATIONSET: |
| 4585 | TODO |
| 4586 | break; |
| 4587 | } |
| 4588 | xmlXPathFreeObject(arg1); |
| 4589 | xmlXPathFreeObject(arg2); |
| 4590 | return(ret); |
| 4591 | } |
| 4592 | |
| 4593 | |
| 4594 | /** |
| 4595 | * xmlXPathCompareValues: |
| 4596 | * @ctxt: the XPath Parser context |
| 4597 | * @inf: less than (1) or greater than (0) |
| 4598 | * @strict: is the comparison strict |
| 4599 | * |
| 4600 | * Implement the compare operation on XPath objects: |
| 4601 | * @arg1 < @arg2 (1, 1, ... |
| 4602 | * @arg1 <= @arg2 (1, 0, ... |
| 4603 | * @arg1 > @arg2 (0, 1, ... |
| 4604 | * @arg1 >= @arg2 (0, 0, ... |
| 4605 | * |
| 4606 | * When neither object to be compared is a node-set and the operator is |
| 4607 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 4608 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 4609 | * comparison will be true if and only if the first number is less than the |
| 4610 | * second number. The <= comparison will be true if and only if the first |
| 4611 | * number is less than or equal to the second number. The > comparison |
| 4612 | * will be true if and only if the first number is greater than the second |
| 4613 | * number. The >= comparison will be true if and only if the first number |
| 4614 | * is greater than or equal to the second number. |
| 4615 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4616 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4617 | */ |
| 4618 | int |
| 4619 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4620 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4621 | xmlXPathObjectPtr arg1, arg2; |
| 4622 | |
| 4623 | arg2 = valuePop(ctxt); |
| 4624 | if (arg2 == NULL) { |
| 4625 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4626 | } |
| 4627 | |
| 4628 | arg1 = valuePop(ctxt); |
| 4629 | if (arg1 == NULL) { |
| 4630 | xmlXPathFreeObject(arg2); |
| 4631 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4632 | } |
| 4633 | |
| 4634 | if ((arg2->type == XPATH_NODESET) || (arg1->type == XPATH_NODESET)) { |
| 4635 | if ((arg2->type == XPATH_NODESET) && (arg1->type == XPATH_NODESET)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4636 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4637 | } else { |
| 4638 | if (arg1->type == XPATH_NODESET) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 4639 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 4640 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4641 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 4642 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 4643 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4644 | } |
| 4645 | } |
| 4646 | return(ret); |
| 4647 | } |
| 4648 | |
| 4649 | if (arg1->type != XPATH_NUMBER) { |
| 4650 | valuePush(ctxt, arg1); |
| 4651 | xmlXPathNumberFunction(ctxt, 1); |
| 4652 | arg1 = valuePop(ctxt); |
| 4653 | } |
| 4654 | if (arg1->type != XPATH_NUMBER) { |
| 4655 | xmlXPathFreeObject(arg1); |
| 4656 | xmlXPathFreeObject(arg2); |
| 4657 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4658 | } |
| 4659 | if (arg2->type != XPATH_NUMBER) { |
| 4660 | valuePush(ctxt, arg2); |
| 4661 | xmlXPathNumberFunction(ctxt, 1); |
| 4662 | arg2 = valuePop(ctxt); |
| 4663 | } |
| 4664 | if (arg2->type != XPATH_NUMBER) { |
| 4665 | xmlXPathFreeObject(arg1); |
| 4666 | xmlXPathFreeObject(arg2); |
| 4667 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4668 | } |
| 4669 | /* |
| 4670 | * Add tests for infinity and nan |
| 4671 | * => feedback on 3.4 for Inf and NaN |
| 4672 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4673 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4674 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4675 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4676 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4677 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 4678 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 4679 | if (inf && strict) { |
| 4680 | if ((arg1i == -1 && arg2i != -1) || |
| 4681 | (arg2i == 1 && arg1i != 1)) { |
| 4682 | ret = 1; |
| 4683 | } else if (arg1i == 0 && arg2i == 0) { |
| 4684 | ret = (arg1->floatval < arg2->floatval); |
| 4685 | } else { |
| 4686 | ret = 0; |
| 4687 | } |
| 4688 | } |
| 4689 | else if (inf && !strict) { |
| 4690 | if (arg1i == -1 || arg2i == 1) { |
| 4691 | ret = 1; |
| 4692 | } else if (arg1i == 0 && arg2i == 0) { |
| 4693 | ret = (arg1->floatval <= arg2->floatval); |
| 4694 | } else { |
| 4695 | ret = 0; |
| 4696 | } |
| 4697 | } |
| 4698 | else if (!inf && strict) { |
| 4699 | if ((arg1i == 1 && arg2i != 1) || |
| 4700 | (arg2i == -1 && arg1i != -1)) { |
| 4701 | ret = 1; |
| 4702 | } else if (arg1i == 0 && arg2i == 0) { |
| 4703 | ret = (arg1->floatval > arg2->floatval); |
| 4704 | } else { |
| 4705 | ret = 0; |
| 4706 | } |
| 4707 | } |
| 4708 | else if (!inf && !strict) { |
| 4709 | if (arg1i == 1 || arg2i == -1) { |
| 4710 | ret = 1; |
| 4711 | } else if (arg1i == 0 && arg2i == 0) { |
| 4712 | ret = (arg1->floatval >= arg2->floatval); |
| 4713 | } else { |
| 4714 | ret = 0; |
| 4715 | } |
| 4716 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4717 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4718 | xmlXPathFreeObject(arg1); |
| 4719 | xmlXPathFreeObject(arg2); |
| 4720 | return(ret); |
| 4721 | } |
| 4722 | |
| 4723 | /** |
| 4724 | * xmlXPathValueFlipSign: |
| 4725 | * @ctxt: the XPath Parser context |
| 4726 | * |
| 4727 | * Implement the unary - operation on an XPath object |
| 4728 | * The numeric operators convert their operands to numbers as if |
| 4729 | * by calling the number function. |
| 4730 | */ |
| 4731 | void |
| 4732 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4733 | CAST_TO_NUMBER; |
| 4734 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 4735 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 4736 | ctxt->value->floatval=xmlXPathNAN; |
| 4737 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 4738 | ctxt->value->floatval=xmlXPathNINF; |
| 4739 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 4740 | ctxt->value->floatval=xmlXPathPINF; |
| 4741 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4742 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 4743 | ctxt->value->floatval = xmlXPathNZERO; |
| 4744 | else |
| 4745 | ctxt->value->floatval = 0; |
| 4746 | } |
| 4747 | else |
| 4748 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | /** |
| 4752 | * xmlXPathAddValues: |
| 4753 | * @ctxt: the XPath Parser context |
| 4754 | * |
| 4755 | * Implement the add operation on XPath objects: |
| 4756 | * The numeric operators convert their operands to numbers as if |
| 4757 | * by calling the number function. |
| 4758 | */ |
| 4759 | void |
| 4760 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 4761 | xmlXPathObjectPtr arg; |
| 4762 | double val; |
| 4763 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4764 | arg = valuePop(ctxt); |
| 4765 | if (arg == NULL) |
| 4766 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 4767 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4768 | xmlXPathFreeObject(arg); |
| 4769 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4770 | CAST_TO_NUMBER; |
| 4771 | CHECK_TYPE(XPATH_NUMBER); |
| 4772 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | /** |
| 4776 | * xmlXPathSubValues: |
| 4777 | * @ctxt: the XPath Parser context |
| 4778 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4779 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4780 | * The numeric operators convert their operands to numbers as if |
| 4781 | * by calling the number function. |
| 4782 | */ |
| 4783 | void |
| 4784 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 4785 | xmlXPathObjectPtr arg; |
| 4786 | double val; |
| 4787 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4788 | arg = valuePop(ctxt); |
| 4789 | if (arg == NULL) |
| 4790 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 4791 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4792 | xmlXPathFreeObject(arg); |
| 4793 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4794 | CAST_TO_NUMBER; |
| 4795 | CHECK_TYPE(XPATH_NUMBER); |
| 4796 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /** |
| 4800 | * xmlXPathMultValues: |
| 4801 | * @ctxt: the XPath Parser context |
| 4802 | * |
| 4803 | * Implement the multiply operation on XPath objects: |
| 4804 | * The numeric operators convert their operands to numbers as if |
| 4805 | * by calling the number function. |
| 4806 | */ |
| 4807 | void |
| 4808 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 4809 | xmlXPathObjectPtr arg; |
| 4810 | double val; |
| 4811 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4812 | arg = valuePop(ctxt); |
| 4813 | if (arg == NULL) |
| 4814 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 4815 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4816 | xmlXPathFreeObject(arg); |
| 4817 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4818 | CAST_TO_NUMBER; |
| 4819 | CHECK_TYPE(XPATH_NUMBER); |
| 4820 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | /** |
| 4824 | * xmlXPathDivValues: |
| 4825 | * @ctxt: the XPath Parser context |
| 4826 | * |
| 4827 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 4828 | * The numeric operators convert their operands to numbers as if |
| 4829 | * by calling the number function. |
| 4830 | */ |
| 4831 | void |
| 4832 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 4833 | xmlXPathObjectPtr arg; |
| 4834 | double val; |
| 4835 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4836 | arg = valuePop(ctxt); |
| 4837 | if (arg == NULL) |
| 4838 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 4839 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4840 | xmlXPathFreeObject(arg); |
| 4841 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4842 | CAST_TO_NUMBER; |
| 4843 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 4844 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 4845 | ctxt->value->floatval = xmlXPathNAN; |
| 4846 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4847 | if (ctxt->value->floatval == 0) |
| 4848 | ctxt->value->floatval = xmlXPathNAN; |
| 4849 | else if (ctxt->value->floatval > 0) |
| 4850 | ctxt->value->floatval = xmlXPathNINF; |
| 4851 | else if (ctxt->value->floatval < 0) |
| 4852 | ctxt->value->floatval = xmlXPathPINF; |
| 4853 | } |
| 4854 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 4855 | if (ctxt->value->floatval == 0) |
| 4856 | ctxt->value->floatval = xmlXPathNAN; |
| 4857 | else if (ctxt->value->floatval > 0) |
| 4858 | ctxt->value->floatval = xmlXPathPINF; |
| 4859 | else if (ctxt->value->floatval < 0) |
| 4860 | ctxt->value->floatval = xmlXPathNINF; |
| 4861 | } else |
| 4862 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /** |
| 4866 | * xmlXPathModValues: |
| 4867 | * @ctxt: the XPath Parser context |
| 4868 | * |
| 4869 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 4870 | * The numeric operators convert their operands to numbers as if |
| 4871 | * by calling the number function. |
| 4872 | */ |
| 4873 | void |
| 4874 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 4875 | xmlXPathObjectPtr arg; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4876 | double arg1, arg2, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4877 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4878 | arg = valuePop(ctxt); |
| 4879 | if (arg == NULL) |
| 4880 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4881 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4882 | xmlXPathFreeObject(arg); |
| 4883 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4884 | CAST_TO_NUMBER; |
| 4885 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4886 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 4887 | if (arg2 == 0) |
| 4888 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 4889 | else { |
| 4890 | tmp=arg1/arg2; |
| 4891 | ctxt->value->floatval = arg2 * (tmp - (double)((int)tmp)); |
| 4892 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | /************************************************************************ |
| 4896 | * * |
| 4897 | * The traversal functions * |
| 4898 | * * |
| 4899 | ************************************************************************/ |
| 4900 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4901 | /* |
| 4902 | * A traversal function enumerates nodes along an axis. |
| 4903 | * Initially it must be called with NULL, and it indicates |
| 4904 | * termination on the axis by returning NULL. |
| 4905 | */ |
| 4906 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 4907 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 4908 | |
| 4909 | /** |
| 4910 | * xmlXPathNextSelf: |
| 4911 | * @ctxt: the XPath Parser context |
| 4912 | * @cur: the current node in the traversal |
| 4913 | * |
| 4914 | * Traversal function for the "self" direction |
| 4915 | * The self axis contains just the context node itself |
| 4916 | * |
| 4917 | * Returns the next element following that axis |
| 4918 | */ |
| 4919 | xmlNodePtr |
| 4920 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 4921 | if (cur == NULL) |
| 4922 | return(ctxt->context->node); |
| 4923 | return(NULL); |
| 4924 | } |
| 4925 | |
| 4926 | /** |
| 4927 | * xmlXPathNextChild: |
| 4928 | * @ctxt: the XPath Parser context |
| 4929 | * @cur: the current node in the traversal |
| 4930 | * |
| 4931 | * Traversal function for the "child" direction |
| 4932 | * The child axis contains the children of the context node in document order. |
| 4933 | * |
| 4934 | * Returns the next element following that axis |
| 4935 | */ |
| 4936 | xmlNodePtr |
| 4937 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 4938 | if (cur == NULL) { |
| 4939 | if (ctxt->context->node == NULL) return(NULL); |
| 4940 | switch (ctxt->context->node->type) { |
| 4941 | case XML_ELEMENT_NODE: |
| 4942 | case XML_TEXT_NODE: |
| 4943 | case XML_CDATA_SECTION_NODE: |
| 4944 | case XML_ENTITY_REF_NODE: |
| 4945 | case XML_ENTITY_NODE: |
| 4946 | case XML_PI_NODE: |
| 4947 | case XML_COMMENT_NODE: |
| 4948 | case XML_NOTATION_NODE: |
| 4949 | case XML_DTD_NODE: |
| 4950 | return(ctxt->context->node->children); |
| 4951 | case XML_DOCUMENT_NODE: |
| 4952 | case XML_DOCUMENT_TYPE_NODE: |
| 4953 | case XML_DOCUMENT_FRAG_NODE: |
| 4954 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4955 | #ifdef LIBXML_DOCB_ENABLED |
| 4956 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4957 | #endif |
| 4958 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 4959 | case XML_ELEMENT_DECL: |
| 4960 | case XML_ATTRIBUTE_DECL: |
| 4961 | case XML_ENTITY_DECL: |
| 4962 | case XML_ATTRIBUTE_NODE: |
| 4963 | case XML_NAMESPACE_DECL: |
| 4964 | case XML_XINCLUDE_START: |
| 4965 | case XML_XINCLUDE_END: |
| 4966 | return(NULL); |
| 4967 | } |
| 4968 | return(NULL); |
| 4969 | } |
| 4970 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4971 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 4972 | return(NULL); |
| 4973 | return(cur->next); |
| 4974 | } |
| 4975 | |
| 4976 | /** |
| 4977 | * xmlXPathNextDescendant: |
| 4978 | * @ctxt: the XPath Parser context |
| 4979 | * @cur: the current node in the traversal |
| 4980 | * |
| 4981 | * Traversal function for the "descendant" direction |
| 4982 | * the descendant axis contains the descendants of the context node in document |
| 4983 | * order; a descendant is a child or a child of a child and so on. |
| 4984 | * |
| 4985 | * Returns the next element following that axis |
| 4986 | */ |
| 4987 | xmlNodePtr |
| 4988 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 4989 | if (cur == NULL) { |
| 4990 | if (ctxt->context->node == NULL) |
| 4991 | return(NULL); |
| 4992 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 4993 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 4994 | return(NULL); |
| 4995 | |
| 4996 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 4997 | return(ctxt->context->doc->children); |
| 4998 | return(ctxt->context->node->children); |
| 4999 | } |
| 5000 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5001 | if (cur->children != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5002 | if (cur->children->type != XML_ENTITY_DECL) |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5003 | return(cur->children); |
| 5004 | } |
| 5005 | |
| 5006 | if (cur == ctxt->context->node) return(NULL); |
| 5007 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5008 | if (cur->next != NULL) return(cur->next); |
| 5009 | |
| 5010 | do { |
| 5011 | cur = cur->parent; |
| 5012 | if (cur == NULL) return(NULL); |
| 5013 | if (cur == ctxt->context->node) return(NULL); |
| 5014 | if (cur->next != NULL) { |
| 5015 | cur = cur->next; |
| 5016 | return(cur); |
| 5017 | } |
| 5018 | } while (cur != NULL); |
| 5019 | return(cur); |
| 5020 | } |
| 5021 | |
| 5022 | /** |
| 5023 | * xmlXPathNextDescendantOrSelf: |
| 5024 | * @ctxt: the XPath Parser context |
| 5025 | * @cur: the current node in the traversal |
| 5026 | * |
| 5027 | * Traversal function for the "descendant-or-self" direction |
| 5028 | * the descendant-or-self axis contains the context node and the descendants |
| 5029 | * of the context node in document order; thus the context node is the first |
| 5030 | * node on the axis, and the first child of the context node is the second node |
| 5031 | * on the axis |
| 5032 | * |
| 5033 | * Returns the next element following that axis |
| 5034 | */ |
| 5035 | xmlNodePtr |
| 5036 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5037 | if (cur == NULL) { |
| 5038 | if (ctxt->context->node == NULL) |
| 5039 | return(NULL); |
| 5040 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5041 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5042 | return(NULL); |
| 5043 | return(ctxt->context->node); |
| 5044 | } |
| 5045 | |
| 5046 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5047 | } |
| 5048 | |
| 5049 | /** |
| 5050 | * xmlXPathNextParent: |
| 5051 | * @ctxt: the XPath Parser context |
| 5052 | * @cur: the current node in the traversal |
| 5053 | * |
| 5054 | * Traversal function for the "parent" direction |
| 5055 | * The parent axis contains the parent of the context node, if there is one. |
| 5056 | * |
| 5057 | * Returns the next element following that axis |
| 5058 | */ |
| 5059 | xmlNodePtr |
| 5060 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5061 | /* |
| 5062 | * the parent of an attribute or namespace node is the element |
| 5063 | * to which the attribute or namespace node is attached |
| 5064 | * Namespace handling !!! |
| 5065 | */ |
| 5066 | if (cur == NULL) { |
| 5067 | if (ctxt->context->node == NULL) return(NULL); |
| 5068 | switch (ctxt->context->node->type) { |
| 5069 | case XML_ELEMENT_NODE: |
| 5070 | case XML_TEXT_NODE: |
| 5071 | case XML_CDATA_SECTION_NODE: |
| 5072 | case XML_ENTITY_REF_NODE: |
| 5073 | case XML_ENTITY_NODE: |
| 5074 | case XML_PI_NODE: |
| 5075 | case XML_COMMENT_NODE: |
| 5076 | case XML_NOTATION_NODE: |
| 5077 | case XML_DTD_NODE: |
| 5078 | case XML_ELEMENT_DECL: |
| 5079 | case XML_ATTRIBUTE_DECL: |
| 5080 | case XML_XINCLUDE_START: |
| 5081 | case XML_XINCLUDE_END: |
| 5082 | case XML_ENTITY_DECL: |
| 5083 | if (ctxt->context->node->parent == NULL) |
| 5084 | return((xmlNodePtr) ctxt->context->doc); |
| 5085 | return(ctxt->context->node->parent); |
| 5086 | case XML_ATTRIBUTE_NODE: { |
| 5087 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5088 | |
| 5089 | return(att->parent); |
| 5090 | } |
| 5091 | case XML_DOCUMENT_NODE: |
| 5092 | case XML_DOCUMENT_TYPE_NODE: |
| 5093 | case XML_DOCUMENT_FRAG_NODE: |
| 5094 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5095 | #ifdef LIBXML_DOCB_ENABLED |
| 5096 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5097 | #endif |
| 5098 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5099 | case XML_NAMESPACE_DECL: { |
| 5100 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5101 | |
| 5102 | if ((ns->next != NULL) && |
| 5103 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5104 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5106 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | return(NULL); |
| 5110 | } |
| 5111 | |
| 5112 | /** |
| 5113 | * xmlXPathNextAncestor: |
| 5114 | * @ctxt: the XPath Parser context |
| 5115 | * @cur: the current node in the traversal |
| 5116 | * |
| 5117 | * Traversal function for the "ancestor" direction |
| 5118 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5119 | * of the context node consist of the parent of context node and the parent's |
| 5120 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5121 | * parent is the first node on the axis, and the parent's parent is the second |
| 5122 | * node on the axis |
| 5123 | * |
| 5124 | * Returns the next element following that axis |
| 5125 | */ |
| 5126 | xmlNodePtr |
| 5127 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5128 | /* |
| 5129 | * the parent of an attribute or namespace node is the element |
| 5130 | * to which the attribute or namespace node is attached |
| 5131 | * !!!!!!!!!!!!! |
| 5132 | */ |
| 5133 | if (cur == NULL) { |
| 5134 | if (ctxt->context->node == NULL) return(NULL); |
| 5135 | switch (ctxt->context->node->type) { |
| 5136 | case XML_ELEMENT_NODE: |
| 5137 | case XML_TEXT_NODE: |
| 5138 | case XML_CDATA_SECTION_NODE: |
| 5139 | case XML_ENTITY_REF_NODE: |
| 5140 | case XML_ENTITY_NODE: |
| 5141 | case XML_PI_NODE: |
| 5142 | case XML_COMMENT_NODE: |
| 5143 | case XML_DTD_NODE: |
| 5144 | case XML_ELEMENT_DECL: |
| 5145 | case XML_ATTRIBUTE_DECL: |
| 5146 | case XML_ENTITY_DECL: |
| 5147 | case XML_NOTATION_NODE: |
| 5148 | case XML_XINCLUDE_START: |
| 5149 | case XML_XINCLUDE_END: |
| 5150 | if (ctxt->context->node->parent == NULL) |
| 5151 | return((xmlNodePtr) ctxt->context->doc); |
| 5152 | return(ctxt->context->node->parent); |
| 5153 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5154 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5155 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5156 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5157 | } |
| 5158 | case XML_DOCUMENT_NODE: |
| 5159 | case XML_DOCUMENT_TYPE_NODE: |
| 5160 | case XML_DOCUMENT_FRAG_NODE: |
| 5161 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5162 | #ifdef LIBXML_DOCB_ENABLED |
| 5163 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5164 | #endif |
| 5165 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5166 | case XML_NAMESPACE_DECL: { |
| 5167 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5168 | |
| 5169 | if ((ns->next != NULL) && |
| 5170 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5171 | return((xmlNodePtr) ns->next); |
| 5172 | /* Bad, how did that namespace ended-up there ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5175 | } |
| 5176 | return(NULL); |
| 5177 | } |
| 5178 | if (cur == ctxt->context->doc->children) |
| 5179 | return((xmlNodePtr) ctxt->context->doc); |
| 5180 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5181 | return(NULL); |
| 5182 | switch (cur->type) { |
| 5183 | case XML_ELEMENT_NODE: |
| 5184 | case XML_TEXT_NODE: |
| 5185 | case XML_CDATA_SECTION_NODE: |
| 5186 | case XML_ENTITY_REF_NODE: |
| 5187 | case XML_ENTITY_NODE: |
| 5188 | case XML_PI_NODE: |
| 5189 | case XML_COMMENT_NODE: |
| 5190 | case XML_NOTATION_NODE: |
| 5191 | case XML_DTD_NODE: |
| 5192 | case XML_ELEMENT_DECL: |
| 5193 | case XML_ATTRIBUTE_DECL: |
| 5194 | case XML_ENTITY_DECL: |
| 5195 | case XML_XINCLUDE_START: |
| 5196 | case XML_XINCLUDE_END: |
| 5197 | return(cur->parent); |
| 5198 | case XML_ATTRIBUTE_NODE: { |
| 5199 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5200 | |
| 5201 | return(att->parent); |
| 5202 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5203 | case XML_NAMESPACE_DECL: { |
| 5204 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5205 | |
| 5206 | if ((ns->next != NULL) && |
| 5207 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5208 | return((xmlNodePtr) ns->next); |
| 5209 | /* Bad, how did that namespace ended-up there ? */ |
| 5210 | return(NULL); |
| 5211 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5212 | case XML_DOCUMENT_NODE: |
| 5213 | case XML_DOCUMENT_TYPE_NODE: |
| 5214 | case XML_DOCUMENT_FRAG_NODE: |
| 5215 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5216 | #ifdef LIBXML_DOCB_ENABLED |
| 5217 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5218 | #endif |
| 5219 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5220 | } |
| 5221 | return(NULL); |
| 5222 | } |
| 5223 | |
| 5224 | /** |
| 5225 | * xmlXPathNextAncestorOrSelf: |
| 5226 | * @ctxt: the XPath Parser context |
| 5227 | * @cur: the current node in the traversal |
| 5228 | * |
| 5229 | * Traversal function for the "ancestor-or-self" direction |
| 5230 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5231 | * the context node in reverse document order; thus the context node is |
| 5232 | * the first node on the axis, and the context node's parent the second; |
| 5233 | * parent here is defined the same as with the parent axis. |
| 5234 | * |
| 5235 | * Returns the next element following that axis |
| 5236 | */ |
| 5237 | xmlNodePtr |
| 5238 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5239 | if (cur == NULL) |
| 5240 | return(ctxt->context->node); |
| 5241 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5242 | } |
| 5243 | |
| 5244 | /** |
| 5245 | * xmlXPathNextFollowingSibling: |
| 5246 | * @ctxt: the XPath Parser context |
| 5247 | * @cur: the current node in the traversal |
| 5248 | * |
| 5249 | * Traversal function for the "following-sibling" direction |
| 5250 | * The following-sibling axis contains the following siblings of the context |
| 5251 | * node in document order. |
| 5252 | * |
| 5253 | * Returns the next element following that axis |
| 5254 | */ |
| 5255 | xmlNodePtr |
| 5256 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5257 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5258 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5259 | return(NULL); |
| 5260 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5261 | return(NULL); |
| 5262 | if (cur == NULL) |
| 5263 | return(ctxt->context->node->next); |
| 5264 | return(cur->next); |
| 5265 | } |
| 5266 | |
| 5267 | /** |
| 5268 | * xmlXPathNextPrecedingSibling: |
| 5269 | * @ctxt: the XPath Parser context |
| 5270 | * @cur: the current node in the traversal |
| 5271 | * |
| 5272 | * Traversal function for the "preceding-sibling" direction |
| 5273 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5274 | * node in reverse document order; the first preceding sibling is first on the |
| 5275 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5276 | * |
| 5277 | * Returns the next element following that axis |
| 5278 | */ |
| 5279 | xmlNodePtr |
| 5280 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5281 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5282 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5283 | return(NULL); |
| 5284 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5285 | return(NULL); |
| 5286 | if (cur == NULL) |
| 5287 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5288 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5289 | cur = cur->prev; |
| 5290 | if (cur == NULL) |
| 5291 | return(ctxt->context->node->prev); |
| 5292 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5293 | return(cur->prev); |
| 5294 | } |
| 5295 | |
| 5296 | /** |
| 5297 | * xmlXPathNextFollowing: |
| 5298 | * @ctxt: the XPath Parser context |
| 5299 | * @cur: the current node in the traversal |
| 5300 | * |
| 5301 | * Traversal function for the "following" direction |
| 5302 | * The following axis contains all nodes in the same document as the context |
| 5303 | * node that are after the context node in document order, excluding any |
| 5304 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5305 | * are ordered in document order |
| 5306 | * |
| 5307 | * Returns the next element following that axis |
| 5308 | */ |
| 5309 | xmlNodePtr |
| 5310 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5311 | if (cur != NULL && cur->children != NULL) |
| 5312 | return cur->children ; |
| 5313 | if (cur == NULL) cur = ctxt->context->node; |
| 5314 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5315 | if (cur->next != NULL) return(cur->next) ; |
| 5316 | do { |
| 5317 | cur = cur->parent; |
| 5318 | if (cur == NULL) return(NULL); |
| 5319 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5320 | if (cur->next != NULL) return(cur->next); |
| 5321 | } while (cur != NULL); |
| 5322 | return(cur); |
| 5323 | } |
| 5324 | |
| 5325 | /* |
| 5326 | * xmlXPathIsAncestor: |
| 5327 | * @ancestor: the ancestor node |
| 5328 | * @node: the current node |
| 5329 | * |
| 5330 | * Check that @ancestor is a @node's ancestor |
| 5331 | * |
| 5332 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5333 | */ |
| 5334 | static int |
| 5335 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5336 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5337 | /* nodes need to be in the same document */ |
| 5338 | if (ancestor->doc != node->doc) return(0); |
| 5339 | /* avoid searching if ancestor or node is the root node */ |
| 5340 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5341 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5342 | while (node->parent != NULL) { |
| 5343 | if (node->parent == ancestor) |
| 5344 | return(1); |
| 5345 | node = node->parent; |
| 5346 | } |
| 5347 | return(0); |
| 5348 | } |
| 5349 | |
| 5350 | /** |
| 5351 | * xmlXPathNextPreceding: |
| 5352 | * @ctxt: the XPath Parser context |
| 5353 | * @cur: the current node in the traversal |
| 5354 | * |
| 5355 | * Traversal function for the "preceding" direction |
| 5356 | * the preceding axis contains all nodes in the same document as the context |
| 5357 | * node that are before the context node in document order, excluding any |
| 5358 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5359 | * ordered in reverse document order |
| 5360 | * |
| 5361 | * Returns the next element following that axis |
| 5362 | */ |
| 5363 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5364 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5365 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5366 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5367 | cur = ctxt->context->node; |
| 5368 | if (cur == NULL) |
| 5369 | return (NULL); |
| 5370 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5371 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5372 | do { |
| 5373 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5374 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5375 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5379 | if (cur == NULL) |
| 5380 | return (NULL); |
| 5381 | if (cur == ctxt->context->doc->children) |
| 5382 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5383 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5384 | return (cur); |
| 5385 | } |
| 5386 | |
| 5387 | /** |
| 5388 | * xmlXPathNextPrecedingInternal: |
| 5389 | * @ctxt: the XPath Parser context |
| 5390 | * @cur: the current node in the traversal |
| 5391 | * |
| 5392 | * Traversal function for the "preceding" direction |
| 5393 | * the preceding axis contains all nodes in the same document as the context |
| 5394 | * node that are before the context node in document order, excluding any |
| 5395 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5396 | * ordered in reverse document order |
| 5397 | * This is a faster implementation but internal only since it requires a |
| 5398 | * state kept in the parser context: ctxt->ancestor. |
| 5399 | * |
| 5400 | * Returns the next element following that axis |
| 5401 | */ |
| 5402 | static xmlNodePtr |
| 5403 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5404 | xmlNodePtr cur) |
| 5405 | { |
| 5406 | if (cur == NULL) { |
| 5407 | cur = ctxt->context->node; |
| 5408 | if (cur == NULL) |
| 5409 | return (NULL); |
| 5410 | ctxt->ancestor = cur->parent; |
| 5411 | } |
| 5412 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5413 | cur = cur->prev; |
| 5414 | while (cur->prev == NULL) { |
| 5415 | cur = cur->parent; |
| 5416 | if (cur == NULL) |
| 5417 | return (NULL); |
| 5418 | if (cur == ctxt->context->doc->children) |
| 5419 | return (NULL); |
| 5420 | if (cur != ctxt->ancestor) |
| 5421 | return (cur); |
| 5422 | ctxt->ancestor = cur->parent; |
| 5423 | } |
| 5424 | cur = cur->prev; |
| 5425 | while (cur->last != NULL) |
| 5426 | cur = cur->last; |
| 5427 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | /** |
| 5431 | * xmlXPathNextNamespace: |
| 5432 | * @ctxt: the XPath Parser context |
| 5433 | * @cur: the current attribute in the traversal |
| 5434 | * |
| 5435 | * Traversal function for the "namespace" direction |
| 5436 | * the namespace axis contains the namespace nodes of the context node; |
| 5437 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5438 | * be empty unless the context node is an element |
| 5439 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5440 | * We keep the XML namespace node at the end of the list. |
| 5441 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5442 | * Returns the next element following that axis |
| 5443 | */ |
| 5444 | xmlNodePtr |
| 5445 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5446 | xmlNodePtr ret; |
| 5447 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5448 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5449 | if (cur == (xmlNodePtr) xmlXPathXMLNamespace) |
| 5450 | return(NULL); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5451 | if ((cur == NULL) || (ctxt->context->tmpNsList == NULL)) { |
| 5452 | if (ctxt->context->tmpNsList != NULL) |
| 5453 | xmlFree(ctxt->context->tmpNsList); |
| 5454 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5455 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5456 | if (ctxt->context->tmpNsList == NULL) return(NULL); |
| 5457 | ctxt->context->tmpNsNr = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5458 | } |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5459 | ret = (xmlNodePtr)ctxt->context->tmpNsList[ctxt->context->tmpNsNr++]; |
| 5460 | if (ret == NULL) { |
| 5461 | xmlFree(ctxt->context->tmpNsList); |
| 5462 | ctxt->context->tmpNsList = NULL; |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5463 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5464 | } |
| 5465 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | /** |
| 5469 | * xmlXPathNextAttribute: |
| 5470 | * @ctxt: the XPath Parser context |
| 5471 | * @cur: the current attribute in the traversal |
| 5472 | * |
| 5473 | * Traversal function for the "attribute" direction |
| 5474 | * TODO: support DTD inherited default attributes |
| 5475 | * |
| 5476 | * Returns the next element following that axis |
| 5477 | */ |
| 5478 | xmlNodePtr |
| 5479 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5480 | if (ctxt->context->node == NULL) |
| 5481 | return(NULL); |
| 5482 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 5483 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5484 | if (cur == NULL) { |
| 5485 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5486 | return(NULL); |
| 5487 | return((xmlNodePtr)ctxt->context->node->properties); |
| 5488 | } |
| 5489 | return((xmlNodePtr)cur->next); |
| 5490 | } |
| 5491 | |
| 5492 | /************************************************************************ |
| 5493 | * * |
| 5494 | * NodeTest Functions * |
| 5495 | * * |
| 5496 | ************************************************************************/ |
| 5497 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5498 | #define IS_FUNCTION 200 |
| 5499 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | |
| 5501 | /************************************************************************ |
| 5502 | * * |
| 5503 | * Implicit tree core function library * |
| 5504 | * * |
| 5505 | ************************************************************************/ |
| 5506 | |
| 5507 | /** |
| 5508 | * xmlXPathRoot: |
| 5509 | * @ctxt: the XPath Parser context |
| 5510 | * |
| 5511 | * Initialize the context to the root of the document |
| 5512 | */ |
| 5513 | void |
| 5514 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
| 5515 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 5516 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5517 | } |
| 5518 | |
| 5519 | /************************************************************************ |
| 5520 | * * |
| 5521 | * The explicit core function library * |
| 5522 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 5523 | * * |
| 5524 | ************************************************************************/ |
| 5525 | |
| 5526 | |
| 5527 | /** |
| 5528 | * xmlXPathLastFunction: |
| 5529 | * @ctxt: the XPath Parser context |
| 5530 | * @nargs: the number of arguments |
| 5531 | * |
| 5532 | * Implement the last() XPath function |
| 5533 | * number last() |
| 5534 | * The last function returns the number of nodes in the context node list. |
| 5535 | */ |
| 5536 | void |
| 5537 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5538 | CHECK_ARITY(0); |
| 5539 | if (ctxt->context->contextSize >= 0) { |
| 5540 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 5541 | #ifdef DEBUG_EXPR |
| 5542 | xmlGenericError(xmlGenericErrorContext, |
| 5543 | "last() : %d\n", ctxt->context->contextSize); |
| 5544 | #endif |
| 5545 | } else { |
| 5546 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | /** |
| 5551 | * xmlXPathPositionFunction: |
| 5552 | * @ctxt: the XPath Parser context |
| 5553 | * @nargs: the number of arguments |
| 5554 | * |
| 5555 | * Implement the position() XPath function |
| 5556 | * number position() |
| 5557 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5558 | * 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] | 5559 | * will be equal to last(). |
| 5560 | */ |
| 5561 | void |
| 5562 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5563 | CHECK_ARITY(0); |
| 5564 | if (ctxt->context->proximityPosition >= 0) { |
| 5565 | valuePush(ctxt, |
| 5566 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 5567 | #ifdef DEBUG_EXPR |
| 5568 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 5569 | ctxt->context->proximityPosition); |
| 5570 | #endif |
| 5571 | } else { |
| 5572 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | /** |
| 5577 | * xmlXPathCountFunction: |
| 5578 | * @ctxt: the XPath Parser context |
| 5579 | * @nargs: the number of arguments |
| 5580 | * |
| 5581 | * Implement the count() XPath function |
| 5582 | * number count(node-set) |
| 5583 | */ |
| 5584 | void |
| 5585 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5586 | xmlXPathObjectPtr cur; |
| 5587 | |
| 5588 | CHECK_ARITY(1); |
| 5589 | if ((ctxt->value == NULL) || |
| 5590 | ((ctxt->value->type != XPATH_NODESET) && |
| 5591 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 5592 | XP_ERROR(XPATH_INVALID_TYPE); |
| 5593 | cur = valuePop(ctxt); |
| 5594 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5595 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 5596 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 5597 | else if (cur->type == XPATH_NODESET) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5598 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 5599 | } else { |
| 5600 | if ((cur->nodesetval->nodeNr != 1) || |
| 5601 | (cur->nodesetval->nodeTab == NULL)) { |
| 5602 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 5603 | } else { |
| 5604 | xmlNodePtr tmp; |
| 5605 | int i = 0; |
| 5606 | |
| 5607 | tmp = cur->nodesetval->nodeTab[0]; |
| 5608 | if (tmp != NULL) { |
| 5609 | tmp = tmp->children; |
| 5610 | while (tmp != NULL) { |
| 5611 | tmp = tmp->next; |
| 5612 | i++; |
| 5613 | } |
| 5614 | } |
| 5615 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 5616 | } |
| 5617 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5618 | xmlXPathFreeObject(cur); |
| 5619 | } |
| 5620 | |
| 5621 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5622 | * xmlXPathGetElementsByIds: |
| 5623 | * @doc: the document |
| 5624 | * @ids: a whitespace separated list of IDs |
| 5625 | * |
| 5626 | * Selects elements by their unique ID. |
| 5627 | * |
| 5628 | * Returns a node-set of selected elements. |
| 5629 | */ |
| 5630 | static xmlNodeSetPtr |
| 5631 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 5632 | xmlNodeSetPtr ret; |
| 5633 | const xmlChar *cur = ids; |
| 5634 | xmlChar *ID; |
| 5635 | xmlAttrPtr attr; |
| 5636 | xmlNodePtr elem = NULL; |
| 5637 | |
| 5638 | ret = xmlXPathNodeSetCreate(NULL); |
| 5639 | |
| 5640 | while (IS_BLANK(*cur)) cur++; |
| 5641 | while (*cur != 0) { |
| 5642 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 5643 | (*cur == '.') || (*cur == '-') || |
| 5644 | (*cur == '_') || (*cur == ':') || |
| 5645 | (IS_COMBINING(*cur)) || |
| 5646 | (IS_EXTENDER(*cur))) |
| 5647 | cur++; |
| 5648 | |
| 5649 | if ((!IS_BLANK(*cur)) && (*cur != 0)) break; |
| 5650 | |
| 5651 | ID = xmlStrndup(ids, cur - ids); |
| 5652 | attr = xmlGetID(doc, ID); |
| 5653 | if (attr != NULL) { |
| 5654 | elem = attr->parent; |
| 5655 | xmlXPathNodeSetAdd(ret, elem); |
| 5656 | } |
| 5657 | if (ID != NULL) |
| 5658 | xmlFree(ID); |
| 5659 | |
| 5660 | while (IS_BLANK(*cur)) cur++; |
| 5661 | ids = cur; |
| 5662 | } |
| 5663 | return(ret); |
| 5664 | } |
| 5665 | |
| 5666 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5667 | * xmlXPathIdFunction: |
| 5668 | * @ctxt: the XPath Parser context |
| 5669 | * @nargs: the number of arguments |
| 5670 | * |
| 5671 | * Implement the id() XPath function |
| 5672 | * node-set id(object) |
| 5673 | * The id function selects elements by their unique ID |
| 5674 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 5675 | * then the result is the union of the result of applying id to the |
| 5676 | * string value of each of the nodes in the argument node-set. When the |
| 5677 | * argument to id is of any other type, the argument is converted to a |
| 5678 | * string as if by a call to the string function; the string is split |
| 5679 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 5680 | * of characters matching the production S); the result is a node-set |
| 5681 | * containing the elements in the same document as the context node that |
| 5682 | * have a unique ID equal to any of the tokens in the list. |
| 5683 | */ |
| 5684 | void |
| 5685 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5686 | xmlChar *tokens; |
| 5687 | xmlNodeSetPtr ret; |
| 5688 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5689 | |
| 5690 | CHECK_ARITY(1); |
| 5691 | obj = valuePop(ctxt); |
| 5692 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
| 5693 | if (obj->type == XPATH_NODESET) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5694 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5695 | int i; |
| 5696 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5697 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5698 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5699 | if (obj->nodesetval != NULL) { |
| 5700 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5701 | tokens = |
| 5702 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 5703 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 5704 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 5705 | xmlXPathFreeNodeSet(ns); |
| 5706 | if (tokens != NULL) |
| 5707 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5712 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5713 | return; |
| 5714 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5715 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5716 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5717 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 5718 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5720 | xmlXPathFreeObject(obj); |
| 5721 | return; |
| 5722 | } |
| 5723 | |
| 5724 | /** |
| 5725 | * xmlXPathLocalNameFunction: |
| 5726 | * @ctxt: the XPath Parser context |
| 5727 | * @nargs: the number of arguments |
| 5728 | * |
| 5729 | * Implement the local-name() XPath function |
| 5730 | * string local-name(node-set?) |
| 5731 | * The local-name function returns a string containing the local part |
| 5732 | * of the name of the node in the argument node-set that is first in |
| 5733 | * document order. If the node-set is empty or the first node has no |
| 5734 | * name, an empty string is returned. If the argument is omitted it |
| 5735 | * defaults to the context node. |
| 5736 | */ |
| 5737 | void |
| 5738 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5739 | xmlXPathObjectPtr cur; |
| 5740 | |
| 5741 | if (nargs == 0) { |
| 5742 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5743 | nargs = 1; |
| 5744 | } |
| 5745 | |
| 5746 | CHECK_ARITY(1); |
| 5747 | if ((ctxt->value == NULL) || |
| 5748 | ((ctxt->value->type != XPATH_NODESET) && |
| 5749 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 5750 | XP_ERROR(XPATH_INVALID_TYPE); |
| 5751 | cur = valuePop(ctxt); |
| 5752 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5753 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | valuePush(ctxt, xmlXPathNewCString("")); |
| 5755 | } else { |
| 5756 | int i = 0; /* Should be first in document order !!!!! */ |
| 5757 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 5758 | case XML_ELEMENT_NODE: |
| 5759 | case XML_ATTRIBUTE_NODE: |
| 5760 | case XML_PI_NODE: |
| 5761 | valuePush(ctxt, |
| 5762 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 5763 | break; |
| 5764 | case XML_NAMESPACE_DECL: |
| 5765 | valuePush(ctxt, xmlXPathNewString( |
| 5766 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 5767 | break; |
| 5768 | default: |
| 5769 | valuePush(ctxt, xmlXPathNewCString("")); |
| 5770 | } |
| 5771 | } |
| 5772 | xmlXPathFreeObject(cur); |
| 5773 | } |
| 5774 | |
| 5775 | /** |
| 5776 | * xmlXPathNamespaceURIFunction: |
| 5777 | * @ctxt: the XPath Parser context |
| 5778 | * @nargs: the number of arguments |
| 5779 | * |
| 5780 | * Implement the namespace-uri() XPath function |
| 5781 | * string namespace-uri(node-set?) |
| 5782 | * The namespace-uri function returns a string containing the |
| 5783 | * namespace URI of the expanded name of the node in the argument |
| 5784 | * node-set that is first in document order. If the node-set is empty, |
| 5785 | * the first node has no name, or the expanded name has no namespace |
| 5786 | * URI, an empty string is returned. If the argument is omitted it |
| 5787 | * defaults to the context node. |
| 5788 | */ |
| 5789 | void |
| 5790 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5791 | xmlXPathObjectPtr cur; |
| 5792 | |
| 5793 | if (nargs == 0) { |
| 5794 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5795 | nargs = 1; |
| 5796 | } |
| 5797 | CHECK_ARITY(1); |
| 5798 | if ((ctxt->value == NULL) || |
| 5799 | ((ctxt->value->type != XPATH_NODESET) && |
| 5800 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 5801 | XP_ERROR(XPATH_INVALID_TYPE); |
| 5802 | cur = valuePop(ctxt); |
| 5803 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5804 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5805 | valuePush(ctxt, xmlXPathNewCString("")); |
| 5806 | } else { |
| 5807 | int i = 0; /* Should be first in document order !!!!! */ |
| 5808 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 5809 | case XML_ELEMENT_NODE: |
| 5810 | case XML_ATTRIBUTE_NODE: |
| 5811 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 5812 | valuePush(ctxt, xmlXPathNewCString("")); |
| 5813 | else |
| 5814 | valuePush(ctxt, xmlXPathNewString( |
| 5815 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 5816 | break; |
| 5817 | default: |
| 5818 | valuePush(ctxt, xmlXPathNewCString("")); |
| 5819 | } |
| 5820 | } |
| 5821 | xmlXPathFreeObject(cur); |
| 5822 | } |
| 5823 | |
| 5824 | /** |
| 5825 | * xmlXPathNameFunction: |
| 5826 | * @ctxt: the XPath Parser context |
| 5827 | * @nargs: the number of arguments |
| 5828 | * |
| 5829 | * Implement the name() XPath function |
| 5830 | * string name(node-set?) |
| 5831 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5832 | * 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] | 5833 | * order. The QName must represent the name with respect to the namespace |
| 5834 | * declarations in effect on the node whose name is being represented. |
| 5835 | * Typically, this will be the form in which the name occurred in the XML |
| 5836 | * source. This need not be the case if there are namespace declarations |
| 5837 | * in effect on the node that associate multiple prefixes with the same |
| 5838 | * namespace. However, an implementation may include information about |
| 5839 | * the original prefix in its representation of nodes; in this case, an |
| 5840 | * implementation can ensure that the returned string is always the same |
| 5841 | * as the QName used in the XML source. If the argument it omitted it |
| 5842 | * defaults to the context node. |
| 5843 | * Libxml keep the original prefix so the "real qualified name" used is |
| 5844 | * returned. |
| 5845 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5846 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5847 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 5848 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5849 | xmlXPathObjectPtr cur; |
| 5850 | |
| 5851 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5852 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5853 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5857 | if ((ctxt->value == NULL) || |
| 5858 | ((ctxt->value->type != XPATH_NODESET) && |
| 5859 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 5860 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5861 | cur = valuePop(ctxt); |
| 5862 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5863 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5864 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5865 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5866 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5867 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 5868 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 5869 | case XML_ELEMENT_NODE: |
| 5870 | case XML_ATTRIBUTE_NODE: |
| 5871 | if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 5872 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) |
| 5873 | valuePush(ctxt, |
| 5874 | xmlXPathNewString(cur->nodesetval-> |
| 5875 | nodeTab[i]->name)); |
| 5876 | |
| 5877 | else { |
| 5878 | char name[2000]; |
| 5879 | |
| 5880 | snprintf(name, sizeof(name), "%s:%s", |
| 5881 | (char *) cur->nodesetval->nodeTab[i]->ns-> |
| 5882 | prefix, |
| 5883 | (char *) cur->nodesetval->nodeTab[i]->name); |
| 5884 | name[sizeof(name) - 1] = 0; |
| 5885 | valuePush(ctxt, xmlXPathNewCString(name)); |
| 5886 | } |
| 5887 | break; |
| 5888 | default: |
| 5889 | valuePush(ctxt, |
| 5890 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 5891 | xmlXPathLocalNameFunction(ctxt, 1); |
| 5892 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5893 | } |
| 5894 | xmlXPathFreeObject(cur); |
| 5895 | } |
| 5896 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 5897 | |
| 5898 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5899 | * xmlXPathStringFunction: |
| 5900 | * @ctxt: the XPath Parser context |
| 5901 | * @nargs: the number of arguments |
| 5902 | * |
| 5903 | * Implement the string() XPath function |
| 5904 | * string string(object?) |
| 5905 | * he string function converts an object to a string as follows: |
| 5906 | * - A node-set is converted to a string by returning the value of |
| 5907 | * the node in the node-set that is first in document order. |
| 5908 | * If the node-set is empty, an empty string is returned. |
| 5909 | * - A number is converted to a string as follows |
| 5910 | * + NaN is converted to the string NaN |
| 5911 | * + positive zero is converted to the string 0 |
| 5912 | * + negative zero is converted to the string 0 |
| 5913 | * + positive infinity is converted to the string Infinity |
| 5914 | * + negative infinity is converted to the string -Infinity |
| 5915 | * + if the number is an integer, the number is represented in |
| 5916 | * decimal form as a Number with no decimal point and no leading |
| 5917 | * zeros, preceded by a minus sign (-) if the number is negative |
| 5918 | * + otherwise, the number is represented in decimal form as a |
| 5919 | * Number including a decimal point with at least one digit |
| 5920 | * before the decimal point and at least one digit after the |
| 5921 | * decimal point, preceded by a minus sign (-) if the number |
| 5922 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5923 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5924 | * before the decimal point; beyond the one required digit |
| 5925 | * after the decimal point there must be as many, but only as |
| 5926 | * many, more digits as are needed to uniquely distinguish the |
| 5927 | * number from all other IEEE 754 numeric values. |
| 5928 | * - The boolean false value is converted to the string false. |
| 5929 | * The boolean true value is converted to the string true. |
| 5930 | * |
| 5931 | * If the argument is omitted, it defaults to a node-set with the |
| 5932 | * context node as its only member. |
| 5933 | */ |
| 5934 | void |
| 5935 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5936 | xmlXPathObjectPtr cur; |
| 5937 | |
| 5938 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5939 | valuePush(ctxt, |
| 5940 | xmlXPathWrapString( |
| 5941 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 5942 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | CHECK_ARITY(1); |
| 5946 | cur = valuePop(ctxt); |
| 5947 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 5948 | cur = xmlXPathConvertString(cur); |
| 5949 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | /** |
| 5953 | * xmlXPathStringLengthFunction: |
| 5954 | * @ctxt: the XPath Parser context |
| 5955 | * @nargs: the number of arguments |
| 5956 | * |
| 5957 | * Implement the string-length() XPath function |
| 5958 | * number string-length(string?) |
| 5959 | * The string-length returns the number of characters in the string |
| 5960 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 5961 | * the context node converted to a string, in other words the value |
| 5962 | * of the context node. |
| 5963 | */ |
| 5964 | void |
| 5965 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5966 | xmlXPathObjectPtr cur; |
| 5967 | |
| 5968 | if (nargs == 0) { |
| 5969 | if (ctxt->context->node == NULL) { |
| 5970 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 5971 | } else { |
| 5972 | xmlChar *content; |
| 5973 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5974 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 5975 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5976 | xmlFree(content); |
| 5977 | } |
| 5978 | return; |
| 5979 | } |
| 5980 | CHECK_ARITY(1); |
| 5981 | CAST_TO_STRING; |
| 5982 | CHECK_TYPE(XPATH_STRING); |
| 5983 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 5984 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5985 | xmlXPathFreeObject(cur); |
| 5986 | } |
| 5987 | |
| 5988 | /** |
| 5989 | * xmlXPathConcatFunction: |
| 5990 | * @ctxt: the XPath Parser context |
| 5991 | * @nargs: the number of arguments |
| 5992 | * |
| 5993 | * Implement the concat() XPath function |
| 5994 | * string concat(string, string, string*) |
| 5995 | * The concat function returns the concatenation of its arguments. |
| 5996 | */ |
| 5997 | void |
| 5998 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5999 | xmlXPathObjectPtr cur, newobj; |
| 6000 | xmlChar *tmp; |
| 6001 | |
| 6002 | if (nargs < 2) { |
| 6003 | CHECK_ARITY(2); |
| 6004 | } |
| 6005 | |
| 6006 | CAST_TO_STRING; |
| 6007 | cur = valuePop(ctxt); |
| 6008 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6009 | xmlXPathFreeObject(cur); |
| 6010 | return; |
| 6011 | } |
| 6012 | nargs--; |
| 6013 | |
| 6014 | while (nargs > 0) { |
| 6015 | CAST_TO_STRING; |
| 6016 | newobj = valuePop(ctxt); |
| 6017 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6018 | xmlXPathFreeObject(newobj); |
| 6019 | xmlXPathFreeObject(cur); |
| 6020 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6021 | } |
| 6022 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6023 | newobj->stringval = cur->stringval; |
| 6024 | cur->stringval = tmp; |
| 6025 | |
| 6026 | xmlXPathFreeObject(newobj); |
| 6027 | nargs--; |
| 6028 | } |
| 6029 | valuePush(ctxt, cur); |
| 6030 | } |
| 6031 | |
| 6032 | /** |
| 6033 | * xmlXPathContainsFunction: |
| 6034 | * @ctxt: the XPath Parser context |
| 6035 | * @nargs: the number of arguments |
| 6036 | * |
| 6037 | * Implement the contains() XPath function |
| 6038 | * boolean contains(string, string) |
| 6039 | * The contains function returns true if the first argument string |
| 6040 | * contains the second argument string, and otherwise returns false. |
| 6041 | */ |
| 6042 | void |
| 6043 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6044 | xmlXPathObjectPtr hay, needle; |
| 6045 | |
| 6046 | CHECK_ARITY(2); |
| 6047 | CAST_TO_STRING; |
| 6048 | CHECK_TYPE(XPATH_STRING); |
| 6049 | needle = valuePop(ctxt); |
| 6050 | CAST_TO_STRING; |
| 6051 | hay = valuePop(ctxt); |
| 6052 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6053 | xmlXPathFreeObject(hay); |
| 6054 | xmlXPathFreeObject(needle); |
| 6055 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6056 | } |
| 6057 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6058 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6059 | else |
| 6060 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6061 | xmlXPathFreeObject(hay); |
| 6062 | xmlXPathFreeObject(needle); |
| 6063 | } |
| 6064 | |
| 6065 | /** |
| 6066 | * xmlXPathStartsWithFunction: |
| 6067 | * @ctxt: the XPath Parser context |
| 6068 | * @nargs: the number of arguments |
| 6069 | * |
| 6070 | * Implement the starts-with() XPath function |
| 6071 | * boolean starts-with(string, string) |
| 6072 | * The starts-with function returns true if the first argument string |
| 6073 | * starts with the second argument string, and otherwise returns false. |
| 6074 | */ |
| 6075 | void |
| 6076 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6077 | xmlXPathObjectPtr hay, needle; |
| 6078 | int n; |
| 6079 | |
| 6080 | CHECK_ARITY(2); |
| 6081 | CAST_TO_STRING; |
| 6082 | CHECK_TYPE(XPATH_STRING); |
| 6083 | needle = valuePop(ctxt); |
| 6084 | CAST_TO_STRING; |
| 6085 | hay = valuePop(ctxt); |
| 6086 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6087 | xmlXPathFreeObject(hay); |
| 6088 | xmlXPathFreeObject(needle); |
| 6089 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6090 | } |
| 6091 | n = xmlStrlen(needle->stringval); |
| 6092 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6093 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6094 | else |
| 6095 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6096 | xmlXPathFreeObject(hay); |
| 6097 | xmlXPathFreeObject(needle); |
| 6098 | } |
| 6099 | |
| 6100 | /** |
| 6101 | * xmlXPathSubstringFunction: |
| 6102 | * @ctxt: the XPath Parser context |
| 6103 | * @nargs: the number of arguments |
| 6104 | * |
| 6105 | * Implement the substring() XPath function |
| 6106 | * string substring(string, number, number?) |
| 6107 | * The substring function returns the substring of the first argument |
| 6108 | * starting at the position specified in the second argument with |
| 6109 | * length specified in the third argument. For example, |
| 6110 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6111 | * specified, it returns the substring starting at the position specified |
| 6112 | * in the second argument and continuing to the end of the string. For |
| 6113 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6114 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6115 | * numeric position: the position of the first character is 1, the position |
| 6116 | * of the second character is 2 and so on. The returned substring contains |
| 6117 | * those characters for which the position of the character is greater than |
| 6118 | * or equal to the second argument and, if the third argument is specified, |
| 6119 | * less than the sum of the second and third arguments; the comparisons |
| 6120 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6121 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6122 | * - substring("12345", 0, 3) returns "12" |
| 6123 | * - substring("12345", 0 div 0, 3) returns "" |
| 6124 | * - substring("12345", 1, 0 div 0) returns "" |
| 6125 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6126 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6127 | */ |
| 6128 | void |
| 6129 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6130 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6131 | double le=0, in; |
| 6132 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6133 | xmlChar *ret; |
| 6134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6135 | if (nargs < 2) { |
| 6136 | CHECK_ARITY(2); |
| 6137 | } |
| 6138 | if (nargs > 3) { |
| 6139 | CHECK_ARITY(3); |
| 6140 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6141 | /* |
| 6142 | * take care of possible last (position) argument |
| 6143 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6144 | if (nargs == 3) { |
| 6145 | CAST_TO_NUMBER; |
| 6146 | CHECK_TYPE(XPATH_NUMBER); |
| 6147 | len = valuePop(ctxt); |
| 6148 | le = len->floatval; |
| 6149 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6150 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6151 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | CAST_TO_NUMBER; |
| 6153 | CHECK_TYPE(XPATH_NUMBER); |
| 6154 | start = valuePop(ctxt); |
| 6155 | in = start->floatval; |
| 6156 | xmlXPathFreeObject(start); |
| 6157 | CAST_TO_STRING; |
| 6158 | CHECK_TYPE(XPATH_STRING); |
| 6159 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6160 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6161 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6162 | /* |
| 6163 | * If last pos not present, calculate last position |
| 6164 | */ |
| 6165 | if (nargs != 3) |
| 6166 | le = m; |
| 6167 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6168 | /* Need to check for the special cases where either |
| 6169 | * the index is NaN, the length is NaN, or both |
| 6170 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6171 | */ |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6172 | if (!xmlXPathIsNaN(in + le)) { |
| 6173 | /* |
| 6174 | * To meet our requirements, initial index calculations |
| 6175 | * must be done before we convert to integer format |
| 6176 | * |
| 6177 | * First we normalize indices |
| 6178 | */ |
| 6179 | in -= 1.0; |
| 6180 | le += in; |
| 6181 | if (in < 0.0) |
| 6182 | in = 0.0; |
| 6183 | if (le > (double)m) |
| 6184 | le = (double)m; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6185 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6186 | /* |
| 6187 | * Now we go to integer form, rounding up |
| 6188 | */ |
| 6189 | i = (int) in; |
| 6190 | if (((double)i) != in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6191 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6192 | l = (int) le; |
| 6193 | if (((double)l) != le) l++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6195 | if (l > m) l=m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6196 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6197 | /* number of chars to copy */ |
| 6198 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6199 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6200 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6201 | } |
| 6202 | else { |
| 6203 | ret = NULL; |
| 6204 | } |
| 6205 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6206 | if (ret == NULL) |
| 6207 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6208 | else { |
| 6209 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6210 | xmlFree(ret); |
| 6211 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6212 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6213 | xmlXPathFreeObject(str); |
| 6214 | } |
| 6215 | |
| 6216 | /** |
| 6217 | * xmlXPathSubstringBeforeFunction: |
| 6218 | * @ctxt: the XPath Parser context |
| 6219 | * @nargs: the number of arguments |
| 6220 | * |
| 6221 | * Implement the substring-before() XPath function |
| 6222 | * string substring-before(string, string) |
| 6223 | * The substring-before function returns the substring of the first |
| 6224 | * argument string that precedes the first occurrence of the second |
| 6225 | * argument string in the first argument string, or the empty string |
| 6226 | * if the first argument string does not contain the second argument |
| 6227 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6228 | */ |
| 6229 | void |
| 6230 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6231 | xmlXPathObjectPtr str; |
| 6232 | xmlXPathObjectPtr find; |
| 6233 | xmlBufferPtr target; |
| 6234 | const xmlChar *point; |
| 6235 | int offset; |
| 6236 | |
| 6237 | CHECK_ARITY(2); |
| 6238 | CAST_TO_STRING; |
| 6239 | find = valuePop(ctxt); |
| 6240 | CAST_TO_STRING; |
| 6241 | str = valuePop(ctxt); |
| 6242 | |
| 6243 | target = xmlBufferCreate(); |
| 6244 | if (target) { |
| 6245 | point = xmlStrstr(str->stringval, find->stringval); |
| 6246 | if (point) { |
| 6247 | offset = (int)(point - str->stringval); |
| 6248 | xmlBufferAdd(target, str->stringval, offset); |
| 6249 | } |
| 6250 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6251 | xmlBufferFree(target); |
| 6252 | } |
| 6253 | |
| 6254 | xmlXPathFreeObject(str); |
| 6255 | xmlXPathFreeObject(find); |
| 6256 | } |
| 6257 | |
| 6258 | /** |
| 6259 | * xmlXPathSubstringAfterFunction: |
| 6260 | * @ctxt: the XPath Parser context |
| 6261 | * @nargs: the number of arguments |
| 6262 | * |
| 6263 | * Implement the substring-after() XPath function |
| 6264 | * string substring-after(string, string) |
| 6265 | * The substring-after function returns the substring of the first |
| 6266 | * argument string that follows the first occurrence of the second |
| 6267 | * argument string in the first argument string, or the empty stringi |
| 6268 | * if the first argument string does not contain the second argument |
| 6269 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6270 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6271 | */ |
| 6272 | void |
| 6273 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6274 | xmlXPathObjectPtr str; |
| 6275 | xmlXPathObjectPtr find; |
| 6276 | xmlBufferPtr target; |
| 6277 | const xmlChar *point; |
| 6278 | int offset; |
| 6279 | |
| 6280 | CHECK_ARITY(2); |
| 6281 | CAST_TO_STRING; |
| 6282 | find = valuePop(ctxt); |
| 6283 | CAST_TO_STRING; |
| 6284 | str = valuePop(ctxt); |
| 6285 | |
| 6286 | target = xmlBufferCreate(); |
| 6287 | if (target) { |
| 6288 | point = xmlStrstr(str->stringval, find->stringval); |
| 6289 | if (point) { |
| 6290 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6291 | xmlBufferAdd(target, &str->stringval[offset], |
| 6292 | xmlStrlen(str->stringval) - offset); |
| 6293 | } |
| 6294 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6295 | xmlBufferFree(target); |
| 6296 | } |
| 6297 | |
| 6298 | xmlXPathFreeObject(str); |
| 6299 | xmlXPathFreeObject(find); |
| 6300 | } |
| 6301 | |
| 6302 | /** |
| 6303 | * xmlXPathNormalizeFunction: |
| 6304 | * @ctxt: the XPath Parser context |
| 6305 | * @nargs: the number of arguments |
| 6306 | * |
| 6307 | * Implement the normalize-space() XPath function |
| 6308 | * string normalize-space(string?) |
| 6309 | * The normalize-space function returns the argument string with white |
| 6310 | * space normalized by stripping leading and trailing whitespace |
| 6311 | * and replacing sequences of whitespace characters by a single |
| 6312 | * space. Whitespace characters are the same allowed by the S production |
| 6313 | * in XML. If the argument is omitted, it defaults to the context |
| 6314 | * node converted to a string, in other words the value of the context node. |
| 6315 | */ |
| 6316 | void |
| 6317 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6318 | xmlXPathObjectPtr obj = NULL; |
| 6319 | xmlChar *source = NULL; |
| 6320 | xmlBufferPtr target; |
| 6321 | xmlChar blank; |
| 6322 | |
| 6323 | if (nargs == 0) { |
| 6324 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6325 | valuePush(ctxt, |
| 6326 | xmlXPathWrapString( |
| 6327 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6328 | nargs = 1; |
| 6329 | } |
| 6330 | |
| 6331 | CHECK_ARITY(1); |
| 6332 | CAST_TO_STRING; |
| 6333 | CHECK_TYPE(XPATH_STRING); |
| 6334 | obj = valuePop(ctxt); |
| 6335 | source = obj->stringval; |
| 6336 | |
| 6337 | target = xmlBufferCreate(); |
| 6338 | if (target && source) { |
| 6339 | |
| 6340 | /* Skip leading whitespaces */ |
| 6341 | while (IS_BLANK(*source)) |
| 6342 | source++; |
| 6343 | |
| 6344 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6345 | blank = 0; |
| 6346 | while (*source) { |
| 6347 | if (IS_BLANK(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6348 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | } else { |
| 6350 | if (blank) { |
| 6351 | xmlBufferAdd(target, &blank, 1); |
| 6352 | blank = 0; |
| 6353 | } |
| 6354 | xmlBufferAdd(target, source, 1); |
| 6355 | } |
| 6356 | source++; |
| 6357 | } |
| 6358 | |
| 6359 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6360 | xmlBufferFree(target); |
| 6361 | } |
| 6362 | xmlXPathFreeObject(obj); |
| 6363 | } |
| 6364 | |
| 6365 | /** |
| 6366 | * xmlXPathTranslateFunction: |
| 6367 | * @ctxt: the XPath Parser context |
| 6368 | * @nargs: the number of arguments |
| 6369 | * |
| 6370 | * Implement the translate() XPath function |
| 6371 | * string translate(string, string, string) |
| 6372 | * The translate function returns the first argument string with |
| 6373 | * occurrences of characters in the second argument string replaced |
| 6374 | * by the character at the corresponding position in the third argument |
| 6375 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6376 | * BAr. If there is a character in the second argument string with no |
| 6377 | * character at a corresponding position in the third argument string |
| 6378 | * (because the second argument string is longer than the third argument |
| 6379 | * string), then occurrences of that character in the first argument |
| 6380 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6381 | * returns "AAA". If a character occurs more than once in second |
| 6382 | * argument string, then the first occurrence determines the replacement |
| 6383 | * character. If the third argument string is longer than the second |
| 6384 | * argument string, then excess characters are ignored. |
| 6385 | */ |
| 6386 | void |
| 6387 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6388 | xmlXPathObjectPtr str; |
| 6389 | xmlXPathObjectPtr from; |
| 6390 | xmlXPathObjectPtr to; |
| 6391 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6392 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6393 | xmlChar ch; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6394 | xmlChar *point; |
| 6395 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6396 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6397 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6398 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6399 | CAST_TO_STRING; |
| 6400 | to = valuePop(ctxt); |
| 6401 | CAST_TO_STRING; |
| 6402 | from = valuePop(ctxt); |
| 6403 | CAST_TO_STRING; |
| 6404 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6405 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6406 | target = xmlBufferCreate(); |
| 6407 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6408 | max = xmlUTF8Strlen(to->stringval); |
| 6409 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6410 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6411 | if (offset >= 0) { |
| 6412 | if (offset < max) { |
| 6413 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6414 | if (point) |
| 6415 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6416 | } |
| 6417 | } else |
| 6418 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6419 | |
| 6420 | /* Step to next character in input */ |
| 6421 | cptr++; |
| 6422 | if ( ch & 0x80 ) { |
| 6423 | /* if not simple ascii, verify proper format */ |
| 6424 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6425 | xmlGenericError(xmlGenericErrorContext, |
| 6426 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6427 | break; |
| 6428 | } |
| 6429 | /* then skip over remaining bytes for this char */ |
| 6430 | while ( (ch <<= 1) & 0x80 ) |
| 6431 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 6432 | xmlGenericError(xmlGenericErrorContext, |
| 6433 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6434 | break; |
| 6435 | } |
| 6436 | if (ch & 0x80) /* must have had error encountered */ |
| 6437 | break; |
| 6438 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6439 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6440 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6441 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6442 | xmlBufferFree(target); |
| 6443 | xmlXPathFreeObject(str); |
| 6444 | xmlXPathFreeObject(from); |
| 6445 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
| 6448 | /** |
| 6449 | * xmlXPathBooleanFunction: |
| 6450 | * @ctxt: the XPath Parser context |
| 6451 | * @nargs: the number of arguments |
| 6452 | * |
| 6453 | * Implement the boolean() XPath function |
| 6454 | * boolean boolean(object) |
| 6455 | * he boolean function converts its argument to a boolean as follows: |
| 6456 | * - a number is true if and only if it is neither positive or |
| 6457 | * negative zero nor NaN |
| 6458 | * - a node-set is true if and only if it is non-empty |
| 6459 | * - a string is true if and only if its length is non-zero |
| 6460 | */ |
| 6461 | void |
| 6462 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6463 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6464 | |
| 6465 | CHECK_ARITY(1); |
| 6466 | cur = valuePop(ctxt); |
| 6467 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6468 | cur = xmlXPathConvertBoolean(cur); |
| 6469 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | /** |
| 6473 | * xmlXPathNotFunction: |
| 6474 | * @ctxt: the XPath Parser context |
| 6475 | * @nargs: the number of arguments |
| 6476 | * |
| 6477 | * Implement the not() XPath function |
| 6478 | * boolean not(boolean) |
| 6479 | * The not function returns true if its argument is false, |
| 6480 | * and false otherwise. |
| 6481 | */ |
| 6482 | void |
| 6483 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6484 | CHECK_ARITY(1); |
| 6485 | CAST_TO_BOOLEAN; |
| 6486 | CHECK_TYPE(XPATH_BOOLEAN); |
| 6487 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 6488 | } |
| 6489 | |
| 6490 | /** |
| 6491 | * xmlXPathTrueFunction: |
| 6492 | * @ctxt: the XPath Parser context |
| 6493 | * @nargs: the number of arguments |
| 6494 | * |
| 6495 | * Implement the true() XPath function |
| 6496 | * boolean true() |
| 6497 | */ |
| 6498 | void |
| 6499 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6500 | CHECK_ARITY(0); |
| 6501 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6502 | } |
| 6503 | |
| 6504 | /** |
| 6505 | * xmlXPathFalseFunction: |
| 6506 | * @ctxt: the XPath Parser context |
| 6507 | * @nargs: the number of arguments |
| 6508 | * |
| 6509 | * Implement the false() XPath function |
| 6510 | * boolean false() |
| 6511 | */ |
| 6512 | void |
| 6513 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6514 | CHECK_ARITY(0); |
| 6515 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6516 | } |
| 6517 | |
| 6518 | /** |
| 6519 | * xmlXPathLangFunction: |
| 6520 | * @ctxt: the XPath Parser context |
| 6521 | * @nargs: the number of arguments |
| 6522 | * |
| 6523 | * Implement the lang() XPath function |
| 6524 | * boolean lang(string) |
| 6525 | * The lang function returns true or false depending on whether the |
| 6526 | * language of the context node as specified by xml:lang attributes |
| 6527 | * is the same as or is a sublanguage of the language specified by |
| 6528 | * the argument string. The language of the context node is determined |
| 6529 | * by the value of the xml:lang attribute on the context node, or, if |
| 6530 | * the context node has no xml:lang attribute, by the value of the |
| 6531 | * xml:lang attribute on the nearest ancestor of the context node that |
| 6532 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 6533 | * returns false. If there is such an attribute, then lang returns |
| 6534 | * true if the attribute value is equal to the argument ignoring case, |
| 6535 | * or if there is some suffix starting with - such that the attribute |
| 6536 | * value is equal to the argument ignoring that suffix of the attribute |
| 6537 | * value and ignoring case. |
| 6538 | */ |
| 6539 | void |
| 6540 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6541 | xmlXPathObjectPtr val; |
| 6542 | const xmlChar *theLang; |
| 6543 | const xmlChar *lang; |
| 6544 | int ret = 0; |
| 6545 | int i; |
| 6546 | |
| 6547 | CHECK_ARITY(1); |
| 6548 | CAST_TO_STRING; |
| 6549 | CHECK_TYPE(XPATH_STRING); |
| 6550 | val = valuePop(ctxt); |
| 6551 | lang = val->stringval; |
| 6552 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 6553 | if ((theLang != NULL) && (lang != NULL)) { |
| 6554 | for (i = 0;lang[i] != 0;i++) |
| 6555 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 6556 | goto not_equal; |
| 6557 | ret = 1; |
| 6558 | } |
| 6559 | not_equal: |
| 6560 | xmlXPathFreeObject(val); |
| 6561 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 6562 | } |
| 6563 | |
| 6564 | /** |
| 6565 | * xmlXPathNumberFunction: |
| 6566 | * @ctxt: the XPath Parser context |
| 6567 | * @nargs: the number of arguments |
| 6568 | * |
| 6569 | * Implement the number() XPath function |
| 6570 | * number number(object?) |
| 6571 | */ |
| 6572 | void |
| 6573 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6574 | xmlXPathObjectPtr cur; |
| 6575 | double res; |
| 6576 | |
| 6577 | if (nargs == 0) { |
| 6578 | if (ctxt->context->node == NULL) { |
| 6579 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 6580 | } else { |
| 6581 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 6582 | |
| 6583 | res = xmlXPathStringEvalNumber(content); |
| 6584 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 6585 | xmlFree(content); |
| 6586 | } |
| 6587 | return; |
| 6588 | } |
| 6589 | |
| 6590 | CHECK_ARITY(1); |
| 6591 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6592 | cur = xmlXPathConvertNumber(cur); |
| 6593 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | /** |
| 6597 | * xmlXPathSumFunction: |
| 6598 | * @ctxt: the XPath Parser context |
| 6599 | * @nargs: the number of arguments |
| 6600 | * |
| 6601 | * Implement the sum() XPath function |
| 6602 | * number sum(node-set) |
| 6603 | * The sum function returns the sum of the values of the nodes in |
| 6604 | * the argument node-set. |
| 6605 | */ |
| 6606 | void |
| 6607 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6608 | xmlXPathObjectPtr cur; |
| 6609 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6610 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6611 | |
| 6612 | CHECK_ARITY(1); |
| 6613 | if ((ctxt->value == NULL) || |
| 6614 | ((ctxt->value->type != XPATH_NODESET) && |
| 6615 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6616 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6617 | cur = valuePop(ctxt); |
| 6618 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6619 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 6621 | } else { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6622 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 6623 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6624 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6625 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6626 | } |
| 6627 | xmlXPathFreeObject(cur); |
| 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * xmlXPathFloorFunction: |
| 6632 | * @ctxt: the XPath Parser context |
| 6633 | * @nargs: the number of arguments |
| 6634 | * |
| 6635 | * Implement the floor() XPath function |
| 6636 | * number floor(number) |
| 6637 | * The floor function returns the largest (closest to positive infinity) |
| 6638 | * number that is not greater than the argument and that is an integer. |
| 6639 | */ |
| 6640 | void |
| 6641 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6642 | double f; |
| 6643 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | CHECK_ARITY(1); |
| 6645 | CAST_TO_NUMBER; |
| 6646 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6647 | |
| 6648 | f = (double)((int) ctxt->value->floatval); |
| 6649 | if (f != ctxt->value->floatval) { |
| 6650 | if (ctxt->value->floatval > 0) |
| 6651 | ctxt->value->floatval = f; |
| 6652 | else |
| 6653 | ctxt->value->floatval = f - 1; |
| 6654 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | /** |
| 6658 | * xmlXPathCeilingFunction: |
| 6659 | * @ctxt: the XPath Parser context |
| 6660 | * @nargs: the number of arguments |
| 6661 | * |
| 6662 | * Implement the ceiling() XPath function |
| 6663 | * number ceiling(number) |
| 6664 | * The ceiling function returns the smallest (closest to negative infinity) |
| 6665 | * number that is not less than the argument and that is an integer. |
| 6666 | */ |
| 6667 | void |
| 6668 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6669 | double f; |
| 6670 | |
| 6671 | CHECK_ARITY(1); |
| 6672 | CAST_TO_NUMBER; |
| 6673 | CHECK_TYPE(XPATH_NUMBER); |
| 6674 | |
| 6675 | #if 0 |
| 6676 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 6677 | #else |
| 6678 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6679 | if (f != ctxt->value->floatval) { |
| 6680 | if (ctxt->value->floatval > 0) |
| 6681 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 6682 | else { |
| 6683 | if (ctxt->value->floatval < 0 && f == 0) |
| 6684 | ctxt->value->floatval = xmlXPathNZERO; |
| 6685 | else |
| 6686 | ctxt->value->floatval = f; |
| 6687 | } |
| 6688 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6690 | #endif |
| 6691 | } |
| 6692 | |
| 6693 | /** |
| 6694 | * xmlXPathRoundFunction: |
| 6695 | * @ctxt: the XPath Parser context |
| 6696 | * @nargs: the number of arguments |
| 6697 | * |
| 6698 | * Implement the round() XPath function |
| 6699 | * number round(number) |
| 6700 | * The round function returns the number that is closest to the |
| 6701 | * argument and that is an integer. If there are two such numbers, |
| 6702 | * then the one that is even is returned. |
| 6703 | */ |
| 6704 | void |
| 6705 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6706 | double f; |
| 6707 | |
| 6708 | CHECK_ARITY(1); |
| 6709 | CAST_TO_NUMBER; |
| 6710 | CHECK_TYPE(XPATH_NUMBER); |
| 6711 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 6712 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 6713 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 6714 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6715 | (ctxt->value->floatval == 0.0)) |
| 6716 | return; |
| 6717 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6718 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6719 | if (ctxt->value->floatval < 0) { |
| 6720 | if (ctxt->value->floatval < f - 0.5) |
| 6721 | ctxt->value->floatval = f - 1; |
| 6722 | else |
| 6723 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 6724 | if (ctxt->value->floatval == 0) |
| 6725 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 6726 | } else { |
| 6727 | if (ctxt->value->floatval < f + 0.5) |
| 6728 | ctxt->value->floatval = f; |
| 6729 | else |
| 6730 | ctxt->value->floatval = f + 1; |
| 6731 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
| 6734 | /************************************************************************ |
| 6735 | * * |
| 6736 | * The Parser * |
| 6737 | * * |
| 6738 | ************************************************************************/ |
| 6739 | |
| 6740 | /* |
| 6741 | * a couple of forward declarations since we use a recursive call based |
| 6742 | * implementation. |
| 6743 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 6744 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6745 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 6746 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6747 | #ifdef VMS |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 6748 | static void xmlXPathCompRelLocationPath(xmlXPathParserContextPtr ctxt); |
| 6749 | #define xmlXPathCompRelativeLocationPath xmlXPathCompRelLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 6751 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6752 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6753 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 6754 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6755 | |
| 6756 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6757 | * xmlXPathCurrentChar: |
| 6758 | * @ctxt: the XPath parser context |
| 6759 | * @cur: pointer to the beginning of the char |
| 6760 | * @len: pointer to the length of the char read |
| 6761 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6762 | * 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] | 6763 | * bytes in the input buffer. |
| 6764 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6765 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6766 | */ |
| 6767 | |
| 6768 | static int |
| 6769 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 6770 | unsigned char c; |
| 6771 | unsigned int val; |
| 6772 | const xmlChar *cur; |
| 6773 | |
| 6774 | if (ctxt == NULL) |
| 6775 | return(0); |
| 6776 | cur = ctxt->cur; |
| 6777 | |
| 6778 | /* |
| 6779 | * We are supposed to handle UTF8, check it's valid |
| 6780 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 6781 | * |
| 6782 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 6783 | * 0000 0000-0000 007F 0xxxxxxx |
| 6784 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 6785 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 6786 | * |
| 6787 | * Check for the 0x110000 limit too |
| 6788 | */ |
| 6789 | c = *cur; |
| 6790 | if (c & 0x80) { |
| 6791 | if ((cur[1] & 0xc0) != 0x80) |
| 6792 | goto encoding_error; |
| 6793 | if ((c & 0xe0) == 0xe0) { |
| 6794 | |
| 6795 | if ((cur[2] & 0xc0) != 0x80) |
| 6796 | goto encoding_error; |
| 6797 | if ((c & 0xf0) == 0xf0) { |
| 6798 | if (((c & 0xf8) != 0xf0) || |
| 6799 | ((cur[3] & 0xc0) != 0x80)) |
| 6800 | goto encoding_error; |
| 6801 | /* 4-byte code */ |
| 6802 | *len = 4; |
| 6803 | val = (cur[0] & 0x7) << 18; |
| 6804 | val |= (cur[1] & 0x3f) << 12; |
| 6805 | val |= (cur[2] & 0x3f) << 6; |
| 6806 | val |= cur[3] & 0x3f; |
| 6807 | } else { |
| 6808 | /* 3-byte code */ |
| 6809 | *len = 3; |
| 6810 | val = (cur[0] & 0xf) << 12; |
| 6811 | val |= (cur[1] & 0x3f) << 6; |
| 6812 | val |= cur[2] & 0x3f; |
| 6813 | } |
| 6814 | } else { |
| 6815 | /* 2-byte code */ |
| 6816 | *len = 2; |
| 6817 | val = (cur[0] & 0x1f) << 6; |
| 6818 | val |= cur[1] & 0x3f; |
| 6819 | } |
| 6820 | if (!IS_CHAR(val)) { |
| 6821 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 6822 | } |
| 6823 | return(val); |
| 6824 | } else { |
| 6825 | /* 1-byte code */ |
| 6826 | *len = 1; |
| 6827 | return((int) *cur); |
| 6828 | } |
| 6829 | encoding_error: |
| 6830 | /* |
| 6831 | * If we detect an UTF8 error that probably mean that the |
| 6832 | * input encoding didn't get properly advertized in the |
| 6833 | * declaration header. Report the error and switch the encoding |
| 6834 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 6835 | * encoding !) |
| 6836 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 6837 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6838 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
| 6841 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6842 | * xmlXPathParseNCName: |
| 6843 | * @ctxt: the XPath Parser context |
| 6844 | * |
| 6845 | * parse an XML namespace non qualified name. |
| 6846 | * |
| 6847 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 6848 | * |
| 6849 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 6850 | * CombiningChar | Extender |
| 6851 | * |
| 6852 | * Returns the namespace name or NULL |
| 6853 | */ |
| 6854 | |
| 6855 | xmlChar * |
| 6856 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6857 | const xmlChar *in; |
| 6858 | xmlChar *ret; |
| 6859 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6860 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6861 | /* |
| 6862 | * Accelerator for simple ASCII names |
| 6863 | */ |
| 6864 | in = ctxt->cur; |
| 6865 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6866 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6867 | (*in == '_')) { |
| 6868 | in++; |
| 6869 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6870 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6871 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 6872 | (*in == '_') || (*in == '.') || |
| 6873 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6874 | in++; |
| 6875 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 6876 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 6877 | (*in == '@') || (*in == '*')) { |
| 6878 | count = in - ctxt->cur; |
| 6879 | if (count == 0) |
| 6880 | return(NULL); |
| 6881 | ret = xmlStrndup(ctxt->cur, count); |
| 6882 | ctxt->cur = in; |
| 6883 | return(ret); |
| 6884 | } |
| 6885 | } |
| 6886 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6887 | } |
| 6888 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6889 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6890 | /** |
| 6891 | * xmlXPathParseQName: |
| 6892 | * @ctxt: the XPath Parser context |
| 6893 | * @prefix: a xmlChar ** |
| 6894 | * |
| 6895 | * parse an XML qualified name |
| 6896 | * |
| 6897 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 6898 | * |
| 6899 | * [NS 6] Prefix ::= NCName |
| 6900 | * |
| 6901 | * [NS 7] LocalPart ::= NCName |
| 6902 | * |
| 6903 | * Returns the function returns the local part, and prefix is updated |
| 6904 | * to get the Prefix if any. |
| 6905 | */ |
| 6906 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6907 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6908 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 6909 | xmlChar *ret = NULL; |
| 6910 | |
| 6911 | *prefix = NULL; |
| 6912 | ret = xmlXPathParseNCName(ctxt); |
| 6913 | if (CUR == ':') { |
| 6914 | *prefix = ret; |
| 6915 | NEXT; |
| 6916 | ret = xmlXPathParseNCName(ctxt); |
| 6917 | } |
| 6918 | return(ret); |
| 6919 | } |
| 6920 | |
| 6921 | /** |
| 6922 | * xmlXPathParseName: |
| 6923 | * @ctxt: the XPath Parser context |
| 6924 | * |
| 6925 | * parse an XML name |
| 6926 | * |
| 6927 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 6928 | * CombiningChar | Extender |
| 6929 | * |
| 6930 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 6931 | * |
| 6932 | * Returns the namespace name or NULL |
| 6933 | */ |
| 6934 | |
| 6935 | xmlChar * |
| 6936 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6937 | const xmlChar *in; |
| 6938 | xmlChar *ret; |
| 6939 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6940 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6941 | /* |
| 6942 | * Accelerator for simple ASCII names |
| 6943 | */ |
| 6944 | in = ctxt->cur; |
| 6945 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6946 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6947 | (*in == '_') || (*in == ':')) { |
| 6948 | in++; |
| 6949 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6950 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6951 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6952 | (*in == '_') || (*in == '-') || |
| 6953 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6954 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6955 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6956 | count = in - ctxt->cur; |
| 6957 | ret = xmlStrndup(ctxt->cur, count); |
| 6958 | ctxt->cur = in; |
| 6959 | return(ret); |
| 6960 | } |
| 6961 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6962 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6965 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6966 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6967 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 6968 | int len = 0, l; |
| 6969 | int c; |
| 6970 | |
| 6971 | /* |
| 6972 | * Handler for more complex cases |
| 6973 | */ |
| 6974 | c = CUR_CHAR(l); |
| 6975 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6976 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 6977 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6978 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6979 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6980 | return(NULL); |
| 6981 | } |
| 6982 | |
| 6983 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 6984 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 6985 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 6986 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 6987 | (IS_COMBINING(c)) || |
| 6988 | (IS_EXTENDER(c)))) { |
| 6989 | COPY_BUF(l,buf,len,c); |
| 6990 | NEXTL(l); |
| 6991 | c = CUR_CHAR(l); |
| 6992 | if (len >= XML_MAX_NAMELEN) { |
| 6993 | /* |
| 6994 | * Okay someone managed to make a huge name, so he's ready to pay |
| 6995 | * for the processing speed. |
| 6996 | */ |
| 6997 | xmlChar *buffer; |
| 6998 | int max = len * 2; |
| 6999 | |
| 7000 | buffer = (xmlChar *) xmlMalloc(max * sizeof(xmlChar)); |
| 7001 | if (buffer == NULL) { |
| 7002 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7003 | } |
| 7004 | memcpy(buffer, buf, len); |
| 7005 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7006 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7007 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7008 | (IS_COMBINING(c)) || |
| 7009 | (IS_EXTENDER(c))) { |
| 7010 | if (len + 10 > max) { |
| 7011 | max *= 2; |
| 7012 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7013 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7014 | if (buffer == NULL) { |
| 7015 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7016 | } |
| 7017 | } |
| 7018 | COPY_BUF(l,buffer,len,c); |
| 7019 | NEXTL(l); |
| 7020 | c = CUR_CHAR(l); |
| 7021 | } |
| 7022 | buffer[len] = 0; |
| 7023 | return(buffer); |
| 7024 | } |
| 7025 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7026 | if (len == 0) |
| 7027 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7028 | return(xmlStrndup(buf, len)); |
| 7029 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7030 | |
| 7031 | #define MAX_FRAC 20 |
| 7032 | |
| 7033 | static double my_pow10[MAX_FRAC] = { |
| 7034 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7035 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7036 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7037 | 100000000000000.0, |
| 7038 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
| 7039 | 1000000000000000000.0, 10000000000000000000.0 |
| 7040 | }; |
| 7041 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7042 | /** |
| 7043 | * xmlXPathStringEvalNumber: |
| 7044 | * @str: A string to scan |
| 7045 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7046 | * [30a] Float ::= Number ('e' Digits?)? |
| 7047 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7048 | * [30] Number ::= Digits ('.' Digits?)? |
| 7049 | * | '.' Digits |
| 7050 | * [31] Digits ::= [0-9]+ |
| 7051 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7052 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7053 | * In complement of the Number expression, this function also handles |
| 7054 | * negative values : '-' Number. |
| 7055 | * |
| 7056 | * Returns the double value. |
| 7057 | */ |
| 7058 | double |
| 7059 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7060 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7061 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7062 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7063 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7064 | int exponent = 0; |
| 7065 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7066 | #ifdef __GNUC__ |
| 7067 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7068 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7069 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7070 | if (cur == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7071 | while (IS_BLANK(*cur)) cur++; |
| 7072 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7073 | return(xmlXPathNAN); |
| 7074 | } |
| 7075 | if (*cur == '-') { |
| 7076 | isneg = 1; |
| 7077 | cur++; |
| 7078 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7079 | |
| 7080 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7081 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7082 | * tmp/temp is a workaround against a gcc compiler bug |
| 7083 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7084 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7085 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7086 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7087 | ret = ret * 10; |
| 7088 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7089 | ok = 1; |
| 7090 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7091 | temp = (double) tmp; |
| 7092 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7093 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7094 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7095 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7096 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7097 | ret = ret * 10 + (*cur - '0'); |
| 7098 | ok = 1; |
| 7099 | cur++; |
| 7100 | } |
| 7101 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7102 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7103 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7104 | int v, frac = 0; |
| 7105 | double fraction = 0; |
| 7106 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7107 | cur++; |
| 7108 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7109 | return(xmlXPathNAN); |
| 7110 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7111 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7112 | v = (*cur - '0'); |
| 7113 | fraction = fraction * 10 + v; |
| 7114 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7115 | cur++; |
| 7116 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7117 | fraction /= my_pow10[frac]; |
| 7118 | ret = ret + fraction; |
| 7119 | while ((*cur >= '0') && (*cur <= '9')) |
| 7120 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7121 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7122 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7123 | cur++; |
| 7124 | if (*cur == '-') { |
| 7125 | is_exponent_negative = 1; |
| 7126 | cur++; |
| 7127 | } |
| 7128 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7129 | exponent = exponent * 10 + (*cur - '0'); |
| 7130 | cur++; |
| 7131 | } |
| 7132 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7133 | while (IS_BLANK(*cur)) cur++; |
| 7134 | if (*cur != 0) return(xmlXPathNAN); |
| 7135 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7136 | if (is_exponent_negative) exponent = -exponent; |
| 7137 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7138 | return(ret); |
| 7139 | } |
| 7140 | |
| 7141 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7142 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7143 | * @ctxt: the XPath Parser context |
| 7144 | * |
| 7145 | * [30] Number ::= Digits ('.' Digits?)? |
| 7146 | * | '.' Digits |
| 7147 | * [31] Digits ::= [0-9]+ |
| 7148 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7149 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7150 | * |
| 7151 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7152 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7153 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7154 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7155 | double ret = 0.0; |
| 7156 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7157 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7158 | int exponent = 0; |
| 7159 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7160 | #ifdef __GNUC__ |
| 7161 | unsigned long tmp = 0; |
| 7162 | double temp; |
| 7163 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7164 | |
| 7165 | CHECK_ERROR; |
| 7166 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7167 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7168 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7169 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7170 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7171 | * tmp/temp is a workaround against a gcc compiler bug |
| 7172 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7173 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7174 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7175 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7176 | ret = ret * 10; |
| 7177 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7178 | ok = 1; |
| 7179 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7180 | temp = (double) tmp; |
| 7181 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7182 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7183 | #else |
| 7184 | ret = 0; |
| 7185 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7186 | ret = ret * 10 + (CUR - '0'); |
| 7187 | ok = 1; |
| 7188 | NEXT; |
| 7189 | } |
| 7190 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7191 | if (CUR == '.') { |
| 7192 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7193 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7194 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7195 | } |
| 7196 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7197 | mult /= 10; |
| 7198 | ret = ret + (CUR - '0') * mult; |
| 7199 | NEXT; |
| 7200 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7201 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7202 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7203 | NEXT; |
| 7204 | if (CUR == '-') { |
| 7205 | is_exponent_negative = 1; |
| 7206 | NEXT; |
| 7207 | } |
| 7208 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7209 | exponent = exponent * 10 + (CUR - '0'); |
| 7210 | NEXT; |
| 7211 | } |
| 7212 | if (is_exponent_negative) |
| 7213 | exponent = -exponent; |
| 7214 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7215 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7216 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7217 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
| 7220 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7221 | * xmlXPathParseLiteral: |
| 7222 | * @ctxt: the XPath Parser context |
| 7223 | * |
| 7224 | * Parse a Literal |
| 7225 | * |
| 7226 | * [29] Literal ::= '"' [^"]* '"' |
| 7227 | * | "'" [^']* "'" |
| 7228 | * |
| 7229 | * Returns the value found or NULL in case of error |
| 7230 | */ |
| 7231 | static xmlChar * |
| 7232 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7233 | const xmlChar *q; |
| 7234 | xmlChar *ret = NULL; |
| 7235 | |
| 7236 | if (CUR == '"') { |
| 7237 | NEXT; |
| 7238 | q = CUR_PTR; |
| 7239 | while ((IS_CHAR(CUR)) && (CUR != '"')) |
| 7240 | NEXT; |
| 7241 | if (!IS_CHAR(CUR)) { |
| 7242 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7243 | } else { |
| 7244 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7245 | NEXT; |
| 7246 | } |
| 7247 | } else if (CUR == '\'') { |
| 7248 | NEXT; |
| 7249 | q = CUR_PTR; |
| 7250 | while ((IS_CHAR(CUR)) && (CUR != '\'')) |
| 7251 | NEXT; |
| 7252 | if (!IS_CHAR(CUR)) { |
| 7253 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7254 | } else { |
| 7255 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7256 | NEXT; |
| 7257 | } |
| 7258 | } else { |
| 7259 | XP_ERROR0(XPATH_START_LITERAL_ERROR); |
| 7260 | } |
| 7261 | return(ret); |
| 7262 | } |
| 7263 | |
| 7264 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7265 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7266 | * @ctxt: the XPath Parser context |
| 7267 | * |
| 7268 | * Parse a Literal and push it on the stack. |
| 7269 | * |
| 7270 | * [29] Literal ::= '"' [^"]* '"' |
| 7271 | * | "'" [^']* "'" |
| 7272 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7273 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7274 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7275 | static void |
| 7276 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7277 | const xmlChar *q; |
| 7278 | xmlChar *ret = NULL; |
| 7279 | |
| 7280 | if (CUR == '"') { |
| 7281 | NEXT; |
| 7282 | q = CUR_PTR; |
| 7283 | while ((IS_CHAR(CUR)) && (CUR != '"')) |
| 7284 | NEXT; |
| 7285 | if (!IS_CHAR(CUR)) { |
| 7286 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7287 | } else { |
| 7288 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7289 | NEXT; |
| 7290 | } |
| 7291 | } else if (CUR == '\'') { |
| 7292 | NEXT; |
| 7293 | q = CUR_PTR; |
| 7294 | while ((IS_CHAR(CUR)) && (CUR != '\'')) |
| 7295 | NEXT; |
| 7296 | if (!IS_CHAR(CUR)) { |
| 7297 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7298 | } else { |
| 7299 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7300 | NEXT; |
| 7301 | } |
| 7302 | } else { |
| 7303 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7304 | } |
| 7305 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7306 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7307 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7308 | xmlFree(ret); |
| 7309 | } |
| 7310 | |
| 7311 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7312 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7313 | * @ctxt: the XPath Parser context |
| 7314 | * |
| 7315 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7316 | * |
| 7317 | * The variable bindings consist of a mapping from variable names |
| 7318 | * to variable values. The value of a variable is an object, which |
| 7319 | * of any of the types that are possible for the value of an expression, |
| 7320 | * and may also be of additional types not specified here. |
| 7321 | * |
| 7322 | * Early evaluation is possible since: |
| 7323 | * The variable bindings [...] used to evaluate a subexpression are |
| 7324 | * always the same as those used to evaluate the containing expression. |
| 7325 | * |
| 7326 | * [36] VariableReference ::= '$' QName |
| 7327 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7328 | static void |
| 7329 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7330 | xmlChar *name; |
| 7331 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7332 | |
| 7333 | SKIP_BLANKS; |
| 7334 | if (CUR != '$') { |
| 7335 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7336 | } |
| 7337 | NEXT; |
| 7338 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7339 | if (name == NULL) { |
| 7340 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7341 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7342 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7343 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7344 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7345 | SKIP_BLANKS; |
| 7346 | } |
| 7347 | |
| 7348 | /** |
| 7349 | * xmlXPathIsNodeType: |
| 7350 | * @ctxt: the XPath Parser context |
| 7351 | * @name: a name string |
| 7352 | * |
| 7353 | * Is the name given a NodeType one. |
| 7354 | * |
| 7355 | * [38] NodeType ::= 'comment' |
| 7356 | * | 'text' |
| 7357 | * | 'processing-instruction' |
| 7358 | * | 'node' |
| 7359 | * |
| 7360 | * Returns 1 if true 0 otherwise |
| 7361 | */ |
| 7362 | int |
| 7363 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7364 | if (name == NULL) |
| 7365 | return(0); |
| 7366 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7367 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7368 | return(1); |
| 7369 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7370 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7371 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7372 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7373 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7374 | return(1); |
| 7375 | return(0); |
| 7376 | } |
| 7377 | |
| 7378 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7379 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7380 | * @ctxt: the XPath Parser context |
| 7381 | * |
| 7382 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7383 | * [17] Argument ::= Expr |
| 7384 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7385 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7386 | * pushed on the stack |
| 7387 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7388 | static void |
| 7389 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7390 | xmlChar *name; |
| 7391 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7392 | int nbargs = 0; |
| 7393 | |
| 7394 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7395 | if (name == NULL) { |
| 7396 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7397 | } |
| 7398 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7399 | #ifdef DEBUG_EXPR |
| 7400 | if (prefix == NULL) |
| 7401 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7402 | name); |
| 7403 | else |
| 7404 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7405 | prefix, name); |
| 7406 | #endif |
| 7407 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7408 | if (CUR != '(') { |
| 7409 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7410 | } |
| 7411 | NEXT; |
| 7412 | SKIP_BLANKS; |
| 7413 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7414 | ctxt->comp->last = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7415 | while (CUR != ')') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7416 | int op1 = ctxt->comp->last; |
| 7417 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7418 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 7419 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7420 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7421 | nbargs++; |
| 7422 | if (CUR == ')') break; |
| 7423 | if (CUR != ',') { |
| 7424 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7425 | } |
| 7426 | NEXT; |
| 7427 | SKIP_BLANKS; |
| 7428 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7429 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 7430 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7431 | NEXT; |
| 7432 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7436 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7437 | * @ctxt: the XPath Parser context |
| 7438 | * |
| 7439 | * [15] PrimaryExpr ::= VariableReference |
| 7440 | * | '(' Expr ')' |
| 7441 | * | Literal |
| 7442 | * | Number |
| 7443 | * | FunctionCall |
| 7444 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7445 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7446 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7447 | static void |
| 7448 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7449 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7450 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7451 | else if (CUR == '(') { |
| 7452 | NEXT; |
| 7453 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7454 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 7455 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7456 | if (CUR != ')') { |
| 7457 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7458 | } |
| 7459 | NEXT; |
| 7460 | SKIP_BLANKS; |
Daniel Veillard | 01917aa | 2002-04-10 11:30:41 +0000 | [diff] [blame] | 7461 | } else if (IS_DIGIT(CUR) || (CUR == '.' && IS_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7462 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7463 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7464 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7465 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7466 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7467 | } |
| 7468 | SKIP_BLANKS; |
| 7469 | } |
| 7470 | |
| 7471 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7472 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7473 | * @ctxt: the XPath Parser context |
| 7474 | * |
| 7475 | * [20] FilterExpr ::= PrimaryExpr |
| 7476 | * | FilterExpr Predicate |
| 7477 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7478 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7479 | * Square brackets are used to filter expressions in the same way that |
| 7480 | * they are used in location paths. It is an error if the expression to |
| 7481 | * be filtered does not evaluate to a node-set. The context node list |
| 7482 | * used for evaluating the expression in square brackets is the node-set |
| 7483 | * to be filtered listed in document order. |
| 7484 | */ |
| 7485 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7486 | static void |
| 7487 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 7488 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7489 | CHECK_ERROR; |
| 7490 | SKIP_BLANKS; |
| 7491 | |
| 7492 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7493 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7494 | SKIP_BLANKS; |
| 7495 | } |
| 7496 | |
| 7497 | |
| 7498 | } |
| 7499 | |
| 7500 | /** |
| 7501 | * xmlXPathScanName: |
| 7502 | * @ctxt: the XPath Parser context |
| 7503 | * |
| 7504 | * Trickery: parse an XML name but without consuming the input flow |
| 7505 | * Needed to avoid insanity in the parser state. |
| 7506 | * |
| 7507 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7508 | * CombiningChar | Extender |
| 7509 | * |
| 7510 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7511 | * |
| 7512 | * [6] Names ::= Name (S Name)* |
| 7513 | * |
| 7514 | * Returns the Name parsed or NULL |
| 7515 | */ |
| 7516 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7517 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7518 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
| 7519 | xmlChar buf[XML_MAX_NAMELEN]; |
| 7520 | int len = 0; |
| 7521 | |
| 7522 | SKIP_BLANKS; |
| 7523 | if (!IS_LETTER(CUR) && (CUR != '_') && |
| 7524 | (CUR != ':')) { |
| 7525 | return(NULL); |
| 7526 | } |
| 7527 | |
| 7528 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 7529 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 7530 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 7531 | (IS_COMBINING(NXT(len))) || |
| 7532 | (IS_EXTENDER(NXT(len)))) { |
| 7533 | buf[len] = NXT(len); |
| 7534 | len++; |
| 7535 | if (len >= XML_MAX_NAMELEN) { |
| 7536 | xmlGenericError(xmlGenericErrorContext, |
| 7537 | "xmlScanName: reached XML_MAX_NAMELEN limit\n"); |
| 7538 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 7539 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 7540 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 7541 | (IS_COMBINING(NXT(len))) || |
| 7542 | (IS_EXTENDER(NXT(len)))) |
| 7543 | len++; |
| 7544 | break; |
| 7545 | } |
| 7546 | } |
| 7547 | return(xmlStrndup(buf, len)); |
| 7548 | } |
| 7549 | |
| 7550 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7551 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7552 | * @ctxt: the XPath Parser context |
| 7553 | * |
| 7554 | * [19] PathExpr ::= LocationPath |
| 7555 | * | FilterExpr |
| 7556 | * | FilterExpr '/' RelativeLocationPath |
| 7557 | * | FilterExpr '//' RelativeLocationPath |
| 7558 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7559 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7560 | * The / operator and // operators combine an arbitrary expression |
| 7561 | * and a relative location path. It is an error if the expression |
| 7562 | * does not evaluate to a node-set. |
| 7563 | * The / operator does composition in the same way as when / is |
| 7564 | * used in a location path. As in location paths, // is short for |
| 7565 | * /descendant-or-self::node()/. |
| 7566 | */ |
| 7567 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7568 | static void |
| 7569 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7570 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 7571 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 7572 | |
| 7573 | SKIP_BLANKS; |
| 7574 | if ((CUR == '$') || (CUR == '(') || (IS_DIGIT(CUR)) || |
Daniel Veillard | 01917aa | 2002-04-10 11:30:41 +0000 | [diff] [blame] | 7575 | (CUR == '\'') || (CUR == '"') || (CUR == '.' && IS_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7576 | lc = 0; |
| 7577 | } else if (CUR == '*') { |
| 7578 | /* relative or absolute location path */ |
| 7579 | lc = 1; |
| 7580 | } else if (CUR == '/') { |
| 7581 | /* relative or absolute location path */ |
| 7582 | lc = 1; |
| 7583 | } else if (CUR == '@') { |
| 7584 | /* relative abbreviated attribute location path */ |
| 7585 | lc = 1; |
| 7586 | } else if (CUR == '.') { |
| 7587 | /* relative abbreviated attribute location path */ |
| 7588 | lc = 1; |
| 7589 | } else { |
| 7590 | /* |
| 7591 | * Problem is finding if we have a name here whether it's: |
| 7592 | * - a nodetype |
| 7593 | * - a function call in which case it's followed by '(' |
| 7594 | * - an axis in which case it's followed by ':' |
| 7595 | * - a element name |
| 7596 | * We do an a priori analysis here rather than having to |
| 7597 | * maintain parsed token content through the recursive function |
| 7598 | * calls. This looks uglier but makes the code quite easier to |
| 7599 | * read/write/debug. |
| 7600 | */ |
| 7601 | SKIP_BLANKS; |
| 7602 | name = xmlXPathScanName(ctxt); |
| 7603 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 7604 | #ifdef DEBUG_STEP |
| 7605 | xmlGenericError(xmlGenericErrorContext, |
| 7606 | "PathExpr: Axis\n"); |
| 7607 | #endif |
| 7608 | lc = 1; |
| 7609 | xmlFree(name); |
| 7610 | } else if (name != NULL) { |
| 7611 | int len =xmlStrlen(name); |
| 7612 | int blank = 0; |
| 7613 | |
| 7614 | |
| 7615 | while (NXT(len) != 0) { |
| 7616 | if (NXT(len) == '/') { |
| 7617 | /* element name */ |
| 7618 | #ifdef DEBUG_STEP |
| 7619 | xmlGenericError(xmlGenericErrorContext, |
| 7620 | "PathExpr: AbbrRelLocation\n"); |
| 7621 | #endif |
| 7622 | lc = 1; |
| 7623 | break; |
| 7624 | } else if (IS_BLANK(NXT(len))) { |
| 7625 | /* skip to next */ |
| 7626 | blank = 1; |
| 7627 | } else if (NXT(len) == ':') { |
| 7628 | #ifdef DEBUG_STEP |
| 7629 | xmlGenericError(xmlGenericErrorContext, |
| 7630 | "PathExpr: AbbrRelLocation\n"); |
| 7631 | #endif |
| 7632 | lc = 1; |
| 7633 | break; |
| 7634 | } else if ((NXT(len) == '(')) { |
| 7635 | /* Note Type or Function */ |
| 7636 | if (xmlXPathIsNodeType(name)) { |
| 7637 | #ifdef DEBUG_STEP |
| 7638 | xmlGenericError(xmlGenericErrorContext, |
| 7639 | "PathExpr: Type search\n"); |
| 7640 | #endif |
| 7641 | lc = 1; |
| 7642 | } else { |
| 7643 | #ifdef DEBUG_STEP |
| 7644 | xmlGenericError(xmlGenericErrorContext, |
| 7645 | "PathExpr: function call\n"); |
| 7646 | #endif |
| 7647 | lc = 0; |
| 7648 | } |
| 7649 | break; |
| 7650 | } else if ((NXT(len) == '[')) { |
| 7651 | /* element name */ |
| 7652 | #ifdef DEBUG_STEP |
| 7653 | xmlGenericError(xmlGenericErrorContext, |
| 7654 | "PathExpr: AbbrRelLocation\n"); |
| 7655 | #endif |
| 7656 | lc = 1; |
| 7657 | break; |
| 7658 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 7659 | (NXT(len) == '=')) { |
| 7660 | lc = 1; |
| 7661 | break; |
| 7662 | } else { |
| 7663 | lc = 1; |
| 7664 | break; |
| 7665 | } |
| 7666 | len++; |
| 7667 | } |
| 7668 | if (NXT(len) == 0) { |
| 7669 | #ifdef DEBUG_STEP |
| 7670 | xmlGenericError(xmlGenericErrorContext, |
| 7671 | "PathExpr: AbbrRelLocation\n"); |
| 7672 | #endif |
| 7673 | /* element name */ |
| 7674 | lc = 1; |
| 7675 | } |
| 7676 | xmlFree(name); |
| 7677 | } else { |
| 7678 | /* make sure all cases are covered explicitely */ |
| 7679 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7680 | } |
| 7681 | } |
| 7682 | |
| 7683 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7684 | if (CUR == '/') { |
| 7685 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 7686 | } else { |
| 7687 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7688 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7689 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7690 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7691 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7692 | CHECK_ERROR; |
| 7693 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 7694 | SKIP(2); |
| 7695 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7696 | |
| 7697 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 7698 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 7699 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 7700 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7701 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7702 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7703 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7704 | } |
| 7705 | } |
| 7706 | SKIP_BLANKS; |
| 7707 | } |
| 7708 | |
| 7709 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7710 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7711 | * @ctxt: the XPath Parser context |
| 7712 | * |
| 7713 | * [18] UnionExpr ::= PathExpr |
| 7714 | * | UnionExpr '|' PathExpr |
| 7715 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7716 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7717 | */ |
| 7718 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7719 | static void |
| 7720 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 7721 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7722 | CHECK_ERROR; |
| 7723 | SKIP_BLANKS; |
| 7724 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7725 | int op1 = ctxt->comp->last; |
| 7726 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7727 | |
| 7728 | NEXT; |
| 7729 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7730 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7731 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7732 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 7733 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7734 | SKIP_BLANKS; |
| 7735 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
| 7738 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7739 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7740 | * @ctxt: the XPath Parser context |
| 7741 | * |
| 7742 | * [27] UnaryExpr ::= UnionExpr |
| 7743 | * | '-' UnaryExpr |
| 7744 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7745 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7746 | */ |
| 7747 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7748 | static void |
| 7749 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7750 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7751 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7752 | |
| 7753 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 7754 | while (CUR == '-') { |
| 7755 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7756 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7757 | NEXT; |
| 7758 | SKIP_BLANKS; |
| 7759 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7760 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7761 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7762 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7763 | if (found) { |
| 7764 | if (minus) |
| 7765 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 7766 | else |
| 7767 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7768 | } |
| 7769 | } |
| 7770 | |
| 7771 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7772 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7773 | * @ctxt: the XPath Parser context |
| 7774 | * |
| 7775 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 7776 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 7777 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7778 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7779 | * [34] MultiplyOperator ::= '*' |
| 7780 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7781 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7782 | */ |
| 7783 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7784 | static void |
| 7785 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 7786 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7787 | CHECK_ERROR; |
| 7788 | SKIP_BLANKS; |
| 7789 | while ((CUR == '*') || |
| 7790 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 7791 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 7792 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7793 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7794 | |
| 7795 | if (CUR == '*') { |
| 7796 | op = 0; |
| 7797 | NEXT; |
| 7798 | } else if (CUR == 'd') { |
| 7799 | op = 1; |
| 7800 | SKIP(3); |
| 7801 | } else if (CUR == 'm') { |
| 7802 | op = 2; |
| 7803 | SKIP(3); |
| 7804 | } |
| 7805 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7806 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7807 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7808 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7809 | SKIP_BLANKS; |
| 7810 | } |
| 7811 | } |
| 7812 | |
| 7813 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7814 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7815 | * @ctxt: the XPath Parser context |
| 7816 | * |
| 7817 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 7818 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7819 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7820 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7821 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7822 | */ |
| 7823 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7824 | static void |
| 7825 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7826 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7827 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7828 | CHECK_ERROR; |
| 7829 | SKIP_BLANKS; |
| 7830 | while ((CUR == '+') || (CUR == '-')) { |
| 7831 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7832 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7833 | |
| 7834 | if (CUR == '+') plus = 1; |
| 7835 | else plus = 0; |
| 7836 | NEXT; |
| 7837 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7838 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7839 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7840 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7841 | SKIP_BLANKS; |
| 7842 | } |
| 7843 | } |
| 7844 | |
| 7845 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7846 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7847 | * @ctxt: the XPath Parser context |
| 7848 | * |
| 7849 | * [24] RelationalExpr ::= AdditiveExpr |
| 7850 | * | RelationalExpr '<' AdditiveExpr |
| 7851 | * | RelationalExpr '>' AdditiveExpr |
| 7852 | * | RelationalExpr '<=' AdditiveExpr |
| 7853 | * | RelationalExpr '>=' AdditiveExpr |
| 7854 | * |
| 7855 | * A <= B > C is allowed ? Answer from James, yes with |
| 7856 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 7857 | * which is basically what got implemented. |
| 7858 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7859 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7860 | * on the stack |
| 7861 | */ |
| 7862 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7863 | static void |
| 7864 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 7865 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7866 | CHECK_ERROR; |
| 7867 | SKIP_BLANKS; |
| 7868 | while ((CUR == '<') || |
| 7869 | (CUR == '>') || |
| 7870 | ((CUR == '<') && (NXT(1) == '=')) || |
| 7871 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7872 | int inf, strict; |
| 7873 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7874 | |
| 7875 | if (CUR == '<') inf = 1; |
| 7876 | else inf = 0; |
| 7877 | if (NXT(1) == '=') strict = 0; |
| 7878 | else strict = 1; |
| 7879 | NEXT; |
| 7880 | if (!strict) NEXT; |
| 7881 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7882 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7883 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7884 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7885 | SKIP_BLANKS; |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7890 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7891 | * @ctxt: the XPath Parser context |
| 7892 | * |
| 7893 | * [23] EqualityExpr ::= RelationalExpr |
| 7894 | * | EqualityExpr '=' RelationalExpr |
| 7895 | * | EqualityExpr '!=' RelationalExpr |
| 7896 | * |
| 7897 | * A != B != C is allowed ? Answer from James, yes with |
| 7898 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 7899 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 7900 | * which is basically what got implemented. |
| 7901 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7902 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7903 | * |
| 7904 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7905 | static void |
| 7906 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 7907 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7908 | CHECK_ERROR; |
| 7909 | SKIP_BLANKS; |
| 7910 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7911 | int eq; |
| 7912 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7913 | |
| 7914 | if (CUR == '=') eq = 1; |
| 7915 | else eq = 0; |
| 7916 | NEXT; |
| 7917 | if (!eq) NEXT; |
| 7918 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7919 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7920 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7921 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7922 | SKIP_BLANKS; |
| 7923 | } |
| 7924 | } |
| 7925 | |
| 7926 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7927 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7928 | * @ctxt: the XPath Parser context |
| 7929 | * |
| 7930 | * [22] AndExpr ::= EqualityExpr |
| 7931 | * | AndExpr 'and' EqualityExpr |
| 7932 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7933 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7934 | * |
| 7935 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7936 | static void |
| 7937 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 7938 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7939 | CHECK_ERROR; |
| 7940 | SKIP_BLANKS; |
| 7941 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7942 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7943 | SKIP(3); |
| 7944 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7945 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7946 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7947 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7948 | SKIP_BLANKS; |
| 7949 | } |
| 7950 | } |
| 7951 | |
| 7952 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7953 | * xmlXPathCompExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7954 | * @ctxt: the XPath Parser context |
| 7955 | * |
| 7956 | * [14] Expr ::= OrExpr |
| 7957 | * [21] OrExpr ::= AndExpr |
| 7958 | * | OrExpr 'or' AndExpr |
| 7959 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7960 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7961 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7962 | static void |
| 7963 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 7964 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7965 | CHECK_ERROR; |
| 7966 | SKIP_BLANKS; |
| 7967 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7968 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7969 | SKIP(2); |
| 7970 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7971 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7972 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7973 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 7974 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7975 | SKIP_BLANKS; |
| 7976 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7977 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 7978 | /* more ops could be optimized too */ |
| 7979 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 7980 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7981 | } |
| 7982 | |
| 7983 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7984 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7985 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7986 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7987 | * |
| 7988 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 7989 | * [9] PredicateExpr ::= Expr |
| 7990 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7991 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7992 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7993 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7994 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7995 | int op1 = ctxt->comp->last; |
| 7996 | |
| 7997 | SKIP_BLANKS; |
| 7998 | if (CUR != '[') { |
| 7999 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8000 | } |
| 8001 | NEXT; |
| 8002 | SKIP_BLANKS; |
| 8003 | |
| 8004 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8005 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8006 | CHECK_ERROR; |
| 8007 | |
| 8008 | if (CUR != ']') { |
| 8009 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8010 | } |
| 8011 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8012 | if (filter) |
| 8013 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8014 | else |
| 8015 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8016 | |
| 8017 | NEXT; |
| 8018 | SKIP_BLANKS; |
| 8019 | } |
| 8020 | |
| 8021 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8022 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8023 | * @ctxt: the XPath Parser context |
| 8024 | * @test: pointer to a xmlXPathTestVal |
| 8025 | * @type: pointer to a xmlXPathTypeVal |
| 8026 | * @prefix: placeholder for a possible name prefix |
| 8027 | * |
| 8028 | * [7] NodeTest ::= NameTest |
| 8029 | * | NodeType '(' ')' |
| 8030 | * | 'processing-instruction' '(' Literal ')' |
| 8031 | * |
| 8032 | * [37] NameTest ::= '*' |
| 8033 | * | NCName ':' '*' |
| 8034 | * | QName |
| 8035 | * [38] NodeType ::= 'comment' |
| 8036 | * | 'text' |
| 8037 | * | 'processing-instruction' |
| 8038 | * | 'node' |
| 8039 | * |
| 8040 | * Returns the name found and update @test, @type and @prefix appropriately |
| 8041 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8042 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8043 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8044 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8045 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8046 | int blanks; |
| 8047 | |
| 8048 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8049 | STRANGE; |
| 8050 | return(NULL); |
| 8051 | } |
| 8052 | *type = 0; |
| 8053 | *test = 0; |
| 8054 | *prefix = NULL; |
| 8055 | SKIP_BLANKS; |
| 8056 | |
| 8057 | if ((name == NULL) && (CUR == '*')) { |
| 8058 | /* |
| 8059 | * All elements |
| 8060 | */ |
| 8061 | NEXT; |
| 8062 | *test = NODE_TEST_ALL; |
| 8063 | return(NULL); |
| 8064 | } |
| 8065 | |
| 8066 | if (name == NULL) |
| 8067 | name = xmlXPathParseNCName(ctxt); |
| 8068 | if (name == NULL) { |
| 8069 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8070 | } |
| 8071 | |
| 8072 | blanks = IS_BLANK(CUR); |
| 8073 | SKIP_BLANKS; |
| 8074 | if (CUR == '(') { |
| 8075 | NEXT; |
| 8076 | /* |
| 8077 | * NodeType or PI search |
| 8078 | */ |
| 8079 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8080 | *type = NODE_TYPE_COMMENT; |
| 8081 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8082 | *type = NODE_TYPE_NODE; |
| 8083 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8084 | *type = NODE_TYPE_PI; |
| 8085 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8086 | *type = NODE_TYPE_TEXT; |
| 8087 | else { |
| 8088 | if (name != NULL) |
| 8089 | xmlFree(name); |
| 8090 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8091 | } |
| 8092 | |
| 8093 | *test = NODE_TEST_TYPE; |
| 8094 | |
| 8095 | SKIP_BLANKS; |
| 8096 | if (*type == NODE_TYPE_PI) { |
| 8097 | /* |
| 8098 | * Specific case: search a PI by name. |
| 8099 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8100 | if (name != NULL) |
| 8101 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8102 | name = NULL; |
| 8103 | if (CUR != ')') { |
| 8104 | name = xmlXPathParseLiteral(ctxt); |
| 8105 | CHECK_ERROR 0; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8106 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8107 | SKIP_BLANKS; |
| 8108 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8109 | } |
| 8110 | if (CUR != ')') { |
| 8111 | if (name != NULL) |
| 8112 | xmlFree(name); |
| 8113 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
| 8114 | } |
| 8115 | NEXT; |
| 8116 | return(name); |
| 8117 | } |
| 8118 | *test = NODE_TEST_NAME; |
| 8119 | if ((!blanks) && (CUR == ':')) { |
| 8120 | NEXT; |
| 8121 | |
| 8122 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8123 | * Since currently the parser context don't have a |
| 8124 | * namespace list associated: |
| 8125 | * The namespace name for this prefix can be computed |
| 8126 | * only at evaluation time. The compilation is done |
| 8127 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8128 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8129 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8130 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8131 | if (name != NULL) |
| 8132 | xmlFree(name); |
| 8133 | if (*prefix == NULL) { |
| 8134 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8135 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8136 | #else |
| 8137 | *prefix = name; |
| 8138 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8139 | |
| 8140 | if (CUR == '*') { |
| 8141 | /* |
| 8142 | * All elements |
| 8143 | */ |
| 8144 | NEXT; |
| 8145 | *test = NODE_TEST_ALL; |
| 8146 | return(NULL); |
| 8147 | } |
| 8148 | |
| 8149 | name = xmlXPathParseNCName(ctxt); |
| 8150 | if (name == NULL) { |
| 8151 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8152 | } |
| 8153 | } |
| 8154 | return(name); |
| 8155 | } |
| 8156 | |
| 8157 | /** |
| 8158 | * xmlXPathIsAxisName: |
| 8159 | * @name: a preparsed name token |
| 8160 | * |
| 8161 | * [6] AxisName ::= 'ancestor' |
| 8162 | * | 'ancestor-or-self' |
| 8163 | * | 'attribute' |
| 8164 | * | 'child' |
| 8165 | * | 'descendant' |
| 8166 | * | 'descendant-or-self' |
| 8167 | * | 'following' |
| 8168 | * | 'following-sibling' |
| 8169 | * | 'namespace' |
| 8170 | * | 'parent' |
| 8171 | * | 'preceding' |
| 8172 | * | 'preceding-sibling' |
| 8173 | * | 'self' |
| 8174 | * |
| 8175 | * Returns the axis or 0 |
| 8176 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8177 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8178 | xmlXPathIsAxisName(const xmlChar *name) { |
| 8179 | xmlXPathAxisVal ret = 0; |
| 8180 | switch (name[0]) { |
| 8181 | case 'a': |
| 8182 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8183 | ret = AXIS_ANCESTOR; |
| 8184 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8185 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8186 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8187 | ret = AXIS_ATTRIBUTE; |
| 8188 | break; |
| 8189 | case 'c': |
| 8190 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8191 | ret = AXIS_CHILD; |
| 8192 | break; |
| 8193 | case 'd': |
| 8194 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8195 | ret = AXIS_DESCENDANT; |
| 8196 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8197 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8198 | break; |
| 8199 | case 'f': |
| 8200 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8201 | ret = AXIS_FOLLOWING; |
| 8202 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8203 | ret = AXIS_FOLLOWING_SIBLING; |
| 8204 | break; |
| 8205 | case 'n': |
| 8206 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8207 | ret = AXIS_NAMESPACE; |
| 8208 | break; |
| 8209 | case 'p': |
| 8210 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8211 | ret = AXIS_PARENT; |
| 8212 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8213 | ret = AXIS_PRECEDING; |
| 8214 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8215 | ret = AXIS_PRECEDING_SIBLING; |
| 8216 | break; |
| 8217 | case 's': |
| 8218 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8219 | ret = AXIS_SELF; |
| 8220 | break; |
| 8221 | } |
| 8222 | return(ret); |
| 8223 | } |
| 8224 | |
| 8225 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8226 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8227 | * @ctxt: the XPath Parser context |
| 8228 | * |
| 8229 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8230 | * | AbbreviatedStep |
| 8231 | * |
| 8232 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8233 | * |
| 8234 | * [5] AxisSpecifier ::= AxisName '::' |
| 8235 | * | AbbreviatedAxisSpecifier |
| 8236 | * |
| 8237 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8238 | * |
| 8239 | * Modified for XPtr range support as: |
| 8240 | * |
| 8241 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8242 | * | AbbreviatedStep |
| 8243 | * | 'range-to' '(' Expr ')' Predicate* |
| 8244 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8245 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8246 | * A location step of . is short for self::node(). This is |
| 8247 | * particularly useful in conjunction with //. For example, the |
| 8248 | * location path .//para is short for |
| 8249 | * self::node()/descendant-or-self::node()/child::para |
| 8250 | * and so will select all para descendant elements of the context |
| 8251 | * node. |
| 8252 | * Similarly, a location step of .. is short for parent::node(). |
| 8253 | * For example, ../title is short for parent::node()/child::title |
| 8254 | * and so will select the title children of the parent of the context |
| 8255 | * node. |
| 8256 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8257 | static void |
| 8258 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8259 | #ifdef LIBXML_XPTR_ENABLED |
| 8260 | int rangeto = 0; |
| 8261 | int op2 = -1; |
| 8262 | #endif |
| 8263 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8264 | SKIP_BLANKS; |
| 8265 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8266 | SKIP(2); |
| 8267 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8268 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8269 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8270 | } else if (CUR == '.') { |
| 8271 | NEXT; |
| 8272 | SKIP_BLANKS; |
| 8273 | } else { |
| 8274 | xmlChar *name = NULL; |
| 8275 | const xmlChar *prefix = NULL; |
| 8276 | xmlXPathTestVal test; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8277 | xmlXPathAxisVal axis = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8278 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8279 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8280 | |
| 8281 | /* |
| 8282 | * The modification needed for XPointer change to the production |
| 8283 | */ |
| 8284 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8285 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8286 | name = xmlXPathParseNCName(ctxt); |
| 8287 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8288 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8289 | xmlFree(name); |
| 8290 | SKIP_BLANKS; |
| 8291 | if (CUR != '(') { |
| 8292 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8293 | } |
| 8294 | NEXT; |
| 8295 | SKIP_BLANKS; |
| 8296 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8297 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8298 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8299 | CHECK_ERROR; |
| 8300 | |
| 8301 | SKIP_BLANKS; |
| 8302 | if (CUR != ')') { |
| 8303 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8304 | } |
| 8305 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8306 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8307 | goto eval_predicates; |
| 8308 | } |
| 8309 | } |
| 8310 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8311 | if (CUR == '*') { |
| 8312 | axis = AXIS_CHILD; |
| 8313 | } else { |
| 8314 | if (name == NULL) |
| 8315 | name = xmlXPathParseNCName(ctxt); |
| 8316 | if (name != NULL) { |
| 8317 | axis = xmlXPathIsAxisName(name); |
| 8318 | if (axis != 0) { |
| 8319 | SKIP_BLANKS; |
| 8320 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8321 | SKIP(2); |
| 8322 | xmlFree(name); |
| 8323 | name = NULL; |
| 8324 | } else { |
| 8325 | /* an element name can conflict with an axis one :-\ */ |
| 8326 | axis = AXIS_CHILD; |
| 8327 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8328 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8329 | axis = AXIS_CHILD; |
| 8330 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8331 | } else if (CUR == '@') { |
| 8332 | NEXT; |
| 8333 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8334 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8335 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8336 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8337 | } |
| 8338 | |
| 8339 | CHECK_ERROR; |
| 8340 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8341 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8342 | if (test == 0) |
| 8343 | return; |
| 8344 | |
| 8345 | #ifdef DEBUG_STEP |
| 8346 | xmlGenericError(xmlGenericErrorContext, |
| 8347 | "Basis : computing new set\n"); |
| 8348 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8349 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8350 | #ifdef DEBUG_STEP |
| 8351 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8352 | if (ctxt->value == NULL) |
| 8353 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8354 | else if (ctxt->value->nodesetval == NULL) |
| 8355 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8356 | else |
| 8357 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8358 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | |
| 8360 | eval_predicates: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8361 | op1 = ctxt->comp->last; |
| 8362 | ctxt->comp->last = -1; |
| 8363 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8364 | SKIP_BLANKS; |
| 8365 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8366 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8367 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8368 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8369 | #ifdef LIBXML_XPTR_ENABLED |
| 8370 | if (rangeto) { |
| 8371 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8372 | } else |
| 8373 | #endif |
| 8374 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8375 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8376 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8377 | } |
| 8378 | #ifdef DEBUG_STEP |
| 8379 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8380 | if (ctxt->value == NULL) |
| 8381 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8382 | else if (ctxt->value->nodesetval == NULL) |
| 8383 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8384 | else |
| 8385 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8386 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8387 | #endif |
| 8388 | } |
| 8389 | |
| 8390 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8391 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8392 | * @ctxt: the XPath Parser context |
| 8393 | * |
| 8394 | * [3] RelativeLocationPath ::= Step |
| 8395 | * | RelativeLocationPath '/' Step |
| 8396 | * | AbbreviatedRelativeLocationPath |
| 8397 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8398 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8399 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8400 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8401 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8402 | #ifdef VMS |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8403 | xmlXPathCompRelLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8404 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8405 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8406 | #endif |
| 8407 | (xmlXPathParserContextPtr ctxt) { |
| 8408 | SKIP_BLANKS; |
| 8409 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8410 | SKIP(2); |
| 8411 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8412 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8413 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8414 | } else if (CUR == '/') { |
| 8415 | NEXT; |
| 8416 | SKIP_BLANKS; |
| 8417 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8418 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8419 | SKIP_BLANKS; |
| 8420 | while (CUR == '/') { |
| 8421 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8422 | SKIP(2); |
| 8423 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8424 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8425 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8426 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8427 | } else if (CUR == '/') { |
| 8428 | NEXT; |
| 8429 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8430 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8431 | } |
| 8432 | SKIP_BLANKS; |
| 8433 | } |
| 8434 | } |
| 8435 | |
| 8436 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8437 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8438 | * @ctxt: the XPath Parser context |
| 8439 | * |
| 8440 | * [1] LocationPath ::= RelativeLocationPath |
| 8441 | * | AbsoluteLocationPath |
| 8442 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 8443 | * | AbbreviatedAbsoluteLocationPath |
| 8444 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 8445 | * '//' RelativeLocationPath |
| 8446 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8447 | * Compile a location path |
| 8448 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8449 | * // is short for /descendant-or-self::node()/. For example, |
| 8450 | * //para is short for /descendant-or-self::node()/child::para and |
| 8451 | * so will select any para element in the document (even a para element |
| 8452 | * that is a document element will be selected by //para since the |
| 8453 | * document element node is a child of the root node); div//para is |
| 8454 | * short for div/descendant-or-self::node()/child::para and so will |
| 8455 | * select all para descendants of div children. |
| 8456 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8457 | static void |
| 8458 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8459 | SKIP_BLANKS; |
| 8460 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8461 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8462 | } else { |
| 8463 | while (CUR == '/') { |
| 8464 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8465 | SKIP(2); |
| 8466 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8467 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8468 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8469 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8470 | } else if (CUR == '/') { |
| 8471 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 8472 | SKIP_BLANKS; |
| 8473 | if ((CUR != 0 ) && |
| 8474 | ((IS_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
| 8475 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8476 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8477 | } |
| 8478 | } |
| 8479 | } |
| 8480 | } |
| 8481 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8482 | /************************************************************************ |
| 8483 | * * |
| 8484 | * XPath precompiled expression evaluation * |
| 8485 | * * |
| 8486 | ************************************************************************/ |
| 8487 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8488 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8489 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 8490 | |
| 8491 | /** |
| 8492 | * xmlXPathNodeCollectAndTest: |
| 8493 | * @ctxt: the XPath Parser context |
| 8494 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8495 | * @first: pointer to the first element in document order |
| 8496 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8497 | * |
| 8498 | * This is the function implementing a step: based on the current list |
| 8499 | * of nodes, it builds up a new list, looking at all nodes under that |
| 8500 | * axis and selecting them it also do the predicate filtering |
| 8501 | * |
| 8502 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8503 | * |
| 8504 | * Returns the number of node traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8505 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8506 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8507 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8508 | xmlXPathStepOpPtr op, |
| 8509 | xmlNodePtr * first, xmlNodePtr * last) |
| 8510 | { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8511 | xmlXPathAxisVal axis = op->value; |
| 8512 | xmlXPathTestVal test = op->value2; |
| 8513 | xmlXPathTypeVal type = op->value3; |
| 8514 | const xmlChar *prefix = op->value4; |
| 8515 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8516 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8517 | |
| 8518 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8519 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8520 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8521 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8522 | xmlNodeSetPtr ret, list; |
| 8523 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8524 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8525 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8526 | xmlNodePtr cur = NULL; |
| 8527 | xmlXPathObjectPtr obj; |
| 8528 | xmlNodeSetPtr nodelist; |
| 8529 | xmlNodePtr tmp; |
| 8530 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8531 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8532 | obj = valuePop(ctxt); |
| 8533 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8534 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8535 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8536 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 8537 | if (URI == NULL) |
| 8538 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8539 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8540 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8541 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | switch (axis) { |
| 8544 | case AXIS_ANCESTOR: |
| 8545 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8546 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8547 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8548 | first = NULL; |
| 8549 | next = xmlXPathNextAncestor; |
| 8550 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8551 | case AXIS_ANCESTOR_OR_SELF: |
| 8552 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8553 | xmlGenericError(xmlGenericErrorContext, |
| 8554 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8555 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8556 | first = NULL; |
| 8557 | next = xmlXPathNextAncestorOrSelf; |
| 8558 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8559 | case AXIS_ATTRIBUTE: |
| 8560 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8561 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8562 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8563 | first = NULL; |
| 8564 | last = NULL; |
| 8565 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8566 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8567 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8568 | case AXIS_CHILD: |
| 8569 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8570 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8571 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8572 | last = NULL; |
| 8573 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8574 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8575 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8576 | case AXIS_DESCENDANT: |
| 8577 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8578 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8579 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8580 | last = NULL; |
| 8581 | next = xmlXPathNextDescendant; |
| 8582 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8583 | case AXIS_DESCENDANT_OR_SELF: |
| 8584 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8585 | xmlGenericError(xmlGenericErrorContext, |
| 8586 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8587 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8588 | last = NULL; |
| 8589 | next = xmlXPathNextDescendantOrSelf; |
| 8590 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8591 | case AXIS_FOLLOWING: |
| 8592 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8593 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8594 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8595 | last = NULL; |
| 8596 | next = xmlXPathNextFollowing; |
| 8597 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8598 | case AXIS_FOLLOWING_SIBLING: |
| 8599 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8600 | xmlGenericError(xmlGenericErrorContext, |
| 8601 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8602 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8603 | last = NULL; |
| 8604 | next = xmlXPathNextFollowingSibling; |
| 8605 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8606 | case AXIS_NAMESPACE: |
| 8607 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8608 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8609 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8610 | first = NULL; |
| 8611 | last = NULL; |
| 8612 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8613 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8614 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8615 | case AXIS_PARENT: |
| 8616 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8617 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8618 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8619 | first = NULL; |
| 8620 | next = xmlXPathNextParent; |
| 8621 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8622 | case AXIS_PRECEDING: |
| 8623 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8624 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8625 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8626 | first = NULL; |
| 8627 | next = xmlXPathNextPrecedingInternal; |
| 8628 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8629 | case AXIS_PRECEDING_SIBLING: |
| 8630 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8631 | xmlGenericError(xmlGenericErrorContext, |
| 8632 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8633 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8634 | first = NULL; |
| 8635 | next = xmlXPathNextPrecedingSibling; |
| 8636 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8637 | case AXIS_SELF: |
| 8638 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8639 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8640 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8641 | first = NULL; |
| 8642 | last = NULL; |
| 8643 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8644 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8645 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8646 | } |
| 8647 | if (next == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8648 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8649 | |
| 8650 | nodelist = obj->nodesetval; |
| 8651 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8652 | xmlXPathFreeObject(obj); |
| 8653 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 8654 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8655 | } |
| 8656 | addNode = xmlXPathNodeSetAddUnique; |
| 8657 | ret = NULL; |
| 8658 | #ifdef DEBUG_STEP |
| 8659 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8660 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8661 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8662 | case NODE_TEST_NONE: |
| 8663 | xmlGenericError(xmlGenericErrorContext, |
| 8664 | " searching for none !!!\n"); |
| 8665 | break; |
| 8666 | case NODE_TEST_TYPE: |
| 8667 | xmlGenericError(xmlGenericErrorContext, |
| 8668 | " searching for type %d\n", type); |
| 8669 | break; |
| 8670 | case NODE_TEST_PI: |
| 8671 | xmlGenericError(xmlGenericErrorContext, |
| 8672 | " searching for PI !!!\n"); |
| 8673 | break; |
| 8674 | case NODE_TEST_ALL: |
| 8675 | xmlGenericError(xmlGenericErrorContext, |
| 8676 | " searching for *\n"); |
| 8677 | break; |
| 8678 | case NODE_TEST_NS: |
| 8679 | xmlGenericError(xmlGenericErrorContext, |
| 8680 | " searching for namespace %s\n", |
| 8681 | prefix); |
| 8682 | break; |
| 8683 | case NODE_TEST_NAME: |
| 8684 | xmlGenericError(xmlGenericErrorContext, |
| 8685 | " searching for name %s\n", name); |
| 8686 | if (prefix != NULL) |
| 8687 | xmlGenericError(xmlGenericErrorContext, |
| 8688 | " with namespace %s\n", prefix); |
| 8689 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8690 | } |
| 8691 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 8692 | #endif |
| 8693 | /* |
| 8694 | * 2.3 Node Tests |
| 8695 | * - For the attribute axis, the principal node type is attribute. |
| 8696 | * - For the namespace axis, the principal node type is namespace. |
| 8697 | * - For other axes, the principal node type is element. |
| 8698 | * |
| 8699 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8700 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8701 | * select all element children of the context node |
| 8702 | */ |
| 8703 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8704 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8705 | ctxt->context->node = nodelist->nodeTab[i]; |
| 8706 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8707 | cur = NULL; |
| 8708 | list = xmlXPathNodeSetCreate(NULL); |
| 8709 | do { |
| 8710 | cur = next(ctxt, cur); |
| 8711 | if (cur == NULL) |
| 8712 | break; |
| 8713 | if ((first != NULL) && (*first == cur)) |
| 8714 | break; |
| 8715 | if (((t % 256) == 0) && |
| 8716 | (first != NULL) && (*first != NULL) && |
| 8717 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 8718 | break; |
| 8719 | if ((last != NULL) && (*last == cur)) |
| 8720 | break; |
| 8721 | if (((t % 256) == 0) && |
| 8722 | (last != NULL) && (*last != NULL) && |
| 8723 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 8724 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8725 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8726 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8727 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 8728 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8729 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8730 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8731 | ctxt->context->node = tmp; |
| 8732 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8733 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8734 | if ((cur->type == type) || |
| 8735 | ((type == NODE_TYPE_NODE) && |
| 8736 | ((cur->type == XML_DOCUMENT_NODE) || |
| 8737 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 8738 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 8739 | (cur->type == XML_NAMESPACE_DECL) || |
| 8740 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8741 | (cur->type == XML_PI_NODE) || |
| 8742 | (cur->type == XML_COMMENT_NODE) || |
| 8743 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 8744 | (cur->type == XML_TEXT_NODE))) || |
| 8745 | ((type == NODE_TYPE_TEXT) && |
| 8746 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8747 | #ifdef DEBUG_STEP |
| 8748 | n++; |
| 8749 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8750 | addNode(list, cur); |
| 8751 | } |
| 8752 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8753 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8754 | if (cur->type == XML_PI_NODE) { |
| 8755 | if ((name != NULL) && |
| 8756 | (!xmlStrEqual(name, cur->name))) |
| 8757 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8758 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8759 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8760 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8761 | addNode(list, cur); |
| 8762 | } |
| 8763 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8764 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8765 | if (axis == AXIS_ATTRIBUTE) { |
| 8766 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8767 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8768 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8769 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8770 | addNode(list, cur); |
| 8771 | } |
| 8772 | } else if (axis == AXIS_NAMESPACE) { |
| 8773 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8774 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8775 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8776 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8777 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 8778 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8779 | } |
| 8780 | } else { |
| 8781 | if (cur->type == XML_ELEMENT_NODE) { |
| 8782 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8783 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8784 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8785 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8786 | addNode(list, cur); |
| 8787 | } else if ((cur->ns != NULL) && |
| 8788 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8789 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8790 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8791 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8792 | addNode(list, cur); |
| 8793 | } |
| 8794 | } |
| 8795 | } |
| 8796 | break; |
| 8797 | case NODE_TEST_NS:{ |
| 8798 | TODO; |
| 8799 | break; |
| 8800 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8801 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8802 | switch (cur->type) { |
| 8803 | case XML_ELEMENT_NODE: |
| 8804 | if (xmlStrEqual(name, cur->name)) { |
| 8805 | if (prefix == NULL) { |
| 8806 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8807 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8808 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8809 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8810 | addNode(list, cur); |
| 8811 | } |
| 8812 | } else { |
| 8813 | if ((cur->ns != NULL) && |
| 8814 | (xmlStrEqual(URI, |
| 8815 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8816 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8817 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8818 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8819 | addNode(list, cur); |
| 8820 | } |
| 8821 | } |
| 8822 | } |
| 8823 | break; |
| 8824 | case XML_ATTRIBUTE_NODE:{ |
| 8825 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8826 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8827 | if (xmlStrEqual(name, attr->name)) { |
| 8828 | if (prefix == NULL) { |
| 8829 | if ((attr->ns == NULL) || |
| 8830 | (attr->ns->prefix == NULL)) { |
| 8831 | #ifdef DEBUG_STEP |
| 8832 | n++; |
| 8833 | #endif |
| 8834 | addNode(list, |
| 8835 | (xmlNodePtr) attr); |
| 8836 | } |
| 8837 | } else { |
| 8838 | if ((attr->ns != NULL) && |
| 8839 | (xmlStrEqual(URI, |
| 8840 | attr->ns-> |
| 8841 | href))) { |
| 8842 | #ifdef DEBUG_STEP |
| 8843 | n++; |
| 8844 | #endif |
| 8845 | addNode(list, |
| 8846 | (xmlNodePtr) attr); |
| 8847 | } |
| 8848 | } |
| 8849 | } |
| 8850 | break; |
| 8851 | } |
| 8852 | case XML_NAMESPACE_DECL: |
| 8853 | if (cur->type == XML_NAMESPACE_DECL) { |
| 8854 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8855 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8856 | if ((ns->prefix != NULL) && (name != NULL) |
| 8857 | && (xmlStrEqual(ns->prefix, name))) { |
| 8858 | #ifdef DEBUG_STEP |
| 8859 | n++; |
| 8860 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8861 | xmlXPathNodeSetAddNs(list, |
| 8862 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8863 | } |
| 8864 | } |
| 8865 | break; |
| 8866 | default: |
| 8867 | break; |
| 8868 | } |
| 8869 | break; |
| 8870 | break; |
| 8871 | } |
| 8872 | } while (cur != NULL); |
| 8873 | |
| 8874 | /* |
| 8875 | * If there is some predicate filtering do it now |
| 8876 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 8877 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8878 | xmlXPathObjectPtr obj2; |
| 8879 | |
| 8880 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 8881 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 8882 | CHECK_TYPE0(XPATH_NODESET); |
| 8883 | obj2 = valuePop(ctxt); |
| 8884 | list = obj2->nodesetval; |
| 8885 | obj2->nodesetval = NULL; |
| 8886 | xmlXPathFreeObject(obj2); |
| 8887 | } |
| 8888 | if (ret == NULL) { |
| 8889 | ret = list; |
| 8890 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8891 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8892 | xmlXPathFreeNodeSet(list); |
| 8893 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8894 | } |
| 8895 | ctxt->context->node = tmp; |
| 8896 | #ifdef DEBUG_STEP |
| 8897 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8898 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 8899 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8900 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8901 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 8902 | if ((obj->boolval) && (obj->user != NULL)) { |
| 8903 | ctxt->value->boolval = 1; |
| 8904 | ctxt->value->user = obj->user; |
| 8905 | obj->user = NULL; |
| 8906 | obj->boolval = 0; |
| 8907 | } |
| 8908 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8909 | return(t); |
| 8910 | } |
| 8911 | |
| 8912 | /** |
| 8913 | * xmlXPathNodeCollectAndTestNth: |
| 8914 | * @ctxt: the XPath Parser context |
| 8915 | * @op: the XPath precompiled step operation |
| 8916 | * @indx: the index to collect |
| 8917 | * @first: pointer to the first element in document order |
| 8918 | * @last: pointer to the last element in document order |
| 8919 | * |
| 8920 | * This is the function implementing a step: based on the current list |
| 8921 | * of nodes, it builds up a new list, looking at all nodes under that |
| 8922 | * axis and selecting them it also do the predicate filtering |
| 8923 | * |
| 8924 | * Pushes the new NodeSet resulting from the search. |
| 8925 | * Returns the number of node traversed |
| 8926 | */ |
| 8927 | static int |
| 8928 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 8929 | xmlXPathStepOpPtr op, int indx, |
| 8930 | xmlNodePtr * first, xmlNodePtr * last) |
| 8931 | { |
| 8932 | xmlXPathAxisVal axis = op->value; |
| 8933 | xmlXPathTestVal test = op->value2; |
| 8934 | xmlXPathTypeVal type = op->value3; |
| 8935 | const xmlChar *prefix = op->value4; |
| 8936 | const xmlChar *name = op->value5; |
| 8937 | const xmlChar *URI = NULL; |
| 8938 | int n = 0, t = 0; |
| 8939 | |
| 8940 | int i; |
| 8941 | xmlNodeSetPtr list; |
| 8942 | xmlXPathTraversalFunction next = NULL; |
| 8943 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 8944 | xmlNodePtr cur = NULL; |
| 8945 | xmlXPathObjectPtr obj; |
| 8946 | xmlNodeSetPtr nodelist; |
| 8947 | xmlNodePtr tmp; |
| 8948 | |
| 8949 | CHECK_TYPE0(XPATH_NODESET); |
| 8950 | obj = valuePop(ctxt); |
| 8951 | addNode = xmlXPathNodeSetAdd; |
| 8952 | if (prefix != NULL) { |
| 8953 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 8954 | if (URI == NULL) |
| 8955 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8956 | } |
| 8957 | #ifdef DEBUG_STEP_NTH |
| 8958 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 8959 | if (first != NULL) { |
| 8960 | if (*first != NULL) |
| 8961 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 8962 | (*first)->name); |
| 8963 | else |
| 8964 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 8965 | } |
| 8966 | if (last != NULL) { |
| 8967 | if (*last != NULL) |
| 8968 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 8969 | (*last)->name); |
| 8970 | else |
| 8971 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 8972 | } |
| 8973 | #endif |
| 8974 | switch (axis) { |
| 8975 | case AXIS_ANCESTOR: |
| 8976 | #ifdef DEBUG_STEP_NTH |
| 8977 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 8978 | #endif |
| 8979 | first = NULL; |
| 8980 | next = xmlXPathNextAncestor; |
| 8981 | break; |
| 8982 | case AXIS_ANCESTOR_OR_SELF: |
| 8983 | #ifdef DEBUG_STEP_NTH |
| 8984 | xmlGenericError(xmlGenericErrorContext, |
| 8985 | "axis 'ancestors-or-self' "); |
| 8986 | #endif |
| 8987 | first = NULL; |
| 8988 | next = xmlXPathNextAncestorOrSelf; |
| 8989 | break; |
| 8990 | case AXIS_ATTRIBUTE: |
| 8991 | #ifdef DEBUG_STEP_NTH |
| 8992 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 8993 | #endif |
| 8994 | first = NULL; |
| 8995 | last = NULL; |
| 8996 | next = xmlXPathNextAttribute; |
| 8997 | break; |
| 8998 | case AXIS_CHILD: |
| 8999 | #ifdef DEBUG_STEP_NTH |
| 9000 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9001 | #endif |
| 9002 | last = NULL; |
| 9003 | next = xmlXPathNextChild; |
| 9004 | break; |
| 9005 | case AXIS_DESCENDANT: |
| 9006 | #ifdef DEBUG_STEP_NTH |
| 9007 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9008 | #endif |
| 9009 | last = NULL; |
| 9010 | next = xmlXPathNextDescendant; |
| 9011 | break; |
| 9012 | case AXIS_DESCENDANT_OR_SELF: |
| 9013 | #ifdef DEBUG_STEP_NTH |
| 9014 | xmlGenericError(xmlGenericErrorContext, |
| 9015 | "axis 'descendant-or-self' "); |
| 9016 | #endif |
| 9017 | last = NULL; |
| 9018 | next = xmlXPathNextDescendantOrSelf; |
| 9019 | break; |
| 9020 | case AXIS_FOLLOWING: |
| 9021 | #ifdef DEBUG_STEP_NTH |
| 9022 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9023 | #endif |
| 9024 | last = NULL; |
| 9025 | next = xmlXPathNextFollowing; |
| 9026 | break; |
| 9027 | case AXIS_FOLLOWING_SIBLING: |
| 9028 | #ifdef DEBUG_STEP_NTH |
| 9029 | xmlGenericError(xmlGenericErrorContext, |
| 9030 | "axis 'following-siblings' "); |
| 9031 | #endif |
| 9032 | last = NULL; |
| 9033 | next = xmlXPathNextFollowingSibling; |
| 9034 | break; |
| 9035 | case AXIS_NAMESPACE: |
| 9036 | #ifdef DEBUG_STEP_NTH |
| 9037 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9038 | #endif |
| 9039 | last = NULL; |
| 9040 | first = NULL; |
| 9041 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9042 | break; |
| 9043 | case AXIS_PARENT: |
| 9044 | #ifdef DEBUG_STEP_NTH |
| 9045 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9046 | #endif |
| 9047 | first = NULL; |
| 9048 | next = xmlXPathNextParent; |
| 9049 | break; |
| 9050 | case AXIS_PRECEDING: |
| 9051 | #ifdef DEBUG_STEP_NTH |
| 9052 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9053 | #endif |
| 9054 | first = NULL; |
| 9055 | next = xmlXPathNextPrecedingInternal; |
| 9056 | break; |
| 9057 | case AXIS_PRECEDING_SIBLING: |
| 9058 | #ifdef DEBUG_STEP_NTH |
| 9059 | xmlGenericError(xmlGenericErrorContext, |
| 9060 | "axis 'preceding-sibling' "); |
| 9061 | #endif |
| 9062 | first = NULL; |
| 9063 | next = xmlXPathNextPrecedingSibling; |
| 9064 | break; |
| 9065 | case AXIS_SELF: |
| 9066 | #ifdef DEBUG_STEP_NTH |
| 9067 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9068 | #endif |
| 9069 | first = NULL; |
| 9070 | last = NULL; |
| 9071 | next = xmlXPathNextSelf; |
| 9072 | break; |
| 9073 | } |
| 9074 | if (next == NULL) |
| 9075 | return(0); |
| 9076 | |
| 9077 | nodelist = obj->nodesetval; |
| 9078 | if (nodelist == NULL) { |
| 9079 | xmlXPathFreeObject(obj); |
| 9080 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9081 | return(0); |
| 9082 | } |
| 9083 | addNode = xmlXPathNodeSetAddUnique; |
| 9084 | #ifdef DEBUG_STEP_NTH |
| 9085 | xmlGenericError(xmlGenericErrorContext, |
| 9086 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9087 | switch (test) { |
| 9088 | case NODE_TEST_NONE: |
| 9089 | xmlGenericError(xmlGenericErrorContext, |
| 9090 | " searching for none !!!\n"); |
| 9091 | break; |
| 9092 | case NODE_TEST_TYPE: |
| 9093 | xmlGenericError(xmlGenericErrorContext, |
| 9094 | " searching for type %d\n", type); |
| 9095 | break; |
| 9096 | case NODE_TEST_PI: |
| 9097 | xmlGenericError(xmlGenericErrorContext, |
| 9098 | " searching for PI !!!\n"); |
| 9099 | break; |
| 9100 | case NODE_TEST_ALL: |
| 9101 | xmlGenericError(xmlGenericErrorContext, |
| 9102 | " searching for *\n"); |
| 9103 | break; |
| 9104 | case NODE_TEST_NS: |
| 9105 | xmlGenericError(xmlGenericErrorContext, |
| 9106 | " searching for namespace %s\n", |
| 9107 | prefix); |
| 9108 | break; |
| 9109 | case NODE_TEST_NAME: |
| 9110 | xmlGenericError(xmlGenericErrorContext, |
| 9111 | " searching for name %s\n", name); |
| 9112 | if (prefix != NULL) |
| 9113 | xmlGenericError(xmlGenericErrorContext, |
| 9114 | " with namespace %s\n", prefix); |
| 9115 | break; |
| 9116 | } |
| 9117 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9118 | #endif |
| 9119 | /* |
| 9120 | * 2.3 Node Tests |
| 9121 | * - For the attribute axis, the principal node type is attribute. |
| 9122 | * - For the namespace axis, the principal node type is namespace. |
| 9123 | * - For other axes, the principal node type is element. |
| 9124 | * |
| 9125 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9126 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9127 | * select all element children of the context node |
| 9128 | */ |
| 9129 | tmp = ctxt->context->node; |
| 9130 | list = xmlXPathNodeSetCreate(NULL); |
| 9131 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9132 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9133 | |
| 9134 | cur = NULL; |
| 9135 | n = 0; |
| 9136 | do { |
| 9137 | cur = next(ctxt, cur); |
| 9138 | if (cur == NULL) |
| 9139 | break; |
| 9140 | if ((first != NULL) && (*first == cur)) |
| 9141 | break; |
| 9142 | if (((t % 256) == 0) && |
| 9143 | (first != NULL) && (*first != NULL) && |
| 9144 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9145 | break; |
| 9146 | if ((last != NULL) && (*last == cur)) |
| 9147 | break; |
| 9148 | if (((t % 256) == 0) && |
| 9149 | (last != NULL) && (*last != NULL) && |
| 9150 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9151 | break; |
| 9152 | t++; |
| 9153 | switch (test) { |
| 9154 | case NODE_TEST_NONE: |
| 9155 | ctxt->context->node = tmp; |
| 9156 | STRANGE return(0); |
| 9157 | case NODE_TEST_TYPE: |
| 9158 | if ((cur->type == type) || |
| 9159 | ((type == NODE_TYPE_NODE) && |
| 9160 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9161 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9162 | (cur->type == XML_ELEMENT_NODE) || |
| 9163 | (cur->type == XML_PI_NODE) || |
| 9164 | (cur->type == XML_COMMENT_NODE) || |
| 9165 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 9166 | (cur->type == XML_TEXT_NODE)))) { |
| 9167 | n++; |
| 9168 | if (n == indx) |
| 9169 | addNode(list, cur); |
| 9170 | } |
| 9171 | break; |
| 9172 | case NODE_TEST_PI: |
| 9173 | if (cur->type == XML_PI_NODE) { |
| 9174 | if ((name != NULL) && |
| 9175 | (!xmlStrEqual(name, cur->name))) |
| 9176 | break; |
| 9177 | n++; |
| 9178 | if (n == indx) |
| 9179 | addNode(list, cur); |
| 9180 | } |
| 9181 | break; |
| 9182 | case NODE_TEST_ALL: |
| 9183 | if (axis == AXIS_ATTRIBUTE) { |
| 9184 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9185 | n++; |
| 9186 | if (n == indx) |
| 9187 | addNode(list, cur); |
| 9188 | } |
| 9189 | } else if (axis == AXIS_NAMESPACE) { |
| 9190 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9191 | n++; |
| 9192 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9193 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9194 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9195 | } |
| 9196 | } else { |
| 9197 | if (cur->type == XML_ELEMENT_NODE) { |
| 9198 | if (prefix == NULL) { |
| 9199 | n++; |
| 9200 | if (n == indx) |
| 9201 | addNode(list, cur); |
| 9202 | } else if ((cur->ns != NULL) && |
| 9203 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9204 | n++; |
| 9205 | if (n == indx) |
| 9206 | addNode(list, cur); |
| 9207 | } |
| 9208 | } |
| 9209 | } |
| 9210 | break; |
| 9211 | case NODE_TEST_NS:{ |
| 9212 | TODO; |
| 9213 | break; |
| 9214 | } |
| 9215 | case NODE_TEST_NAME: |
| 9216 | switch (cur->type) { |
| 9217 | case XML_ELEMENT_NODE: |
| 9218 | if (xmlStrEqual(name, cur->name)) { |
| 9219 | if (prefix == NULL) { |
| 9220 | if (cur->ns == NULL) { |
| 9221 | n++; |
| 9222 | if (n == indx) |
| 9223 | addNode(list, cur); |
| 9224 | } |
| 9225 | } else { |
| 9226 | if ((cur->ns != NULL) && |
| 9227 | (xmlStrEqual(URI, |
| 9228 | cur->ns->href))) { |
| 9229 | n++; |
| 9230 | if (n == indx) |
| 9231 | addNode(list, cur); |
| 9232 | } |
| 9233 | } |
| 9234 | } |
| 9235 | break; |
| 9236 | case XML_ATTRIBUTE_NODE:{ |
| 9237 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9238 | |
| 9239 | if (xmlStrEqual(name, attr->name)) { |
| 9240 | if (prefix == NULL) { |
| 9241 | if ((attr->ns == NULL) || |
| 9242 | (attr->ns->prefix == NULL)) { |
| 9243 | n++; |
| 9244 | if (n == indx) |
| 9245 | addNode(list, cur); |
| 9246 | } |
| 9247 | } else { |
| 9248 | if ((attr->ns != NULL) && |
| 9249 | (xmlStrEqual(URI, |
| 9250 | attr->ns-> |
| 9251 | href))) { |
| 9252 | n++; |
| 9253 | if (n == indx) |
| 9254 | addNode(list, cur); |
| 9255 | } |
| 9256 | } |
| 9257 | } |
| 9258 | break; |
| 9259 | } |
| 9260 | case XML_NAMESPACE_DECL: |
| 9261 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9262 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9263 | |
| 9264 | if ((ns->prefix != NULL) && (name != NULL) |
| 9265 | && (xmlStrEqual(ns->prefix, name))) { |
| 9266 | n++; |
| 9267 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9268 | xmlXPathNodeSetAddNs(list, |
| 9269 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9270 | } |
| 9271 | } |
| 9272 | break; |
| 9273 | default: |
| 9274 | break; |
| 9275 | } |
| 9276 | break; |
| 9277 | break; |
| 9278 | } |
| 9279 | } while (n < indx); |
| 9280 | } |
| 9281 | ctxt->context->node = tmp; |
| 9282 | #ifdef DEBUG_STEP_NTH |
| 9283 | xmlGenericError(xmlGenericErrorContext, |
| 9284 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9285 | t, list->nodeNr); |
| 9286 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9287 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9288 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9289 | ctxt->value->boolval = 1; |
| 9290 | ctxt->value->user = obj->user; |
| 9291 | obj->user = NULL; |
| 9292 | obj->boolval = 0; |
| 9293 | } |
| 9294 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9295 | return(t); |
| 9296 | } |
| 9297 | |
| 9298 | /** |
| 9299 | * xmlXPathCompOpEvalFirst: |
| 9300 | * @ctxt: the XPath parser context with the compiled expression |
| 9301 | * @op: an XPath compiled operation |
| 9302 | * @first: the first elem found so far |
| 9303 | * |
| 9304 | * Evaluate the Precompiled XPath operation searching only the first |
| 9305 | * element in document order |
| 9306 | * |
| 9307 | * Returns the number of examined objects. |
| 9308 | */ |
| 9309 | static int |
| 9310 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9311 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9312 | { |
| 9313 | int total = 0, cur; |
| 9314 | xmlXPathCompExprPtr comp; |
| 9315 | xmlXPathObjectPtr arg1, arg2; |
| 9316 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9317 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9318 | comp = ctxt->comp; |
| 9319 | switch (op->op) { |
| 9320 | case XPATH_OP_END: |
| 9321 | return (0); |
| 9322 | case XPATH_OP_UNION: |
| 9323 | total = |
| 9324 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9325 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9326 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9327 | if ((ctxt->value != NULL) |
| 9328 | && (ctxt->value->type == XPATH_NODESET) |
| 9329 | && (ctxt->value->nodesetval != NULL) |
| 9330 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9331 | /* |
| 9332 | * limit tree traversing to first node in the result |
| 9333 | */ |
| 9334 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9335 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9336 | } |
| 9337 | cur = |
| 9338 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9339 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9340 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9341 | CHECK_TYPE0(XPATH_NODESET); |
| 9342 | arg2 = valuePop(ctxt); |
| 9343 | |
| 9344 | CHECK_TYPE0(XPATH_NODESET); |
| 9345 | arg1 = valuePop(ctxt); |
| 9346 | |
| 9347 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9348 | arg2->nodesetval); |
| 9349 | valuePush(ctxt, arg1); |
| 9350 | xmlXPathFreeObject(arg2); |
| 9351 | /* optimizer */ |
| 9352 | if (total > cur) |
| 9353 | xmlXPathCompSwap(op); |
| 9354 | return (total + cur); |
| 9355 | case XPATH_OP_ROOT: |
| 9356 | xmlXPathRoot(ctxt); |
| 9357 | return (0); |
| 9358 | case XPATH_OP_NODE: |
| 9359 | if (op->ch1 != -1) |
| 9360 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9361 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9362 | if (op->ch2 != -1) |
| 9363 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9364 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9365 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9366 | return (total); |
| 9367 | case XPATH_OP_RESET: |
| 9368 | if (op->ch1 != -1) |
| 9369 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9370 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9371 | if (op->ch2 != -1) |
| 9372 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9373 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9374 | ctxt->context->node = NULL; |
| 9375 | return (total); |
| 9376 | case XPATH_OP_COLLECT:{ |
| 9377 | if (op->ch1 == -1) |
| 9378 | return (total); |
| 9379 | |
| 9380 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9381 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9382 | |
| 9383 | /* |
| 9384 | * Optimization for [n] selection where n is a number |
| 9385 | */ |
| 9386 | if ((op->ch2 != -1) && |
| 9387 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9388 | (comp->steps[op->ch2].ch1 == -1) && |
| 9389 | (comp->steps[op->ch2].ch2 != -1) && |
| 9390 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9391 | XPATH_OP_VALUE)) { |
| 9392 | xmlXPathObjectPtr val; |
| 9393 | |
| 9394 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9395 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9396 | int indx = (int) val->floatval; |
| 9397 | |
| 9398 | if (val->floatval == (float) indx) { |
| 9399 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 9400 | first, NULL); |
| 9401 | return (total); |
| 9402 | } |
| 9403 | } |
| 9404 | } |
| 9405 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 9406 | return (total); |
| 9407 | } |
| 9408 | case XPATH_OP_VALUE: |
| 9409 | valuePush(ctxt, |
| 9410 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9411 | return (0); |
| 9412 | case XPATH_OP_SORT: |
| 9413 | if (op->ch1 != -1) |
| 9414 | total += |
| 9415 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9416 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9417 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9418 | if ((ctxt->value != NULL) |
| 9419 | && (ctxt->value->type == XPATH_NODESET) |
| 9420 | && (ctxt->value->nodesetval != NULL)) |
| 9421 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9422 | return (total); |
| 9423 | default: |
| 9424 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9425 | } |
| 9426 | } |
| 9427 | |
| 9428 | /** |
| 9429 | * xmlXPathCompOpEvalLast: |
| 9430 | * @ctxt: the XPath parser context with the compiled expression |
| 9431 | * @op: an XPath compiled operation |
| 9432 | * @last: the last elem found so far |
| 9433 | * |
| 9434 | * Evaluate the Precompiled XPath operation searching only the last |
| 9435 | * element in document order |
| 9436 | * |
| 9437 | * Returns the number of node traversed |
| 9438 | */ |
| 9439 | static int |
| 9440 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 9441 | xmlNodePtr * last) |
| 9442 | { |
| 9443 | int total = 0, cur; |
| 9444 | xmlXPathCompExprPtr comp; |
| 9445 | xmlXPathObjectPtr arg1, arg2; |
| 9446 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9447 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9448 | comp = ctxt->comp; |
| 9449 | switch (op->op) { |
| 9450 | case XPATH_OP_END: |
| 9451 | return (0); |
| 9452 | case XPATH_OP_UNION: |
| 9453 | total = |
| 9454 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9455 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9456 | if ((ctxt->value != NULL) |
| 9457 | && (ctxt->value->type == XPATH_NODESET) |
| 9458 | && (ctxt->value->nodesetval != NULL) |
| 9459 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9460 | /* |
| 9461 | * limit tree traversing to first node in the result |
| 9462 | */ |
| 9463 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9464 | *last = |
| 9465 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 9466 | nodesetval->nodeNr - |
| 9467 | 1]; |
| 9468 | } |
| 9469 | cur = |
| 9470 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9471 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9472 | if ((ctxt->value != NULL) |
| 9473 | && (ctxt->value->type == XPATH_NODESET) |
| 9474 | && (ctxt->value->nodesetval != NULL) |
| 9475 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9476 | } |
| 9477 | CHECK_TYPE0(XPATH_NODESET); |
| 9478 | arg2 = valuePop(ctxt); |
| 9479 | |
| 9480 | CHECK_TYPE0(XPATH_NODESET); |
| 9481 | arg1 = valuePop(ctxt); |
| 9482 | |
| 9483 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9484 | arg2->nodesetval); |
| 9485 | valuePush(ctxt, arg1); |
| 9486 | xmlXPathFreeObject(arg2); |
| 9487 | /* optimizer */ |
| 9488 | if (total > cur) |
| 9489 | xmlXPathCompSwap(op); |
| 9490 | return (total + cur); |
| 9491 | case XPATH_OP_ROOT: |
| 9492 | xmlXPathRoot(ctxt); |
| 9493 | return (0); |
| 9494 | case XPATH_OP_NODE: |
| 9495 | if (op->ch1 != -1) |
| 9496 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9497 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9498 | if (op->ch2 != -1) |
| 9499 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9500 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9501 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9502 | return (total); |
| 9503 | case XPATH_OP_RESET: |
| 9504 | if (op->ch1 != -1) |
| 9505 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9506 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9507 | if (op->ch2 != -1) |
| 9508 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9509 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9510 | ctxt->context->node = NULL; |
| 9511 | return (total); |
| 9512 | case XPATH_OP_COLLECT:{ |
| 9513 | if (op->ch1 == -1) |
| 9514 | return (0); |
| 9515 | |
| 9516 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9517 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9518 | |
| 9519 | /* |
| 9520 | * Optimization for [n] selection where n is a number |
| 9521 | */ |
| 9522 | if ((op->ch2 != -1) && |
| 9523 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9524 | (comp->steps[op->ch2].ch1 == -1) && |
| 9525 | (comp->steps[op->ch2].ch2 != -1) && |
| 9526 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9527 | XPATH_OP_VALUE)) { |
| 9528 | xmlXPathObjectPtr val; |
| 9529 | |
| 9530 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9531 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9532 | int indx = (int) val->floatval; |
| 9533 | |
| 9534 | if (val->floatval == (float) indx) { |
| 9535 | total += |
| 9536 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 9537 | indx, NULL, |
| 9538 | last); |
| 9539 | return (total); |
| 9540 | } |
| 9541 | } |
| 9542 | } |
| 9543 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 9544 | return (total); |
| 9545 | } |
| 9546 | case XPATH_OP_VALUE: |
| 9547 | valuePush(ctxt, |
| 9548 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9549 | return (0); |
| 9550 | case XPATH_OP_SORT: |
| 9551 | if (op->ch1 != -1) |
| 9552 | total += |
| 9553 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 9554 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9555 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9556 | if ((ctxt->value != NULL) |
| 9557 | && (ctxt->value->type == XPATH_NODESET) |
| 9558 | && (ctxt->value->nodesetval != NULL)) |
| 9559 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9560 | return (total); |
| 9561 | default: |
| 9562 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9563 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9564 | } |
| 9565 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9566 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9567 | * xmlXPathCompOpEval: |
| 9568 | * @ctxt: the XPath parser context with the compiled expression |
| 9569 | * @op: an XPath compiled operation |
| 9570 | * |
| 9571 | * Evaluate the Precompiled XPath operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9572 | * Returns the number of node traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9573 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9574 | static int |
| 9575 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 9576 | { |
| 9577 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9578 | int equal, ret; |
| 9579 | xmlXPathCompExprPtr comp; |
| 9580 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9581 | xmlNodePtr bak; |
| 9582 | xmlDocPtr bakd; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9583 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9584 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9585 | comp = ctxt->comp; |
| 9586 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9587 | case XPATH_OP_END: |
| 9588 | return (0); |
| 9589 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9590 | bakd = ctxt->context->doc; |
| 9591 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9592 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9593 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9594 | xmlXPathBooleanFunction(ctxt, 1); |
| 9595 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 9596 | return (total); |
| 9597 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9598 | ctxt->context->doc = bakd; |
| 9599 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9600 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9601 | if (ctxt->error) { |
| 9602 | xmlXPathFreeObject(arg2); |
| 9603 | return(0); |
| 9604 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9605 | xmlXPathBooleanFunction(ctxt, 1); |
| 9606 | arg1 = valuePop(ctxt); |
| 9607 | arg1->boolval &= arg2->boolval; |
| 9608 | valuePush(ctxt, arg1); |
| 9609 | xmlXPathFreeObject(arg2); |
| 9610 | return (total); |
| 9611 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9612 | bakd = ctxt->context->doc; |
| 9613 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9614 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9615 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9616 | xmlXPathBooleanFunction(ctxt, 1); |
| 9617 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 9618 | return (total); |
| 9619 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9620 | ctxt->context->doc = bakd; |
| 9621 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9622 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9623 | if (ctxt->error) { |
| 9624 | xmlXPathFreeObject(arg2); |
| 9625 | return(0); |
| 9626 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9627 | xmlXPathBooleanFunction(ctxt, 1); |
| 9628 | arg1 = valuePop(ctxt); |
| 9629 | arg1->boolval |= arg2->boolval; |
| 9630 | valuePush(ctxt, arg1); |
| 9631 | xmlXPathFreeObject(arg2); |
| 9632 | return (total); |
| 9633 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9634 | bakd = ctxt->context->doc; |
| 9635 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9636 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9637 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9638 | ctxt->context->doc = bakd; |
| 9639 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9640 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9641 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9642 | equal = xmlXPathEqualValues(ctxt); |
| 9643 | if (op->value) |
| 9644 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
| 9645 | else |
| 9646 | valuePush(ctxt, xmlXPathNewBoolean(!equal)); |
| 9647 | return (total); |
| 9648 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9649 | bakd = ctxt->context->doc; |
| 9650 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9651 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9652 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9653 | ctxt->context->doc = bakd; |
| 9654 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9655 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9656 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9657 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 9658 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 9659 | return (total); |
| 9660 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9661 | bakd = ctxt->context->doc; |
| 9662 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9663 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9664 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9665 | if (op->ch2 != -1) { |
| 9666 | ctxt->context->doc = bakd; |
| 9667 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9668 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9669 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9670 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9671 | if (op->value == 0) |
| 9672 | xmlXPathSubValues(ctxt); |
| 9673 | else if (op->value == 1) |
| 9674 | xmlXPathAddValues(ctxt); |
| 9675 | else if (op->value == 2) |
| 9676 | xmlXPathValueFlipSign(ctxt); |
| 9677 | else if (op->value == 3) { |
| 9678 | CAST_TO_NUMBER; |
| 9679 | CHECK_TYPE0(XPATH_NUMBER); |
| 9680 | } |
| 9681 | return (total); |
| 9682 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9683 | bakd = ctxt->context->doc; |
| 9684 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9685 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9686 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9687 | ctxt->context->doc = bakd; |
| 9688 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9689 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9690 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9691 | if (op->value == 0) |
| 9692 | xmlXPathMultValues(ctxt); |
| 9693 | else if (op->value == 1) |
| 9694 | xmlXPathDivValues(ctxt); |
| 9695 | else if (op->value == 2) |
| 9696 | xmlXPathModValues(ctxt); |
| 9697 | return (total); |
| 9698 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9699 | bakd = ctxt->context->doc; |
| 9700 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9701 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9702 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9703 | ctxt->context->doc = bakd; |
| 9704 | ctxt->context->node = bak; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9705 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9706 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9707 | CHECK_TYPE0(XPATH_NODESET); |
| 9708 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9709 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9710 | CHECK_TYPE0(XPATH_NODESET); |
| 9711 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9712 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9713 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9714 | arg2->nodesetval); |
| 9715 | valuePush(ctxt, arg1); |
| 9716 | xmlXPathFreeObject(arg2); |
| 9717 | return (total); |
| 9718 | case XPATH_OP_ROOT: |
| 9719 | xmlXPathRoot(ctxt); |
| 9720 | return (total); |
| 9721 | case XPATH_OP_NODE: |
| 9722 | if (op->ch1 != -1) |
| 9723 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9724 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9725 | if (op->ch2 != -1) |
| 9726 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9727 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9728 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9729 | return (total); |
| 9730 | case XPATH_OP_RESET: |
| 9731 | if (op->ch1 != -1) |
| 9732 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9733 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9734 | if (op->ch2 != -1) |
| 9735 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9736 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9737 | ctxt->context->node = NULL; |
| 9738 | return (total); |
| 9739 | case XPATH_OP_COLLECT:{ |
| 9740 | if (op->ch1 == -1) |
| 9741 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9742 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9743 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9744 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9745 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9746 | /* |
| 9747 | * Optimization for [n] selection where n is a number |
| 9748 | */ |
| 9749 | if ((op->ch2 != -1) && |
| 9750 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9751 | (comp->steps[op->ch2].ch1 == -1) && |
| 9752 | (comp->steps[op->ch2].ch2 != -1) && |
| 9753 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9754 | XPATH_OP_VALUE)) { |
| 9755 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 9756 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9757 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9758 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9759 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9760 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9761 | if (val->floatval == (float) indx) { |
| 9762 | total += |
| 9763 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 9764 | indx, NULL, |
| 9765 | NULL); |
| 9766 | return (total); |
| 9767 | } |
| 9768 | } |
| 9769 | } |
| 9770 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 9771 | return (total); |
| 9772 | } |
| 9773 | case XPATH_OP_VALUE: |
| 9774 | valuePush(ctxt, |
| 9775 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9776 | return (total); |
| 9777 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9778 | xmlXPathObjectPtr val; |
| 9779 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9780 | if (op->ch1 != -1) |
| 9781 | total += |
| 9782 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9783 | if (op->value5 == NULL) { |
| 9784 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 9785 | if (val == NULL) { |
| 9786 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 9787 | return(0); |
| 9788 | } |
| 9789 | valuePush(ctxt, val); |
| 9790 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9791 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9792 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9793 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 9794 | if (URI == NULL) { |
| 9795 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9796 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9797 | op->value4, op->value5); |
| 9798 | return (total); |
| 9799 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9800 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 9801 | op->value4, URI); |
| 9802 | if (val == NULL) { |
| 9803 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 9804 | return(0); |
| 9805 | } |
| 9806 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9807 | } |
| 9808 | return (total); |
| 9809 | } |
| 9810 | case XPATH_OP_FUNCTION:{ |
| 9811 | xmlXPathFunction func; |
| 9812 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9813 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9814 | |
| 9815 | if (op->ch1 != -1) |
| 9816 | total += |
| 9817 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9818 | if (ctxt->valueNr < op->value) { |
| 9819 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9820 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9821 | ctxt->error = XPATH_INVALID_OPERAND; |
| 9822 | return (total); |
| 9823 | } |
| 9824 | for (i = 0; i < op->value; i++) |
| 9825 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 9826 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9827 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9828 | ctxt->error = XPATH_INVALID_OPERAND; |
| 9829 | return (total); |
| 9830 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9831 | if (op->cache != NULL) |
| 9832 | func = (xmlXPathFunction) op->cache; |
| 9833 | else { |
| 9834 | const xmlChar *URI = NULL; |
| 9835 | |
| 9836 | if (op->value5 == NULL) |
| 9837 | func = |
| 9838 | xmlXPathFunctionLookup(ctxt->context, |
| 9839 | op->value4); |
| 9840 | else { |
| 9841 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 9842 | if (URI == NULL) { |
| 9843 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9844 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9845 | op->value4, op->value5); |
| 9846 | return (total); |
| 9847 | } |
| 9848 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 9849 | op->value4, URI); |
| 9850 | } |
| 9851 | if (func == NULL) { |
| 9852 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9853 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9854 | op->value4); |
| 9855 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9856 | } |
| 9857 | op->cache = (void *) func; |
| 9858 | op->cacheURI = (void *) URI; |
| 9859 | } |
| 9860 | oldFunc = ctxt->context->function; |
| 9861 | oldFuncURI = ctxt->context->functionURI; |
| 9862 | ctxt->context->function = op->value4; |
| 9863 | ctxt->context->functionURI = op->cacheURI; |
| 9864 | func(ctxt, op->value); |
| 9865 | ctxt->context->function = oldFunc; |
| 9866 | ctxt->context->functionURI = oldFuncURI; |
| 9867 | return (total); |
| 9868 | } |
| 9869 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 9870 | bakd = ctxt->context->doc; |
| 9871 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9872 | if (op->ch1 != -1) |
| 9873 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 9874 | ctxt->context->doc = bakd; |
| 9875 | ctxt->context->node = bak; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9876 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9877 | if (op->ch2 != -1) |
| 9878 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 9879 | ctxt->context->doc = bakd; |
| 9880 | ctxt->context->node = bak; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9881 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9882 | return (total); |
| 9883 | case XPATH_OP_PREDICATE: |
| 9884 | case XPATH_OP_FILTER:{ |
| 9885 | xmlXPathObjectPtr res; |
| 9886 | xmlXPathObjectPtr obj, tmp; |
| 9887 | xmlNodeSetPtr newset = NULL; |
| 9888 | xmlNodeSetPtr oldset; |
| 9889 | xmlNodePtr oldnode; |
| 9890 | int i; |
| 9891 | |
| 9892 | /* |
| 9893 | * Optimization for ()[1] selection i.e. the first elem |
| 9894 | */ |
| 9895 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 9896 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 9897 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 9898 | xmlXPathObjectPtr val; |
| 9899 | |
| 9900 | val = comp->steps[op->ch2].value4; |
| 9901 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 9902 | (val->floatval == 1.0)) { |
| 9903 | xmlNodePtr first = NULL; |
| 9904 | |
| 9905 | total += |
| 9906 | xmlXPathCompOpEvalFirst(ctxt, |
| 9907 | &comp->steps[op->ch1], |
| 9908 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9909 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9910 | /* |
| 9911 | * The nodeset should be in document order, |
| 9912 | * Keep only the first value |
| 9913 | */ |
| 9914 | if ((ctxt->value != NULL) && |
| 9915 | (ctxt->value->type == XPATH_NODESET) && |
| 9916 | (ctxt->value->nodesetval != NULL) && |
| 9917 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 9918 | ctxt->value->nodesetval->nodeNr = 1; |
| 9919 | return (total); |
| 9920 | } |
| 9921 | } |
| 9922 | /* |
| 9923 | * Optimization for ()[last()] selection i.e. the last elem |
| 9924 | */ |
| 9925 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 9926 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 9927 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 9928 | int f = comp->steps[op->ch2].ch1; |
| 9929 | |
| 9930 | if ((f != -1) && |
| 9931 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 9932 | (comp->steps[f].value5 == NULL) && |
| 9933 | (comp->steps[f].value == 0) && |
| 9934 | (comp->steps[f].value4 != NULL) && |
| 9935 | (xmlStrEqual |
| 9936 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 9937 | xmlNodePtr last = NULL; |
| 9938 | |
| 9939 | total += |
| 9940 | xmlXPathCompOpEvalLast(ctxt, |
| 9941 | &comp->steps[op->ch1], |
| 9942 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9943 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9944 | /* |
| 9945 | * The nodeset should be in document order, |
| 9946 | * Keep only the last value |
| 9947 | */ |
| 9948 | if ((ctxt->value != NULL) && |
| 9949 | (ctxt->value->type == XPATH_NODESET) && |
| 9950 | (ctxt->value->nodesetval != NULL) && |
| 9951 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 9952 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 9953 | ctxt->value->nodesetval->nodeTab[0] = |
| 9954 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 9955 | value-> |
| 9956 | nodesetval-> |
| 9957 | nodeNr - |
| 9958 | 1]; |
| 9959 | ctxt->value->nodesetval->nodeNr = 1; |
| 9960 | } |
| 9961 | return (total); |
| 9962 | } |
| 9963 | } |
| 9964 | |
| 9965 | if (op->ch1 != -1) |
| 9966 | total += |
| 9967 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9968 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9969 | if (op->ch2 == -1) |
| 9970 | return (total); |
| 9971 | if (ctxt->value == NULL) |
| 9972 | return (total); |
| 9973 | |
| 9974 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9975 | |
| 9976 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9977 | /* |
| 9978 | * Hum are we filtering the result of an XPointer expression |
| 9979 | */ |
| 9980 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 9981 | xmlLocationSetPtr newlocset = NULL; |
| 9982 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9983 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9984 | /* |
| 9985 | * Extract the old locset, and then evaluate the result of the |
| 9986 | * expression for all the element in the locset. use it to grow |
| 9987 | * up a new locset. |
| 9988 | */ |
| 9989 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 9990 | obj = valuePop(ctxt); |
| 9991 | oldlocset = obj->user; |
| 9992 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9993 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9994 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 9995 | ctxt->context->contextSize = 0; |
| 9996 | ctxt->context->proximityPosition = 0; |
| 9997 | if (op->ch2 != -1) |
| 9998 | total += |
| 9999 | xmlXPathCompOpEval(ctxt, |
| 10000 | &comp->steps[op->ch2]); |
| 10001 | res = valuePop(ctxt); |
| 10002 | if (res != NULL) |
| 10003 | xmlXPathFreeObject(res); |
| 10004 | valuePush(ctxt, obj); |
| 10005 | CHECK_ERROR0; |
| 10006 | return (total); |
| 10007 | } |
| 10008 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10009 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10010 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10011 | /* |
| 10012 | * Run the evaluation with a node list made of a |
| 10013 | * single item in the nodelocset. |
| 10014 | */ |
| 10015 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10016 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10017 | valuePush(ctxt, tmp); |
| 10018 | ctxt->context->contextSize = oldlocset->locNr; |
| 10019 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10020 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10021 | if (op->ch2 != -1) |
| 10022 | total += |
| 10023 | xmlXPathCompOpEval(ctxt, |
| 10024 | &comp->steps[op->ch2]); |
| 10025 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10026 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10027 | /* |
| 10028 | * The result of the evaluation need to be tested to |
| 10029 | * decided whether the filter succeeded or not |
| 10030 | */ |
| 10031 | res = valuePop(ctxt); |
| 10032 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10033 | xmlXPtrLocationSetAdd(newlocset, |
| 10034 | xmlXPathObjectCopy |
| 10035 | (oldlocset->locTab[i])); |
| 10036 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10037 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10038 | /* |
| 10039 | * Cleanup |
| 10040 | */ |
| 10041 | if (res != NULL) |
| 10042 | xmlXPathFreeObject(res); |
| 10043 | if (ctxt->value == tmp) { |
| 10044 | res = valuePop(ctxt); |
| 10045 | xmlXPathFreeObject(res); |
| 10046 | } |
| 10047 | |
| 10048 | ctxt->context->node = NULL; |
| 10049 | } |
| 10050 | |
| 10051 | /* |
| 10052 | * The result is used as the new evaluation locset. |
| 10053 | */ |
| 10054 | xmlXPathFreeObject(obj); |
| 10055 | ctxt->context->node = NULL; |
| 10056 | ctxt->context->contextSize = -1; |
| 10057 | ctxt->context->proximityPosition = -1; |
| 10058 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10059 | ctxt->context->node = oldnode; |
| 10060 | return (total); |
| 10061 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10062 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10063 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10064 | /* |
| 10065 | * Extract the old set, and then evaluate the result of the |
| 10066 | * expression for all the element in the set. use it to grow |
| 10067 | * up a new set. |
| 10068 | */ |
| 10069 | CHECK_TYPE0(XPATH_NODESET); |
| 10070 | obj = valuePop(ctxt); |
| 10071 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10072 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10073 | oldnode = ctxt->context->node; |
| 10074 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10075 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10076 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10077 | ctxt->context->contextSize = 0; |
| 10078 | ctxt->context->proximityPosition = 0; |
| 10079 | if (op->ch2 != -1) |
| 10080 | total += |
| 10081 | xmlXPathCompOpEval(ctxt, |
| 10082 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10083 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10084 | res = valuePop(ctxt); |
| 10085 | if (res != NULL) |
| 10086 | xmlXPathFreeObject(res); |
| 10087 | valuePush(ctxt, obj); |
| 10088 | ctxt->context->node = oldnode; |
| 10089 | CHECK_ERROR0; |
| 10090 | } else { |
| 10091 | /* |
| 10092 | * Initialize the new set. |
| 10093 | */ |
| 10094 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10095 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10096 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10097 | /* |
| 10098 | * Run the evaluation with a node list made of |
| 10099 | * a single item in the nodeset. |
| 10100 | */ |
| 10101 | ctxt->context->node = oldset->nodeTab[i]; |
| 10102 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10103 | valuePush(ctxt, tmp); |
| 10104 | ctxt->context->contextSize = oldset->nodeNr; |
| 10105 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10106 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10107 | if (op->ch2 != -1) |
| 10108 | total += |
| 10109 | xmlXPathCompOpEval(ctxt, |
| 10110 | &comp->steps[op->ch2]); |
| 10111 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10112 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10113 | /* |
| 10114 | * The result of the evaluation need to be tested to |
| 10115 | * decided whether the filter succeeded or not |
| 10116 | */ |
| 10117 | res = valuePop(ctxt); |
| 10118 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10119 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10120 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10121 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10122 | /* |
| 10123 | * Cleanup |
| 10124 | */ |
| 10125 | if (res != NULL) |
| 10126 | xmlXPathFreeObject(res); |
| 10127 | if (ctxt->value == tmp) { |
| 10128 | res = valuePop(ctxt); |
| 10129 | xmlXPathFreeObject(res); |
| 10130 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10131 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10132 | ctxt->context->node = NULL; |
| 10133 | } |
| 10134 | |
| 10135 | /* |
| 10136 | * The result is used as the new evaluation set. |
| 10137 | */ |
| 10138 | xmlXPathFreeObject(obj); |
| 10139 | ctxt->context->node = NULL; |
| 10140 | ctxt->context->contextSize = -1; |
| 10141 | ctxt->context->proximityPosition = -1; |
| 10142 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10143 | } |
| 10144 | ctxt->context->node = oldnode; |
| 10145 | return (total); |
| 10146 | } |
| 10147 | case XPATH_OP_SORT: |
| 10148 | if (op->ch1 != -1) |
| 10149 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10150 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10151 | if ((ctxt->value != NULL) && |
| 10152 | (ctxt->value->type == XPATH_NODESET) && |
| 10153 | (ctxt->value->nodesetval != NULL)) |
| 10154 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10155 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10156 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10157 | case XPATH_OP_RANGETO:{ |
| 10158 | xmlXPathObjectPtr range; |
| 10159 | xmlXPathObjectPtr res, obj; |
| 10160 | xmlXPathObjectPtr tmp; |
| 10161 | xmlLocationSetPtr newset = NULL; |
| 10162 | xmlNodeSetPtr oldset; |
| 10163 | int i; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10164 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10165 | if (op->ch1 != -1) |
| 10166 | total += |
| 10167 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10168 | if (op->ch2 == -1) |
| 10169 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10170 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10171 | CHECK_TYPE0(XPATH_NODESET); |
| 10172 | obj = valuePop(ctxt); |
| 10173 | oldset = obj->nodesetval; |
| 10174 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10175 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10176 | newset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10177 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10178 | if (oldset != NULL) { |
| 10179 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10180 | /* |
| 10181 | * Run the evaluation with a node list made of a single item |
| 10182 | * in the nodeset. |
| 10183 | */ |
| 10184 | ctxt->context->node = oldset->nodeTab[i]; |
| 10185 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10186 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10187 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10188 | if (op->ch2 != -1) |
| 10189 | total += |
| 10190 | xmlXPathCompOpEval(ctxt, |
| 10191 | &comp->steps[op->ch2]); |
| 10192 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10193 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10194 | /* |
| 10195 | * The result of the evaluation need to be tested to |
| 10196 | * decided whether the filter succeeded or not |
| 10197 | */ |
| 10198 | res = valuePop(ctxt); |
| 10199 | range = |
| 10200 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10201 | res); |
| 10202 | if (range != NULL) { |
| 10203 | xmlXPtrLocationSetAdd(newset, range); |
| 10204 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10205 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10206 | /* |
| 10207 | * Cleanup |
| 10208 | */ |
| 10209 | if (res != NULL) |
| 10210 | xmlXPathFreeObject(res); |
| 10211 | if (ctxt->value == tmp) { |
| 10212 | res = valuePop(ctxt); |
| 10213 | xmlXPathFreeObject(res); |
| 10214 | } |
| 10215 | |
| 10216 | ctxt->context->node = NULL; |
| 10217 | } |
| 10218 | } |
| 10219 | |
| 10220 | /* |
| 10221 | * The result is used as the new evaluation set. |
| 10222 | */ |
| 10223 | xmlXPathFreeObject(obj); |
| 10224 | ctxt->context->node = NULL; |
| 10225 | ctxt->context->contextSize = -1; |
| 10226 | ctxt->context->proximityPosition = -1; |
| 10227 | valuePush(ctxt, xmlXPtrWrapLocationSet(newset)); |
| 10228 | return (total); |
| 10229 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10230 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10231 | } |
| 10232 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10233 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10234 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10235 | } |
| 10236 | |
| 10237 | /** |
| 10238 | * xmlXPathRunEval: |
| 10239 | * @ctxt: the XPath parser context with the compiled expression |
| 10240 | * |
| 10241 | * Evaluate the Precompiled XPath expression in the given context. |
| 10242 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10243 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10244 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 10245 | xmlXPathCompExprPtr comp; |
| 10246 | |
| 10247 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 10248 | return; |
| 10249 | |
| 10250 | if (ctxt->valueTab == NULL) { |
| 10251 | /* Allocate the value stack */ |
| 10252 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 10253 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 10254 | if (ctxt->valueTab == NULL) { |
| 10255 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10256 | } |
| 10257 | ctxt->valueNr = 0; |
| 10258 | ctxt->valueMax = 10; |
| 10259 | ctxt->value = NULL; |
| 10260 | } |
| 10261 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 10262 | if(comp->last < 0) { |
| 10263 | xmlGenericError(xmlGenericErrorContext, |
| 10264 | "xmlXPathRunEval: last is less than zero\n"); |
| 10265 | return; |
| 10266 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10267 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 10268 | } |
| 10269 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10270 | /************************************************************************ |
| 10271 | * * |
| 10272 | * Public interfaces * |
| 10273 | * * |
| 10274 | ************************************************************************/ |
| 10275 | |
| 10276 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10277 | * xmlXPathEvalPredicate: |
| 10278 | * @ctxt: the XPath context |
| 10279 | * @res: the Predicate Expression evaluation result |
| 10280 | * |
| 10281 | * Evaluate a predicate result for the current node. |
| 10282 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10283 | * the result to a boolean. If the result is a number, the result will |
| 10284 | * be converted to true if the number is equal to the position of the |
| 10285 | * context node in the context node list (as returned by the position |
| 10286 | * function) and will be converted to false otherwise; if the result |
| 10287 | * is not a number, then the result will be converted as if by a call |
| 10288 | * to the boolean function. |
| 10289 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10290 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10291 | */ |
| 10292 | int |
| 10293 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
| 10294 | if (res == NULL) return(0); |
| 10295 | switch (res->type) { |
| 10296 | case XPATH_BOOLEAN: |
| 10297 | return(res->boolval); |
| 10298 | case XPATH_NUMBER: |
| 10299 | return(res->floatval == ctxt->proximityPosition); |
| 10300 | case XPATH_NODESET: |
| 10301 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10302 | if (res->nodesetval == NULL) |
| 10303 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10304 | return(res->nodesetval->nodeNr != 0); |
| 10305 | case XPATH_STRING: |
| 10306 | return((res->stringval != NULL) && |
| 10307 | (xmlStrlen(res->stringval) != 0)); |
| 10308 | default: |
| 10309 | STRANGE |
| 10310 | } |
| 10311 | return(0); |
| 10312 | } |
| 10313 | |
| 10314 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10315 | * xmlXPathEvaluatePredicateResult: |
| 10316 | * @ctxt: the XPath Parser context |
| 10317 | * @res: the Predicate Expression evaluation result |
| 10318 | * |
| 10319 | * Evaluate a predicate result for the current node. |
| 10320 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10321 | * the result to a boolean. If the result is a number, the result will |
| 10322 | * be converted to true if the number is equal to the position of the |
| 10323 | * context node in the context node list (as returned by the position |
| 10324 | * function) and will be converted to false otherwise; if the result |
| 10325 | * is not a number, then the result will be converted as if by a call |
| 10326 | * to the boolean function. |
| 10327 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10328 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10329 | */ |
| 10330 | int |
| 10331 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 10332 | xmlXPathObjectPtr res) { |
| 10333 | if (res == NULL) return(0); |
| 10334 | switch (res->type) { |
| 10335 | case XPATH_BOOLEAN: |
| 10336 | return(res->boolval); |
| 10337 | case XPATH_NUMBER: |
| 10338 | return(res->floatval == ctxt->context->proximityPosition); |
| 10339 | case XPATH_NODESET: |
| 10340 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 10341 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10342 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10343 | return(res->nodesetval->nodeNr != 0); |
| 10344 | case XPATH_STRING: |
| 10345 | return((res->stringval != NULL) && |
| 10346 | (xmlStrlen(res->stringval) != 0)); |
| 10347 | default: |
| 10348 | STRANGE |
| 10349 | } |
| 10350 | return(0); |
| 10351 | } |
| 10352 | |
| 10353 | /** |
| 10354 | * xmlXPathCompile: |
| 10355 | * @str: the XPath expression |
| 10356 | * |
| 10357 | * Compile an XPath expression |
| 10358 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10359 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10360 | * the caller has to free the object. |
| 10361 | */ |
| 10362 | xmlXPathCompExprPtr |
| 10363 | xmlXPathCompile(const xmlChar *str) { |
| 10364 | xmlXPathParserContextPtr ctxt; |
| 10365 | xmlXPathCompExprPtr comp; |
| 10366 | |
| 10367 | xmlXPathInit(); |
| 10368 | |
| 10369 | ctxt = xmlXPathNewParserContext(str, NULL); |
| 10370 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10371 | |
Daniel Veillard | 40af649 | 2001-04-22 08:50:55 +0000 | [diff] [blame] | 10372 | if (*ctxt->cur != 0) { |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10373 | /* |
| 10374 | * aleksey: in some cases this line prints *second* error message |
| 10375 | * (see bug #78858) and probably this should be fixed. |
| 10376 | * However, we are not sure that all error messages are printed |
| 10377 | * out in other places. It's not critical so we leave it as-is for now |
| 10378 | */ |
Daniel Veillard | 40af649 | 2001-04-22 08:50:55 +0000 | [diff] [blame] | 10379 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 10380 | comp = NULL; |
| 10381 | } else { |
| 10382 | comp = ctxt->comp; |
| 10383 | ctxt->comp = NULL; |
| 10384 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10385 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10386 | #ifdef DEBUG_EVAL_COUNTS |
| 10387 | if (comp != NULL) { |
| 10388 | comp->string = xmlStrdup(str); |
| 10389 | comp->nb = 0; |
| 10390 | } |
| 10391 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10392 | return(comp); |
| 10393 | } |
| 10394 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10395 | /** |
| 10396 | * xmlXPathCompiledEval: |
| 10397 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10398 | * @ctx: the XPath context |
| 10399 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10400 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10401 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10402 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10403 | * the caller has to free the object. |
| 10404 | */ |
| 10405 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10406 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10407 | xmlXPathParserContextPtr ctxt; |
| 10408 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 10409 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10410 | #ifndef LIBXML_THREAD_ENABLED |
| 10411 | static int reentance = 0; |
| 10412 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10413 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10414 | if ((comp == NULL) || (ctx == NULL)) |
| 10415 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10416 | xmlXPathInit(); |
| 10417 | |
| 10418 | CHECK_CONTEXT(ctx) |
| 10419 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10420 | #ifndef LIBXML_THREAD_ENABLED |
| 10421 | reentance++; |
| 10422 | if (reentance > 1) |
| 10423 | xmlXPathDisableOptimizer = 1; |
| 10424 | #endif |
| 10425 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10426 | #ifdef DEBUG_EVAL_COUNTS |
| 10427 | comp->nb++; |
| 10428 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 10429 | fprintf(stderr, "100 x %s\n", comp->string); |
| 10430 | comp->nb = 0; |
| 10431 | } |
| 10432 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10433 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 10434 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10435 | |
| 10436 | if (ctxt->value == NULL) { |
| 10437 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10438 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10439 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10440 | } else { |
| 10441 | res = valuePop(ctxt); |
| 10442 | } |
| 10443 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10444 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10445 | do { |
| 10446 | tmp = valuePop(ctxt); |
| 10447 | if (tmp != NULL) { |
| 10448 | if (tmp != init) |
| 10449 | stack++; |
| 10450 | xmlXPathFreeObject(tmp); |
| 10451 | } |
| 10452 | } while (tmp != NULL); |
| 10453 | if ((stack != 0) && (res != NULL)) { |
| 10454 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10455 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | stack); |
| 10457 | } |
| 10458 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10459 | xmlXPathFreeObject(res); |
| 10460 | res = NULL; |
| 10461 | } |
| 10462 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10463 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10464 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10465 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10466 | #ifndef LIBXML_THREAD_ENABLED |
| 10467 | reentance--; |
| 10468 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10469 | return(res); |
| 10470 | } |
| 10471 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10472 | /** |
| 10473 | * xmlXPathEvalExpr: |
| 10474 | * @ctxt: the XPath Parser context |
| 10475 | * |
| 10476 | * Parse and evaluate an XPath expression in the given context, |
| 10477 | * then push the result on the context stack |
| 10478 | */ |
| 10479 | void |
| 10480 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
| 10481 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10482 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10483 | xmlXPathRunEval(ctxt); |
| 10484 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10485 | |
| 10486 | /** |
| 10487 | * xmlXPathEval: |
| 10488 | * @str: the XPath expression |
| 10489 | * @ctx: the XPath context |
| 10490 | * |
| 10491 | * Evaluate the XPath Location Path in the given context. |
| 10492 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10493 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10494 | * the caller has to free the object. |
| 10495 | */ |
| 10496 | xmlXPathObjectPtr |
| 10497 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 10498 | xmlXPathParserContextPtr ctxt; |
| 10499 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 10500 | int stack = 0; |
| 10501 | |
| 10502 | xmlXPathInit(); |
| 10503 | |
| 10504 | CHECK_CONTEXT(ctx) |
| 10505 | |
| 10506 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 10507 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10508 | |
| 10509 | if (ctxt->value == NULL) { |
| 10510 | xmlGenericError(xmlGenericErrorContext, |
| 10511 | "xmlXPathEval: evaluation failed\n"); |
| 10512 | res = NULL; |
| 10513 | } else if (*ctxt->cur != 0) { |
| 10514 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 10515 | res = NULL; |
| 10516 | } else { |
| 10517 | res = valuePop(ctxt); |
| 10518 | } |
| 10519 | |
| 10520 | do { |
| 10521 | tmp = valuePop(ctxt); |
| 10522 | if (tmp != NULL) { |
| 10523 | if (tmp != init) |
| 10524 | stack++; |
| 10525 | xmlXPathFreeObject(tmp); |
| 10526 | } |
| 10527 | } while (tmp != NULL); |
| 10528 | if ((stack != 0) && (res != NULL)) { |
| 10529 | xmlGenericError(xmlGenericErrorContext, |
| 10530 | "xmlXPathEval: %d object left on the stack\n", |
| 10531 | stack); |
| 10532 | } |
| 10533 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10534 | xmlXPathFreeObject(res); |
| 10535 | res = NULL; |
| 10536 | } |
| 10537 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10538 | xmlXPathFreeParserContext(ctxt); |
| 10539 | return(res); |
| 10540 | } |
| 10541 | |
| 10542 | /** |
| 10543 | * xmlXPathEvalExpression: |
| 10544 | * @str: the XPath expression |
| 10545 | * @ctxt: the XPath context |
| 10546 | * |
| 10547 | * Evaluate the XPath expression in the given context. |
| 10548 | * |
| 10549 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 10550 | * the caller has to free the object. |
| 10551 | */ |
| 10552 | xmlXPathObjectPtr |
| 10553 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 10554 | xmlXPathParserContextPtr pctxt; |
| 10555 | xmlXPathObjectPtr res, tmp; |
| 10556 | int stack = 0; |
| 10557 | |
| 10558 | xmlXPathInit(); |
| 10559 | |
| 10560 | CHECK_CONTEXT(ctxt) |
| 10561 | |
| 10562 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 10563 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10564 | |
| 10565 | if (*pctxt->cur != 0) { |
| 10566 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 10567 | res = NULL; |
| 10568 | } else { |
| 10569 | res = valuePop(pctxt); |
| 10570 | } |
| 10571 | do { |
| 10572 | tmp = valuePop(pctxt); |
| 10573 | if (tmp != NULL) { |
| 10574 | xmlXPathFreeObject(tmp); |
| 10575 | stack++; |
| 10576 | } |
| 10577 | } while (tmp != NULL); |
| 10578 | if ((stack != 0) && (res != NULL)) { |
| 10579 | xmlGenericError(xmlGenericErrorContext, |
| 10580 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 10581 | stack); |
| 10582 | } |
| 10583 | xmlXPathFreeParserContext(pctxt); |
| 10584 | return(res); |
| 10585 | } |
| 10586 | |
| 10587 | /** |
| 10588 | * xmlXPathRegisterAllFunctions: |
| 10589 | * @ctxt: the XPath context |
| 10590 | * |
| 10591 | * Registers all default XPath functions in this context |
| 10592 | */ |
| 10593 | void |
| 10594 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 10595 | { |
| 10596 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 10597 | xmlXPathBooleanFunction); |
| 10598 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 10599 | xmlXPathCeilingFunction); |
| 10600 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 10601 | xmlXPathCountFunction); |
| 10602 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 10603 | xmlXPathConcatFunction); |
| 10604 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 10605 | xmlXPathContainsFunction); |
| 10606 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 10607 | xmlXPathIdFunction); |
| 10608 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 10609 | xmlXPathFalseFunction); |
| 10610 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 10611 | xmlXPathFloorFunction); |
| 10612 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 10613 | xmlXPathLastFunction); |
| 10614 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 10615 | xmlXPathLangFunction); |
| 10616 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 10617 | xmlXPathLocalNameFunction); |
| 10618 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 10619 | xmlXPathNotFunction); |
| 10620 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 10621 | xmlXPathNameFunction); |
| 10622 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 10623 | xmlXPathNamespaceURIFunction); |
| 10624 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 10625 | xmlXPathNormalizeFunction); |
| 10626 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 10627 | xmlXPathNumberFunction); |
| 10628 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 10629 | xmlXPathPositionFunction); |
| 10630 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 10631 | xmlXPathRoundFunction); |
| 10632 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 10633 | xmlXPathStringFunction); |
| 10634 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 10635 | xmlXPathStringLengthFunction); |
| 10636 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 10637 | xmlXPathStartsWithFunction); |
| 10638 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 10639 | xmlXPathSubstringFunction); |
| 10640 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 10641 | xmlXPathSubstringBeforeFunction); |
| 10642 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 10643 | xmlXPathSubstringAfterFunction); |
| 10644 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 10645 | xmlXPathSumFunction); |
| 10646 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 10647 | xmlXPathTrueFunction); |
| 10648 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 10649 | xmlXPathTranslateFunction); |
| 10650 | } |
| 10651 | |
| 10652 | #endif /* LIBXML_XPATH_ENABLED */ |