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 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 17 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 18 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
| 22 | #ifdef HAVE_SYS_TYPES_H |
| 23 | #include <sys/types.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_MATH_H |
| 26 | #include <math.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_FLOAT_H |
| 29 | #include <float.h> |
| 30 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | #ifdef HAVE_CTYPE_H |
| 32 | #include <ctype.h> |
| 33 | #endif |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SIGNAL_H |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 36 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | |
| 38 | #include <libxml/xmlmemory.h> |
| 39 | #include <libxml/tree.h> |
| 40 | #include <libxml/valid.h> |
| 41 | #include <libxml/xpath.h> |
| 42 | #include <libxml/xpathInternals.h> |
| 43 | #include <libxml/parserInternals.h> |
| 44 | #include <libxml/hash.h> |
| 45 | #ifdef LIBXML_XPTR_ENABLED |
| 46 | #include <libxml/xpointer.h> |
| 47 | #endif |
| 48 | #ifdef LIBXML_DEBUG_ENABLED |
| 49 | #include <libxml/debugXML.h> |
| 50 | #endif |
| 51 | #include <libxml/xmlerror.h> |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 52 | #include <libxml/threads.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 53 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 54 | |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 55 | #define TODO \ |
| 56 | xmlGenericError(xmlGenericErrorContext, \ |
| 57 | "Unimplemented block at %s:%d\n", \ |
| 58 | __FILE__, __LINE__); |
| 59 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 60 | #if defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XPATH_ENABLED) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 61 | /************************************************************************ |
| 62 | * * |
| 63 | * Floating point stuff * |
| 64 | * * |
| 65 | ************************************************************************/ |
| 66 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 67 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 68 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 69 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 70 | #include "trionan.c" |
| 71 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 72 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 73 | * The lack of portability of this section of the libc is annoying ! |
| 74 | */ |
| 75 | double xmlXPathNAN = 0; |
| 76 | double xmlXPathPINF = 1; |
| 77 | double xmlXPathNINF = -1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 78 | double xmlXPathNZERO = 0; |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 79 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 80 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 81 | /** |
| 82 | * xmlXPathInit: |
| 83 | * |
| 84 | * Initialize the XPath environment |
| 85 | */ |
| 86 | void |
| 87 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 88 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 89 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 90 | xmlXPathPINF = trio_pinf(); |
| 91 | xmlXPathNINF = trio_ninf(); |
| 92 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 93 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 94 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 95 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 98 | /** |
| 99 | * xmlXPathIsNaN: |
| 100 | * @val: a double value |
| 101 | * |
| 102 | * Provides a portable isnan() function to detect whether a double |
| 103 | * is a NotaNumber. Based on trio code |
| 104 | * http://sourceforge.net/projects/ctrio/ |
| 105 | * |
| 106 | * Returns 1 if the value is a NaN, 0 otherwise |
| 107 | */ |
| 108 | int |
| 109 | xmlXPathIsNaN(double val) { |
| 110 | return(trio_isnan(val)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * xmlXPathIsInf: |
| 115 | * @val: a double value |
| 116 | * |
| 117 | * Provides a portable isinf() function to detect whether a double |
| 118 | * is a +Infinite or -Infinite. Based on trio code |
| 119 | * http://sourceforge.net/projects/ctrio/ |
| 120 | * |
| 121 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 122 | */ |
| 123 | int |
| 124 | xmlXPathIsInf(double val) { |
| 125 | return(trio_isinf(val)); |
| 126 | } |
| 127 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 128 | #endif /* SCHEMAS or XPATH */ |
| 129 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 130 | /** |
| 131 | * xmlXPathGetSign: |
| 132 | * @val: a double value |
| 133 | * |
| 134 | * Provides a portable function to detect the sign of a double |
| 135 | * Modified from trio code |
| 136 | * http://sourceforge.net/projects/ctrio/ |
| 137 | * |
| 138 | * Returns 1 if the value is Negative, 0 if positive |
| 139 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 140 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 141 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 142 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 146 | /* |
| 147 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 148 | * the test should just be name[0] = ' ' |
| 149 | */ |
| 150 | /* #define DEBUG */ |
| 151 | /* #define DEBUG_STEP */ |
| 152 | /* #define DEBUG_STEP_NTH */ |
| 153 | /* #define DEBUG_EXPR */ |
| 154 | /* #define DEBUG_EVAL_COUNTS */ |
| 155 | |
| 156 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 157 | NULL, |
| 158 | XML_NAMESPACE_DECL, |
| 159 | XML_XML_NAMESPACE, |
| 160 | BAD_CAST "xml", |
| 161 | NULL |
| 162 | }; |
| 163 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 164 | #ifndef LIBXML_THREAD_ENABLED |
| 165 | /* |
| 166 | * Optimizer is disabled only when threaded apps are detected while |
| 167 | * the library ain't compiled for thread safety. |
| 168 | */ |
| 169 | static int xmlXPathDisableOptimizer = 0; |
| 170 | #endif |
| 171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 173 | * * |
| 174 | * Error handling routines * |
| 175 | * * |
| 176 | ************************************************************************/ |
| 177 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 178 | /* |
| 179 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 180 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 181 | static const char *xmlXPathErrorMessages[] = { |
| 182 | "Ok\n", |
| 183 | "Number encoding\n", |
| 184 | "Unfinished literal\n", |
| 185 | "Start of literal\n", |
| 186 | "Expected $ for variable reference\n", |
| 187 | "Undefined variable\n", |
| 188 | "Invalid predicate\n", |
| 189 | "Invalid expression\n", |
| 190 | "Missing closing curly brace\n", |
| 191 | "Unregistered function\n", |
| 192 | "Invalid operand\n", |
| 193 | "Invalid type\n", |
| 194 | "Invalid number of arguments\n", |
| 195 | "Invalid context size\n", |
| 196 | "Invalid context position\n", |
| 197 | "Memory allocation error\n", |
| 198 | "Syntax error\n", |
| 199 | "Resource error\n", |
| 200 | "Sub resource error\n", |
| 201 | "Undefined namespace prefix\n", |
| 202 | "Encoding error\n", |
| 203 | "Char out of XML range\n" |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * xmlXPathErrMemory: |
| 209 | * @ctxt: an XPath context |
| 210 | * @extra: extra informations |
| 211 | * |
| 212 | * Handle a redefinition of attribute error |
| 213 | */ |
| 214 | static void |
| 215 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 216 | { |
| 217 | if (ctxt != NULL) { |
| 218 | if (extra) { |
| 219 | xmlChar buf[200]; |
| 220 | |
| 221 | xmlStrPrintf(buf, 200, |
| 222 | BAD_CAST "Memory allocation failed : %s\n", |
| 223 | extra); |
| 224 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 225 | } else { |
| 226 | ctxt->lastError.message = (char *) |
| 227 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 228 | } |
| 229 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 230 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 231 | if (ctxt->error != NULL) |
| 232 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 233 | } else { |
| 234 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 235 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 236 | NULL, NULL, XML_FROM_XPATH, |
| 237 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 238 | extra, NULL, NULL, 0, 0, |
| 239 | "Memory allocation failed : %s\n", extra); |
| 240 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 241 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 242 | NULL, NULL, XML_FROM_XPATH, |
| 243 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 244 | NULL, NULL, NULL, 0, 0, |
| 245 | "Memory allocation failed\n"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * xmlXPathErrMemory: |
| 251 | * @ctxt: an XPath parser context |
| 252 | * @extra: extra informations |
| 253 | * |
| 254 | * Handle a redefinition of attribute error |
| 255 | */ |
| 256 | static void |
| 257 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 258 | { |
| 259 | ctxt->error = XPATH_MEMORY_ERROR; |
| 260 | if (ctxt == NULL) |
| 261 | xmlXPathErrMemory(NULL, extra); |
| 262 | else |
| 263 | xmlXPathErrMemory(ctxt->context, extra); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * xmlXPathErr: |
| 268 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 269 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 270 | * |
| 271 | * Handle a Relax NG Parsing error |
| 272 | */ |
| 273 | void |
| 274 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 275 | { |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 276 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 277 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 278 | NULL, NULL, XML_FROM_XPATH, |
| 279 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 280 | XML_ERR_ERROR, NULL, 0, |
| 281 | NULL, NULL, NULL, 0, 0, |
| 282 | xmlXPathErrorMessages[error]); |
| 283 | return; |
| 284 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 285 | ctxt->error = error; |
| 286 | if (ctxt->context == NULL) { |
| 287 | __xmlRaiseError(NULL, NULL, NULL, |
| 288 | NULL, NULL, XML_FROM_XPATH, |
| 289 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 290 | XML_ERR_ERROR, NULL, 0, |
| 291 | (const char *) ctxt->base, NULL, NULL, |
| 292 | ctxt->cur - ctxt->base, 0, |
| 293 | xmlXPathErrorMessages[error]); |
| 294 | return; |
| 295 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 296 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 297 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 298 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 299 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 300 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 301 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 302 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 303 | if (ctxt->context->error != NULL) { |
| 304 | ctxt->context->error(ctxt->context->userData, |
| 305 | &ctxt->context->lastError); |
| 306 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 307 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 308 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 309 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 310 | XML_ERR_ERROR, NULL, 0, |
| 311 | (const char *) ctxt->base, NULL, NULL, |
| 312 | ctxt->cur - ctxt->base, 0, |
| 313 | xmlXPathErrorMessages[error]); |
| 314 | } |
| 315 | |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * xmlXPatherror: |
| 320 | * @ctxt: the XPath Parser context |
| 321 | * @file: the file name |
| 322 | * @line: the line number |
| 323 | * @no: the error number |
| 324 | * |
| 325 | * Formats an error message. |
| 326 | */ |
| 327 | void |
| 328 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 329 | int line ATTRIBUTE_UNUSED, int no) { |
| 330 | xmlXPathErr(ctxt, no); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 335 | * * |
| 336 | * Parser Types * |
| 337 | * * |
| 338 | ************************************************************************/ |
| 339 | |
| 340 | /* |
| 341 | * Types are private: |
| 342 | */ |
| 343 | |
| 344 | typedef enum { |
| 345 | XPATH_OP_END=0, |
| 346 | XPATH_OP_AND, |
| 347 | XPATH_OP_OR, |
| 348 | XPATH_OP_EQUAL, |
| 349 | XPATH_OP_CMP, |
| 350 | XPATH_OP_PLUS, |
| 351 | XPATH_OP_MULT, |
| 352 | XPATH_OP_UNION, |
| 353 | XPATH_OP_ROOT, |
| 354 | XPATH_OP_NODE, |
| 355 | XPATH_OP_RESET, |
| 356 | XPATH_OP_COLLECT, |
| 357 | XPATH_OP_VALUE, |
| 358 | XPATH_OP_VARIABLE, |
| 359 | XPATH_OP_FUNCTION, |
| 360 | XPATH_OP_ARG, |
| 361 | XPATH_OP_PREDICATE, |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 362 | XPATH_OP_FILTER, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 363 | XPATH_OP_SORT |
| 364 | #ifdef LIBXML_XPTR_ENABLED |
| 365 | ,XPATH_OP_RANGETO |
| 366 | #endif |
| 367 | } xmlXPathOp; |
| 368 | |
| 369 | typedef enum { |
| 370 | AXIS_ANCESTOR = 1, |
| 371 | AXIS_ANCESTOR_OR_SELF, |
| 372 | AXIS_ATTRIBUTE, |
| 373 | AXIS_CHILD, |
| 374 | AXIS_DESCENDANT, |
| 375 | AXIS_DESCENDANT_OR_SELF, |
| 376 | AXIS_FOLLOWING, |
| 377 | AXIS_FOLLOWING_SIBLING, |
| 378 | AXIS_NAMESPACE, |
| 379 | AXIS_PARENT, |
| 380 | AXIS_PRECEDING, |
| 381 | AXIS_PRECEDING_SIBLING, |
| 382 | AXIS_SELF |
| 383 | } xmlXPathAxisVal; |
| 384 | |
| 385 | typedef enum { |
| 386 | NODE_TEST_NONE = 0, |
| 387 | NODE_TEST_TYPE = 1, |
| 388 | NODE_TEST_PI = 2, |
| 389 | NODE_TEST_ALL = 3, |
| 390 | NODE_TEST_NS = 4, |
| 391 | NODE_TEST_NAME = 5 |
| 392 | } xmlXPathTestVal; |
| 393 | |
| 394 | typedef enum { |
| 395 | NODE_TYPE_NODE = 0, |
| 396 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 397 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 398 | NODE_TYPE_PI = XML_PI_NODE |
| 399 | } xmlXPathTypeVal; |
| 400 | |
| 401 | |
| 402 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 403 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 404 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 405 | xmlXPathOp op; /* The identifier of the operation */ |
| 406 | int ch1; /* First child */ |
| 407 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 408 | int value; |
| 409 | int value2; |
| 410 | int value3; |
| 411 | void *value4; |
| 412 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 413 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 414 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 418 | int nbStep; /* Number of steps in this expression */ |
| 419 | int maxStep; /* Maximum number of steps allocated */ |
| 420 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 421 | int last; /* index of last step in expression */ |
| 422 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 423 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 424 | #ifdef DEBUG_EVAL_COUNTS |
| 425 | int nb; |
| 426 | xmlChar *string; |
| 427 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | /************************************************************************ |
| 431 | * * |
| 432 | * Parser Type functions * |
| 433 | * * |
| 434 | ************************************************************************/ |
| 435 | |
| 436 | /** |
| 437 | * xmlXPathNewCompExpr: |
| 438 | * |
| 439 | * Create a new Xpath component |
| 440 | * |
| 441 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 442 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 443 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 444 | xmlXPathNewCompExpr(void) { |
| 445 | xmlXPathCompExprPtr cur; |
| 446 | |
| 447 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 448 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 449 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 450 | return(NULL); |
| 451 | } |
| 452 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 453 | cur->maxStep = 10; |
| 454 | cur->nbStep = 0; |
| 455 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 456 | sizeof(xmlXPathStepOp)); |
| 457 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 458 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 459 | xmlFree(cur); |
| 460 | return(NULL); |
| 461 | } |
| 462 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 463 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 464 | #ifdef DEBUG_EVAL_COUNTS |
| 465 | cur->nb = 0; |
| 466 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 467 | return(cur); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * xmlXPathFreeCompExpr: |
| 472 | * @comp: an XPATH comp |
| 473 | * |
| 474 | * Free up the memory allocated by @comp |
| 475 | */ |
| 476 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 477 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 478 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 479 | xmlXPathStepOpPtr op; |
| 480 | int i; |
| 481 | |
| 482 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 483 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 484 | if (comp->dict == NULL) { |
| 485 | for (i = 0; i < comp->nbStep; i++) { |
| 486 | op = &comp->steps[i]; |
| 487 | if (op->value4 != NULL) { |
| 488 | if (op->op == XPATH_OP_VALUE) |
| 489 | xmlXPathFreeObject(op->value4); |
| 490 | else |
| 491 | xmlFree(op->value4); |
| 492 | } |
| 493 | if (op->value5 != NULL) |
| 494 | xmlFree(op->value5); |
| 495 | } |
| 496 | } else { |
| 497 | for (i = 0; i < comp->nbStep; i++) { |
| 498 | op = &comp->steps[i]; |
| 499 | if (op->value4 != NULL) { |
| 500 | if (op->op == XPATH_OP_VALUE) |
| 501 | xmlXPathFreeObject(op->value4); |
| 502 | } |
| 503 | } |
| 504 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 505 | } |
| 506 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 507 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 508 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 509 | #ifdef DEBUG_EVAL_COUNTS |
| 510 | if (comp->string != NULL) { |
| 511 | xmlFree(comp->string); |
| 512 | } |
| 513 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 514 | if (comp->expr != NULL) { |
| 515 | xmlFree(comp->expr); |
| 516 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 517 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 518 | xmlFree(comp); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * xmlXPathCompExprAdd: |
| 523 | * @comp: the compiled expression |
| 524 | * @ch1: first child index |
| 525 | * @ch2: second child index |
| 526 | * @op: an op |
| 527 | * @value: the first int value |
| 528 | * @value2: the second int value |
| 529 | * @value3: the third int value |
| 530 | * @value4: the first string value |
| 531 | * @value5: the second string value |
| 532 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 533 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 534 | * |
| 535 | * Returns -1 in case of failure, the index otherwise |
| 536 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 537 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 538 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 539 | xmlXPathOp op, int value, |
| 540 | int value2, int value3, void *value4, void *value5) { |
| 541 | if (comp->nbStep >= comp->maxStep) { |
| 542 | xmlXPathStepOp *real; |
| 543 | |
| 544 | comp->maxStep *= 2; |
| 545 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 546 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 547 | if (real == NULL) { |
| 548 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 549 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 550 | return(-1); |
| 551 | } |
| 552 | comp->steps = real; |
| 553 | } |
| 554 | comp->last = comp->nbStep; |
| 555 | comp->steps[comp->nbStep].ch1 = ch1; |
| 556 | comp->steps[comp->nbStep].ch2 = ch2; |
| 557 | comp->steps[comp->nbStep].op = op; |
| 558 | comp->steps[comp->nbStep].value = value; |
| 559 | comp->steps[comp->nbStep].value2 = value2; |
| 560 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 561 | if ((comp->dict != NULL) && |
| 562 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 563 | (op == XPATH_OP_COLLECT))) { |
| 564 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 565 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 566 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 567 | xmlFree(value4); |
| 568 | } else |
| 569 | comp->steps[comp->nbStep].value4 = NULL; |
| 570 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 571 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 572 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 573 | xmlFree(value5); |
| 574 | } else |
| 575 | comp->steps[comp->nbStep].value5 = NULL; |
| 576 | } else { |
| 577 | comp->steps[comp->nbStep].value4 = value4; |
| 578 | comp->steps[comp->nbStep].value5 = value5; |
| 579 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 580 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 581 | return(comp->nbStep++); |
| 582 | } |
| 583 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 584 | /** |
| 585 | * xmlXPathCompSwap: |
| 586 | * @comp: the compiled expression |
| 587 | * @op: operation index |
| 588 | * |
| 589 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 590 | */ |
| 591 | static void |
| 592 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 593 | int tmp; |
| 594 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 595 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 596 | /* |
| 597 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 598 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 599 | * application |
| 600 | */ |
| 601 | if (xmlXPathDisableOptimizer) |
| 602 | return; |
| 603 | #endif |
| 604 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 605 | tmp = op->ch1; |
| 606 | op->ch1 = op->ch2; |
| 607 | op->ch2 = tmp; |
| 608 | } |
| 609 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 610 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 611 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 612 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 613 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 614 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 615 | (op), (val), (val2), (val3), (val4), (val5)) |
| 616 | |
| 617 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 618 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 619 | |
| 620 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 621 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 622 | |
| 623 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 624 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 625 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 626 | |
| 627 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | * * |
| 629 | * Debugging related functions * |
| 630 | * * |
| 631 | ************************************************************************/ |
| 632 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | #define STRANGE \ |
| 634 | xmlGenericError(xmlGenericErrorContext, \ |
| 635 | "Internal error at %s:%d\n", \ |
| 636 | __FILE__, __LINE__); |
| 637 | |
| 638 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 639 | static void |
| 640 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 641 | int i; |
| 642 | char shift[100]; |
| 643 | |
| 644 | for (i = 0;((i < depth) && (i < 25));i++) |
| 645 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 646 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 647 | if (cur == NULL) { |
| 648 | fprintf(output, shift); |
| 649 | fprintf(output, "Node is NULL !\n"); |
| 650 | return; |
| 651 | |
| 652 | } |
| 653 | |
| 654 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 655 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 656 | fprintf(output, shift); |
| 657 | fprintf(output, " /\n"); |
| 658 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 659 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 660 | else |
| 661 | xmlDebugDumpOneNode(output, cur, depth); |
| 662 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 663 | static void |
| 664 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 665 | xmlNodePtr tmp; |
| 666 | int i; |
| 667 | char shift[100]; |
| 668 | |
| 669 | for (i = 0;((i < depth) && (i < 25));i++) |
| 670 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 671 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 672 | if (cur == NULL) { |
| 673 | fprintf(output, shift); |
| 674 | fprintf(output, "Node is NULL !\n"); |
| 675 | return; |
| 676 | |
| 677 | } |
| 678 | |
| 679 | while (cur != NULL) { |
| 680 | tmp = cur; |
| 681 | cur = cur->next; |
| 682 | xmlDebugDumpOneNode(output, tmp, depth); |
| 683 | } |
| 684 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 685 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 686 | static void |
| 687 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 688 | int i; |
| 689 | char shift[100]; |
| 690 | |
| 691 | for (i = 0;((i < depth) && (i < 25));i++) |
| 692 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 693 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 694 | |
| 695 | if (cur == NULL) { |
| 696 | fprintf(output, shift); |
| 697 | fprintf(output, "NodeSet is NULL !\n"); |
| 698 | return; |
| 699 | |
| 700 | } |
| 701 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 702 | if (cur != NULL) { |
| 703 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 704 | for (i = 0;i < cur->nodeNr;i++) { |
| 705 | fprintf(output, shift); |
| 706 | fprintf(output, "%d", i + 1); |
| 707 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 712 | static void |
| 713 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 714 | int i; |
| 715 | char shift[100]; |
| 716 | |
| 717 | for (i = 0;((i < depth) && (i < 25));i++) |
| 718 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 719 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 720 | |
| 721 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 722 | fprintf(output, shift); |
| 723 | fprintf(output, "Value Tree is NULL !\n"); |
| 724 | return; |
| 725 | |
| 726 | } |
| 727 | |
| 728 | fprintf(output, shift); |
| 729 | fprintf(output, "%d", i + 1); |
| 730 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 731 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 732 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 733 | static void |
| 734 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 735 | int i; |
| 736 | char shift[100]; |
| 737 | |
| 738 | for (i = 0;((i < depth) && (i < 25));i++) |
| 739 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 740 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 741 | |
| 742 | if (cur == NULL) { |
| 743 | fprintf(output, shift); |
| 744 | fprintf(output, "LocationSet is NULL !\n"); |
| 745 | return; |
| 746 | |
| 747 | } |
| 748 | |
| 749 | for (i = 0;i < cur->locNr;i++) { |
| 750 | fprintf(output, shift); |
| 751 | fprintf(output, "%d : ", i + 1); |
| 752 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 753 | } |
| 754 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 755 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 756 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 757 | /** |
| 758 | * xmlXPathDebugDumpObject: |
| 759 | * @output: the FILE * to dump the output |
| 760 | * @cur: the object to inspect |
| 761 | * @depth: indentation level |
| 762 | * |
| 763 | * Dump the content of the object for debugging purposes |
| 764 | */ |
| 765 | void |
| 766 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 767 | int i; |
| 768 | char shift[100]; |
| 769 | |
| 770 | for (i = 0;((i < depth) && (i < 25));i++) |
| 771 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 772 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 773 | |
| 774 | fprintf(output, shift); |
| 775 | |
| 776 | if (cur == NULL) { |
| 777 | fprintf(output, "Object is empty (NULL)\n"); |
| 778 | return; |
| 779 | } |
| 780 | switch(cur->type) { |
| 781 | case XPATH_UNDEFINED: |
| 782 | fprintf(output, "Object is uninitialized\n"); |
| 783 | break; |
| 784 | case XPATH_NODESET: |
| 785 | fprintf(output, "Object is a Node Set :\n"); |
| 786 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 787 | break; |
| 788 | case XPATH_XSLT_TREE: |
| 789 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 790 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 791 | break; |
| 792 | case XPATH_BOOLEAN: |
| 793 | fprintf(output, "Object is a Boolean : "); |
| 794 | if (cur->boolval) fprintf(output, "true\n"); |
| 795 | else fprintf(output, "false\n"); |
| 796 | break; |
| 797 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 798 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 799 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 800 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 801 | break; |
| 802 | case -1: |
| 803 | fprintf(output, "Object is a number : -Infinity\n"); |
| 804 | break; |
| 805 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 806 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 807 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 808 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 809 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 810 | } else { |
| 811 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 812 | } |
| 813 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 814 | break; |
| 815 | case XPATH_STRING: |
| 816 | fprintf(output, "Object is a string : "); |
| 817 | xmlDebugDumpString(output, cur->stringval); |
| 818 | fprintf(output, "\n"); |
| 819 | break; |
| 820 | case XPATH_POINT: |
| 821 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 822 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 823 | fprintf(output, "\n"); |
| 824 | break; |
| 825 | case XPATH_RANGE: |
| 826 | if ((cur->user2 == NULL) || |
| 827 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 828 | fprintf(output, "Object is a collapsed range :\n"); |
| 829 | fprintf(output, shift); |
| 830 | if (cur->index >= 0) |
| 831 | fprintf(output, "index %d in ", cur->index); |
| 832 | fprintf(output, "node\n"); |
| 833 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 834 | depth + 1); |
| 835 | } else { |
| 836 | fprintf(output, "Object is a range :\n"); |
| 837 | fprintf(output, shift); |
| 838 | fprintf(output, "From "); |
| 839 | if (cur->index >= 0) |
| 840 | fprintf(output, "index %d in ", cur->index); |
| 841 | fprintf(output, "node\n"); |
| 842 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 843 | depth + 1); |
| 844 | fprintf(output, shift); |
| 845 | fprintf(output, "To "); |
| 846 | if (cur->index2 >= 0) |
| 847 | fprintf(output, "index %d in ", cur->index2); |
| 848 | fprintf(output, "node\n"); |
| 849 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 850 | depth + 1); |
| 851 | fprintf(output, "\n"); |
| 852 | } |
| 853 | break; |
| 854 | case XPATH_LOCATIONSET: |
| 855 | #if defined(LIBXML_XPTR_ENABLED) |
| 856 | fprintf(output, "Object is a Location Set:\n"); |
| 857 | xmlXPathDebugDumpLocationSet(output, |
| 858 | (xmlLocationSetPtr) cur->user, depth); |
| 859 | #endif |
| 860 | break; |
| 861 | case XPATH_USERS: |
| 862 | fprintf(output, "Object is user defined\n"); |
| 863 | break; |
| 864 | } |
| 865 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 866 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 867 | static void |
| 868 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 869 | xmlXPathStepOpPtr op, int depth) { |
| 870 | int i; |
| 871 | char shift[100]; |
| 872 | |
| 873 | for (i = 0;((i < depth) && (i < 25));i++) |
| 874 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 875 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 876 | |
| 877 | fprintf(output, shift); |
| 878 | if (op == NULL) { |
| 879 | fprintf(output, "Step is NULL\n"); |
| 880 | return; |
| 881 | } |
| 882 | switch (op->op) { |
| 883 | case XPATH_OP_END: |
| 884 | fprintf(output, "END"); break; |
| 885 | case XPATH_OP_AND: |
| 886 | fprintf(output, "AND"); break; |
| 887 | case XPATH_OP_OR: |
| 888 | fprintf(output, "OR"); break; |
| 889 | case XPATH_OP_EQUAL: |
| 890 | if (op->value) |
| 891 | fprintf(output, "EQUAL ="); |
| 892 | else |
| 893 | fprintf(output, "EQUAL !="); |
| 894 | break; |
| 895 | case XPATH_OP_CMP: |
| 896 | if (op->value) |
| 897 | fprintf(output, "CMP <"); |
| 898 | else |
| 899 | fprintf(output, "CMP >"); |
| 900 | if (!op->value2) |
| 901 | fprintf(output, "="); |
| 902 | break; |
| 903 | case XPATH_OP_PLUS: |
| 904 | if (op->value == 0) |
| 905 | fprintf(output, "PLUS -"); |
| 906 | else if (op->value == 1) |
| 907 | fprintf(output, "PLUS +"); |
| 908 | else if (op->value == 2) |
| 909 | fprintf(output, "PLUS unary -"); |
| 910 | else if (op->value == 3) |
| 911 | fprintf(output, "PLUS unary - -"); |
| 912 | break; |
| 913 | case XPATH_OP_MULT: |
| 914 | if (op->value == 0) |
| 915 | fprintf(output, "MULT *"); |
| 916 | else if (op->value == 1) |
| 917 | fprintf(output, "MULT div"); |
| 918 | else |
| 919 | fprintf(output, "MULT mod"); |
| 920 | break; |
| 921 | case XPATH_OP_UNION: |
| 922 | fprintf(output, "UNION"); break; |
| 923 | case XPATH_OP_ROOT: |
| 924 | fprintf(output, "ROOT"); break; |
| 925 | case XPATH_OP_NODE: |
| 926 | fprintf(output, "NODE"); break; |
| 927 | case XPATH_OP_RESET: |
| 928 | fprintf(output, "RESET"); break; |
| 929 | case XPATH_OP_SORT: |
| 930 | fprintf(output, "SORT"); break; |
| 931 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 932 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 933 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 934 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 935 | const xmlChar *prefix = op->value4; |
| 936 | const xmlChar *name = op->value5; |
| 937 | |
| 938 | fprintf(output, "COLLECT "); |
| 939 | switch (axis) { |
| 940 | case AXIS_ANCESTOR: |
| 941 | fprintf(output, " 'ancestors' "); break; |
| 942 | case AXIS_ANCESTOR_OR_SELF: |
| 943 | fprintf(output, " 'ancestors-or-self' "); break; |
| 944 | case AXIS_ATTRIBUTE: |
| 945 | fprintf(output, " 'attributes' "); break; |
| 946 | case AXIS_CHILD: |
| 947 | fprintf(output, " 'child' "); break; |
| 948 | case AXIS_DESCENDANT: |
| 949 | fprintf(output, " 'descendant' "); break; |
| 950 | case AXIS_DESCENDANT_OR_SELF: |
| 951 | fprintf(output, " 'descendant-or-self' "); break; |
| 952 | case AXIS_FOLLOWING: |
| 953 | fprintf(output, " 'following' "); break; |
| 954 | case AXIS_FOLLOWING_SIBLING: |
| 955 | fprintf(output, " 'following-siblings' "); break; |
| 956 | case AXIS_NAMESPACE: |
| 957 | fprintf(output, " 'namespace' "); break; |
| 958 | case AXIS_PARENT: |
| 959 | fprintf(output, " 'parent' "); break; |
| 960 | case AXIS_PRECEDING: |
| 961 | fprintf(output, " 'preceding' "); break; |
| 962 | case AXIS_PRECEDING_SIBLING: |
| 963 | fprintf(output, " 'preceding-sibling' "); break; |
| 964 | case AXIS_SELF: |
| 965 | fprintf(output, " 'self' "); break; |
| 966 | } |
| 967 | switch (test) { |
| 968 | case NODE_TEST_NONE: |
| 969 | fprintf(output, "'none' "); break; |
| 970 | case NODE_TEST_TYPE: |
| 971 | fprintf(output, "'type' "); break; |
| 972 | case NODE_TEST_PI: |
| 973 | fprintf(output, "'PI' "); break; |
| 974 | case NODE_TEST_ALL: |
| 975 | fprintf(output, "'all' "); break; |
| 976 | case NODE_TEST_NS: |
| 977 | fprintf(output, "'namespace' "); break; |
| 978 | case NODE_TEST_NAME: |
| 979 | fprintf(output, "'name' "); break; |
| 980 | } |
| 981 | switch (type) { |
| 982 | case NODE_TYPE_NODE: |
| 983 | fprintf(output, "'node' "); break; |
| 984 | case NODE_TYPE_COMMENT: |
| 985 | fprintf(output, "'comment' "); break; |
| 986 | case NODE_TYPE_TEXT: |
| 987 | fprintf(output, "'text' "); break; |
| 988 | case NODE_TYPE_PI: |
| 989 | fprintf(output, "'PI' "); break; |
| 990 | } |
| 991 | if (prefix != NULL) |
| 992 | fprintf(output, "%s:", prefix); |
| 993 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 994 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 995 | break; |
| 996 | |
| 997 | } |
| 998 | case XPATH_OP_VALUE: { |
| 999 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1000 | |
| 1001 | fprintf(output, "ELEM "); |
| 1002 | xmlXPathDebugDumpObject(output, object, 0); |
| 1003 | goto finish; |
| 1004 | } |
| 1005 | case XPATH_OP_VARIABLE: { |
| 1006 | const xmlChar *prefix = op->value5; |
| 1007 | const xmlChar *name = op->value4; |
| 1008 | |
| 1009 | if (prefix != NULL) |
| 1010 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1011 | else |
| 1012 | fprintf(output, "VARIABLE %s", name); |
| 1013 | break; |
| 1014 | } |
| 1015 | case XPATH_OP_FUNCTION: { |
| 1016 | int nbargs = op->value; |
| 1017 | const xmlChar *prefix = op->value5; |
| 1018 | const xmlChar *name = op->value4; |
| 1019 | |
| 1020 | if (prefix != NULL) |
| 1021 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1022 | prefix, name, nbargs); |
| 1023 | else |
| 1024 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1025 | break; |
| 1026 | } |
| 1027 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1028 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1029 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1030 | #ifdef LIBXML_XPTR_ENABLED |
| 1031 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1032 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1033 | default: |
| 1034 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1035 | } |
| 1036 | fprintf(output, "\n"); |
| 1037 | finish: |
| 1038 | if (op->ch1 >= 0) |
| 1039 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1040 | if (op->ch2 >= 0) |
| 1041 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1042 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1043 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1044 | /** |
| 1045 | * xmlXPathDebugDumpCompExpr: |
| 1046 | * @output: the FILE * for the output |
| 1047 | * @comp: the precompiled XPath expression |
| 1048 | * @depth: the indentation level. |
| 1049 | * |
| 1050 | * Dumps the tree of the compiled XPath expression. |
| 1051 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1052 | void |
| 1053 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1054 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1055 | int i; |
| 1056 | char shift[100]; |
| 1057 | |
| 1058 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1059 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1060 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1061 | |
| 1062 | fprintf(output, shift); |
| 1063 | |
| 1064 | if (comp == NULL) { |
| 1065 | fprintf(output, "Compiled Expression is NULL\n"); |
| 1066 | return; |
| 1067 | } |
| 1068 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1069 | comp->nbStep); |
| 1070 | i = comp->last; |
| 1071 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1072 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1073 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1074 | |
| 1075 | /************************************************************************ |
| 1076 | * * |
| 1077 | * Parser stacks related functions and macros * |
| 1078 | * * |
| 1079 | ************************************************************************/ |
| 1080 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1081 | /** |
| 1082 | * valuePop: |
| 1083 | * @ctxt: an XPath evaluation context |
| 1084 | * |
| 1085 | * Pops the top XPath object from the value stack |
| 1086 | * |
| 1087 | * Returns the XPath object just removed |
| 1088 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1089 | extern xmlXPathObjectPtr |
| 1090 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1091 | { |
| 1092 | xmlXPathObjectPtr ret; |
| 1093 | |
| 1094 | if (ctxt->valueNr <= 0) |
| 1095 | return (0); |
| 1096 | ctxt->valueNr--; |
| 1097 | if (ctxt->valueNr > 0) |
| 1098 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1099 | else |
| 1100 | ctxt->value = NULL; |
| 1101 | ret = ctxt->valueTab[ctxt->valueNr]; |
| 1102 | ctxt->valueTab[ctxt->valueNr] = 0; |
| 1103 | return (ret); |
| 1104 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1105 | /** |
| 1106 | * valuePush: |
| 1107 | * @ctxt: an XPath evaluation context |
| 1108 | * @value: the XPath object |
| 1109 | * |
| 1110 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1111 | * |
| 1112 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1113 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1114 | extern int |
| 1115 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1116 | { |
| 1117 | if (ctxt->valueNr >= ctxt->valueMax) { |
| 1118 | ctxt->valueMax *= 2; |
| 1119 | ctxt->valueTab = |
| 1120 | (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1121 | ctxt->valueMax * |
| 1122 | sizeof(ctxt->valueTab[0])); |
| 1123 | if (ctxt->valueTab == NULL) { |
| 1124 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1125 | return (0); |
| 1126 | } |
| 1127 | } |
| 1128 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1129 | ctxt->value = value; |
| 1130 | return (ctxt->valueNr++); |
| 1131 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1132 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1133 | /** |
| 1134 | * xmlXPathPopBoolean: |
| 1135 | * @ctxt: an XPath parser context |
| 1136 | * |
| 1137 | * Pops a boolean from the stack, handling conversion if needed. |
| 1138 | * Check error with #xmlXPathCheckError. |
| 1139 | * |
| 1140 | * Returns the boolean |
| 1141 | */ |
| 1142 | int |
| 1143 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1144 | xmlXPathObjectPtr obj; |
| 1145 | int ret; |
| 1146 | |
| 1147 | obj = valuePop(ctxt); |
| 1148 | if (obj == NULL) { |
| 1149 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1150 | return(0); |
| 1151 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1152 | if (obj->type != XPATH_BOOLEAN) |
| 1153 | ret = xmlXPathCastToBoolean(obj); |
| 1154 | else |
| 1155 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1156 | xmlXPathFreeObject(obj); |
| 1157 | return(ret); |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * xmlXPathPopNumber: |
| 1162 | * @ctxt: an XPath parser context |
| 1163 | * |
| 1164 | * Pops a number from the stack, handling conversion if needed. |
| 1165 | * Check error with #xmlXPathCheckError. |
| 1166 | * |
| 1167 | * Returns the number |
| 1168 | */ |
| 1169 | double |
| 1170 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1171 | xmlXPathObjectPtr obj; |
| 1172 | double ret; |
| 1173 | |
| 1174 | obj = valuePop(ctxt); |
| 1175 | if (obj == NULL) { |
| 1176 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1177 | return(0); |
| 1178 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1179 | if (obj->type != XPATH_NUMBER) |
| 1180 | ret = xmlXPathCastToNumber(obj); |
| 1181 | else |
| 1182 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1183 | xmlXPathFreeObject(obj); |
| 1184 | return(ret); |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * xmlXPathPopString: |
| 1189 | * @ctxt: an XPath parser context |
| 1190 | * |
| 1191 | * Pops a string from the stack, handling conversion if needed. |
| 1192 | * Check error with #xmlXPathCheckError. |
| 1193 | * |
| 1194 | * Returns the string |
| 1195 | */ |
| 1196 | xmlChar * |
| 1197 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1198 | xmlXPathObjectPtr obj; |
| 1199 | xmlChar * ret; |
| 1200 | |
| 1201 | obj = valuePop(ctxt); |
| 1202 | if (obj == NULL) { |
| 1203 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1204 | return(NULL); |
| 1205 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1206 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1207 | /* TODO: needs refactoring somewhere else */ |
| 1208 | if (obj->stringval == ret) |
| 1209 | obj->stringval = NULL; |
| 1210 | xmlXPathFreeObject(obj); |
| 1211 | return(ret); |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * xmlXPathPopNodeSet: |
| 1216 | * @ctxt: an XPath parser context |
| 1217 | * |
| 1218 | * Pops a node-set from the stack, handling conversion if needed. |
| 1219 | * Check error with #xmlXPathCheckError. |
| 1220 | * |
| 1221 | * Returns the node-set |
| 1222 | */ |
| 1223 | xmlNodeSetPtr |
| 1224 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1225 | xmlXPathObjectPtr obj; |
| 1226 | xmlNodeSetPtr ret; |
| 1227 | |
| 1228 | if (ctxt->value == NULL) { |
| 1229 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1230 | return(NULL); |
| 1231 | } |
| 1232 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1233 | xmlXPathSetTypeError(ctxt); |
| 1234 | return(NULL); |
| 1235 | } |
| 1236 | obj = valuePop(ctxt); |
| 1237 | ret = obj->nodesetval; |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1238 | /* to fix memory leak of not clearing obj->user */ |
| 1239 | if (obj->boolval && obj->user != NULL) |
| 1240 | xmlFreeNodeList((xmlNodePtr) obj->user); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1241 | xmlXPathFreeNodeSetList(obj); |
| 1242 | return(ret); |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * xmlXPathPopExternal: |
| 1247 | * @ctxt: an XPath parser context |
| 1248 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1249 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1250 | * Check error with #xmlXPathCheckError. |
| 1251 | * |
| 1252 | * Returns the object |
| 1253 | */ |
| 1254 | void * |
| 1255 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1256 | xmlXPathObjectPtr obj; |
| 1257 | void * ret; |
| 1258 | |
| 1259 | if (ctxt->value == NULL) { |
| 1260 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1261 | return(NULL); |
| 1262 | } |
| 1263 | if (ctxt->value->type != XPATH_USERS) { |
| 1264 | xmlXPathSetTypeError(ctxt); |
| 1265 | return(NULL); |
| 1266 | } |
| 1267 | obj = valuePop(ctxt); |
| 1268 | ret = obj->user; |
| 1269 | xmlXPathFreeObject(obj); |
| 1270 | return(ret); |
| 1271 | } |
| 1272 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1273 | /* |
| 1274 | * Macros for accessing the content. Those should be used only by the parser, |
| 1275 | * and not exported. |
| 1276 | * |
| 1277 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1278 | * |
| 1279 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1280 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1281 | * in ISO-Latin or UTF-8. |
| 1282 | * This should be used internally by the parser |
| 1283 | * only to compare to ASCII values otherwise it would break when |
| 1284 | * running with UTF-8 encoding. |
| 1285 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1286 | * to compare on ASCII based substring. |
| 1287 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1288 | * strings within the parser. |
| 1289 | * CURRENT Returns the current char value, with the full decoding of |
| 1290 | * UTF-8 if we are using this mode. It returns an int. |
| 1291 | * NEXT Skip to the next character, this does the proper decoding |
| 1292 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1293 | * It returns the pointer to the current xmlChar. |
| 1294 | */ |
| 1295 | |
| 1296 | #define CUR (*ctxt->cur) |
| 1297 | #define SKIP(val) ctxt->cur += (val) |
| 1298 | #define NXT(val) ctxt->cur[(val)] |
| 1299 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1300 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1301 | |
| 1302 | #define COPY_BUF(l,b,i,v) \ |
| 1303 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1304 | else i += xmlCopyChar(l,&b[i],v) |
| 1305 | |
| 1306 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1307 | |
| 1308 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1309 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1310 | |
| 1311 | #define CURRENT (*ctxt->cur) |
| 1312 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1313 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1314 | |
| 1315 | #ifndef DBL_DIG |
| 1316 | #define DBL_DIG 16 |
| 1317 | #endif |
| 1318 | #ifndef DBL_EPSILON |
| 1319 | #define DBL_EPSILON 1E-9 |
| 1320 | #endif |
| 1321 | |
| 1322 | #define UPPER_DOUBLE 1E9 |
| 1323 | #define LOWER_DOUBLE 1E-5 |
| 1324 | |
| 1325 | #define INTEGER_DIGITS DBL_DIG |
| 1326 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1327 | #define EXPONENT_DIGITS (3 + 2) |
| 1328 | |
| 1329 | /** |
| 1330 | * xmlXPathFormatNumber: |
| 1331 | * @number: number to format |
| 1332 | * @buffer: output buffer |
| 1333 | * @buffersize: size of output buffer |
| 1334 | * |
| 1335 | * Convert the number into a string representation. |
| 1336 | */ |
| 1337 | static void |
| 1338 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1339 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1340 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1341 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1342 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1343 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1344 | break; |
| 1345 | case -1: |
| 1346 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1347 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1348 | break; |
| 1349 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1350 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1351 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1352 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1353 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1354 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1355 | } else if (number == ((int) number)) { |
| 1356 | char work[30]; |
| 1357 | char *ptr, *cur; |
| 1358 | int res, value = (int) number; |
| 1359 | |
| 1360 | ptr = &buffer[0]; |
| 1361 | if (value < 0) { |
| 1362 | *ptr++ = '-'; |
| 1363 | value = -value; |
| 1364 | } |
| 1365 | if (value == 0) { |
| 1366 | *ptr++ = '0'; |
| 1367 | } else { |
| 1368 | cur = &work[0]; |
| 1369 | while (value != 0) { |
| 1370 | res = value % 10; |
| 1371 | value = value / 10; |
| 1372 | *cur++ = '0' + res; |
| 1373 | } |
| 1374 | cur--; |
| 1375 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1376 | *ptr++ = *cur--; |
| 1377 | } |
| 1378 | } |
| 1379 | if (ptr - buffer < buffersize) { |
| 1380 | *ptr = 0; |
| 1381 | } else if (buffersize > 0) { |
| 1382 | ptr--; |
| 1383 | *ptr = 0; |
| 1384 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1385 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1386 | /* 3 is sign, decimal point, and terminating zero */ |
| 1387 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1388 | int integer_place, fraction_place; |
| 1389 | char *ptr; |
| 1390 | char *after_fraction; |
| 1391 | double absolute_value; |
| 1392 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1393 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1394 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1395 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1396 | /* |
| 1397 | * First choose format - scientific or regular floating point. |
| 1398 | * In either case, result is in work, and after_fraction points |
| 1399 | * just past the fractional part. |
| 1400 | */ |
| 1401 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1402 | (absolute_value < LOWER_DOUBLE)) && |
| 1403 | (absolute_value != 0.0) ) { |
| 1404 | /* Use scientific notation */ |
| 1405 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1406 | fraction_place = DBL_DIG - 1; |
| 1407 | snprintf(work, sizeof(work),"%*.*e", |
| 1408 | integer_place, fraction_place, number); |
| 1409 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1410 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1411 | else { |
| 1412 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1413 | if (absolute_value > 0.0) |
| 1414 | integer_place = 1 + (int)log10(absolute_value); |
| 1415 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1416 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1417 | fraction_place = (integer_place > 0) |
| 1418 | ? DBL_DIG - integer_place |
| 1419 | : DBL_DIG; |
| 1420 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1421 | fraction_place, number); |
| 1422 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1425 | /* Remove fractional trailing zeroes */ |
| 1426 | ptr = after_fraction; |
| 1427 | while (*(--ptr) == '0') |
| 1428 | ; |
| 1429 | if (*ptr != '.') |
| 1430 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1431 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1432 | |
| 1433 | /* Finally copy result back to caller */ |
| 1434 | size = strlen(work) + 1; |
| 1435 | if (size > buffersize) { |
| 1436 | work[buffersize - 1] = 0; |
| 1437 | size = buffersize; |
| 1438 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1439 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1440 | } |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1445 | |
| 1446 | /************************************************************************ |
| 1447 | * * |
| 1448 | * Routines to handle NodeSets * |
| 1449 | * * |
| 1450 | ************************************************************************/ |
| 1451 | |
| 1452 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1453 | * xmlXPathOrderDocElems: |
| 1454 | * @doc: an input document |
| 1455 | * |
| 1456 | * Call this routine to speed up XPath computation on static documents. |
| 1457 | * This stamps all the element nodes with the document order |
| 1458 | * Like for line information, the order is kept in the element->content |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1459 | * field, the value stored is actually - the node number (starting at -1) |
| 1460 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1461 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1462 | * Returns the number of elements found in the document or -1 in case |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1463 | * of error. |
| 1464 | */ |
| 1465 | long |
| 1466 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1467 | long count = 0; |
| 1468 | xmlNodePtr cur; |
| 1469 | |
| 1470 | if (doc == NULL) |
| 1471 | return(-1); |
| 1472 | cur = doc->children; |
| 1473 | while (cur != NULL) { |
| 1474 | if (cur->type == XML_ELEMENT_NODE) { |
| 1475 | cur->content = (void *) (-(++count)); |
| 1476 | if (cur->children != NULL) { |
| 1477 | cur = cur->children; |
| 1478 | continue; |
| 1479 | } |
| 1480 | } |
| 1481 | if (cur->next != NULL) { |
| 1482 | cur = cur->next; |
| 1483 | continue; |
| 1484 | } |
| 1485 | do { |
| 1486 | cur = cur->parent; |
| 1487 | if (cur == NULL) |
| 1488 | break; |
| 1489 | if (cur == (xmlNodePtr) doc) { |
| 1490 | cur = NULL; |
| 1491 | break; |
| 1492 | } |
| 1493 | if (cur->next != NULL) { |
| 1494 | cur = cur->next; |
| 1495 | break; |
| 1496 | } |
| 1497 | } while (cur != NULL); |
| 1498 | } |
| 1499 | return(count); |
| 1500 | } |
| 1501 | |
| 1502 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1503 | * xmlXPathCmpNodes: |
| 1504 | * @node1: the first node |
| 1505 | * @node2: the second node |
| 1506 | * |
| 1507 | * Compare two nodes w.r.t document order |
| 1508 | * |
| 1509 | * Returns -2 in case of error 1 if first point < second point, 0 if |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1510 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1511 | */ |
| 1512 | int |
| 1513 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1514 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1515 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1516 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1517 | xmlNodePtr cur, root; |
| 1518 | |
| 1519 | if ((node1 == NULL) || (node2 == NULL)) |
| 1520 | return(-2); |
| 1521 | /* |
| 1522 | * a couple of optimizations which will avoid computations in most cases |
| 1523 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1524 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1525 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1526 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1527 | node1 = node1->parent; |
| 1528 | } |
| 1529 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1530 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1531 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1532 | node2 = node2->parent; |
| 1533 | } |
| 1534 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1535 | if (attr1 == attr2) { |
| 1536 | /* not required, but we keep attributes in order */ |
| 1537 | if (attr1 != 0) { |
| 1538 | cur = attrNode2->prev; |
| 1539 | while (cur != NULL) { |
| 1540 | if (cur == attrNode1) |
| 1541 | return (1); |
| 1542 | cur = cur->prev; |
| 1543 | } |
| 1544 | return (-1); |
| 1545 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1546 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1547 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1548 | if (attr2 == 1) |
| 1549 | return(1); |
| 1550 | return(-1); |
| 1551 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1552 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1553 | (node2->type == XML_NAMESPACE_DECL)) |
| 1554 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1555 | if (node1 == node2->prev) |
| 1556 | return(1); |
| 1557 | if (node1 == node2->next) |
| 1558 | return(-1); |
| 1559 | |
| 1560 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1561 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1562 | */ |
| 1563 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1564 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1565 | (0 > (long) node1->content) && |
| 1566 | (0 > (long) node2->content) && |
| 1567 | (node1->doc == node2->doc)) { |
| 1568 | long l1, l2; |
| 1569 | |
| 1570 | l1 = -((long) node1->content); |
| 1571 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1572 | if (l1 < l2) |
| 1573 | return(1); |
| 1574 | if (l1 > l2) |
| 1575 | return(-1); |
| 1576 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1577 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1578 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1579 | * compute depth to root |
| 1580 | */ |
| 1581 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1582 | if (cur == node1) |
| 1583 | return(1); |
| 1584 | depth2++; |
| 1585 | } |
| 1586 | root = cur; |
| 1587 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1588 | if (cur == node2) |
| 1589 | return(-1); |
| 1590 | depth1++; |
| 1591 | } |
| 1592 | /* |
| 1593 | * Distinct document (or distinct entities :-( ) case. |
| 1594 | */ |
| 1595 | if (root != cur) { |
| 1596 | return(-2); |
| 1597 | } |
| 1598 | /* |
| 1599 | * get the nearest common ancestor. |
| 1600 | */ |
| 1601 | while (depth1 > depth2) { |
| 1602 | depth1--; |
| 1603 | node1 = node1->parent; |
| 1604 | } |
| 1605 | while (depth2 > depth1) { |
| 1606 | depth2--; |
| 1607 | node2 = node2->parent; |
| 1608 | } |
| 1609 | while (node1->parent != node2->parent) { |
| 1610 | node1 = node1->parent; |
| 1611 | node2 = node2->parent; |
| 1612 | /* should not happen but just in case ... */ |
| 1613 | if ((node1 == NULL) || (node2 == NULL)) |
| 1614 | return(-2); |
| 1615 | } |
| 1616 | /* |
| 1617 | * Find who's first. |
| 1618 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1619 | if (node1 == node2->prev) |
| 1620 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1621 | if (node1 == node2->next) |
| 1622 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | * Speedup using document order if availble. |
| 1625 | */ |
| 1626 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1627 | (node2->type == XML_ELEMENT_NODE) && |
| 1628 | (0 > (long) node1->content) && |
| 1629 | (0 > (long) node2->content) && |
| 1630 | (node1->doc == node2->doc)) { |
| 1631 | long l1, l2; |
| 1632 | |
| 1633 | l1 = -((long) node1->content); |
| 1634 | l2 = -((long) node2->content); |
| 1635 | if (l1 < l2) |
| 1636 | return(1); |
| 1637 | if (l1 > l2) |
| 1638 | return(-1); |
| 1639 | } |
| 1640 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1641 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1642 | if (cur == node2) |
| 1643 | return(1); |
| 1644 | return(-1); /* assume there is no sibling list corruption */ |
| 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * xmlXPathNodeSetSort: |
| 1649 | * @set: the node set |
| 1650 | * |
| 1651 | * Sort the node set in document order |
| 1652 | */ |
| 1653 | void |
| 1654 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1655 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1656 | xmlNodePtr tmp; |
| 1657 | |
| 1658 | if (set == NULL) |
| 1659 | return; |
| 1660 | |
| 1661 | /* Use Shell's sort to sort the node-set */ |
| 1662 | len = set->nodeNr; |
| 1663 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1664 | for (i = incr; i < len; i++) { |
| 1665 | j = i - incr; |
| 1666 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1667 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1668 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1669 | tmp = set->nodeTab[j]; |
| 1670 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1671 | set->nodeTab[j + incr] = tmp; |
| 1672 | j -= incr; |
| 1673 | } else |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | #define XML_NODESET_DEFAULT 10 |
| 1681 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1682 | * xmlXPathNodeSetDupNs: |
| 1683 | * @node: the parent node of the namespace XPath node |
| 1684 | * @ns: the libxml namespace declaration node. |
| 1685 | * |
| 1686 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1687 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1688 | * parent node in the XPath semantic. |
| 1689 | * |
| 1690 | * Returns the newly created object. |
| 1691 | */ |
| 1692 | static xmlNodePtr |
| 1693 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1694 | xmlNsPtr cur; |
| 1695 | |
| 1696 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1697 | return(NULL); |
| 1698 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1699 | return((xmlNodePtr) ns); |
| 1700 | |
| 1701 | /* |
| 1702 | * Allocate a new Namespace and fill the fields. |
| 1703 | */ |
| 1704 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1705 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1706 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1707 | return(NULL); |
| 1708 | } |
| 1709 | memset(cur, 0, sizeof(xmlNs)); |
| 1710 | cur->type = XML_NAMESPACE_DECL; |
| 1711 | if (ns->href != NULL) |
| 1712 | cur->href = xmlStrdup(ns->href); |
| 1713 | if (ns->prefix != NULL) |
| 1714 | cur->prefix = xmlStrdup(ns->prefix); |
| 1715 | cur->next = (xmlNsPtr) node; |
| 1716 | return((xmlNodePtr) cur); |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * xmlXPathNodeSetFreeNs: |
| 1721 | * @ns: the XPath namespace node found in a nodeset. |
| 1722 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1723 | * Namespace nodes in libxml don't match the XPath semantic. In a node set |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1724 | * the namespace nodes are duplicated and the next pointer is set to the |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1725 | * parent node in the XPath semantic. Check if such a node needs to be freed |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1726 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1727 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1728 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1729 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1730 | return; |
| 1731 | |
| 1732 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1733 | if (ns->href != NULL) |
| 1734 | xmlFree((xmlChar *)ns->href); |
| 1735 | if (ns->prefix != NULL) |
| 1736 | xmlFree((xmlChar *)ns->prefix); |
| 1737 | xmlFree(ns); |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1742 | * xmlXPathNodeSetCreate: |
| 1743 | * @val: an initial xmlNodePtr, or NULL |
| 1744 | * |
| 1745 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1746 | * |
| 1747 | * Returns the newly created object. |
| 1748 | */ |
| 1749 | xmlNodeSetPtr |
| 1750 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1751 | xmlNodeSetPtr ret; |
| 1752 | |
| 1753 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1754 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1755 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1756 | return(NULL); |
| 1757 | } |
| 1758 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1759 | if (val != NULL) { |
| 1760 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1761 | sizeof(xmlNodePtr)); |
| 1762 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1763 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 1764 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1765 | return(NULL); |
| 1766 | } |
| 1767 | memset(ret->nodeTab, 0 , |
| 1768 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1769 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1770 | if (val->type == XML_NAMESPACE_DECL) { |
| 1771 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1772 | |
| 1773 | ret->nodeTab[ret->nodeNr++] = |
| 1774 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1775 | } else |
| 1776 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1777 | } |
| 1778 | return(ret); |
| 1779 | } |
| 1780 | |
| 1781 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1782 | * xmlXPathNodeSetContains: |
| 1783 | * @cur: the node-set |
| 1784 | * @val: the node |
| 1785 | * |
| 1786 | * checks whether @cur contains @val |
| 1787 | * |
| 1788 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1789 | */ |
| 1790 | int |
| 1791 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1792 | int i; |
| 1793 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1794 | if (val->type == XML_NAMESPACE_DECL) { |
| 1795 | for (i = 0; i < cur->nodeNr; i++) { |
| 1796 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1797 | xmlNsPtr ns1, ns2; |
| 1798 | |
| 1799 | ns1 = (xmlNsPtr) val; |
| 1800 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1801 | if (ns1 == ns2) |
| 1802 | return(1); |
| 1803 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1804 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1805 | return(1); |
| 1806 | } |
| 1807 | } |
| 1808 | } else { |
| 1809 | for (i = 0; i < cur->nodeNr; i++) { |
| 1810 | if (cur->nodeTab[i] == val) |
| 1811 | return(1); |
| 1812 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1813 | } |
| 1814 | return(0); |
| 1815 | } |
| 1816 | |
| 1817 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1818 | * xmlXPathNodeSetAddNs: |
| 1819 | * @cur: the initial node set |
| 1820 | * @node: the hosting node |
| 1821 | * @ns: a the namespace node |
| 1822 | * |
| 1823 | * add a new namespace node to an existing NodeSet |
| 1824 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1825 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1826 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1827 | int i; |
| 1828 | |
| 1829 | if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) || |
| 1830 | (node->type != XML_ELEMENT_NODE)) |
| 1831 | return; |
| 1832 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1833 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1834 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1835 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1836 | */ |
| 1837 | for (i = 0;i < cur->nodeNr;i++) { |
| 1838 | if ((cur->nodeTab[i] != NULL) && |
| 1839 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1840 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1841 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1842 | return; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * grow the nodeTab if needed |
| 1847 | */ |
| 1848 | if (cur->nodeMax == 0) { |
| 1849 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1850 | sizeof(xmlNodePtr)); |
| 1851 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1852 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1853 | return; |
| 1854 | } |
| 1855 | memset(cur->nodeTab, 0 , |
| 1856 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1857 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1858 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1859 | xmlNodePtr *temp; |
| 1860 | |
| 1861 | cur->nodeMax *= 2; |
| 1862 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1863 | sizeof(xmlNodePtr)); |
| 1864 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1865 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1866 | return; |
| 1867 | } |
| 1868 | cur->nodeTab = temp; |
| 1869 | } |
| 1870 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1874 | * xmlXPathNodeSetAdd: |
| 1875 | * @cur: the initial node set |
| 1876 | * @val: a new xmlNodePtr |
| 1877 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1878 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1879 | */ |
| 1880 | void |
| 1881 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1882 | int i; |
| 1883 | |
| 1884 | if (val == NULL) return; |
| 1885 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1886 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1887 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1888 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1889 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1890 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1891 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1892 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1893 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | */ |
| 1895 | for (i = 0;i < cur->nodeNr;i++) |
| 1896 | if (cur->nodeTab[i] == val) return; |
| 1897 | |
| 1898 | /* |
| 1899 | * grow the nodeTab if needed |
| 1900 | */ |
| 1901 | if (cur->nodeMax == 0) { |
| 1902 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1903 | sizeof(xmlNodePtr)); |
| 1904 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1905 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1906 | return; |
| 1907 | } |
| 1908 | memset(cur->nodeTab, 0 , |
| 1909 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1910 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1911 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1912 | xmlNodePtr *temp; |
| 1913 | |
| 1914 | cur->nodeMax *= 2; |
| 1915 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1916 | sizeof(xmlNodePtr)); |
| 1917 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1918 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1919 | return; |
| 1920 | } |
| 1921 | cur->nodeTab = temp; |
| 1922 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1923 | if (val->type == XML_NAMESPACE_DECL) { |
| 1924 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1925 | |
| 1926 | cur->nodeTab[cur->nodeNr++] = |
| 1927 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1928 | } else |
| 1929 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * xmlXPathNodeSetAddUnique: |
| 1934 | * @cur: the initial node set |
| 1935 | * @val: a new xmlNodePtr |
| 1936 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1937 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1938 | * when we are sure the node is not already in the set. |
| 1939 | */ |
| 1940 | void |
| 1941 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1942 | if (val == NULL) return; |
| 1943 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1944 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1945 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1946 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1947 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1948 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1949 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1950 | /* |
| 1951 | * grow the nodeTab if needed |
| 1952 | */ |
| 1953 | if (cur->nodeMax == 0) { |
| 1954 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1955 | sizeof(xmlNodePtr)); |
| 1956 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1957 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1958 | return; |
| 1959 | } |
| 1960 | memset(cur->nodeTab, 0 , |
| 1961 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1962 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1963 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1964 | xmlNodePtr *temp; |
| 1965 | |
| 1966 | cur->nodeMax *= 2; |
| 1967 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1968 | sizeof(xmlNodePtr)); |
| 1969 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1970 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1971 | return; |
| 1972 | } |
| 1973 | cur->nodeTab = temp; |
| 1974 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1975 | if (val->type == XML_NAMESPACE_DECL) { |
| 1976 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1977 | |
| 1978 | cur->nodeTab[cur->nodeNr++] = |
| 1979 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1980 | } else |
| 1981 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * xmlXPathNodeSetMerge: |
| 1986 | * @val1: the first NodeSet or NULL |
| 1987 | * @val2: the second NodeSet |
| 1988 | * |
| 1989 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 1990 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 1991 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1992 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1993 | */ |
| 1994 | xmlNodeSetPtr |
| 1995 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1996 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1997 | |
| 1998 | if (val2 == NULL) return(val1); |
| 1999 | if (val1 == NULL) { |
| 2000 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2001 | } |
| 2002 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2003 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2004 | initNr = val1->nodeNr; |
| 2005 | |
| 2006 | for (i = 0;i < val2->nodeNr;i++) { |
| 2007 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2008 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2009 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2010 | skip = 0; |
| 2011 | for (j = 0; j < initNr; j++) { |
| 2012 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 2013 | skip = 1; |
| 2014 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2015 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 2016 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 2017 | xmlNsPtr ns1, ns2; |
| 2018 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 2019 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 2020 | if ((ns1->next == ns2->next) && |
| 2021 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 2022 | skip = 1; |
| 2023 | break; |
| 2024 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | if (skip) |
| 2028 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | |
| 2030 | /* |
| 2031 | * grow the nodeTab if needed |
| 2032 | */ |
| 2033 | if (val1->nodeMax == 0) { |
| 2034 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2035 | sizeof(xmlNodePtr)); |
| 2036 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2037 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2038 | return(NULL); |
| 2039 | } |
| 2040 | memset(val1->nodeTab, 0 , |
| 2041 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2042 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2043 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2044 | xmlNodePtr *temp; |
| 2045 | |
| 2046 | val1->nodeMax *= 2; |
| 2047 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2048 | sizeof(xmlNodePtr)); |
| 2049 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2050 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2051 | return(NULL); |
| 2052 | } |
| 2053 | val1->nodeTab = temp; |
| 2054 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2055 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2056 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2057 | |
| 2058 | val1->nodeTab[val1->nodeNr++] = |
| 2059 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2060 | } else |
| 2061 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | return(val1); |
| 2065 | } |
| 2066 | |
| 2067 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2068 | * xmlXPathNodeSetMergeUnique: |
| 2069 | * @val1: the first NodeSet or NULL |
| 2070 | * @val2: the second NodeSet |
| 2071 | * |
| 2072 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2073 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2074 | * |
| 2075 | * Returns @val1 once extended or NULL in case of error. |
| 2076 | */ |
| 2077 | static xmlNodeSetPtr |
| 2078 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2079 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2080 | |
| 2081 | if (val2 == NULL) return(val1); |
| 2082 | if (val1 == NULL) { |
| 2083 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2084 | } |
| 2085 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2086 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2087 | |
| 2088 | for (i = 0;i < val2->nodeNr;i++) { |
| 2089 | /* |
| 2090 | * grow the nodeTab if needed |
| 2091 | */ |
| 2092 | if (val1->nodeMax == 0) { |
| 2093 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2094 | sizeof(xmlNodePtr)); |
| 2095 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2096 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2097 | return(NULL); |
| 2098 | } |
| 2099 | memset(val1->nodeTab, 0 , |
| 2100 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2101 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2102 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2103 | xmlNodePtr *temp; |
| 2104 | |
| 2105 | val1->nodeMax *= 2; |
| 2106 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2107 | sizeof(xmlNodePtr)); |
| 2108 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2109 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2110 | return(NULL); |
| 2111 | } |
| 2112 | val1->nodeTab = temp; |
| 2113 | } |
| 2114 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2115 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2116 | |
| 2117 | val1->nodeTab[val1->nodeNr++] = |
| 2118 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2119 | } else |
| 2120 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2121 | } |
| 2122 | |
| 2123 | return(val1); |
| 2124 | } |
| 2125 | |
| 2126 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | * xmlXPathNodeSetDel: |
| 2128 | * @cur: the initial node set |
| 2129 | * @val: an xmlNodePtr |
| 2130 | * |
| 2131 | * Removes an xmlNodePtr from an existing NodeSet |
| 2132 | */ |
| 2133 | void |
| 2134 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2135 | int i; |
| 2136 | |
| 2137 | if (cur == NULL) return; |
| 2138 | if (val == NULL) return; |
| 2139 | |
| 2140 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2141 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2142 | */ |
| 2143 | for (i = 0;i < cur->nodeNr;i++) |
| 2144 | if (cur->nodeTab[i] == val) break; |
| 2145 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2146 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2147 | #ifdef DEBUG |
| 2148 | xmlGenericError(xmlGenericErrorContext, |
| 2149 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2150 | val->name); |
| 2151 | #endif |
| 2152 | return; |
| 2153 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2154 | if ((cur->nodeTab[i] != NULL) && |
| 2155 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2156 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2157 | cur->nodeNr--; |
| 2158 | for (;i < cur->nodeNr;i++) |
| 2159 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2160 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2161 | } |
| 2162 | |
| 2163 | /** |
| 2164 | * xmlXPathNodeSetRemove: |
| 2165 | * @cur: the initial node set |
| 2166 | * @val: the index to remove |
| 2167 | * |
| 2168 | * Removes an entry from an existing NodeSet list. |
| 2169 | */ |
| 2170 | void |
| 2171 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2172 | if (cur == NULL) return; |
| 2173 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2174 | if ((cur->nodeTab[val] != NULL) && |
| 2175 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2176 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2177 | cur->nodeNr--; |
| 2178 | for (;val < cur->nodeNr;val++) |
| 2179 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2180 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2181 | } |
| 2182 | |
| 2183 | /** |
| 2184 | * xmlXPathFreeNodeSet: |
| 2185 | * @obj: the xmlNodeSetPtr to free |
| 2186 | * |
| 2187 | * Free the NodeSet compound (not the actual nodes !). |
| 2188 | */ |
| 2189 | void |
| 2190 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2191 | if (obj == NULL) return; |
| 2192 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2193 | int i; |
| 2194 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2195 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2196 | for (i = 0;i < obj->nodeNr;i++) |
| 2197 | if ((obj->nodeTab[i] != NULL) && |
| 2198 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2199 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | xmlFree(obj->nodeTab); |
| 2201 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2202 | xmlFree(obj); |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * xmlXPathFreeValueTree: |
| 2207 | * @obj: the xmlNodeSetPtr to free |
| 2208 | * |
| 2209 | * Free the NodeSet compound and the actual tree, this is different |
| 2210 | * from xmlXPathFreeNodeSet() |
| 2211 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2212 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2213 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2214 | int i; |
| 2215 | |
| 2216 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2219 | for (i = 0;i < obj->nodeNr;i++) { |
| 2220 | if (obj->nodeTab[i] != NULL) { |
| 2221 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2222 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2223 | } else { |
| 2224 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2225 | } |
| 2226 | } |
| 2227 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2228 | xmlFree(obj->nodeTab); |
| 2229 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2230 | xmlFree(obj); |
| 2231 | } |
| 2232 | |
| 2233 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2234 | /** |
| 2235 | * xmlGenericErrorContextNodeSet: |
| 2236 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2237 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2238 | * |
| 2239 | * Quick display of a NodeSet |
| 2240 | */ |
| 2241 | void |
| 2242 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2243 | int i; |
| 2244 | |
| 2245 | if (output == NULL) output = xmlGenericErrorContext; |
| 2246 | if (obj == NULL) { |
| 2247 | fprintf(output, "NodeSet == NULL !\n"); |
| 2248 | return; |
| 2249 | } |
| 2250 | if (obj->nodeNr == 0) { |
| 2251 | fprintf(output, "NodeSet is empty\n"); |
| 2252 | return; |
| 2253 | } |
| 2254 | if (obj->nodeTab == NULL) { |
| 2255 | fprintf(output, " nodeTab == NULL !\n"); |
| 2256 | return; |
| 2257 | } |
| 2258 | for (i = 0; i < obj->nodeNr; i++) { |
| 2259 | if (obj->nodeTab[i] == NULL) { |
| 2260 | fprintf(output, " NULL !\n"); |
| 2261 | return; |
| 2262 | } |
| 2263 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2264 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2265 | fprintf(output, " /"); |
| 2266 | else if (obj->nodeTab[i]->name == NULL) |
| 2267 | fprintf(output, " noname!"); |
| 2268 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2269 | } |
| 2270 | fprintf(output, "\n"); |
| 2271 | } |
| 2272 | #endif |
| 2273 | |
| 2274 | /** |
| 2275 | * xmlXPathNewNodeSet: |
| 2276 | * @val: the NodePtr value |
| 2277 | * |
| 2278 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2279 | * it with the single Node @val |
| 2280 | * |
| 2281 | * Returns the newly created object. |
| 2282 | */ |
| 2283 | xmlXPathObjectPtr |
| 2284 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2285 | xmlXPathObjectPtr ret; |
| 2286 | |
| 2287 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2288 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2289 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2290 | return(NULL); |
| 2291 | } |
| 2292 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2293 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2294 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2295 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2296 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2297 | return(ret); |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * xmlXPathNewValueTree: |
| 2302 | * @val: the NodePtr value |
| 2303 | * |
| 2304 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2305 | * it with the tree root @val |
| 2306 | * |
| 2307 | * Returns the newly created object. |
| 2308 | */ |
| 2309 | xmlXPathObjectPtr |
| 2310 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2311 | xmlXPathObjectPtr ret; |
| 2312 | |
| 2313 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2314 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2315 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2316 | return(NULL); |
| 2317 | } |
| 2318 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2319 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2320 | ret->boolval = 1; |
| 2321 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2322 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2323 | return(ret); |
| 2324 | } |
| 2325 | |
| 2326 | /** |
| 2327 | * xmlXPathNewNodeSetList: |
| 2328 | * @val: an existing NodeSet |
| 2329 | * |
| 2330 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2331 | * it with the Nodeset @val |
| 2332 | * |
| 2333 | * Returns the newly created object. |
| 2334 | */ |
| 2335 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2336 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2337 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2338 | xmlXPathObjectPtr ret; |
| 2339 | int i; |
| 2340 | |
| 2341 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2342 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2344 | ret = xmlXPathNewNodeSet(NULL); |
| 2345 | else { |
| 2346 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2347 | for (i = 1; i < val->nodeNr; ++i) |
| 2348 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2349 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2350 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2351 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | /** |
| 2355 | * xmlXPathWrapNodeSet: |
| 2356 | * @val: the NodePtr value |
| 2357 | * |
| 2358 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2359 | * |
| 2360 | * Returns the newly created object. |
| 2361 | */ |
| 2362 | xmlXPathObjectPtr |
| 2363 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2364 | xmlXPathObjectPtr ret; |
| 2365 | |
| 2366 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2367 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2368 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | return(NULL); |
| 2370 | } |
| 2371 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2372 | ret->type = XPATH_NODESET; |
| 2373 | ret->nodesetval = val; |
| 2374 | return(ret); |
| 2375 | } |
| 2376 | |
| 2377 | /** |
| 2378 | * xmlXPathFreeNodeSetList: |
| 2379 | * @obj: an existing NodeSetList object |
| 2380 | * |
| 2381 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2382 | * the list contrary to xmlXPathFreeObject(). |
| 2383 | */ |
| 2384 | void |
| 2385 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2386 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2387 | xmlFree(obj); |
| 2388 | } |
| 2389 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2390 | /** |
| 2391 | * xmlXPathDifference: |
| 2392 | * @nodes1: a node-set |
| 2393 | * @nodes2: a node-set |
| 2394 | * |
| 2395 | * Implements the EXSLT - Sets difference() function: |
| 2396 | * node-set set:difference (node-set, node-set) |
| 2397 | * |
| 2398 | * Returns the difference between the two node sets, or nodes1 if |
| 2399 | * nodes2 is empty |
| 2400 | */ |
| 2401 | xmlNodeSetPtr |
| 2402 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2403 | xmlNodeSetPtr ret; |
| 2404 | int i, l1; |
| 2405 | xmlNodePtr cur; |
| 2406 | |
| 2407 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2408 | return(nodes1); |
| 2409 | |
| 2410 | ret = xmlXPathNodeSetCreate(NULL); |
| 2411 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2412 | return(ret); |
| 2413 | |
| 2414 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2415 | |
| 2416 | for (i = 0; i < l1; i++) { |
| 2417 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2418 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2419 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2420 | } |
| 2421 | return(ret); |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * xmlXPathIntersection: |
| 2426 | * @nodes1: a node-set |
| 2427 | * @nodes2: a node-set |
| 2428 | * |
| 2429 | * Implements the EXSLT - Sets intersection() function: |
| 2430 | * node-set set:intersection (node-set, node-set) |
| 2431 | * |
| 2432 | * Returns a node set comprising the nodes that are within both the |
| 2433 | * node sets passed as arguments |
| 2434 | */ |
| 2435 | xmlNodeSetPtr |
| 2436 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2437 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2438 | int i, l1; |
| 2439 | xmlNodePtr cur; |
| 2440 | |
| 2441 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2442 | return(ret); |
| 2443 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2444 | return(ret); |
| 2445 | |
| 2446 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2447 | |
| 2448 | for (i = 0; i < l1; i++) { |
| 2449 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2450 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2451 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2452 | } |
| 2453 | return(ret); |
| 2454 | } |
| 2455 | |
| 2456 | /** |
| 2457 | * xmlXPathDistinctSorted: |
| 2458 | * @nodes: a node-set, sorted by document order |
| 2459 | * |
| 2460 | * Implements the EXSLT - Sets distinct() function: |
| 2461 | * node-set set:distinct (node-set) |
| 2462 | * |
| 2463 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2464 | * it is empty |
| 2465 | */ |
| 2466 | xmlNodeSetPtr |
| 2467 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2468 | xmlNodeSetPtr ret; |
| 2469 | xmlHashTablePtr hash; |
| 2470 | int i, l; |
| 2471 | xmlChar * strval; |
| 2472 | xmlNodePtr cur; |
| 2473 | |
| 2474 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2475 | return(nodes); |
| 2476 | |
| 2477 | ret = xmlXPathNodeSetCreate(NULL); |
| 2478 | l = xmlXPathNodeSetGetLength(nodes); |
| 2479 | hash = xmlHashCreate (l); |
| 2480 | for (i = 0; i < l; i++) { |
| 2481 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2482 | strval = xmlXPathCastNodeToString(cur); |
| 2483 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2484 | xmlHashAddEntry(hash, strval, strval); |
| 2485 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2486 | } else { |
| 2487 | xmlFree(strval); |
| 2488 | } |
| 2489 | } |
| 2490 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2491 | return(ret); |
| 2492 | } |
| 2493 | |
| 2494 | /** |
| 2495 | * xmlXPathDistinct: |
| 2496 | * @nodes: a node-set |
| 2497 | * |
| 2498 | * Implements the EXSLT - Sets distinct() function: |
| 2499 | * node-set set:distinct (node-set) |
| 2500 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2501 | * is called with the sorted node-set |
| 2502 | * |
| 2503 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2504 | * it is empty |
| 2505 | */ |
| 2506 | xmlNodeSetPtr |
| 2507 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2508 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2509 | return(nodes); |
| 2510 | |
| 2511 | xmlXPathNodeSetSort(nodes); |
| 2512 | return(xmlXPathDistinctSorted(nodes)); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * xmlXPathHasSameNodes: |
| 2517 | * @nodes1: a node-set |
| 2518 | * @nodes2: a node-set |
| 2519 | * |
| 2520 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2521 | * boolean set:has-same-node(node-set, node-set) |
| 2522 | * |
| 2523 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2524 | * otherwise |
| 2525 | */ |
| 2526 | int |
| 2527 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2528 | int i, l; |
| 2529 | xmlNodePtr cur; |
| 2530 | |
| 2531 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2532 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2533 | return(0); |
| 2534 | |
| 2535 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2536 | for (i = 0; i < l; i++) { |
| 2537 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2538 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2539 | return(1); |
| 2540 | } |
| 2541 | return(0); |
| 2542 | } |
| 2543 | |
| 2544 | /** |
| 2545 | * xmlXPathNodeLeadingSorted: |
| 2546 | * @nodes: a node-set, sorted by document order |
| 2547 | * @node: a node |
| 2548 | * |
| 2549 | * Implements the EXSLT - Sets leading() function: |
| 2550 | * node-set set:leading (node-set, node-set) |
| 2551 | * |
| 2552 | * Returns the nodes in @nodes that precede @node in document order, |
| 2553 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2554 | * doesn't contain @node |
| 2555 | */ |
| 2556 | xmlNodeSetPtr |
| 2557 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2558 | int i, l; |
| 2559 | xmlNodePtr cur; |
| 2560 | xmlNodeSetPtr ret; |
| 2561 | |
| 2562 | if (node == NULL) |
| 2563 | return(nodes); |
| 2564 | |
| 2565 | ret = xmlXPathNodeSetCreate(NULL); |
| 2566 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2567 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2568 | return(ret); |
| 2569 | |
| 2570 | l = xmlXPathNodeSetGetLength(nodes); |
| 2571 | for (i = 0; i < l; i++) { |
| 2572 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2573 | if (cur == node) |
| 2574 | break; |
| 2575 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2576 | } |
| 2577 | return(ret); |
| 2578 | } |
| 2579 | |
| 2580 | /** |
| 2581 | * xmlXPathNodeLeading: |
| 2582 | * @nodes: a node-set |
| 2583 | * @node: a node |
| 2584 | * |
| 2585 | * Implements the EXSLT - Sets leading() function: |
| 2586 | * node-set set:leading (node-set, node-set) |
| 2587 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2588 | * is called. |
| 2589 | * |
| 2590 | * Returns the nodes in @nodes that precede @node in document order, |
| 2591 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2592 | * doesn't contain @node |
| 2593 | */ |
| 2594 | xmlNodeSetPtr |
| 2595 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2596 | xmlXPathNodeSetSort(nodes); |
| 2597 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2598 | } |
| 2599 | |
| 2600 | /** |
| 2601 | * xmlXPathLeadingSorted: |
| 2602 | * @nodes1: a node-set, sorted by document order |
| 2603 | * @nodes2: a node-set, sorted by document order |
| 2604 | * |
| 2605 | * Implements the EXSLT - Sets leading() function: |
| 2606 | * node-set set:leading (node-set, node-set) |
| 2607 | * |
| 2608 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2609 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2610 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2611 | */ |
| 2612 | xmlNodeSetPtr |
| 2613 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2614 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2615 | return(nodes1); |
| 2616 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2617 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2618 | } |
| 2619 | |
| 2620 | /** |
| 2621 | * xmlXPathLeading: |
| 2622 | * @nodes1: a node-set |
| 2623 | * @nodes2: a node-set |
| 2624 | * |
| 2625 | * Implements the EXSLT - Sets leading() function: |
| 2626 | * node-set set:leading (node-set, node-set) |
| 2627 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2628 | * #exslSetsLeadingSorted is called. |
| 2629 | * |
| 2630 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2631 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2632 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2633 | */ |
| 2634 | xmlNodeSetPtr |
| 2635 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2636 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2637 | return(nodes1); |
| 2638 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2639 | return(xmlXPathNodeSetCreate(NULL)); |
| 2640 | xmlXPathNodeSetSort(nodes1); |
| 2641 | xmlXPathNodeSetSort(nodes2); |
| 2642 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2643 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * xmlXPathNodeTrailingSorted: |
| 2648 | * @nodes: a node-set, sorted by document order |
| 2649 | * @node: a node |
| 2650 | * |
| 2651 | * Implements the EXSLT - Sets trailing() function: |
| 2652 | * node-set set:trailing (node-set, node-set) |
| 2653 | * |
| 2654 | * Returns the nodes in @nodes that follow @node in document order, |
| 2655 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2656 | * doesn't contain @node |
| 2657 | */ |
| 2658 | xmlNodeSetPtr |
| 2659 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2660 | int i, l; |
| 2661 | xmlNodePtr cur; |
| 2662 | xmlNodeSetPtr ret; |
| 2663 | |
| 2664 | if (node == NULL) |
| 2665 | return(nodes); |
| 2666 | |
| 2667 | ret = xmlXPathNodeSetCreate(NULL); |
| 2668 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2669 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2670 | return(ret); |
| 2671 | |
| 2672 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2673 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2674 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2675 | if (cur == node) |
| 2676 | break; |
| 2677 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2678 | } |
| 2679 | return(ret); |
| 2680 | } |
| 2681 | |
| 2682 | /** |
| 2683 | * xmlXPathNodeTrailing: |
| 2684 | * @nodes: a node-set |
| 2685 | * @node: a node |
| 2686 | * |
| 2687 | * Implements the EXSLT - Sets trailing() function: |
| 2688 | * node-set set:trailing (node-set, node-set) |
| 2689 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2690 | * is called. |
| 2691 | * |
| 2692 | * Returns the nodes in @nodes that follow @node in document order, |
| 2693 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2694 | * doesn't contain @node |
| 2695 | */ |
| 2696 | xmlNodeSetPtr |
| 2697 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2698 | xmlXPathNodeSetSort(nodes); |
| 2699 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2700 | } |
| 2701 | |
| 2702 | /** |
| 2703 | * xmlXPathTrailingSorted: |
| 2704 | * @nodes1: a node-set, sorted by document order |
| 2705 | * @nodes2: a node-set, sorted by document order |
| 2706 | * |
| 2707 | * Implements the EXSLT - Sets trailing() function: |
| 2708 | * node-set set:trailing (node-set, node-set) |
| 2709 | * |
| 2710 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2711 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2712 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2713 | */ |
| 2714 | xmlNodeSetPtr |
| 2715 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2716 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2717 | return(nodes1); |
| 2718 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2719 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2720 | } |
| 2721 | |
| 2722 | /** |
| 2723 | * xmlXPathTrailing: |
| 2724 | * @nodes1: a node-set |
| 2725 | * @nodes2: a node-set |
| 2726 | * |
| 2727 | * Implements the EXSLT - Sets trailing() function: |
| 2728 | * node-set set:trailing (node-set, node-set) |
| 2729 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2730 | * #xmlXPathTrailingSorted is called. |
| 2731 | * |
| 2732 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2733 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2734 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2735 | */ |
| 2736 | xmlNodeSetPtr |
| 2737 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2738 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2739 | return(nodes1); |
| 2740 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2741 | return(xmlXPathNodeSetCreate(NULL)); |
| 2742 | xmlXPathNodeSetSort(nodes1); |
| 2743 | xmlXPathNodeSetSort(nodes2); |
| 2744 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2745 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2746 | } |
| 2747 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2748 | /************************************************************************ |
| 2749 | * * |
| 2750 | * Routines to handle extra functions * |
| 2751 | * * |
| 2752 | ************************************************************************/ |
| 2753 | |
| 2754 | /** |
| 2755 | * xmlXPathRegisterFunc: |
| 2756 | * @ctxt: the XPath context |
| 2757 | * @name: the function name |
| 2758 | * @f: the function implementation or NULL |
| 2759 | * |
| 2760 | * Register a new function. If @f is NULL it unregisters the function |
| 2761 | * |
| 2762 | * Returns 0 in case of success, -1 in case of error |
| 2763 | */ |
| 2764 | int |
| 2765 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2766 | xmlXPathFunction f) { |
| 2767 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2768 | } |
| 2769 | |
| 2770 | /** |
| 2771 | * xmlXPathRegisterFuncNS: |
| 2772 | * @ctxt: the XPath context |
| 2773 | * @name: the function name |
| 2774 | * @ns_uri: the function namespace URI |
| 2775 | * @f: the function implementation or NULL |
| 2776 | * |
| 2777 | * Register a new function. If @f is NULL it unregisters the function |
| 2778 | * |
| 2779 | * Returns 0 in case of success, -1 in case of error |
| 2780 | */ |
| 2781 | int |
| 2782 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2783 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2784 | if (ctxt == NULL) |
| 2785 | return(-1); |
| 2786 | if (name == NULL) |
| 2787 | return(-1); |
| 2788 | |
| 2789 | if (ctxt->funcHash == NULL) |
| 2790 | ctxt->funcHash = xmlHashCreate(0); |
| 2791 | if (ctxt->funcHash == NULL) |
| 2792 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2793 | if (f == NULL) |
| 2794 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2795 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
| 2796 | } |
| 2797 | |
| 2798 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2799 | * xmlXPathRegisterFuncLookup: |
| 2800 | * @ctxt: the XPath context |
| 2801 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2802 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2803 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2804 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2805 | */ |
| 2806 | void |
| 2807 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2808 | xmlXPathFuncLookupFunc f, |
| 2809 | void *funcCtxt) { |
| 2810 | if (ctxt == NULL) |
| 2811 | return; |
| 2812 | ctxt->funcLookupFunc = (void *) f; |
| 2813 | ctxt->funcLookupData = funcCtxt; |
| 2814 | } |
| 2815 | |
| 2816 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | * xmlXPathFunctionLookup: |
| 2818 | * @ctxt: the XPath context |
| 2819 | * @name: the function name |
| 2820 | * |
| 2821 | * Search in the Function array of the context for the given |
| 2822 | * function. |
| 2823 | * |
| 2824 | * Returns the xmlXPathFunction or NULL if not found |
| 2825 | */ |
| 2826 | xmlXPathFunction |
| 2827 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2828 | if (ctxt == NULL) |
| 2829 | return (NULL); |
| 2830 | |
| 2831 | if (ctxt->funcLookupFunc != NULL) { |
| 2832 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2833 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2834 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2835 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2836 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2837 | if (ret != NULL) |
| 2838 | return(ret); |
| 2839 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2840 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * xmlXPathFunctionLookupNS: |
| 2845 | * @ctxt: the XPath context |
| 2846 | * @name: the function name |
| 2847 | * @ns_uri: the function namespace URI |
| 2848 | * |
| 2849 | * Search in the Function array of the context for the given |
| 2850 | * function. |
| 2851 | * |
| 2852 | * Returns the xmlXPathFunction or NULL if not found |
| 2853 | */ |
| 2854 | xmlXPathFunction |
| 2855 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2856 | const xmlChar *ns_uri) { |
| 2857 | if (ctxt == NULL) |
| 2858 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2859 | if (name == NULL) |
| 2860 | return(NULL); |
| 2861 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2862 | if (ctxt->funcLookupFunc != NULL) { |
| 2863 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2864 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2865 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2866 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2867 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2868 | if (ret != NULL) |
| 2869 | return(ret); |
| 2870 | } |
| 2871 | |
| 2872 | if (ctxt->funcHash == NULL) |
| 2873 | return(NULL); |
| 2874 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2875 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
| 2876 | } |
| 2877 | |
| 2878 | /** |
| 2879 | * xmlXPathRegisteredFuncsCleanup: |
| 2880 | * @ctxt: the XPath context |
| 2881 | * |
| 2882 | * Cleanup the XPath context data associated to registered functions |
| 2883 | */ |
| 2884 | void |
| 2885 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2886 | if (ctxt == NULL) |
| 2887 | return; |
| 2888 | |
| 2889 | xmlHashFree(ctxt->funcHash, NULL); |
| 2890 | ctxt->funcHash = NULL; |
| 2891 | } |
| 2892 | |
| 2893 | /************************************************************************ |
| 2894 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2895 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2896 | * * |
| 2897 | ************************************************************************/ |
| 2898 | |
| 2899 | /** |
| 2900 | * xmlXPathRegisterVariable: |
| 2901 | * @ctxt: the XPath context |
| 2902 | * @name: the variable name |
| 2903 | * @value: the variable value or NULL |
| 2904 | * |
| 2905 | * Register a new variable value. If @value is NULL it unregisters |
| 2906 | * the variable |
| 2907 | * |
| 2908 | * Returns 0 in case of success, -1 in case of error |
| 2909 | */ |
| 2910 | int |
| 2911 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2912 | xmlXPathObjectPtr value) { |
| 2913 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2914 | } |
| 2915 | |
| 2916 | /** |
| 2917 | * xmlXPathRegisterVariableNS: |
| 2918 | * @ctxt: the XPath context |
| 2919 | * @name: the variable name |
| 2920 | * @ns_uri: the variable namespace URI |
| 2921 | * @value: the variable value or NULL |
| 2922 | * |
| 2923 | * Register a new variable value. If @value is NULL it unregisters |
| 2924 | * the variable |
| 2925 | * |
| 2926 | * Returns 0 in case of success, -1 in case of error |
| 2927 | */ |
| 2928 | int |
| 2929 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2930 | const xmlChar *ns_uri, |
| 2931 | xmlXPathObjectPtr value) { |
| 2932 | if (ctxt == NULL) |
| 2933 | return(-1); |
| 2934 | if (name == NULL) |
| 2935 | return(-1); |
| 2936 | |
| 2937 | if (ctxt->varHash == NULL) |
| 2938 | ctxt->varHash = xmlHashCreate(0); |
| 2939 | if (ctxt->varHash == NULL) |
| 2940 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2941 | if (value == NULL) |
| 2942 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 2943 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2944 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2945 | (void *) value, |
| 2946 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2947 | } |
| 2948 | |
| 2949 | /** |
| 2950 | * xmlXPathRegisterVariableLookup: |
| 2951 | * @ctxt: the XPath context |
| 2952 | * @f: the lookup function |
| 2953 | * @data: the lookup data |
| 2954 | * |
| 2955 | * register an external mechanism to do variable lookup |
| 2956 | */ |
| 2957 | void |
| 2958 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 2959 | xmlXPathVariableLookupFunc f, void *data) { |
| 2960 | if (ctxt == NULL) |
| 2961 | return; |
| 2962 | ctxt->varLookupFunc = (void *) f; |
| 2963 | ctxt->varLookupData = data; |
| 2964 | } |
| 2965 | |
| 2966 | /** |
| 2967 | * xmlXPathVariableLookup: |
| 2968 | * @ctxt: the XPath context |
| 2969 | * @name: the variable name |
| 2970 | * |
| 2971 | * Search in the Variable array of the context for the given |
| 2972 | * variable value. |
| 2973 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2974 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2975 | */ |
| 2976 | xmlXPathObjectPtr |
| 2977 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 2978 | if (ctxt == NULL) |
| 2979 | return(NULL); |
| 2980 | |
| 2981 | if (ctxt->varLookupFunc != NULL) { |
| 2982 | xmlXPathObjectPtr ret; |
| 2983 | |
| 2984 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 2985 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 2986 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2987 | } |
| 2988 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * xmlXPathVariableLookupNS: |
| 2993 | * @ctxt: the XPath context |
| 2994 | * @name: the variable name |
| 2995 | * @ns_uri: the variable namespace URI |
| 2996 | * |
| 2997 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2998 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2999 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3000 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3001 | */ |
| 3002 | xmlXPathObjectPtr |
| 3003 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3004 | const xmlChar *ns_uri) { |
| 3005 | if (ctxt == NULL) |
| 3006 | return(NULL); |
| 3007 | |
| 3008 | if (ctxt->varLookupFunc != NULL) { |
| 3009 | xmlXPathObjectPtr ret; |
| 3010 | |
| 3011 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3012 | (ctxt->varLookupData, name, ns_uri); |
| 3013 | if (ret != NULL) return(ret); |
| 3014 | } |
| 3015 | |
| 3016 | if (ctxt->varHash == NULL) |
| 3017 | return(NULL); |
| 3018 | if (name == NULL) |
| 3019 | return(NULL); |
| 3020 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 3021 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 3022 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /** |
| 3026 | * xmlXPathRegisteredVariablesCleanup: |
| 3027 | * @ctxt: the XPath context |
| 3028 | * |
| 3029 | * Cleanup the XPath context data associated to registered variables |
| 3030 | */ |
| 3031 | void |
| 3032 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 3033 | if (ctxt == NULL) |
| 3034 | return; |
| 3035 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3036 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | ctxt->varHash = NULL; |
| 3038 | } |
| 3039 | |
| 3040 | /** |
| 3041 | * xmlXPathRegisterNs: |
| 3042 | * @ctxt: the XPath context |
| 3043 | * @prefix: the namespace prefix |
| 3044 | * @ns_uri: the namespace name |
| 3045 | * |
| 3046 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 3047 | * the namespace |
| 3048 | * |
| 3049 | * Returns 0 in case of success, -1 in case of error |
| 3050 | */ |
| 3051 | int |
| 3052 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3053 | const xmlChar *ns_uri) { |
| 3054 | if (ctxt == NULL) |
| 3055 | return(-1); |
| 3056 | if (prefix == NULL) |
| 3057 | return(-1); |
| 3058 | |
| 3059 | if (ctxt->nsHash == NULL) |
| 3060 | ctxt->nsHash = xmlHashCreate(10); |
| 3061 | if (ctxt->nsHash == NULL) |
| 3062 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3063 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3064 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3065 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3066 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | (xmlHashDeallocator)xmlFree)); |
| 3068 | } |
| 3069 | |
| 3070 | /** |
| 3071 | * xmlXPathNsLookup: |
| 3072 | * @ctxt: the XPath context |
| 3073 | * @prefix: the namespace prefix value |
| 3074 | * |
| 3075 | * Search in the namespace declaration array of the context for the given |
| 3076 | * namespace name associated to the given prefix |
| 3077 | * |
| 3078 | * Returns the value or NULL if not found |
| 3079 | */ |
| 3080 | const xmlChar * |
| 3081 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3082 | if (ctxt == NULL) |
| 3083 | return(NULL); |
| 3084 | if (prefix == NULL) |
| 3085 | return(NULL); |
| 3086 | |
| 3087 | #ifdef XML_XML_NAMESPACE |
| 3088 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3089 | return(XML_XML_NAMESPACE); |
| 3090 | #endif |
| 3091 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3092 | if (ctxt->namespaces != NULL) { |
| 3093 | int i; |
| 3094 | |
| 3095 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3096 | if ((ctxt->namespaces[i] != NULL) && |
| 3097 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3098 | return(ctxt->namespaces[i]->href); |
| 3099 | } |
| 3100 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3101 | |
| 3102 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3103 | } |
| 3104 | |
| 3105 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3106 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3107 | * @ctxt: the XPath context |
| 3108 | * |
| 3109 | * Cleanup the XPath context data associated to registered variables |
| 3110 | */ |
| 3111 | void |
| 3112 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3113 | if (ctxt == NULL) |
| 3114 | return; |
| 3115 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3116 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | ctxt->nsHash = NULL; |
| 3118 | } |
| 3119 | |
| 3120 | /************************************************************************ |
| 3121 | * * |
| 3122 | * Routines to handle Values * |
| 3123 | * * |
| 3124 | ************************************************************************/ |
| 3125 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3126 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3127 | |
| 3128 | /** |
| 3129 | * xmlXPathNewFloat: |
| 3130 | * @val: the double value |
| 3131 | * |
| 3132 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3133 | * |
| 3134 | * Returns the newly created object. |
| 3135 | */ |
| 3136 | xmlXPathObjectPtr |
| 3137 | xmlXPathNewFloat(double val) { |
| 3138 | xmlXPathObjectPtr ret; |
| 3139 | |
| 3140 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3141 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3142 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3143 | return(NULL); |
| 3144 | } |
| 3145 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3146 | ret->type = XPATH_NUMBER; |
| 3147 | ret->floatval = val; |
| 3148 | return(ret); |
| 3149 | } |
| 3150 | |
| 3151 | /** |
| 3152 | * xmlXPathNewBoolean: |
| 3153 | * @val: the boolean value |
| 3154 | * |
| 3155 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3156 | * |
| 3157 | * Returns the newly created object. |
| 3158 | */ |
| 3159 | xmlXPathObjectPtr |
| 3160 | xmlXPathNewBoolean(int val) { |
| 3161 | xmlXPathObjectPtr ret; |
| 3162 | |
| 3163 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3164 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3165 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3166 | return(NULL); |
| 3167 | } |
| 3168 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3169 | ret->type = XPATH_BOOLEAN; |
| 3170 | ret->boolval = (val != 0); |
| 3171 | return(ret); |
| 3172 | } |
| 3173 | |
| 3174 | /** |
| 3175 | * xmlXPathNewString: |
| 3176 | * @val: the xmlChar * value |
| 3177 | * |
| 3178 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3179 | * |
| 3180 | * Returns the newly created object. |
| 3181 | */ |
| 3182 | xmlXPathObjectPtr |
| 3183 | xmlXPathNewString(const xmlChar *val) { |
| 3184 | xmlXPathObjectPtr ret; |
| 3185 | |
| 3186 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3187 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3188 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3189 | return(NULL); |
| 3190 | } |
| 3191 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3192 | ret->type = XPATH_STRING; |
| 3193 | if (val != NULL) |
| 3194 | ret->stringval = xmlStrdup(val); |
| 3195 | else |
| 3196 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3197 | return(ret); |
| 3198 | } |
| 3199 | |
| 3200 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3201 | * xmlXPathWrapString: |
| 3202 | * @val: the xmlChar * value |
| 3203 | * |
| 3204 | * Wraps the @val string into an XPath object. |
| 3205 | * |
| 3206 | * Returns the newly created object. |
| 3207 | */ |
| 3208 | xmlXPathObjectPtr |
| 3209 | xmlXPathWrapString (xmlChar *val) { |
| 3210 | xmlXPathObjectPtr ret; |
| 3211 | |
| 3212 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3213 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3214 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3215 | return(NULL); |
| 3216 | } |
| 3217 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3218 | ret->type = XPATH_STRING; |
| 3219 | ret->stringval = val; |
| 3220 | return(ret); |
| 3221 | } |
| 3222 | |
| 3223 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3224 | * xmlXPathNewCString: |
| 3225 | * @val: the char * value |
| 3226 | * |
| 3227 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3228 | * |
| 3229 | * Returns the newly created object. |
| 3230 | */ |
| 3231 | xmlXPathObjectPtr |
| 3232 | xmlXPathNewCString(const char *val) { |
| 3233 | xmlXPathObjectPtr ret; |
| 3234 | |
| 3235 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3236 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3237 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3238 | return(NULL); |
| 3239 | } |
| 3240 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3241 | ret->type = XPATH_STRING; |
| 3242 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3243 | return(ret); |
| 3244 | } |
| 3245 | |
| 3246 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3247 | * xmlXPathWrapCString: |
| 3248 | * @val: the char * value |
| 3249 | * |
| 3250 | * Wraps a string into an XPath object. |
| 3251 | * |
| 3252 | * Returns the newly created object. |
| 3253 | */ |
| 3254 | xmlXPathObjectPtr |
| 3255 | xmlXPathWrapCString (char * val) { |
| 3256 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3257 | } |
| 3258 | |
| 3259 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3260 | * xmlXPathWrapExternal: |
| 3261 | * @val: the user data |
| 3262 | * |
| 3263 | * Wraps the @val data into an XPath object. |
| 3264 | * |
| 3265 | * Returns the newly created object. |
| 3266 | */ |
| 3267 | xmlXPathObjectPtr |
| 3268 | xmlXPathWrapExternal (void *val) { |
| 3269 | xmlXPathObjectPtr ret; |
| 3270 | |
| 3271 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3272 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3273 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3274 | return(NULL); |
| 3275 | } |
| 3276 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3277 | ret->type = XPATH_USERS; |
| 3278 | ret->user = val; |
| 3279 | return(ret); |
| 3280 | } |
| 3281 | |
| 3282 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3283 | * xmlXPathObjectCopy: |
| 3284 | * @val: the original object |
| 3285 | * |
| 3286 | * allocate a new copy of a given object |
| 3287 | * |
| 3288 | * Returns the newly created object. |
| 3289 | */ |
| 3290 | xmlXPathObjectPtr |
| 3291 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3292 | xmlXPathObjectPtr ret; |
| 3293 | |
| 3294 | if (val == NULL) |
| 3295 | return(NULL); |
| 3296 | |
| 3297 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3298 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3299 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3300 | return(NULL); |
| 3301 | } |
| 3302 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3303 | switch (val->type) { |
| 3304 | case XPATH_BOOLEAN: |
| 3305 | case XPATH_NUMBER: |
| 3306 | case XPATH_POINT: |
| 3307 | case XPATH_RANGE: |
| 3308 | break; |
| 3309 | case XPATH_STRING: |
| 3310 | ret->stringval = xmlStrdup(val->stringval); |
| 3311 | break; |
| 3312 | case XPATH_XSLT_TREE: |
| 3313 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3314 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3315 | xmlNodePtr cur, tmp; |
| 3316 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3317 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3318 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3319 | top = xmlNewDoc(NULL); |
| 3320 | top->name = (char *) |
| 3321 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3322 | ret->user = top; |
| 3323 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3324 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3325 | cur = val->nodesetval->nodeTab[0]->children; |
| 3326 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3327 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3328 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3329 | cur = cur->next; |
| 3330 | } |
| 3331 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3332 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3333 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3334 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3335 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3336 | break; |
| 3337 | case XPATH_NODESET: |
| 3338 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3339 | /* Do not deallocate the copied tree value */ |
| 3340 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3341 | break; |
| 3342 | case XPATH_LOCATIONSET: |
| 3343 | #ifdef LIBXML_XPTR_ENABLED |
| 3344 | { |
| 3345 | xmlLocationSetPtr loc = val->user; |
| 3346 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3347 | break; |
| 3348 | } |
| 3349 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3350 | case XPATH_USERS: |
| 3351 | ret->user = val->user; |
| 3352 | break; |
| 3353 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3354 | xmlGenericError(xmlGenericErrorContext, |
| 3355 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3356 | val->type); |
| 3357 | break; |
| 3358 | } |
| 3359 | return(ret); |
| 3360 | } |
| 3361 | |
| 3362 | /** |
| 3363 | * xmlXPathFreeObject: |
| 3364 | * @obj: the object to free |
| 3365 | * |
| 3366 | * Free up an xmlXPathObjectPtr object. |
| 3367 | */ |
| 3368 | void |
| 3369 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3370 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3371 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3372 | if (obj->boolval) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3373 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3374 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3375 | xmlFreeNodeList((xmlNodePtr) obj->user); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3376 | } else if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3377 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3378 | } else { |
| 3379 | if (obj->nodesetval != NULL) |
| 3380 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3381 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | #ifdef LIBXML_XPTR_ENABLED |
| 3383 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3384 | if (obj->user != NULL) |
| 3385 | xmlXPtrFreeLocationSet(obj->user); |
| 3386 | #endif |
| 3387 | } else if (obj->type == XPATH_STRING) { |
| 3388 | if (obj->stringval != NULL) |
| 3389 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3392 | xmlFree(obj); |
| 3393 | } |
| 3394 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3395 | |
| 3396 | /************************************************************************ |
| 3397 | * * |
| 3398 | * Type Casting Routines * |
| 3399 | * * |
| 3400 | ************************************************************************/ |
| 3401 | |
| 3402 | /** |
| 3403 | * xmlXPathCastBooleanToString: |
| 3404 | * @val: a boolean |
| 3405 | * |
| 3406 | * Converts a boolean to its string value. |
| 3407 | * |
| 3408 | * Returns a newly allocated string. |
| 3409 | */ |
| 3410 | xmlChar * |
| 3411 | xmlXPathCastBooleanToString (int val) { |
| 3412 | xmlChar *ret; |
| 3413 | if (val) |
| 3414 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3415 | else |
| 3416 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3417 | return(ret); |
| 3418 | } |
| 3419 | |
| 3420 | /** |
| 3421 | * xmlXPathCastNumberToString: |
| 3422 | * @val: a number |
| 3423 | * |
| 3424 | * Converts a number to its string value. |
| 3425 | * |
| 3426 | * Returns a newly allocated string. |
| 3427 | */ |
| 3428 | xmlChar * |
| 3429 | xmlXPathCastNumberToString (double val) { |
| 3430 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3431 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3432 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3433 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3434 | break; |
| 3435 | case -1: |
| 3436 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3437 | break; |
| 3438 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3439 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3440 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3441 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3442 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3443 | } else { |
| 3444 | /* could be improved */ |
| 3445 | char buf[100]; |
| 3446 | xmlXPathFormatNumber(val, buf, 100); |
| 3447 | ret = xmlStrdup((const xmlChar *) buf); |
| 3448 | } |
| 3449 | } |
| 3450 | return(ret); |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * xmlXPathCastNodeToString: |
| 3455 | * @node: a node |
| 3456 | * |
| 3457 | * Converts a node to its string value. |
| 3458 | * |
| 3459 | * Returns a newly allocated string. |
| 3460 | */ |
| 3461 | xmlChar * |
| 3462 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3463 | return(xmlNodeGetContent(node)); |
| 3464 | } |
| 3465 | |
| 3466 | /** |
| 3467 | * xmlXPathCastNodeSetToString: |
| 3468 | * @ns: a node-set |
| 3469 | * |
| 3470 | * Converts a node-set to its string value. |
| 3471 | * |
| 3472 | * Returns a newly allocated string. |
| 3473 | */ |
| 3474 | xmlChar * |
| 3475 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3476 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3477 | return(xmlStrdup((const xmlChar *) "")); |
| 3478 | |
| 3479 | xmlXPathNodeSetSort(ns); |
| 3480 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3481 | } |
| 3482 | |
| 3483 | /** |
| 3484 | * xmlXPathCastToString: |
| 3485 | * @val: an XPath object |
| 3486 | * |
| 3487 | * Converts an existing object to its string() equivalent |
| 3488 | * |
| 3489 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3490 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3491 | * string object). |
| 3492 | */ |
| 3493 | xmlChar * |
| 3494 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3495 | xmlChar *ret = NULL; |
| 3496 | |
| 3497 | if (val == NULL) |
| 3498 | return(xmlStrdup((const xmlChar *) "")); |
| 3499 | switch (val->type) { |
| 3500 | case XPATH_UNDEFINED: |
| 3501 | #ifdef DEBUG_EXPR |
| 3502 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3503 | #endif |
| 3504 | ret = xmlStrdup((const xmlChar *) ""); |
| 3505 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3506 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3507 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3508 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3509 | break; |
| 3510 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3511 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3512 | case XPATH_BOOLEAN: |
| 3513 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3514 | break; |
| 3515 | case XPATH_NUMBER: { |
| 3516 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3517 | break; |
| 3518 | } |
| 3519 | case XPATH_USERS: |
| 3520 | case XPATH_POINT: |
| 3521 | case XPATH_RANGE: |
| 3522 | case XPATH_LOCATIONSET: |
| 3523 | TODO |
| 3524 | ret = xmlStrdup((const xmlChar *) ""); |
| 3525 | break; |
| 3526 | } |
| 3527 | return(ret); |
| 3528 | } |
| 3529 | |
| 3530 | /** |
| 3531 | * xmlXPathConvertString: |
| 3532 | * @val: an XPath object |
| 3533 | * |
| 3534 | * Converts an existing object to its string() equivalent |
| 3535 | * |
| 3536 | * Returns the new object, the old one is freed (or the operation |
| 3537 | * is done directly on @val) |
| 3538 | */ |
| 3539 | xmlXPathObjectPtr |
| 3540 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3541 | xmlChar *res = NULL; |
| 3542 | |
| 3543 | if (val == NULL) |
| 3544 | return(xmlXPathNewCString("")); |
| 3545 | |
| 3546 | switch (val->type) { |
| 3547 | case XPATH_UNDEFINED: |
| 3548 | #ifdef DEBUG_EXPR |
| 3549 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3550 | #endif |
| 3551 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3552 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3553 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3554 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3555 | break; |
| 3556 | case XPATH_STRING: |
| 3557 | return(val); |
| 3558 | case XPATH_BOOLEAN: |
| 3559 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3560 | break; |
| 3561 | case XPATH_NUMBER: |
| 3562 | res = xmlXPathCastNumberToString(val->floatval); |
| 3563 | break; |
| 3564 | case XPATH_USERS: |
| 3565 | case XPATH_POINT: |
| 3566 | case XPATH_RANGE: |
| 3567 | case XPATH_LOCATIONSET: |
| 3568 | TODO; |
| 3569 | break; |
| 3570 | } |
| 3571 | xmlXPathFreeObject(val); |
| 3572 | if (res == NULL) |
| 3573 | return(xmlXPathNewCString("")); |
| 3574 | return(xmlXPathWrapString(res)); |
| 3575 | } |
| 3576 | |
| 3577 | /** |
| 3578 | * xmlXPathCastBooleanToNumber: |
| 3579 | * @val: a boolean |
| 3580 | * |
| 3581 | * Converts a boolean to its number value |
| 3582 | * |
| 3583 | * Returns the number value |
| 3584 | */ |
| 3585 | double |
| 3586 | xmlXPathCastBooleanToNumber(int val) { |
| 3587 | if (val) |
| 3588 | return(1.0); |
| 3589 | return(0.0); |
| 3590 | } |
| 3591 | |
| 3592 | /** |
| 3593 | * xmlXPathCastStringToNumber: |
| 3594 | * @val: a string |
| 3595 | * |
| 3596 | * Converts a string to its number value |
| 3597 | * |
| 3598 | * Returns the number value |
| 3599 | */ |
| 3600 | double |
| 3601 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3602 | return(xmlXPathStringEvalNumber(val)); |
| 3603 | } |
| 3604 | |
| 3605 | /** |
| 3606 | * xmlXPathCastNodeToNumber: |
| 3607 | * @node: a node |
| 3608 | * |
| 3609 | * Converts a node to its number value |
| 3610 | * |
| 3611 | * Returns the number value |
| 3612 | */ |
| 3613 | double |
| 3614 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3615 | xmlChar *strval; |
| 3616 | double ret; |
| 3617 | |
| 3618 | if (node == NULL) |
| 3619 | return(xmlXPathNAN); |
| 3620 | strval = xmlXPathCastNodeToString(node); |
| 3621 | if (strval == NULL) |
| 3622 | return(xmlXPathNAN); |
| 3623 | ret = xmlXPathCastStringToNumber(strval); |
| 3624 | xmlFree(strval); |
| 3625 | |
| 3626 | return(ret); |
| 3627 | } |
| 3628 | |
| 3629 | /** |
| 3630 | * xmlXPathCastNodeSetToNumber: |
| 3631 | * @ns: a node-set |
| 3632 | * |
| 3633 | * Converts a node-set to its number value |
| 3634 | * |
| 3635 | * Returns the number value |
| 3636 | */ |
| 3637 | double |
| 3638 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3639 | xmlChar *str; |
| 3640 | double ret; |
| 3641 | |
| 3642 | if (ns == NULL) |
| 3643 | return(xmlXPathNAN); |
| 3644 | str = xmlXPathCastNodeSetToString(ns); |
| 3645 | ret = xmlXPathCastStringToNumber(str); |
| 3646 | xmlFree(str); |
| 3647 | return(ret); |
| 3648 | } |
| 3649 | |
| 3650 | /** |
| 3651 | * xmlXPathCastToNumber: |
| 3652 | * @val: an XPath object |
| 3653 | * |
| 3654 | * Converts an XPath object to its number value |
| 3655 | * |
| 3656 | * Returns the number value |
| 3657 | */ |
| 3658 | double |
| 3659 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3660 | double ret = 0.0; |
| 3661 | |
| 3662 | if (val == NULL) |
| 3663 | return(xmlXPathNAN); |
| 3664 | switch (val->type) { |
| 3665 | case XPATH_UNDEFINED: |
| 3666 | #ifdef DEGUB_EXPR |
| 3667 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3668 | #endif |
| 3669 | ret = xmlXPathNAN; |
| 3670 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3671 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3672 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3673 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3674 | break; |
| 3675 | case XPATH_STRING: |
| 3676 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3677 | break; |
| 3678 | case XPATH_NUMBER: |
| 3679 | ret = val->floatval; |
| 3680 | break; |
| 3681 | case XPATH_BOOLEAN: |
| 3682 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3683 | break; |
| 3684 | case XPATH_USERS: |
| 3685 | case XPATH_POINT: |
| 3686 | case XPATH_RANGE: |
| 3687 | case XPATH_LOCATIONSET: |
| 3688 | TODO; |
| 3689 | ret = xmlXPathNAN; |
| 3690 | break; |
| 3691 | } |
| 3692 | return(ret); |
| 3693 | } |
| 3694 | |
| 3695 | /** |
| 3696 | * xmlXPathConvertNumber: |
| 3697 | * @val: an XPath object |
| 3698 | * |
| 3699 | * Converts an existing object to its number() equivalent |
| 3700 | * |
| 3701 | * Returns the new object, the old one is freed (or the operation |
| 3702 | * is done directly on @val) |
| 3703 | */ |
| 3704 | xmlXPathObjectPtr |
| 3705 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3706 | xmlXPathObjectPtr ret; |
| 3707 | |
| 3708 | if (val == NULL) |
| 3709 | return(xmlXPathNewFloat(0.0)); |
| 3710 | if (val->type == XPATH_NUMBER) |
| 3711 | return(val); |
| 3712 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3713 | xmlXPathFreeObject(val); |
| 3714 | return(ret); |
| 3715 | } |
| 3716 | |
| 3717 | /** |
| 3718 | * xmlXPathCastNumberToBoolean: |
| 3719 | * @val: a number |
| 3720 | * |
| 3721 | * Converts a number to its boolean value |
| 3722 | * |
| 3723 | * Returns the boolean value |
| 3724 | */ |
| 3725 | int |
| 3726 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3727 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3728 | return(0); |
| 3729 | return(1); |
| 3730 | } |
| 3731 | |
| 3732 | /** |
| 3733 | * xmlXPathCastStringToBoolean: |
| 3734 | * @val: a string |
| 3735 | * |
| 3736 | * Converts a string to its boolean value |
| 3737 | * |
| 3738 | * Returns the boolean value |
| 3739 | */ |
| 3740 | int |
| 3741 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3742 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3743 | return(0); |
| 3744 | return(1); |
| 3745 | } |
| 3746 | |
| 3747 | /** |
| 3748 | * xmlXPathCastNodeSetToBoolean: |
| 3749 | * @ns: a node-set |
| 3750 | * |
| 3751 | * Converts a node-set to its boolean value |
| 3752 | * |
| 3753 | * Returns the boolean value |
| 3754 | */ |
| 3755 | int |
| 3756 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3757 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3758 | return(0); |
| 3759 | return(1); |
| 3760 | } |
| 3761 | |
| 3762 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3763 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3764 | * @val: an XPath object |
| 3765 | * |
| 3766 | * Converts an XPath object to its boolean value |
| 3767 | * |
| 3768 | * Returns the boolean value |
| 3769 | */ |
| 3770 | int |
| 3771 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3772 | int ret = 0; |
| 3773 | |
| 3774 | if (val == NULL) |
| 3775 | return(0); |
| 3776 | switch (val->type) { |
| 3777 | case XPATH_UNDEFINED: |
| 3778 | #ifdef DEBUG_EXPR |
| 3779 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3780 | #endif |
| 3781 | ret = 0; |
| 3782 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3783 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3784 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3785 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3786 | break; |
| 3787 | case XPATH_STRING: |
| 3788 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3789 | break; |
| 3790 | case XPATH_NUMBER: |
| 3791 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3792 | break; |
| 3793 | case XPATH_BOOLEAN: |
| 3794 | ret = val->boolval; |
| 3795 | break; |
| 3796 | case XPATH_USERS: |
| 3797 | case XPATH_POINT: |
| 3798 | case XPATH_RANGE: |
| 3799 | case XPATH_LOCATIONSET: |
| 3800 | TODO; |
| 3801 | ret = 0; |
| 3802 | break; |
| 3803 | } |
| 3804 | return(ret); |
| 3805 | } |
| 3806 | |
| 3807 | |
| 3808 | /** |
| 3809 | * xmlXPathConvertBoolean: |
| 3810 | * @val: an XPath object |
| 3811 | * |
| 3812 | * Converts an existing object to its boolean() equivalent |
| 3813 | * |
| 3814 | * Returns the new object, the old one is freed (or the operation |
| 3815 | * is done directly on @val) |
| 3816 | */ |
| 3817 | xmlXPathObjectPtr |
| 3818 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3819 | xmlXPathObjectPtr ret; |
| 3820 | |
| 3821 | if (val == NULL) |
| 3822 | return(xmlXPathNewBoolean(0)); |
| 3823 | if (val->type == XPATH_BOOLEAN) |
| 3824 | return(val); |
| 3825 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3826 | xmlXPathFreeObject(val); |
| 3827 | return(ret); |
| 3828 | } |
| 3829 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3830 | /************************************************************************ |
| 3831 | * * |
| 3832 | * Routines to handle XPath contexts * |
| 3833 | * * |
| 3834 | ************************************************************************/ |
| 3835 | |
| 3836 | /** |
| 3837 | * xmlXPathNewContext: |
| 3838 | * @doc: the XML document |
| 3839 | * |
| 3840 | * Create a new xmlXPathContext |
| 3841 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3842 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3843 | */ |
| 3844 | xmlXPathContextPtr |
| 3845 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3846 | xmlXPathContextPtr ret; |
| 3847 | |
| 3848 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3849 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3850 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3851 | return(NULL); |
| 3852 | } |
| 3853 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3854 | ret->doc = doc; |
| 3855 | ret->node = NULL; |
| 3856 | |
| 3857 | ret->varHash = NULL; |
| 3858 | |
| 3859 | ret->nb_types = 0; |
| 3860 | ret->max_types = 0; |
| 3861 | ret->types = NULL; |
| 3862 | |
| 3863 | ret->funcHash = xmlHashCreate(0); |
| 3864 | |
| 3865 | ret->nb_axis = 0; |
| 3866 | ret->max_axis = 0; |
| 3867 | ret->axis = NULL; |
| 3868 | |
| 3869 | ret->nsHash = NULL; |
| 3870 | ret->user = NULL; |
| 3871 | |
| 3872 | ret->contextSize = -1; |
| 3873 | ret->proximityPosition = -1; |
| 3874 | |
| 3875 | xmlXPathRegisterAllFunctions(ret); |
| 3876 | |
| 3877 | return(ret); |
| 3878 | } |
| 3879 | |
| 3880 | /** |
| 3881 | * xmlXPathFreeContext: |
| 3882 | * @ctxt: the context to free |
| 3883 | * |
| 3884 | * Free up an xmlXPathContext |
| 3885 | */ |
| 3886 | void |
| 3887 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
| 3888 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3889 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3890 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | xmlFree(ctxt); |
| 3892 | } |
| 3893 | |
| 3894 | /************************************************************************ |
| 3895 | * * |
| 3896 | * Routines to handle XPath parser contexts * |
| 3897 | * * |
| 3898 | ************************************************************************/ |
| 3899 | |
| 3900 | #define CHECK_CTXT(ctxt) \ |
| 3901 | if (ctxt == NULL) { \ |
| 3902 | xmlGenericError(xmlGenericErrorContext, \ |
| 3903 | "%s:%d Internal error: ctxt == NULL\n", \ |
| 3904 | __FILE__, __LINE__); \ |
| 3905 | } \ |
| 3906 | |
| 3907 | |
| 3908 | #define CHECK_CONTEXT(ctxt) \ |
| 3909 | if (ctxt == NULL) { \ |
| 3910 | xmlGenericError(xmlGenericErrorContext, \ |
| 3911 | "%s:%d Internal error: no context\n", \ |
| 3912 | __FILE__, __LINE__); \ |
| 3913 | } \ |
| 3914 | else if (ctxt->doc == NULL) { \ |
| 3915 | xmlGenericError(xmlGenericErrorContext, \ |
| 3916 | "%s:%d Internal error: no document\n", \ |
| 3917 | __FILE__, __LINE__); \ |
| 3918 | } \ |
| 3919 | else if (ctxt->doc->children == NULL) { \ |
| 3920 | xmlGenericError(xmlGenericErrorContext, \ |
| 3921 | "%s:%d Internal error: document without root\n", \ |
| 3922 | __FILE__, __LINE__); \ |
| 3923 | } \ |
| 3924 | |
| 3925 | |
| 3926 | /** |
| 3927 | * xmlXPathNewParserContext: |
| 3928 | * @str: the XPath expression |
| 3929 | * @ctxt: the XPath context |
| 3930 | * |
| 3931 | * Create a new xmlXPathParserContext |
| 3932 | * |
| 3933 | * Returns the xmlXPathParserContext just allocated. |
| 3934 | */ |
| 3935 | xmlXPathParserContextPtr |
| 3936 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3937 | xmlXPathParserContextPtr ret; |
| 3938 | |
| 3939 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3940 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3941 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3942 | return(NULL); |
| 3943 | } |
| 3944 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3945 | ret->cur = ret->base = str; |
| 3946 | ret->context = ctxt; |
| 3947 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3948 | ret->comp = xmlXPathNewCompExpr(); |
| 3949 | if (ret->comp == NULL) { |
| 3950 | xmlFree(ret->valueTab); |
| 3951 | xmlFree(ret); |
| 3952 | return(NULL); |
| 3953 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 3954 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 3955 | ret->comp->dict = ctxt->dict; |
| 3956 | xmlDictReference(ret->comp->dict); |
| 3957 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3958 | |
| 3959 | return(ret); |
| 3960 | } |
| 3961 | |
| 3962 | /** |
| 3963 | * xmlXPathCompParserContext: |
| 3964 | * @comp: the XPath compiled expression |
| 3965 | * @ctxt: the XPath context |
| 3966 | * |
| 3967 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 3968 | * |
| 3969 | * Returns the xmlXPathParserContext just allocated. |
| 3970 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3971 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3972 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 3973 | xmlXPathParserContextPtr ret; |
| 3974 | |
| 3975 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3976 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3977 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3978 | return(NULL); |
| 3979 | } |
| 3980 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3981 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3982 | /* Allocate the value stack */ |
| 3983 | ret->valueTab = (xmlXPathObjectPtr *) |
| 3984 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3985 | if (ret->valueTab == NULL) { |
| 3986 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3987 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3988 | return(NULL); |
| 3989 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3990 | ret->valueNr = 0; |
| 3991 | ret->valueMax = 10; |
| 3992 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3993 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 3994 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3995 | ret->comp = comp; |
| 3996 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3997 | return(ret); |
| 3998 | } |
| 3999 | |
| 4000 | /** |
| 4001 | * xmlXPathFreeParserContext: |
| 4002 | * @ctxt: the context to free |
| 4003 | * |
| 4004 | * Free up an xmlXPathParserContext |
| 4005 | */ |
| 4006 | void |
| 4007 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 4008 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4009 | xmlFree(ctxt->valueTab); |
| 4010 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4011 | if (ctxt->comp) |
| 4012 | xmlXPathFreeCompExpr(ctxt->comp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4013 | xmlFree(ctxt); |
| 4014 | } |
| 4015 | |
| 4016 | /************************************************************************ |
| 4017 | * * |
| 4018 | * The implicit core function library * |
| 4019 | * * |
| 4020 | ************************************************************************/ |
| 4021 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4022 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4023 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4024 | * @node: a node pointer |
| 4025 | * |
| 4026 | * Function computing the beginning of the string value of the node, |
| 4027 | * used to speed up comparisons |
| 4028 | * |
| 4029 | * Returns an int usable as a hash |
| 4030 | */ |
| 4031 | static unsigned int |
| 4032 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 4033 | int len = 2; |
| 4034 | const xmlChar * string = NULL; |
| 4035 | xmlNodePtr tmp = NULL; |
| 4036 | unsigned int ret = 0; |
| 4037 | |
| 4038 | if (node == NULL) |
| 4039 | return(0); |
| 4040 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4041 | if (node->type == XML_DOCUMENT_NODE) { |
| 4042 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 4043 | if (tmp == NULL) |
| 4044 | node = node->children; |
| 4045 | else |
| 4046 | node = tmp; |
| 4047 | |
| 4048 | if (node == NULL) |
| 4049 | return(0); |
| 4050 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4051 | |
| 4052 | switch (node->type) { |
| 4053 | case XML_COMMENT_NODE: |
| 4054 | case XML_PI_NODE: |
| 4055 | case XML_CDATA_SECTION_NODE: |
| 4056 | case XML_TEXT_NODE: |
| 4057 | string = node->content; |
| 4058 | if (string == NULL) |
| 4059 | return(0); |
| 4060 | if (string[0] == 0) |
| 4061 | return(0); |
| 4062 | return(((unsigned int) string[0]) + |
| 4063 | (((unsigned int) string[1]) << 8)); |
| 4064 | case XML_NAMESPACE_DECL: |
| 4065 | string = ((xmlNsPtr)node)->href; |
| 4066 | if (string == NULL) |
| 4067 | return(0); |
| 4068 | if (string[0] == 0) |
| 4069 | return(0); |
| 4070 | return(((unsigned int) string[0]) + |
| 4071 | (((unsigned int) string[1]) << 8)); |
| 4072 | case XML_ATTRIBUTE_NODE: |
| 4073 | tmp = ((xmlAttrPtr) node)->children; |
| 4074 | break; |
| 4075 | case XML_ELEMENT_NODE: |
| 4076 | tmp = node->children; |
| 4077 | break; |
| 4078 | default: |
| 4079 | return(0); |
| 4080 | } |
| 4081 | while (tmp != NULL) { |
| 4082 | switch (tmp->type) { |
| 4083 | case XML_COMMENT_NODE: |
| 4084 | case XML_PI_NODE: |
| 4085 | case XML_CDATA_SECTION_NODE: |
| 4086 | case XML_TEXT_NODE: |
| 4087 | string = tmp->content; |
| 4088 | break; |
| 4089 | case XML_NAMESPACE_DECL: |
| 4090 | string = ((xmlNsPtr)tmp)->href; |
| 4091 | break; |
| 4092 | default: |
| 4093 | break; |
| 4094 | } |
| 4095 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4096 | if (len == 1) { |
| 4097 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4098 | } |
| 4099 | if (string[1] == 0) { |
| 4100 | len = 1; |
| 4101 | ret = (unsigned int) string[0]; |
| 4102 | } else { |
| 4103 | return(((unsigned int) string[0]) + |
| 4104 | (((unsigned int) string[1]) << 8)); |
| 4105 | } |
| 4106 | } |
| 4107 | /* |
| 4108 | * Skip to next node |
| 4109 | */ |
| 4110 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4111 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4112 | tmp = tmp->children; |
| 4113 | continue; |
| 4114 | } |
| 4115 | } |
| 4116 | if (tmp == node) |
| 4117 | break; |
| 4118 | |
| 4119 | if (tmp->next != NULL) { |
| 4120 | tmp = tmp->next; |
| 4121 | continue; |
| 4122 | } |
| 4123 | |
| 4124 | do { |
| 4125 | tmp = tmp->parent; |
| 4126 | if (tmp == NULL) |
| 4127 | break; |
| 4128 | if (tmp == node) { |
| 4129 | tmp = NULL; |
| 4130 | break; |
| 4131 | } |
| 4132 | if (tmp->next != NULL) { |
| 4133 | tmp = tmp->next; |
| 4134 | break; |
| 4135 | } |
| 4136 | } while (tmp != NULL); |
| 4137 | } |
| 4138 | return(ret); |
| 4139 | } |
| 4140 | |
| 4141 | /** |
| 4142 | * xmlXPathStringHash: |
| 4143 | * @string: a string |
| 4144 | * |
| 4145 | * Function computing the beginning of the string value of the node, |
| 4146 | * used to speed up comparisons |
| 4147 | * |
| 4148 | * Returns an int usable as a hash |
| 4149 | */ |
| 4150 | static unsigned int |
| 4151 | xmlXPathStringHash(const xmlChar * string) { |
| 4152 | if (string == NULL) |
| 4153 | return((unsigned int) 0); |
| 4154 | if (string[0] == 0) |
| 4155 | return(0); |
| 4156 | return(((unsigned int) string[0]) + |
| 4157 | (((unsigned int) string[1]) << 8)); |
| 4158 | } |
| 4159 | |
| 4160 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4161 | * xmlXPathCompareNodeSetFloat: |
| 4162 | * @ctxt: the XPath Parser context |
| 4163 | * @inf: less than (1) or greater than (0) |
| 4164 | * @strict: is the comparison strict |
| 4165 | * @arg: the node set |
| 4166 | * @f: the value |
| 4167 | * |
| 4168 | * Implement the compare operation between a nodeset and a number |
| 4169 | * @ns < @val (1, 1, ... |
| 4170 | * @ns <= @val (1, 0, ... |
| 4171 | * @ns > @val (0, 1, ... |
| 4172 | * @ns >= @val (0, 0, ... |
| 4173 | * |
| 4174 | * If one object to be compared is a node-set and the other is a number, |
| 4175 | * then the comparison will be true if and only if there is a node in the |
| 4176 | * node-set such that the result of performing the comparison on the number |
| 4177 | * to be compared and on the result of converting the string-value of that |
| 4178 | * node to a number using the number function is true. |
| 4179 | * |
| 4180 | * Returns 0 or 1 depending on the results of the test. |
| 4181 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4182 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4183 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4184 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4185 | int i, ret = 0; |
| 4186 | xmlNodeSetPtr ns; |
| 4187 | xmlChar *str2; |
| 4188 | |
| 4189 | if ((f == NULL) || (arg == NULL) || |
| 4190 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4191 | xmlXPathFreeObject(arg); |
| 4192 | xmlXPathFreeObject(f); |
| 4193 | return(0); |
| 4194 | } |
| 4195 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4196 | if (ns != NULL) { |
| 4197 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4198 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4199 | if (str2 != NULL) { |
| 4200 | valuePush(ctxt, |
| 4201 | xmlXPathNewString(str2)); |
| 4202 | xmlFree(str2); |
| 4203 | xmlXPathNumberFunction(ctxt, 1); |
| 4204 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4205 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4206 | if (ret) |
| 4207 | break; |
| 4208 | } |
| 4209 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | } |
| 4211 | xmlXPathFreeObject(arg); |
| 4212 | xmlXPathFreeObject(f); |
| 4213 | return(ret); |
| 4214 | } |
| 4215 | |
| 4216 | /** |
| 4217 | * xmlXPathCompareNodeSetString: |
| 4218 | * @ctxt: the XPath Parser context |
| 4219 | * @inf: less than (1) or greater than (0) |
| 4220 | * @strict: is the comparison strict |
| 4221 | * @arg: the node set |
| 4222 | * @s: the value |
| 4223 | * |
| 4224 | * Implement the compare operation between a nodeset and a string |
| 4225 | * @ns < @val (1, 1, ... |
| 4226 | * @ns <= @val (1, 0, ... |
| 4227 | * @ns > @val (0, 1, ... |
| 4228 | * @ns >= @val (0, 0, ... |
| 4229 | * |
| 4230 | * If one object to be compared is a node-set and the other is a string, |
| 4231 | * then the comparison will be true if and only if there is a node in |
| 4232 | * the node-set such that the result of performing the comparison on the |
| 4233 | * string-value of the node and the other string is true. |
| 4234 | * |
| 4235 | * Returns 0 or 1 depending on the results of the test. |
| 4236 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4237 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4238 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4239 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4240 | int i, ret = 0; |
| 4241 | xmlNodeSetPtr ns; |
| 4242 | xmlChar *str2; |
| 4243 | |
| 4244 | if ((s == NULL) || (arg == NULL) || |
| 4245 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4246 | xmlXPathFreeObject(arg); |
| 4247 | xmlXPathFreeObject(s); |
| 4248 | return(0); |
| 4249 | } |
| 4250 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4251 | if (ns != NULL) { |
| 4252 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4253 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4254 | if (str2 != NULL) { |
| 4255 | valuePush(ctxt, |
| 4256 | xmlXPathNewString(str2)); |
| 4257 | xmlFree(str2); |
| 4258 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4259 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4260 | if (ret) |
| 4261 | break; |
| 4262 | } |
| 4263 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4264 | } |
| 4265 | xmlXPathFreeObject(arg); |
| 4266 | xmlXPathFreeObject(s); |
| 4267 | return(ret); |
| 4268 | } |
| 4269 | |
| 4270 | /** |
| 4271 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4272 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4273 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4274 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | * @arg2: the second node set object |
| 4276 | * |
| 4277 | * Implement the compare operation on nodesets: |
| 4278 | * |
| 4279 | * If both objects to be compared are node-sets, then the comparison |
| 4280 | * will be true if and only if there is a node in the first node-set |
| 4281 | * and a node in the second node-set such that the result of performing |
| 4282 | * the comparison on the string-values of the two nodes is true. |
| 4283 | * .... |
| 4284 | * When neither object to be compared is a node-set and the operator |
| 4285 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4286 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4287 | * .... |
| 4288 | * The number function converts its argument to a number as follows: |
| 4289 | * - a string that consists of optional whitespace followed by an |
| 4290 | * optional minus sign followed by a Number followed by whitespace |
| 4291 | * is converted to the IEEE 754 number that is nearest (according |
| 4292 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4293 | * represented by the string; any other string is converted to NaN |
| 4294 | * |
| 4295 | * Conclusion all nodes need to be converted first to their string value |
| 4296 | * and then the comparison must be done when possible |
| 4297 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4298 | static int |
| 4299 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4300 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4301 | int i, j, init = 0; |
| 4302 | double val1; |
| 4303 | double *values2; |
| 4304 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4305 | xmlNodeSetPtr ns1; |
| 4306 | xmlNodeSetPtr ns2; |
| 4307 | |
| 4308 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4309 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4310 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4311 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4312 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4313 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4314 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4315 | xmlXPathFreeObject(arg1); |
| 4316 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4317 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4318 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4319 | |
| 4320 | ns1 = arg1->nodesetval; |
| 4321 | ns2 = arg2->nodesetval; |
| 4322 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4323 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4324 | xmlXPathFreeObject(arg1); |
| 4325 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4326 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4327 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4328 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4329 | xmlXPathFreeObject(arg1); |
| 4330 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4332 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4333 | |
| 4334 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4335 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4336 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4337 | xmlXPathFreeObject(arg1); |
| 4338 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | return(0); |
| 4340 | } |
| 4341 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4342 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4343 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4344 | continue; |
| 4345 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4346 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4347 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4348 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4349 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4350 | continue; |
| 4351 | if (inf && strict) |
| 4352 | ret = (val1 < values2[j]); |
| 4353 | else if (inf && !strict) |
| 4354 | ret = (val1 <= values2[j]); |
| 4355 | else if (!inf && strict) |
| 4356 | ret = (val1 > values2[j]); |
| 4357 | else if (!inf && !strict) |
| 4358 | ret = (val1 >= values2[j]); |
| 4359 | if (ret) |
| 4360 | break; |
| 4361 | } |
| 4362 | if (ret) |
| 4363 | break; |
| 4364 | init = 1; |
| 4365 | } |
| 4366 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4367 | xmlXPathFreeObject(arg1); |
| 4368 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4369 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /** |
| 4373 | * xmlXPathCompareNodeSetValue: |
| 4374 | * @ctxt: the XPath Parser context |
| 4375 | * @inf: less than (1) or greater than (0) |
| 4376 | * @strict: is the comparison strict |
| 4377 | * @arg: the node set |
| 4378 | * @val: the value |
| 4379 | * |
| 4380 | * Implement the compare operation between a nodeset and a value |
| 4381 | * @ns < @val (1, 1, ... |
| 4382 | * @ns <= @val (1, 0, ... |
| 4383 | * @ns > @val (0, 1, ... |
| 4384 | * @ns >= @val (0, 0, ... |
| 4385 | * |
| 4386 | * If one object to be compared is a node-set and the other is a boolean, |
| 4387 | * then the comparison will be true if and only if the result of performing |
| 4388 | * the comparison on the boolean and on the result of converting |
| 4389 | * the node-set to a boolean using the boolean function is true. |
| 4390 | * |
| 4391 | * Returns 0 or 1 depending on the results of the test. |
| 4392 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4393 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4394 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4395 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4396 | if ((val == NULL) || (arg == NULL) || |
| 4397 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4398 | return(0); |
| 4399 | |
| 4400 | switch(val->type) { |
| 4401 | case XPATH_NUMBER: |
| 4402 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4403 | case XPATH_NODESET: |
| 4404 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4405 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4406 | case XPATH_STRING: |
| 4407 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4408 | case XPATH_BOOLEAN: |
| 4409 | valuePush(ctxt, arg); |
| 4410 | xmlXPathBooleanFunction(ctxt, 1); |
| 4411 | valuePush(ctxt, val); |
| 4412 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4413 | default: |
| 4414 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4415 | } |
| 4416 | return(0); |
| 4417 | } |
| 4418 | |
| 4419 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4420 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4421 | * @arg: the nodeset object argument |
| 4422 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4423 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4424 | * |
| 4425 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4426 | * If one object to be compared is a node-set and the other is a string, |
| 4427 | * then the comparison will be true if and only if there is a node in |
| 4428 | * the node-set such that the result of performing the comparison on the |
| 4429 | * string-value of the node and the other string is true. |
| 4430 | * |
| 4431 | * Returns 0 or 1 depending on the results of the test. |
| 4432 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4433 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4434 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4435 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4436 | int i; |
| 4437 | xmlNodeSetPtr ns; |
| 4438 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4439 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4440 | |
| 4441 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4442 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4443 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4444 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4445 | /* |
| 4446 | * A NULL nodeset compared with a string is always false |
| 4447 | * (since there is no node equal, and no node not equal) |
| 4448 | */ |
| 4449 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4450 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4451 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4452 | for (i = 0; i < ns->nodeNr; i++) { |
| 4453 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4454 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4455 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4456 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4457 | if (neq) |
| 4458 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4459 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4460 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4461 | if (neq) |
| 4462 | continue; |
| 4463 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4464 | } else if (neq) { |
| 4465 | if (str2 != NULL) |
| 4466 | xmlFree(str2); |
| 4467 | return (1); |
| 4468 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4469 | if (str2 != NULL) |
| 4470 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4471 | } else if (neq) |
| 4472 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4473 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4474 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4478 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4479 | * @arg: the nodeset object argument |
| 4480 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4481 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4482 | * |
| 4483 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4484 | * If one object to be compared is a node-set and the other is a number, |
| 4485 | * then the comparison will be true if and only if there is a node in |
| 4486 | * the node-set such that the result of performing the comparison on the |
| 4487 | * number to be compared and on the result of converting the string-value |
| 4488 | * of that node to a number using the number function is true. |
| 4489 | * |
| 4490 | * Returns 0 or 1 depending on the results of the test. |
| 4491 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4492 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4493 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4494 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4495 | int i, ret=0; |
| 4496 | xmlNodeSetPtr ns; |
| 4497 | xmlChar *str2; |
| 4498 | xmlXPathObjectPtr val; |
| 4499 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4500 | |
| 4501 | if ((arg == NULL) || |
| 4502 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4503 | return(0); |
| 4504 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4505 | ns = arg->nodesetval; |
| 4506 | if (ns != NULL) { |
| 4507 | for (i=0;i<ns->nodeNr;i++) { |
| 4508 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4509 | if (str2 != NULL) { |
| 4510 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4511 | xmlFree(str2); |
| 4512 | xmlXPathNumberFunction(ctxt, 1); |
| 4513 | val = valuePop(ctxt); |
| 4514 | v = val->floatval; |
| 4515 | xmlXPathFreeObject(val); |
| 4516 | if (!xmlXPathIsNaN(v)) { |
| 4517 | if ((!neq) && (v==f)) { |
| 4518 | ret = 1; |
| 4519 | break; |
| 4520 | } else if ((neq) && (v!=f)) { |
| 4521 | ret = 1; |
| 4522 | break; |
| 4523 | } |
| 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | |
| 4533 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4534 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4535 | * @arg1: first nodeset object argument |
| 4536 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4537 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4539 | * Implement the equal / not equal operation on XPath nodesets: |
| 4540 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4541 | * If both objects to be compared are node-sets, then the comparison |
| 4542 | * will be true if and only if there is a node in the first node-set and |
| 4543 | * a node in the second node-set such that the result of performing the |
| 4544 | * comparison on the string-values of the two nodes is true. |
| 4545 | * |
| 4546 | * (needless to say, this is a costly operation) |
| 4547 | * |
| 4548 | * Returns 0 or 1 depending on the results of the test. |
| 4549 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4550 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4551 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4552 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4553 | unsigned int *hashs1; |
| 4554 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4555 | xmlChar **values1; |
| 4556 | xmlChar **values2; |
| 4557 | int ret = 0; |
| 4558 | xmlNodeSetPtr ns1; |
| 4559 | xmlNodeSetPtr ns2; |
| 4560 | |
| 4561 | if ((arg1 == NULL) || |
| 4562 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4563 | return(0); |
| 4564 | if ((arg2 == NULL) || |
| 4565 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4566 | return(0); |
| 4567 | |
| 4568 | ns1 = arg1->nodesetval; |
| 4569 | ns2 = arg2->nodesetval; |
| 4570 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4571 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4572 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4573 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4574 | return(0); |
| 4575 | |
| 4576 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4577 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4578 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4579 | if (neq == 0) |
| 4580 | for (i = 0;i < ns1->nodeNr;i++) |
| 4581 | for (j = 0;j < ns2->nodeNr;j++) |
| 4582 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4583 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4584 | |
| 4585 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4586 | if (values1 == NULL) { |
| 4587 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4588 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4589 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4590 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4591 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4592 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4593 | xmlFree(values1); |
| 4594 | return(0); |
| 4595 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4596 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4597 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4598 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4599 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4600 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4601 | xmlFree(values1); |
| 4602 | return(0); |
| 4603 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4604 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4605 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4606 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4607 | xmlFree(hashs1); |
| 4608 | xmlFree(values1); |
| 4609 | xmlFree(values2); |
| 4610 | return(0); |
| 4611 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4612 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4613 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4614 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4615 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4616 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4617 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4618 | if (hashs1[i] != hashs2[j]) { |
| 4619 | if (neq) { |
| 4620 | ret = 1; |
| 4621 | break; |
| 4622 | } |
| 4623 | } |
| 4624 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4625 | if (values1[i] == NULL) |
| 4626 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4627 | if (values2[j] == NULL) |
| 4628 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4629 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4630 | if (ret) |
| 4631 | break; |
| 4632 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4633 | } |
| 4634 | if (ret) |
| 4635 | break; |
| 4636 | } |
| 4637 | for (i = 0;i < ns1->nodeNr;i++) |
| 4638 | if (values1[i] != NULL) |
| 4639 | xmlFree(values1[i]); |
| 4640 | for (j = 0;j < ns2->nodeNr;j++) |
| 4641 | if (values2[j] != NULL) |
| 4642 | xmlFree(values2[j]); |
| 4643 | xmlFree(values1); |
| 4644 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4645 | xmlFree(hashs1); |
| 4646 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4647 | return(ret); |
| 4648 | } |
| 4649 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4650 | static int |
| 4651 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 4652 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4653 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4654 | /* |
| 4655 | *At this point we are assured neither arg1 nor arg2 |
| 4656 | *is a nodeset, so we can just pick the appropriate routine. |
| 4657 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4658 | switch (arg1->type) { |
| 4659 | case XPATH_UNDEFINED: |
| 4660 | #ifdef DEBUG_EXPR |
| 4661 | xmlGenericError(xmlGenericErrorContext, |
| 4662 | "Equal: undefined\n"); |
| 4663 | #endif |
| 4664 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4665 | case XPATH_BOOLEAN: |
| 4666 | switch (arg2->type) { |
| 4667 | case XPATH_UNDEFINED: |
| 4668 | #ifdef DEBUG_EXPR |
| 4669 | xmlGenericError(xmlGenericErrorContext, |
| 4670 | "Equal: undefined\n"); |
| 4671 | #endif |
| 4672 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4673 | case XPATH_BOOLEAN: |
| 4674 | #ifdef DEBUG_EXPR |
| 4675 | xmlGenericError(xmlGenericErrorContext, |
| 4676 | "Equal: %d boolean %d \n", |
| 4677 | arg1->boolval, arg2->boolval); |
| 4678 | #endif |
| 4679 | ret = (arg1->boolval == arg2->boolval); |
| 4680 | break; |
| 4681 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4682 | ret = (arg1->boolval == |
| 4683 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4684 | break; |
| 4685 | case XPATH_STRING: |
| 4686 | if ((arg2->stringval == NULL) || |
| 4687 | (arg2->stringval[0] == 0)) ret = 0; |
| 4688 | else |
| 4689 | ret = 1; |
| 4690 | ret = (arg1->boolval == ret); |
| 4691 | break; |
| 4692 | case XPATH_USERS: |
| 4693 | case XPATH_POINT: |
| 4694 | case XPATH_RANGE: |
| 4695 | case XPATH_LOCATIONSET: |
| 4696 | TODO |
| 4697 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4698 | case XPATH_NODESET: |
| 4699 | case XPATH_XSLT_TREE: |
| 4700 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | } |
| 4702 | break; |
| 4703 | case XPATH_NUMBER: |
| 4704 | switch (arg2->type) { |
| 4705 | case XPATH_UNDEFINED: |
| 4706 | #ifdef DEBUG_EXPR |
| 4707 | xmlGenericError(xmlGenericErrorContext, |
| 4708 | "Equal: undefined\n"); |
| 4709 | #endif |
| 4710 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4711 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4712 | ret = (arg2->boolval== |
| 4713 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | case XPATH_STRING: |
| 4716 | valuePush(ctxt, arg2); |
| 4717 | xmlXPathNumberFunction(ctxt, 1); |
| 4718 | arg2 = valuePop(ctxt); |
| 4719 | /* no break on purpose */ |
| 4720 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4721 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4722 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4723 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4724 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4725 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4726 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4727 | ret = 1; |
| 4728 | else |
| 4729 | ret = 0; |
| 4730 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4731 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4732 | ret = 1; |
| 4733 | else |
| 4734 | ret = 0; |
| 4735 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4736 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4737 | ret = 1; |
| 4738 | else |
| 4739 | ret = 0; |
| 4740 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4741 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4742 | ret = 1; |
| 4743 | else |
| 4744 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4745 | } else { |
| 4746 | ret = (arg1->floatval == arg2->floatval); |
| 4747 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4748 | break; |
| 4749 | case XPATH_USERS: |
| 4750 | case XPATH_POINT: |
| 4751 | case XPATH_RANGE: |
| 4752 | case XPATH_LOCATIONSET: |
| 4753 | TODO |
| 4754 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4755 | case XPATH_NODESET: |
| 4756 | case XPATH_XSLT_TREE: |
| 4757 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4758 | } |
| 4759 | break; |
| 4760 | case XPATH_STRING: |
| 4761 | switch (arg2->type) { |
| 4762 | case XPATH_UNDEFINED: |
| 4763 | #ifdef DEBUG_EXPR |
| 4764 | xmlGenericError(xmlGenericErrorContext, |
| 4765 | "Equal: undefined\n"); |
| 4766 | #endif |
| 4767 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4768 | case XPATH_BOOLEAN: |
| 4769 | if ((arg1->stringval == NULL) || |
| 4770 | (arg1->stringval[0] == 0)) ret = 0; |
| 4771 | else |
| 4772 | ret = 1; |
| 4773 | ret = (arg2->boolval == ret); |
| 4774 | break; |
| 4775 | case XPATH_STRING: |
| 4776 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4777 | break; |
| 4778 | case XPATH_NUMBER: |
| 4779 | valuePush(ctxt, arg1); |
| 4780 | xmlXPathNumberFunction(ctxt, 1); |
| 4781 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4782 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4783 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4784 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4785 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4786 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4787 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4788 | ret = 1; |
| 4789 | else |
| 4790 | ret = 0; |
| 4791 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4792 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4793 | ret = 1; |
| 4794 | else |
| 4795 | ret = 0; |
| 4796 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4797 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4798 | ret = 1; |
| 4799 | else |
| 4800 | ret = 0; |
| 4801 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4802 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4803 | ret = 1; |
| 4804 | else |
| 4805 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4806 | } else { |
| 4807 | ret = (arg1->floatval == arg2->floatval); |
| 4808 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4809 | break; |
| 4810 | case XPATH_USERS: |
| 4811 | case XPATH_POINT: |
| 4812 | case XPATH_RANGE: |
| 4813 | case XPATH_LOCATIONSET: |
| 4814 | TODO |
| 4815 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4816 | case XPATH_NODESET: |
| 4817 | case XPATH_XSLT_TREE: |
| 4818 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | } |
| 4820 | break; |
| 4821 | case XPATH_USERS: |
| 4822 | case XPATH_POINT: |
| 4823 | case XPATH_RANGE: |
| 4824 | case XPATH_LOCATIONSET: |
| 4825 | TODO |
| 4826 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4827 | case XPATH_NODESET: |
| 4828 | case XPATH_XSLT_TREE: |
| 4829 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4830 | } |
| 4831 | xmlXPathFreeObject(arg1); |
| 4832 | xmlXPathFreeObject(arg2); |
| 4833 | return(ret); |
| 4834 | } |
| 4835 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4836 | /** |
| 4837 | * xmlXPathEqualValues: |
| 4838 | * @ctxt: the XPath Parser context |
| 4839 | * |
| 4840 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4841 | * |
| 4842 | * Returns 0 or 1 depending on the results of the test. |
| 4843 | */ |
| 4844 | int |
| 4845 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4846 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4847 | int ret = 0; |
| 4848 | |
| 4849 | arg2 = valuePop(ctxt); |
| 4850 | arg1 = valuePop(ctxt); |
| 4851 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4852 | if (arg1 != NULL) |
| 4853 | xmlXPathFreeObject(arg1); |
| 4854 | else |
| 4855 | xmlXPathFreeObject(arg2); |
| 4856 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4857 | } |
| 4858 | |
| 4859 | if (arg1 == arg2) { |
| 4860 | #ifdef DEBUG_EXPR |
| 4861 | xmlGenericError(xmlGenericErrorContext, |
| 4862 | "Equal: by pointer\n"); |
| 4863 | #endif |
| 4864 | return(1); |
| 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | *If either argument is a nodeset, it's a 'special case' |
| 4869 | */ |
| 4870 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4871 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4872 | /* |
| 4873 | *Hack it to assure arg1 is the nodeset |
| 4874 | */ |
| 4875 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4876 | argtmp = arg2; |
| 4877 | arg2 = arg1; |
| 4878 | arg1 = argtmp; |
| 4879 | } |
| 4880 | switch (arg2->type) { |
| 4881 | case XPATH_UNDEFINED: |
| 4882 | #ifdef DEBUG_EXPR |
| 4883 | xmlGenericError(xmlGenericErrorContext, |
| 4884 | "Equal: undefined\n"); |
| 4885 | #endif |
| 4886 | break; |
| 4887 | case XPATH_NODESET: |
| 4888 | case XPATH_XSLT_TREE: |
| 4889 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 4890 | break; |
| 4891 | case XPATH_BOOLEAN: |
| 4892 | if ((arg1->nodesetval == NULL) || |
| 4893 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4894 | else |
| 4895 | ret = 1; |
| 4896 | ret = (ret == arg2->boolval); |
| 4897 | break; |
| 4898 | case XPATH_NUMBER: |
| 4899 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 4900 | break; |
| 4901 | case XPATH_STRING: |
| 4902 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 4903 | break; |
| 4904 | case XPATH_USERS: |
| 4905 | case XPATH_POINT: |
| 4906 | case XPATH_RANGE: |
| 4907 | case XPATH_LOCATIONSET: |
| 4908 | TODO |
| 4909 | break; |
| 4910 | } |
| 4911 | xmlXPathFreeObject(arg1); |
| 4912 | xmlXPathFreeObject(arg2); |
| 4913 | return(ret); |
| 4914 | } |
| 4915 | |
| 4916 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4917 | } |
| 4918 | |
| 4919 | /** |
| 4920 | * xmlXPathNotEqualValues: |
| 4921 | * @ctxt: the XPath Parser context |
| 4922 | * |
| 4923 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4924 | * |
| 4925 | * Returns 0 or 1 depending on the results of the test. |
| 4926 | */ |
| 4927 | int |
| 4928 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4929 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4930 | int ret = 0; |
| 4931 | |
| 4932 | arg2 = valuePop(ctxt); |
| 4933 | arg1 = valuePop(ctxt); |
| 4934 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4935 | if (arg1 != NULL) |
| 4936 | xmlXPathFreeObject(arg1); |
| 4937 | else |
| 4938 | xmlXPathFreeObject(arg2); |
| 4939 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4940 | } |
| 4941 | |
| 4942 | if (arg1 == arg2) { |
| 4943 | #ifdef DEBUG_EXPR |
| 4944 | xmlGenericError(xmlGenericErrorContext, |
| 4945 | "NotEqual: by pointer\n"); |
| 4946 | #endif |
| 4947 | return(0); |
| 4948 | } |
| 4949 | |
| 4950 | /* |
| 4951 | *If either argument is a nodeset, it's a 'special case' |
| 4952 | */ |
| 4953 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4954 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4955 | /* |
| 4956 | *Hack it to assure arg1 is the nodeset |
| 4957 | */ |
| 4958 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4959 | argtmp = arg2; |
| 4960 | arg2 = arg1; |
| 4961 | arg1 = argtmp; |
| 4962 | } |
| 4963 | switch (arg2->type) { |
| 4964 | case XPATH_UNDEFINED: |
| 4965 | #ifdef DEBUG_EXPR |
| 4966 | xmlGenericError(xmlGenericErrorContext, |
| 4967 | "NotEqual: undefined\n"); |
| 4968 | #endif |
| 4969 | break; |
| 4970 | case XPATH_NODESET: |
| 4971 | case XPATH_XSLT_TREE: |
| 4972 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 4973 | break; |
| 4974 | case XPATH_BOOLEAN: |
| 4975 | if ((arg1->nodesetval == NULL) || |
| 4976 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4977 | else |
| 4978 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4979 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4980 | break; |
| 4981 | case XPATH_NUMBER: |
| 4982 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 4983 | break; |
| 4984 | case XPATH_STRING: |
| 4985 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 4986 | break; |
| 4987 | case XPATH_USERS: |
| 4988 | case XPATH_POINT: |
| 4989 | case XPATH_RANGE: |
| 4990 | case XPATH_LOCATIONSET: |
| 4991 | TODO |
| 4992 | break; |
| 4993 | } |
| 4994 | xmlXPathFreeObject(arg1); |
| 4995 | xmlXPathFreeObject(arg2); |
| 4996 | return(ret); |
| 4997 | } |
| 4998 | |
| 4999 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5000 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5001 | |
| 5002 | /** |
| 5003 | * xmlXPathCompareValues: |
| 5004 | * @ctxt: the XPath Parser context |
| 5005 | * @inf: less than (1) or greater than (0) |
| 5006 | * @strict: is the comparison strict |
| 5007 | * |
| 5008 | * Implement the compare operation on XPath objects: |
| 5009 | * @arg1 < @arg2 (1, 1, ... |
| 5010 | * @arg1 <= @arg2 (1, 0, ... |
| 5011 | * @arg1 > @arg2 (0, 1, ... |
| 5012 | * @arg1 >= @arg2 (0, 0, ... |
| 5013 | * |
| 5014 | * When neither object to be compared is a node-set and the operator is |
| 5015 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 5016 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 5017 | * comparison will be true if and only if the first number is less than the |
| 5018 | * second number. The <= comparison will be true if and only if the first |
| 5019 | * number is less than or equal to the second number. The > comparison |
| 5020 | * will be true if and only if the first number is greater than the second |
| 5021 | * number. The >= comparison will be true if and only if the first number |
| 5022 | * is greater than or equal to the second number. |
| 5023 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5024 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5025 | */ |
| 5026 | int |
| 5027 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5028 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5029 | xmlXPathObjectPtr arg1, arg2; |
| 5030 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5031 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5032 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5033 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5034 | if (arg1 != NULL) |
| 5035 | xmlXPathFreeObject(arg1); |
| 5036 | else |
| 5037 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5038 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5039 | } |
| 5040 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5041 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5042 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5043 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 5044 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5045 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5046 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5047 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5048 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 5049 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5050 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5051 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 5052 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
| 5055 | return(ret); |
| 5056 | } |
| 5057 | |
| 5058 | if (arg1->type != XPATH_NUMBER) { |
| 5059 | valuePush(ctxt, arg1); |
| 5060 | xmlXPathNumberFunction(ctxt, 1); |
| 5061 | arg1 = valuePop(ctxt); |
| 5062 | } |
| 5063 | if (arg1->type != XPATH_NUMBER) { |
| 5064 | xmlXPathFreeObject(arg1); |
| 5065 | xmlXPathFreeObject(arg2); |
| 5066 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5067 | } |
| 5068 | if (arg2->type != XPATH_NUMBER) { |
| 5069 | valuePush(ctxt, arg2); |
| 5070 | xmlXPathNumberFunction(ctxt, 1); |
| 5071 | arg2 = valuePop(ctxt); |
| 5072 | } |
| 5073 | if (arg2->type != XPATH_NUMBER) { |
| 5074 | xmlXPathFreeObject(arg1); |
| 5075 | xmlXPathFreeObject(arg2); |
| 5076 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5077 | } |
| 5078 | /* |
| 5079 | * Add tests for infinity and nan |
| 5080 | * => feedback on 3.4 for Inf and NaN |
| 5081 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5082 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5083 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5084 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5085 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5086 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5087 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5088 | if (inf && strict) { |
| 5089 | if ((arg1i == -1 && arg2i != -1) || |
| 5090 | (arg2i == 1 && arg1i != 1)) { |
| 5091 | ret = 1; |
| 5092 | } else if (arg1i == 0 && arg2i == 0) { |
| 5093 | ret = (arg1->floatval < arg2->floatval); |
| 5094 | } else { |
| 5095 | ret = 0; |
| 5096 | } |
| 5097 | } |
| 5098 | else if (inf && !strict) { |
| 5099 | if (arg1i == -1 || arg2i == 1) { |
| 5100 | ret = 1; |
| 5101 | } else if (arg1i == 0 && arg2i == 0) { |
| 5102 | ret = (arg1->floatval <= arg2->floatval); |
| 5103 | } else { |
| 5104 | ret = 0; |
| 5105 | } |
| 5106 | } |
| 5107 | else if (!inf && strict) { |
| 5108 | if ((arg1i == 1 && arg2i != 1) || |
| 5109 | (arg2i == -1 && arg1i != -1)) { |
| 5110 | ret = 1; |
| 5111 | } else if (arg1i == 0 && arg2i == 0) { |
| 5112 | ret = (arg1->floatval > arg2->floatval); |
| 5113 | } else { |
| 5114 | ret = 0; |
| 5115 | } |
| 5116 | } |
| 5117 | else if (!inf && !strict) { |
| 5118 | if (arg1i == 1 || arg2i == -1) { |
| 5119 | ret = 1; |
| 5120 | } else if (arg1i == 0 && arg2i == 0) { |
| 5121 | ret = (arg1->floatval >= arg2->floatval); |
| 5122 | } else { |
| 5123 | ret = 0; |
| 5124 | } |
| 5125 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5126 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | xmlXPathFreeObject(arg1); |
| 5128 | xmlXPathFreeObject(arg2); |
| 5129 | return(ret); |
| 5130 | } |
| 5131 | |
| 5132 | /** |
| 5133 | * xmlXPathValueFlipSign: |
| 5134 | * @ctxt: the XPath Parser context |
| 5135 | * |
| 5136 | * Implement the unary - operation on an XPath object |
| 5137 | * The numeric operators convert their operands to numbers as if |
| 5138 | * by calling the number function. |
| 5139 | */ |
| 5140 | void |
| 5141 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5142 | CAST_TO_NUMBER; |
| 5143 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5144 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5145 | ctxt->value->floatval=xmlXPathNAN; |
| 5146 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5147 | ctxt->value->floatval=xmlXPathNINF; |
| 5148 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5149 | ctxt->value->floatval=xmlXPathPINF; |
| 5150 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5151 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5152 | ctxt->value->floatval = xmlXPathNZERO; |
| 5153 | else |
| 5154 | ctxt->value->floatval = 0; |
| 5155 | } |
| 5156 | else |
| 5157 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /** |
| 5161 | * xmlXPathAddValues: |
| 5162 | * @ctxt: the XPath Parser context |
| 5163 | * |
| 5164 | * Implement the add operation on XPath objects: |
| 5165 | * The numeric operators convert their operands to numbers as if |
| 5166 | * by calling the number function. |
| 5167 | */ |
| 5168 | void |
| 5169 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5170 | xmlXPathObjectPtr arg; |
| 5171 | double val; |
| 5172 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5173 | arg = valuePop(ctxt); |
| 5174 | if (arg == NULL) |
| 5175 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5176 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | xmlXPathFreeObject(arg); |
| 5178 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5179 | CAST_TO_NUMBER; |
| 5180 | CHECK_TYPE(XPATH_NUMBER); |
| 5181 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /** |
| 5185 | * xmlXPathSubValues: |
| 5186 | * @ctxt: the XPath Parser context |
| 5187 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5188 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5189 | * The numeric operators convert their operands to numbers as if |
| 5190 | * by calling the number function. |
| 5191 | */ |
| 5192 | void |
| 5193 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5194 | xmlXPathObjectPtr arg; |
| 5195 | double val; |
| 5196 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5197 | arg = valuePop(ctxt); |
| 5198 | if (arg == NULL) |
| 5199 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5200 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5201 | xmlXPathFreeObject(arg); |
| 5202 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5203 | CAST_TO_NUMBER; |
| 5204 | CHECK_TYPE(XPATH_NUMBER); |
| 5205 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | /** |
| 5209 | * xmlXPathMultValues: |
| 5210 | * @ctxt: the XPath Parser context |
| 5211 | * |
| 5212 | * Implement the multiply operation on XPath objects: |
| 5213 | * The numeric operators convert their operands to numbers as if |
| 5214 | * by calling the number function. |
| 5215 | */ |
| 5216 | void |
| 5217 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5218 | xmlXPathObjectPtr arg; |
| 5219 | double val; |
| 5220 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5221 | arg = valuePop(ctxt); |
| 5222 | if (arg == NULL) |
| 5223 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5224 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5225 | xmlXPathFreeObject(arg); |
| 5226 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5227 | CAST_TO_NUMBER; |
| 5228 | CHECK_TYPE(XPATH_NUMBER); |
| 5229 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | /** |
| 5233 | * xmlXPathDivValues: |
| 5234 | * @ctxt: the XPath Parser context |
| 5235 | * |
| 5236 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5237 | * The numeric operators convert their operands to numbers as if |
| 5238 | * by calling the number function. |
| 5239 | */ |
| 5240 | void |
| 5241 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5242 | xmlXPathObjectPtr arg; |
| 5243 | double val; |
| 5244 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5245 | arg = valuePop(ctxt); |
| 5246 | if (arg == NULL) |
| 5247 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5248 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | xmlXPathFreeObject(arg); |
| 5250 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5251 | CAST_TO_NUMBER; |
| 5252 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5253 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5254 | ctxt->value->floatval = xmlXPathNAN; |
| 5255 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5256 | if (ctxt->value->floatval == 0) |
| 5257 | ctxt->value->floatval = xmlXPathNAN; |
| 5258 | else if (ctxt->value->floatval > 0) |
| 5259 | ctxt->value->floatval = xmlXPathNINF; |
| 5260 | else if (ctxt->value->floatval < 0) |
| 5261 | ctxt->value->floatval = xmlXPathPINF; |
| 5262 | } |
| 5263 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5264 | if (ctxt->value->floatval == 0) |
| 5265 | ctxt->value->floatval = xmlXPathNAN; |
| 5266 | else if (ctxt->value->floatval > 0) |
| 5267 | ctxt->value->floatval = xmlXPathPINF; |
| 5268 | else if (ctxt->value->floatval < 0) |
| 5269 | ctxt->value->floatval = xmlXPathNINF; |
| 5270 | } else |
| 5271 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | /** |
| 5275 | * xmlXPathModValues: |
| 5276 | * @ctxt: the XPath Parser context |
| 5277 | * |
| 5278 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5279 | * The numeric operators convert their operands to numbers as if |
| 5280 | * by calling the number function. |
| 5281 | */ |
| 5282 | void |
| 5283 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5284 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5285 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5286 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5287 | arg = valuePop(ctxt); |
| 5288 | if (arg == NULL) |
| 5289 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5290 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5291 | xmlXPathFreeObject(arg); |
| 5292 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5293 | CAST_TO_NUMBER; |
| 5294 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5295 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5296 | if (arg2 == 0) |
| 5297 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5298 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5299 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5300 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | /************************************************************************ |
| 5304 | * * |
| 5305 | * The traversal functions * |
| 5306 | * * |
| 5307 | ************************************************************************/ |
| 5308 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | /* |
| 5310 | * A traversal function enumerates nodes along an axis. |
| 5311 | * Initially it must be called with NULL, and it indicates |
| 5312 | * termination on the axis by returning NULL. |
| 5313 | */ |
| 5314 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5315 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5316 | |
| 5317 | /** |
| 5318 | * xmlXPathNextSelf: |
| 5319 | * @ctxt: the XPath Parser context |
| 5320 | * @cur: the current node in the traversal |
| 5321 | * |
| 5322 | * Traversal function for the "self" direction |
| 5323 | * The self axis contains just the context node itself |
| 5324 | * |
| 5325 | * Returns the next element following that axis |
| 5326 | */ |
| 5327 | xmlNodePtr |
| 5328 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5329 | if (cur == NULL) |
| 5330 | return(ctxt->context->node); |
| 5331 | return(NULL); |
| 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlXPathNextChild: |
| 5336 | * @ctxt: the XPath Parser context |
| 5337 | * @cur: the current node in the traversal |
| 5338 | * |
| 5339 | * Traversal function for the "child" direction |
| 5340 | * The child axis contains the children of the context node in document order. |
| 5341 | * |
| 5342 | * Returns the next element following that axis |
| 5343 | */ |
| 5344 | xmlNodePtr |
| 5345 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5346 | if (cur == NULL) { |
| 5347 | if (ctxt->context->node == NULL) return(NULL); |
| 5348 | switch (ctxt->context->node->type) { |
| 5349 | case XML_ELEMENT_NODE: |
| 5350 | case XML_TEXT_NODE: |
| 5351 | case XML_CDATA_SECTION_NODE: |
| 5352 | case XML_ENTITY_REF_NODE: |
| 5353 | case XML_ENTITY_NODE: |
| 5354 | case XML_PI_NODE: |
| 5355 | case XML_COMMENT_NODE: |
| 5356 | case XML_NOTATION_NODE: |
| 5357 | case XML_DTD_NODE: |
| 5358 | return(ctxt->context->node->children); |
| 5359 | case XML_DOCUMENT_NODE: |
| 5360 | case XML_DOCUMENT_TYPE_NODE: |
| 5361 | case XML_DOCUMENT_FRAG_NODE: |
| 5362 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5363 | #ifdef LIBXML_DOCB_ENABLED |
| 5364 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5365 | #endif |
| 5366 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5367 | case XML_ELEMENT_DECL: |
| 5368 | case XML_ATTRIBUTE_DECL: |
| 5369 | case XML_ENTITY_DECL: |
| 5370 | case XML_ATTRIBUTE_NODE: |
| 5371 | case XML_NAMESPACE_DECL: |
| 5372 | case XML_XINCLUDE_START: |
| 5373 | case XML_XINCLUDE_END: |
| 5374 | return(NULL); |
| 5375 | } |
| 5376 | return(NULL); |
| 5377 | } |
| 5378 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5379 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5380 | return(NULL); |
| 5381 | return(cur->next); |
| 5382 | } |
| 5383 | |
| 5384 | /** |
| 5385 | * xmlXPathNextDescendant: |
| 5386 | * @ctxt: the XPath Parser context |
| 5387 | * @cur: the current node in the traversal |
| 5388 | * |
| 5389 | * Traversal function for the "descendant" direction |
| 5390 | * the descendant axis contains the descendants of the context node in document |
| 5391 | * order; a descendant is a child or a child of a child and so on. |
| 5392 | * |
| 5393 | * Returns the next element following that axis |
| 5394 | */ |
| 5395 | xmlNodePtr |
| 5396 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5397 | if (cur == NULL) { |
| 5398 | if (ctxt->context->node == NULL) |
| 5399 | return(NULL); |
| 5400 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5401 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5402 | return(NULL); |
| 5403 | |
| 5404 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5405 | return(ctxt->context->doc->children); |
| 5406 | return(ctxt->context->node->children); |
| 5407 | } |
| 5408 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5409 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5410 | /* |
| 5411 | * Do not descend on entities declarations |
| 5412 | */ |
| 5413 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5414 | cur = cur->children; |
| 5415 | /* |
| 5416 | * Skip DTDs |
| 5417 | */ |
| 5418 | if (cur->type != XML_DTD_NODE) |
| 5419 | return(cur); |
| 5420 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | if (cur == ctxt->context->node) return(NULL); |
| 5424 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5425 | while (cur->next != NULL) { |
| 5426 | cur = cur->next; |
| 5427 | if ((cur->type != XML_ENTITY_DECL) && |
| 5428 | (cur->type != XML_DTD_NODE)) |
| 5429 | return(cur); |
| 5430 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5431 | |
| 5432 | do { |
| 5433 | cur = cur->parent; |
| 5434 | if (cur == NULL) return(NULL); |
| 5435 | if (cur == ctxt->context->node) return(NULL); |
| 5436 | if (cur->next != NULL) { |
| 5437 | cur = cur->next; |
| 5438 | return(cur); |
| 5439 | } |
| 5440 | } while (cur != NULL); |
| 5441 | return(cur); |
| 5442 | } |
| 5443 | |
| 5444 | /** |
| 5445 | * xmlXPathNextDescendantOrSelf: |
| 5446 | * @ctxt: the XPath Parser context |
| 5447 | * @cur: the current node in the traversal |
| 5448 | * |
| 5449 | * Traversal function for the "descendant-or-self" direction |
| 5450 | * the descendant-or-self axis contains the context node and the descendants |
| 5451 | * of the context node in document order; thus the context node is the first |
| 5452 | * node on the axis, and the first child of the context node is the second node |
| 5453 | * on the axis |
| 5454 | * |
| 5455 | * Returns the next element following that axis |
| 5456 | */ |
| 5457 | xmlNodePtr |
| 5458 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5459 | if (cur == NULL) { |
| 5460 | if (ctxt->context->node == NULL) |
| 5461 | return(NULL); |
| 5462 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5463 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5464 | return(NULL); |
| 5465 | return(ctxt->context->node); |
| 5466 | } |
| 5467 | |
| 5468 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5469 | } |
| 5470 | |
| 5471 | /** |
| 5472 | * xmlXPathNextParent: |
| 5473 | * @ctxt: the XPath Parser context |
| 5474 | * @cur: the current node in the traversal |
| 5475 | * |
| 5476 | * Traversal function for the "parent" direction |
| 5477 | * The parent axis contains the parent of the context node, if there is one. |
| 5478 | * |
| 5479 | * Returns the next element following that axis |
| 5480 | */ |
| 5481 | xmlNodePtr |
| 5482 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5483 | /* |
| 5484 | * the parent of an attribute or namespace node is the element |
| 5485 | * to which the attribute or namespace node is attached |
| 5486 | * Namespace handling !!! |
| 5487 | */ |
| 5488 | if (cur == NULL) { |
| 5489 | if (ctxt->context->node == NULL) return(NULL); |
| 5490 | switch (ctxt->context->node->type) { |
| 5491 | case XML_ELEMENT_NODE: |
| 5492 | case XML_TEXT_NODE: |
| 5493 | case XML_CDATA_SECTION_NODE: |
| 5494 | case XML_ENTITY_REF_NODE: |
| 5495 | case XML_ENTITY_NODE: |
| 5496 | case XML_PI_NODE: |
| 5497 | case XML_COMMENT_NODE: |
| 5498 | case XML_NOTATION_NODE: |
| 5499 | case XML_DTD_NODE: |
| 5500 | case XML_ELEMENT_DECL: |
| 5501 | case XML_ATTRIBUTE_DECL: |
| 5502 | case XML_XINCLUDE_START: |
| 5503 | case XML_XINCLUDE_END: |
| 5504 | case XML_ENTITY_DECL: |
| 5505 | if (ctxt->context->node->parent == NULL) |
| 5506 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5507 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5508 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5509 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5510 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5511 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5512 | return(ctxt->context->node->parent); |
| 5513 | case XML_ATTRIBUTE_NODE: { |
| 5514 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5515 | |
| 5516 | return(att->parent); |
| 5517 | } |
| 5518 | case XML_DOCUMENT_NODE: |
| 5519 | case XML_DOCUMENT_TYPE_NODE: |
| 5520 | case XML_DOCUMENT_FRAG_NODE: |
| 5521 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5522 | #ifdef LIBXML_DOCB_ENABLED |
| 5523 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5524 | #endif |
| 5525 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5526 | case XML_NAMESPACE_DECL: { |
| 5527 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5528 | |
| 5529 | if ((ns->next != NULL) && |
| 5530 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5531 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5532 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5533 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5534 | } |
| 5535 | } |
| 5536 | return(NULL); |
| 5537 | } |
| 5538 | |
| 5539 | /** |
| 5540 | * xmlXPathNextAncestor: |
| 5541 | * @ctxt: the XPath Parser context |
| 5542 | * @cur: the current node in the traversal |
| 5543 | * |
| 5544 | * Traversal function for the "ancestor" direction |
| 5545 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5546 | * of the context node consist of the parent of context node and the parent's |
| 5547 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5548 | * parent is the first node on the axis, and the parent's parent is the second |
| 5549 | * node on the axis |
| 5550 | * |
| 5551 | * Returns the next element following that axis |
| 5552 | */ |
| 5553 | xmlNodePtr |
| 5554 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5555 | /* |
| 5556 | * the parent of an attribute or namespace node is the element |
| 5557 | * to which the attribute or namespace node is attached |
| 5558 | * !!!!!!!!!!!!! |
| 5559 | */ |
| 5560 | if (cur == NULL) { |
| 5561 | if (ctxt->context->node == NULL) return(NULL); |
| 5562 | switch (ctxt->context->node->type) { |
| 5563 | case XML_ELEMENT_NODE: |
| 5564 | case XML_TEXT_NODE: |
| 5565 | case XML_CDATA_SECTION_NODE: |
| 5566 | case XML_ENTITY_REF_NODE: |
| 5567 | case XML_ENTITY_NODE: |
| 5568 | case XML_PI_NODE: |
| 5569 | case XML_COMMENT_NODE: |
| 5570 | case XML_DTD_NODE: |
| 5571 | case XML_ELEMENT_DECL: |
| 5572 | case XML_ATTRIBUTE_DECL: |
| 5573 | case XML_ENTITY_DECL: |
| 5574 | case XML_NOTATION_NODE: |
| 5575 | case XML_XINCLUDE_START: |
| 5576 | case XML_XINCLUDE_END: |
| 5577 | if (ctxt->context->node->parent == NULL) |
| 5578 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5579 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5580 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5581 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5582 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5583 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5584 | return(ctxt->context->node->parent); |
| 5585 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5586 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5587 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5588 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5589 | } |
| 5590 | case XML_DOCUMENT_NODE: |
| 5591 | case XML_DOCUMENT_TYPE_NODE: |
| 5592 | case XML_DOCUMENT_FRAG_NODE: |
| 5593 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5594 | #ifdef LIBXML_DOCB_ENABLED |
| 5595 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | #endif |
| 5597 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5598 | case XML_NAMESPACE_DECL: { |
| 5599 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5600 | |
| 5601 | if ((ns->next != NULL) && |
| 5602 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5603 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5604 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5605 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5606 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5607 | } |
| 5608 | return(NULL); |
| 5609 | } |
| 5610 | if (cur == ctxt->context->doc->children) |
| 5611 | return((xmlNodePtr) ctxt->context->doc); |
| 5612 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5613 | return(NULL); |
| 5614 | switch (cur->type) { |
| 5615 | case XML_ELEMENT_NODE: |
| 5616 | case XML_TEXT_NODE: |
| 5617 | case XML_CDATA_SECTION_NODE: |
| 5618 | case XML_ENTITY_REF_NODE: |
| 5619 | case XML_ENTITY_NODE: |
| 5620 | case XML_PI_NODE: |
| 5621 | case XML_COMMENT_NODE: |
| 5622 | case XML_NOTATION_NODE: |
| 5623 | case XML_DTD_NODE: |
| 5624 | case XML_ELEMENT_DECL: |
| 5625 | case XML_ATTRIBUTE_DECL: |
| 5626 | case XML_ENTITY_DECL: |
| 5627 | case XML_XINCLUDE_START: |
| 5628 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5629 | if (cur->parent == NULL) |
| 5630 | return(NULL); |
| 5631 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5632 | ((cur->parent->name[0] == ' ') || |
| 5633 | (xmlStrEqual(cur->parent->name, |
| 5634 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5635 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5636 | return(cur->parent); |
| 5637 | case XML_ATTRIBUTE_NODE: { |
| 5638 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5639 | |
| 5640 | return(att->parent); |
| 5641 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5642 | case XML_NAMESPACE_DECL: { |
| 5643 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5644 | |
| 5645 | if ((ns->next != NULL) && |
| 5646 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5647 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5648 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5649 | return(NULL); |
| 5650 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5651 | case XML_DOCUMENT_NODE: |
| 5652 | case XML_DOCUMENT_TYPE_NODE: |
| 5653 | case XML_DOCUMENT_FRAG_NODE: |
| 5654 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5655 | #ifdef LIBXML_DOCB_ENABLED |
| 5656 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | #endif |
| 5658 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5659 | } |
| 5660 | return(NULL); |
| 5661 | } |
| 5662 | |
| 5663 | /** |
| 5664 | * xmlXPathNextAncestorOrSelf: |
| 5665 | * @ctxt: the XPath Parser context |
| 5666 | * @cur: the current node in the traversal |
| 5667 | * |
| 5668 | * Traversal function for the "ancestor-or-self" direction |
| 5669 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5670 | * the context node in reverse document order; thus the context node is |
| 5671 | * the first node on the axis, and the context node's parent the second; |
| 5672 | * parent here is defined the same as with the parent axis. |
| 5673 | * |
| 5674 | * Returns the next element following that axis |
| 5675 | */ |
| 5676 | xmlNodePtr |
| 5677 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5678 | if (cur == NULL) |
| 5679 | return(ctxt->context->node); |
| 5680 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5681 | } |
| 5682 | |
| 5683 | /** |
| 5684 | * xmlXPathNextFollowingSibling: |
| 5685 | * @ctxt: the XPath Parser context |
| 5686 | * @cur: the current node in the traversal |
| 5687 | * |
| 5688 | * Traversal function for the "following-sibling" direction |
| 5689 | * The following-sibling axis contains the following siblings of the context |
| 5690 | * node in document order. |
| 5691 | * |
| 5692 | * Returns the next element following that axis |
| 5693 | */ |
| 5694 | xmlNodePtr |
| 5695 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5696 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5697 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5698 | return(NULL); |
| 5699 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5700 | return(NULL); |
| 5701 | if (cur == NULL) |
| 5702 | return(ctxt->context->node->next); |
| 5703 | return(cur->next); |
| 5704 | } |
| 5705 | |
| 5706 | /** |
| 5707 | * xmlXPathNextPrecedingSibling: |
| 5708 | * @ctxt: the XPath Parser context |
| 5709 | * @cur: the current node in the traversal |
| 5710 | * |
| 5711 | * Traversal function for the "preceding-sibling" direction |
| 5712 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5713 | * node in reverse document order; the first preceding sibling is first on the |
| 5714 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5715 | * |
| 5716 | * Returns the next element following that axis |
| 5717 | */ |
| 5718 | xmlNodePtr |
| 5719 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5720 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5721 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5722 | return(NULL); |
| 5723 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5724 | return(NULL); |
| 5725 | if (cur == NULL) |
| 5726 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5727 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5728 | cur = cur->prev; |
| 5729 | if (cur == NULL) |
| 5730 | return(ctxt->context->node->prev); |
| 5731 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5732 | return(cur->prev); |
| 5733 | } |
| 5734 | |
| 5735 | /** |
| 5736 | * xmlXPathNextFollowing: |
| 5737 | * @ctxt: the XPath Parser context |
| 5738 | * @cur: the current node in the traversal |
| 5739 | * |
| 5740 | * Traversal function for the "following" direction |
| 5741 | * The following axis contains all nodes in the same document as the context |
| 5742 | * node that are after the context node in document order, excluding any |
| 5743 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5744 | * are ordered in document order |
| 5745 | * |
| 5746 | * Returns the next element following that axis |
| 5747 | */ |
| 5748 | xmlNodePtr |
| 5749 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5750 | if (cur != NULL && cur->children != NULL) |
| 5751 | return cur->children ; |
| 5752 | if (cur == NULL) cur = ctxt->context->node; |
| 5753 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5754 | if (cur->next != NULL) return(cur->next) ; |
| 5755 | do { |
| 5756 | cur = cur->parent; |
| 5757 | if (cur == NULL) return(NULL); |
| 5758 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5759 | if (cur->next != NULL) return(cur->next); |
| 5760 | } while (cur != NULL); |
| 5761 | return(cur); |
| 5762 | } |
| 5763 | |
| 5764 | /* |
| 5765 | * xmlXPathIsAncestor: |
| 5766 | * @ancestor: the ancestor node |
| 5767 | * @node: the current node |
| 5768 | * |
| 5769 | * Check that @ancestor is a @node's ancestor |
| 5770 | * |
| 5771 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5772 | */ |
| 5773 | static int |
| 5774 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5775 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5776 | /* nodes need to be in the same document */ |
| 5777 | if (ancestor->doc != node->doc) return(0); |
| 5778 | /* avoid searching if ancestor or node is the root node */ |
| 5779 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5780 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5781 | while (node->parent != NULL) { |
| 5782 | if (node->parent == ancestor) |
| 5783 | return(1); |
| 5784 | node = node->parent; |
| 5785 | } |
| 5786 | return(0); |
| 5787 | } |
| 5788 | |
| 5789 | /** |
| 5790 | * xmlXPathNextPreceding: |
| 5791 | * @ctxt: the XPath Parser context |
| 5792 | * @cur: the current node in the traversal |
| 5793 | * |
| 5794 | * Traversal function for the "preceding" direction |
| 5795 | * the preceding axis contains all nodes in the same document as the context |
| 5796 | * node that are before the context node in document order, excluding any |
| 5797 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5798 | * ordered in reverse document order |
| 5799 | * |
| 5800 | * Returns the next element following that axis |
| 5801 | */ |
| 5802 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5803 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5804 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5805 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5806 | cur = ctxt->context->node; |
| 5807 | if (cur == NULL) |
| 5808 | return (NULL); |
| 5809 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5810 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5811 | do { |
| 5812 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5813 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5814 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5818 | if (cur == NULL) |
| 5819 | return (NULL); |
| 5820 | if (cur == ctxt->context->doc->children) |
| 5821 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5822 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5823 | return (cur); |
| 5824 | } |
| 5825 | |
| 5826 | /** |
| 5827 | * xmlXPathNextPrecedingInternal: |
| 5828 | * @ctxt: the XPath Parser context |
| 5829 | * @cur: the current node in the traversal |
| 5830 | * |
| 5831 | * Traversal function for the "preceding" direction |
| 5832 | * the preceding axis contains all nodes in the same document as the context |
| 5833 | * node that are before the context node in document order, excluding any |
| 5834 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5835 | * ordered in reverse document order |
| 5836 | * This is a faster implementation but internal only since it requires a |
| 5837 | * state kept in the parser context: ctxt->ancestor. |
| 5838 | * |
| 5839 | * Returns the next element following that axis |
| 5840 | */ |
| 5841 | static xmlNodePtr |
| 5842 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5843 | xmlNodePtr cur) |
| 5844 | { |
| 5845 | if (cur == NULL) { |
| 5846 | cur = ctxt->context->node; |
| 5847 | if (cur == NULL) |
| 5848 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 5849 | if (cur->type == XML_NAMESPACE_DECL) |
| 5850 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5851 | ctxt->ancestor = cur->parent; |
| 5852 | } |
| 5853 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5854 | cur = cur->prev; |
| 5855 | while (cur->prev == NULL) { |
| 5856 | cur = cur->parent; |
| 5857 | if (cur == NULL) |
| 5858 | return (NULL); |
| 5859 | if (cur == ctxt->context->doc->children) |
| 5860 | return (NULL); |
| 5861 | if (cur != ctxt->ancestor) |
| 5862 | return (cur); |
| 5863 | ctxt->ancestor = cur->parent; |
| 5864 | } |
| 5865 | cur = cur->prev; |
| 5866 | while (cur->last != NULL) |
| 5867 | cur = cur->last; |
| 5868 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * xmlXPathNextNamespace: |
| 5873 | * @ctxt: the XPath Parser context |
| 5874 | * @cur: the current attribute in the traversal |
| 5875 | * |
| 5876 | * Traversal function for the "namespace" direction |
| 5877 | * the namespace axis contains the namespace nodes of the context node; |
| 5878 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5879 | * be empty unless the context node is an element |
| 5880 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5881 | * We keep the XML namespace node at the end of the list. |
| 5882 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5883 | * Returns the next element following that axis |
| 5884 | */ |
| 5885 | xmlNodePtr |
| 5886 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5887 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5888 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5889 | if (ctxt->context->tmpNsList != NULL) |
| 5890 | xmlFree(ctxt->context->tmpNsList); |
| 5891 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5892 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5893 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5894 | if (ctxt->context->tmpNsList != NULL) { |
| 5895 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 5896 | ctxt->context->tmpNsNr++; |
| 5897 | } |
| 5898 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5899 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5900 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5901 | if (ctxt->context->tmpNsNr > 0) { |
| 5902 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 5903 | } else { |
| 5904 | if (ctxt->context->tmpNsList != NULL) |
| 5905 | xmlFree(ctxt->context->tmpNsList); |
| 5906 | ctxt->context->tmpNsList = NULL; |
| 5907 | return(NULL); |
| 5908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | /** |
| 5912 | * xmlXPathNextAttribute: |
| 5913 | * @ctxt: the XPath Parser context |
| 5914 | * @cur: the current attribute in the traversal |
| 5915 | * |
| 5916 | * Traversal function for the "attribute" direction |
| 5917 | * TODO: support DTD inherited default attributes |
| 5918 | * |
| 5919 | * Returns the next element following that axis |
| 5920 | */ |
| 5921 | xmlNodePtr |
| 5922 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5923 | if (ctxt->context->node == NULL) |
| 5924 | return(NULL); |
| 5925 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 5926 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5927 | if (cur == NULL) { |
| 5928 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5929 | return(NULL); |
| 5930 | return((xmlNodePtr)ctxt->context->node->properties); |
| 5931 | } |
| 5932 | return((xmlNodePtr)cur->next); |
| 5933 | } |
| 5934 | |
| 5935 | /************************************************************************ |
| 5936 | * * |
| 5937 | * NodeTest Functions * |
| 5938 | * * |
| 5939 | ************************************************************************/ |
| 5940 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5941 | #define IS_FUNCTION 200 |
| 5942 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5943 | |
| 5944 | /************************************************************************ |
| 5945 | * * |
| 5946 | * Implicit tree core function library * |
| 5947 | * * |
| 5948 | ************************************************************************/ |
| 5949 | |
| 5950 | /** |
| 5951 | * xmlXPathRoot: |
| 5952 | * @ctxt: the XPath Parser context |
| 5953 | * |
| 5954 | * Initialize the context to the root of the document |
| 5955 | */ |
| 5956 | void |
| 5957 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
| 5958 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 5959 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5960 | } |
| 5961 | |
| 5962 | /************************************************************************ |
| 5963 | * * |
| 5964 | * The explicit core function library * |
| 5965 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 5966 | * * |
| 5967 | ************************************************************************/ |
| 5968 | |
| 5969 | |
| 5970 | /** |
| 5971 | * xmlXPathLastFunction: |
| 5972 | * @ctxt: the XPath Parser context |
| 5973 | * @nargs: the number of arguments |
| 5974 | * |
| 5975 | * Implement the last() XPath function |
| 5976 | * number last() |
| 5977 | * The last function returns the number of nodes in the context node list. |
| 5978 | */ |
| 5979 | void |
| 5980 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5981 | CHECK_ARITY(0); |
| 5982 | if (ctxt->context->contextSize >= 0) { |
| 5983 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 5984 | #ifdef DEBUG_EXPR |
| 5985 | xmlGenericError(xmlGenericErrorContext, |
| 5986 | "last() : %d\n", ctxt->context->contextSize); |
| 5987 | #endif |
| 5988 | } else { |
| 5989 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | /** |
| 5994 | * xmlXPathPositionFunction: |
| 5995 | * @ctxt: the XPath Parser context |
| 5996 | * @nargs: the number of arguments |
| 5997 | * |
| 5998 | * Implement the position() XPath function |
| 5999 | * number position() |
| 6000 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6001 | * 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] | 6002 | * will be equal to last(). |
| 6003 | */ |
| 6004 | void |
| 6005 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6006 | CHECK_ARITY(0); |
| 6007 | if (ctxt->context->proximityPosition >= 0) { |
| 6008 | valuePush(ctxt, |
| 6009 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 6010 | #ifdef DEBUG_EXPR |
| 6011 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 6012 | ctxt->context->proximityPosition); |
| 6013 | #endif |
| 6014 | } else { |
| 6015 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | /** |
| 6020 | * xmlXPathCountFunction: |
| 6021 | * @ctxt: the XPath Parser context |
| 6022 | * @nargs: the number of arguments |
| 6023 | * |
| 6024 | * Implement the count() XPath function |
| 6025 | * number count(node-set) |
| 6026 | */ |
| 6027 | void |
| 6028 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6029 | xmlXPathObjectPtr cur; |
| 6030 | |
| 6031 | CHECK_ARITY(1); |
| 6032 | if ((ctxt->value == NULL) || |
| 6033 | ((ctxt->value->type != XPATH_NODESET) && |
| 6034 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6035 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6036 | cur = valuePop(ctxt); |
| 6037 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6038 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 6039 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6040 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6041 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 6042 | } else { |
| 6043 | if ((cur->nodesetval->nodeNr != 1) || |
| 6044 | (cur->nodesetval->nodeTab == NULL)) { |
| 6045 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 6046 | } else { |
| 6047 | xmlNodePtr tmp; |
| 6048 | int i = 0; |
| 6049 | |
| 6050 | tmp = cur->nodesetval->nodeTab[0]; |
| 6051 | if (tmp != NULL) { |
| 6052 | tmp = tmp->children; |
| 6053 | while (tmp != NULL) { |
| 6054 | tmp = tmp->next; |
| 6055 | i++; |
| 6056 | } |
| 6057 | } |
| 6058 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6059 | } |
| 6060 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6061 | xmlXPathFreeObject(cur); |
| 6062 | } |
| 6063 | |
| 6064 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6065 | * xmlXPathGetElementsByIds: |
| 6066 | * @doc: the document |
| 6067 | * @ids: a whitespace separated list of IDs |
| 6068 | * |
| 6069 | * Selects elements by their unique ID. |
| 6070 | * |
| 6071 | * Returns a node-set of selected elements. |
| 6072 | */ |
| 6073 | static xmlNodeSetPtr |
| 6074 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6075 | xmlNodeSetPtr ret; |
| 6076 | const xmlChar *cur = ids; |
| 6077 | xmlChar *ID; |
| 6078 | xmlAttrPtr attr; |
| 6079 | xmlNodePtr elem = NULL; |
| 6080 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6081 | if (ids == NULL) return(NULL); |
| 6082 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6083 | ret = xmlXPathNodeSetCreate(NULL); |
| 6084 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6085 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6086 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6087 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6088 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6089 | |
| 6090 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6091 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 6092 | /* |
| 6093 | * We used to check the fact that the value passed |
| 6094 | * was an NCName, but this generated much troubles for |
| 6095 | * me and Aleksey Sanin, people blatantly violated that |
| 6096 | * constaint, like Visa3D spec. |
| 6097 | * if (xmlValidateNCName(ID, 1) == 0) |
| 6098 | */ |
| 6099 | attr = xmlGetID(doc, ID); |
| 6100 | if (attr != NULL) { |
| 6101 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6102 | elem = attr->parent; |
| 6103 | else if (attr->type == XML_ELEMENT_NODE) |
| 6104 | elem = (xmlNodePtr) attr; |
| 6105 | else |
| 6106 | elem = NULL; |
| 6107 | if (elem != NULL) |
| 6108 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6109 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6110 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6111 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6112 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6113 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6114 | ids = cur; |
| 6115 | } |
| 6116 | return(ret); |
| 6117 | } |
| 6118 | |
| 6119 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6120 | * xmlXPathIdFunction: |
| 6121 | * @ctxt: the XPath Parser context |
| 6122 | * @nargs: the number of arguments |
| 6123 | * |
| 6124 | * Implement the id() XPath function |
| 6125 | * node-set id(object) |
| 6126 | * The id function selects elements by their unique ID |
| 6127 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6128 | * then the result is the union of the result of applying id to the |
| 6129 | * string value of each of the nodes in the argument node-set. When the |
| 6130 | * argument to id is of any other type, the argument is converted to a |
| 6131 | * string as if by a call to the string function; the string is split |
| 6132 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6133 | * of characters matching the production S); the result is a node-set |
| 6134 | * containing the elements in the same document as the context node that |
| 6135 | * have a unique ID equal to any of the tokens in the list. |
| 6136 | */ |
| 6137 | void |
| 6138 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6139 | xmlChar *tokens; |
| 6140 | xmlNodeSetPtr ret; |
| 6141 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6142 | |
| 6143 | CHECK_ARITY(1); |
| 6144 | obj = valuePop(ctxt); |
| 6145 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6146 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6147 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6148 | int i; |
| 6149 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6150 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6151 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6152 | if (obj->nodesetval != NULL) { |
| 6153 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6154 | tokens = |
| 6155 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6156 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6157 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6158 | xmlXPathFreeNodeSet(ns); |
| 6159 | if (tokens != NULL) |
| 6160 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6161 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6165 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6166 | return; |
| 6167 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6168 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6169 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6170 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6171 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6173 | xmlXPathFreeObject(obj); |
| 6174 | return; |
| 6175 | } |
| 6176 | |
| 6177 | /** |
| 6178 | * xmlXPathLocalNameFunction: |
| 6179 | * @ctxt: the XPath Parser context |
| 6180 | * @nargs: the number of arguments |
| 6181 | * |
| 6182 | * Implement the local-name() XPath function |
| 6183 | * string local-name(node-set?) |
| 6184 | * The local-name function returns a string containing the local part |
| 6185 | * of the name of the node in the argument node-set that is first in |
| 6186 | * document order. If the node-set is empty or the first node has no |
| 6187 | * name, an empty string is returned. If the argument is omitted it |
| 6188 | * defaults to the context node. |
| 6189 | */ |
| 6190 | void |
| 6191 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6192 | xmlXPathObjectPtr cur; |
| 6193 | |
| 6194 | if (nargs == 0) { |
| 6195 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6196 | nargs = 1; |
| 6197 | } |
| 6198 | |
| 6199 | CHECK_ARITY(1); |
| 6200 | if ((ctxt->value == NULL) || |
| 6201 | ((ctxt->value->type != XPATH_NODESET) && |
| 6202 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6203 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6204 | cur = valuePop(ctxt); |
| 6205 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6206 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6207 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6208 | } else { |
| 6209 | int i = 0; /* Should be first in document order !!!!! */ |
| 6210 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6211 | case XML_ELEMENT_NODE: |
| 6212 | case XML_ATTRIBUTE_NODE: |
| 6213 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6214 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6215 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6216 | else |
| 6217 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6219 | break; |
| 6220 | case XML_NAMESPACE_DECL: |
| 6221 | valuePush(ctxt, xmlXPathNewString( |
| 6222 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6223 | break; |
| 6224 | default: |
| 6225 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6226 | } |
| 6227 | } |
| 6228 | xmlXPathFreeObject(cur); |
| 6229 | } |
| 6230 | |
| 6231 | /** |
| 6232 | * xmlXPathNamespaceURIFunction: |
| 6233 | * @ctxt: the XPath Parser context |
| 6234 | * @nargs: the number of arguments |
| 6235 | * |
| 6236 | * Implement the namespace-uri() XPath function |
| 6237 | * string namespace-uri(node-set?) |
| 6238 | * The namespace-uri function returns a string containing the |
| 6239 | * namespace URI of the expanded name of the node in the argument |
| 6240 | * node-set that is first in document order. If the node-set is empty, |
| 6241 | * the first node has no name, or the expanded name has no namespace |
| 6242 | * URI, an empty string is returned. If the argument is omitted it |
| 6243 | * defaults to the context node. |
| 6244 | */ |
| 6245 | void |
| 6246 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6247 | xmlXPathObjectPtr cur; |
| 6248 | |
| 6249 | if (nargs == 0) { |
| 6250 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6251 | nargs = 1; |
| 6252 | } |
| 6253 | CHECK_ARITY(1); |
| 6254 | if ((ctxt->value == NULL) || |
| 6255 | ((ctxt->value->type != XPATH_NODESET) && |
| 6256 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6257 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6258 | cur = valuePop(ctxt); |
| 6259 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6260 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6262 | } else { |
| 6263 | int i = 0; /* Should be first in document order !!!!! */ |
| 6264 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6265 | case XML_ELEMENT_NODE: |
| 6266 | case XML_ATTRIBUTE_NODE: |
| 6267 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6268 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6269 | else |
| 6270 | valuePush(ctxt, xmlXPathNewString( |
| 6271 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6272 | break; |
| 6273 | default: |
| 6274 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6275 | } |
| 6276 | } |
| 6277 | xmlXPathFreeObject(cur); |
| 6278 | } |
| 6279 | |
| 6280 | /** |
| 6281 | * xmlXPathNameFunction: |
| 6282 | * @ctxt: the XPath Parser context |
| 6283 | * @nargs: the number of arguments |
| 6284 | * |
| 6285 | * Implement the name() XPath function |
| 6286 | * string name(node-set?) |
| 6287 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6288 | * 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] | 6289 | * order. The QName must represent the name with respect to the namespace |
| 6290 | * declarations in effect on the node whose name is being represented. |
| 6291 | * Typically, this will be the form in which the name occurred in the XML |
| 6292 | * source. This need not be the case if there are namespace declarations |
| 6293 | * in effect on the node that associate multiple prefixes with the same |
| 6294 | * namespace. However, an implementation may include information about |
| 6295 | * the original prefix in its representation of nodes; in this case, an |
| 6296 | * implementation can ensure that the returned string is always the same |
| 6297 | * as the QName used in the XML source. If the argument it omitted it |
| 6298 | * defaults to the context node. |
| 6299 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6300 | * returned. |
| 6301 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6302 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6303 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6304 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6305 | xmlXPathObjectPtr cur; |
| 6306 | |
| 6307 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6308 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6309 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6313 | if ((ctxt->value == NULL) || |
| 6314 | ((ctxt->value->type != XPATH_NODESET) && |
| 6315 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6316 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6317 | cur = valuePop(ctxt); |
| 6318 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6319 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6320 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6321 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6322 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6323 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6324 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6325 | case XML_ELEMENT_NODE: |
| 6326 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6327 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6328 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6329 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6330 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6331 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6332 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6333 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6334 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6335 | xmlChar *fullname; |
| 6336 | |
| 6337 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6338 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6339 | NULL, 0); |
| 6340 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6341 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6342 | if (fullname == NULL) { |
| 6343 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6344 | } |
| 6345 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6346 | } |
| 6347 | break; |
| 6348 | default: |
| 6349 | valuePush(ctxt, |
| 6350 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6351 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6352 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6353 | } |
| 6354 | xmlXPathFreeObject(cur); |
| 6355 | } |
| 6356 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6357 | |
| 6358 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6359 | * xmlXPathStringFunction: |
| 6360 | * @ctxt: the XPath Parser context |
| 6361 | * @nargs: the number of arguments |
| 6362 | * |
| 6363 | * Implement the string() XPath function |
| 6364 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6365 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | * - A node-set is converted to a string by returning the value of |
| 6367 | * the node in the node-set that is first in document order. |
| 6368 | * If the node-set is empty, an empty string is returned. |
| 6369 | * - A number is converted to a string as follows |
| 6370 | * + NaN is converted to the string NaN |
| 6371 | * + positive zero is converted to the string 0 |
| 6372 | * + negative zero is converted to the string 0 |
| 6373 | * + positive infinity is converted to the string Infinity |
| 6374 | * + negative infinity is converted to the string -Infinity |
| 6375 | * + if the number is an integer, the number is represented in |
| 6376 | * decimal form as a Number with no decimal point and no leading |
| 6377 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6378 | * + otherwise, the number is represented in decimal form as a |
| 6379 | * Number including a decimal point with at least one digit |
| 6380 | * before the decimal point and at least one digit after the |
| 6381 | * decimal point, preceded by a minus sign (-) if the number |
| 6382 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6383 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6384 | * before the decimal point; beyond the one required digit |
| 6385 | * after the decimal point there must be as many, but only as |
| 6386 | * many, more digits as are needed to uniquely distinguish the |
| 6387 | * number from all other IEEE 754 numeric values. |
| 6388 | * - The boolean false value is converted to the string false. |
| 6389 | * The boolean true value is converted to the string true. |
| 6390 | * |
| 6391 | * If the argument is omitted, it defaults to a node-set with the |
| 6392 | * context node as its only member. |
| 6393 | */ |
| 6394 | void |
| 6395 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6396 | xmlXPathObjectPtr cur; |
| 6397 | |
| 6398 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6399 | valuePush(ctxt, |
| 6400 | xmlXPathWrapString( |
| 6401 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6402 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | CHECK_ARITY(1); |
| 6406 | cur = valuePop(ctxt); |
| 6407 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6408 | cur = xmlXPathConvertString(cur); |
| 6409 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | /** |
| 6413 | * xmlXPathStringLengthFunction: |
| 6414 | * @ctxt: the XPath Parser context |
| 6415 | * @nargs: the number of arguments |
| 6416 | * |
| 6417 | * Implement the string-length() XPath function |
| 6418 | * number string-length(string?) |
| 6419 | * The string-length returns the number of characters in the string |
| 6420 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6421 | * the context node converted to a string, in other words the value |
| 6422 | * of the context node. |
| 6423 | */ |
| 6424 | void |
| 6425 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6426 | xmlXPathObjectPtr cur; |
| 6427 | |
| 6428 | if (nargs == 0) { |
| 6429 | if (ctxt->context->node == NULL) { |
| 6430 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6431 | } else { |
| 6432 | xmlChar *content; |
| 6433 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6434 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6435 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6436 | xmlFree(content); |
| 6437 | } |
| 6438 | return; |
| 6439 | } |
| 6440 | CHECK_ARITY(1); |
| 6441 | CAST_TO_STRING; |
| 6442 | CHECK_TYPE(XPATH_STRING); |
| 6443 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6444 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6445 | xmlXPathFreeObject(cur); |
| 6446 | } |
| 6447 | |
| 6448 | /** |
| 6449 | * xmlXPathConcatFunction: |
| 6450 | * @ctxt: the XPath Parser context |
| 6451 | * @nargs: the number of arguments |
| 6452 | * |
| 6453 | * Implement the concat() XPath function |
| 6454 | * string concat(string, string, string*) |
| 6455 | * The concat function returns the concatenation of its arguments. |
| 6456 | */ |
| 6457 | void |
| 6458 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6459 | xmlXPathObjectPtr cur, newobj; |
| 6460 | xmlChar *tmp; |
| 6461 | |
| 6462 | if (nargs < 2) { |
| 6463 | CHECK_ARITY(2); |
| 6464 | } |
| 6465 | |
| 6466 | CAST_TO_STRING; |
| 6467 | cur = valuePop(ctxt); |
| 6468 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6469 | xmlXPathFreeObject(cur); |
| 6470 | return; |
| 6471 | } |
| 6472 | nargs--; |
| 6473 | |
| 6474 | while (nargs > 0) { |
| 6475 | CAST_TO_STRING; |
| 6476 | newobj = valuePop(ctxt); |
| 6477 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6478 | xmlXPathFreeObject(newobj); |
| 6479 | xmlXPathFreeObject(cur); |
| 6480 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6481 | } |
| 6482 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6483 | newobj->stringval = cur->stringval; |
| 6484 | cur->stringval = tmp; |
| 6485 | |
| 6486 | xmlXPathFreeObject(newobj); |
| 6487 | nargs--; |
| 6488 | } |
| 6489 | valuePush(ctxt, cur); |
| 6490 | } |
| 6491 | |
| 6492 | /** |
| 6493 | * xmlXPathContainsFunction: |
| 6494 | * @ctxt: the XPath Parser context |
| 6495 | * @nargs: the number of arguments |
| 6496 | * |
| 6497 | * Implement the contains() XPath function |
| 6498 | * boolean contains(string, string) |
| 6499 | * The contains function returns true if the first argument string |
| 6500 | * contains the second argument string, and otherwise returns false. |
| 6501 | */ |
| 6502 | void |
| 6503 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6504 | xmlXPathObjectPtr hay, needle; |
| 6505 | |
| 6506 | CHECK_ARITY(2); |
| 6507 | CAST_TO_STRING; |
| 6508 | CHECK_TYPE(XPATH_STRING); |
| 6509 | needle = valuePop(ctxt); |
| 6510 | CAST_TO_STRING; |
| 6511 | hay = valuePop(ctxt); |
| 6512 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6513 | xmlXPathFreeObject(hay); |
| 6514 | xmlXPathFreeObject(needle); |
| 6515 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6516 | } |
| 6517 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6518 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6519 | else |
| 6520 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6521 | xmlXPathFreeObject(hay); |
| 6522 | xmlXPathFreeObject(needle); |
| 6523 | } |
| 6524 | |
| 6525 | /** |
| 6526 | * xmlXPathStartsWithFunction: |
| 6527 | * @ctxt: the XPath Parser context |
| 6528 | * @nargs: the number of arguments |
| 6529 | * |
| 6530 | * Implement the starts-with() XPath function |
| 6531 | * boolean starts-with(string, string) |
| 6532 | * The starts-with function returns true if the first argument string |
| 6533 | * starts with the second argument string, and otherwise returns false. |
| 6534 | */ |
| 6535 | void |
| 6536 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6537 | xmlXPathObjectPtr hay, needle; |
| 6538 | int n; |
| 6539 | |
| 6540 | CHECK_ARITY(2); |
| 6541 | CAST_TO_STRING; |
| 6542 | CHECK_TYPE(XPATH_STRING); |
| 6543 | needle = valuePop(ctxt); |
| 6544 | CAST_TO_STRING; |
| 6545 | hay = valuePop(ctxt); |
| 6546 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6547 | xmlXPathFreeObject(hay); |
| 6548 | xmlXPathFreeObject(needle); |
| 6549 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6550 | } |
| 6551 | n = xmlStrlen(needle->stringval); |
| 6552 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6553 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6554 | else |
| 6555 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6556 | xmlXPathFreeObject(hay); |
| 6557 | xmlXPathFreeObject(needle); |
| 6558 | } |
| 6559 | |
| 6560 | /** |
| 6561 | * xmlXPathSubstringFunction: |
| 6562 | * @ctxt: the XPath Parser context |
| 6563 | * @nargs: the number of arguments |
| 6564 | * |
| 6565 | * Implement the substring() XPath function |
| 6566 | * string substring(string, number, number?) |
| 6567 | * The substring function returns the substring of the first argument |
| 6568 | * starting at the position specified in the second argument with |
| 6569 | * length specified in the third argument. For example, |
| 6570 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6571 | * specified, it returns the substring starting at the position specified |
| 6572 | * in the second argument and continuing to the end of the string. For |
| 6573 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6574 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6575 | * numeric position: the position of the first character is 1, the position |
| 6576 | * of the second character is 2 and so on. The returned substring contains |
| 6577 | * those characters for which the position of the character is greater than |
| 6578 | * or equal to the second argument and, if the third argument is specified, |
| 6579 | * less than the sum of the second and third arguments; the comparisons |
| 6580 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6581 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6582 | * - substring("12345", 0, 3) returns "12" |
| 6583 | * - substring("12345", 0 div 0, 3) returns "" |
| 6584 | * - substring("12345", 1, 0 div 0) returns "" |
| 6585 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6586 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6587 | */ |
| 6588 | void |
| 6589 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6590 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6591 | double le=0, in; |
| 6592 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6593 | xmlChar *ret; |
| 6594 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6595 | if (nargs < 2) { |
| 6596 | CHECK_ARITY(2); |
| 6597 | } |
| 6598 | if (nargs > 3) { |
| 6599 | CHECK_ARITY(3); |
| 6600 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6601 | /* |
| 6602 | * take care of possible last (position) argument |
| 6603 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | if (nargs == 3) { |
| 6605 | CAST_TO_NUMBER; |
| 6606 | CHECK_TYPE(XPATH_NUMBER); |
| 6607 | len = valuePop(ctxt); |
| 6608 | le = len->floatval; |
| 6609 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6610 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6611 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6612 | CAST_TO_NUMBER; |
| 6613 | CHECK_TYPE(XPATH_NUMBER); |
| 6614 | start = valuePop(ctxt); |
| 6615 | in = start->floatval; |
| 6616 | xmlXPathFreeObject(start); |
| 6617 | CAST_TO_STRING; |
| 6618 | CHECK_TYPE(XPATH_STRING); |
| 6619 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6620 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6621 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6622 | /* |
| 6623 | * If last pos not present, calculate last position |
| 6624 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6625 | if (nargs != 3) { |
| 6626 | le = (double)m; |
| 6627 | if (in < 1.0) |
| 6628 | in = 1.0; |
| 6629 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6630 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6631 | /* Need to check for the special cases where either |
| 6632 | * the index is NaN, the length is NaN, or both |
| 6633 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6634 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6635 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6636 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6637 | * To meet the requirements of the spec, the arguments |
| 6638 | * must be converted to integer format before |
| 6639 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6640 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6641 | * First we go to integer form, rounding up |
| 6642 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6643 | */ |
| 6644 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6645 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6647 | if (xmlXPathIsInf(le) == 1) { |
| 6648 | l = m; |
| 6649 | if (i < 1) |
| 6650 | i = 1; |
| 6651 | } |
| 6652 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 6653 | l = 0; |
| 6654 | else { |
| 6655 | l = (int) le; |
| 6656 | if (((double)l)+0.5 <= le) l++; |
| 6657 | } |
| 6658 | |
| 6659 | /* Now we normalize inidices */ |
| 6660 | i -= 1; |
| 6661 | l += i; |
| 6662 | if (i < 0) |
| 6663 | i = 0; |
| 6664 | if (l > m) |
| 6665 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6666 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6667 | /* number of chars to copy */ |
| 6668 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6669 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6670 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6671 | } |
| 6672 | else { |
| 6673 | ret = NULL; |
| 6674 | } |
| 6675 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6676 | if (ret == NULL) |
| 6677 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6678 | else { |
| 6679 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6680 | xmlFree(ret); |
| 6681 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6682 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6683 | xmlXPathFreeObject(str); |
| 6684 | } |
| 6685 | |
| 6686 | /** |
| 6687 | * xmlXPathSubstringBeforeFunction: |
| 6688 | * @ctxt: the XPath Parser context |
| 6689 | * @nargs: the number of arguments |
| 6690 | * |
| 6691 | * Implement the substring-before() XPath function |
| 6692 | * string substring-before(string, string) |
| 6693 | * The substring-before function returns the substring of the first |
| 6694 | * argument string that precedes the first occurrence of the second |
| 6695 | * argument string in the first argument string, or the empty string |
| 6696 | * if the first argument string does not contain the second argument |
| 6697 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6698 | */ |
| 6699 | void |
| 6700 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6701 | xmlXPathObjectPtr str; |
| 6702 | xmlXPathObjectPtr find; |
| 6703 | xmlBufferPtr target; |
| 6704 | const xmlChar *point; |
| 6705 | int offset; |
| 6706 | |
| 6707 | CHECK_ARITY(2); |
| 6708 | CAST_TO_STRING; |
| 6709 | find = valuePop(ctxt); |
| 6710 | CAST_TO_STRING; |
| 6711 | str = valuePop(ctxt); |
| 6712 | |
| 6713 | target = xmlBufferCreate(); |
| 6714 | if (target) { |
| 6715 | point = xmlStrstr(str->stringval, find->stringval); |
| 6716 | if (point) { |
| 6717 | offset = (int)(point - str->stringval); |
| 6718 | xmlBufferAdd(target, str->stringval, offset); |
| 6719 | } |
| 6720 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6721 | xmlBufferFree(target); |
| 6722 | } |
| 6723 | |
| 6724 | xmlXPathFreeObject(str); |
| 6725 | xmlXPathFreeObject(find); |
| 6726 | } |
| 6727 | |
| 6728 | /** |
| 6729 | * xmlXPathSubstringAfterFunction: |
| 6730 | * @ctxt: the XPath Parser context |
| 6731 | * @nargs: the number of arguments |
| 6732 | * |
| 6733 | * Implement the substring-after() XPath function |
| 6734 | * string substring-after(string, string) |
| 6735 | * The substring-after function returns the substring of the first |
| 6736 | * argument string that follows the first occurrence of the second |
| 6737 | * argument string in the first argument string, or the empty stringi |
| 6738 | * if the first argument string does not contain the second argument |
| 6739 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6740 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6741 | */ |
| 6742 | void |
| 6743 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6744 | xmlXPathObjectPtr str; |
| 6745 | xmlXPathObjectPtr find; |
| 6746 | xmlBufferPtr target; |
| 6747 | const xmlChar *point; |
| 6748 | int offset; |
| 6749 | |
| 6750 | CHECK_ARITY(2); |
| 6751 | CAST_TO_STRING; |
| 6752 | find = valuePop(ctxt); |
| 6753 | CAST_TO_STRING; |
| 6754 | str = valuePop(ctxt); |
| 6755 | |
| 6756 | target = xmlBufferCreate(); |
| 6757 | if (target) { |
| 6758 | point = xmlStrstr(str->stringval, find->stringval); |
| 6759 | if (point) { |
| 6760 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6761 | xmlBufferAdd(target, &str->stringval[offset], |
| 6762 | xmlStrlen(str->stringval) - offset); |
| 6763 | } |
| 6764 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6765 | xmlBufferFree(target); |
| 6766 | } |
| 6767 | |
| 6768 | xmlXPathFreeObject(str); |
| 6769 | xmlXPathFreeObject(find); |
| 6770 | } |
| 6771 | |
| 6772 | /** |
| 6773 | * xmlXPathNormalizeFunction: |
| 6774 | * @ctxt: the XPath Parser context |
| 6775 | * @nargs: the number of arguments |
| 6776 | * |
| 6777 | * Implement the normalize-space() XPath function |
| 6778 | * string normalize-space(string?) |
| 6779 | * The normalize-space function returns the argument string with white |
| 6780 | * space normalized by stripping leading and trailing whitespace |
| 6781 | * and replacing sequences of whitespace characters by a single |
| 6782 | * space. Whitespace characters are the same allowed by the S production |
| 6783 | * in XML. If the argument is omitted, it defaults to the context |
| 6784 | * node converted to a string, in other words the value of the context node. |
| 6785 | */ |
| 6786 | void |
| 6787 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6788 | xmlXPathObjectPtr obj = NULL; |
| 6789 | xmlChar *source = NULL; |
| 6790 | xmlBufferPtr target; |
| 6791 | xmlChar blank; |
| 6792 | |
| 6793 | if (nargs == 0) { |
| 6794 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6795 | valuePush(ctxt, |
| 6796 | xmlXPathWrapString( |
| 6797 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6798 | nargs = 1; |
| 6799 | } |
| 6800 | |
| 6801 | CHECK_ARITY(1); |
| 6802 | CAST_TO_STRING; |
| 6803 | CHECK_TYPE(XPATH_STRING); |
| 6804 | obj = valuePop(ctxt); |
| 6805 | source = obj->stringval; |
| 6806 | |
| 6807 | target = xmlBufferCreate(); |
| 6808 | if (target && source) { |
| 6809 | |
| 6810 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6811 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6812 | source++; |
| 6813 | |
| 6814 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6815 | blank = 0; |
| 6816 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6817 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6818 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6819 | } else { |
| 6820 | if (blank) { |
| 6821 | xmlBufferAdd(target, &blank, 1); |
| 6822 | blank = 0; |
| 6823 | } |
| 6824 | xmlBufferAdd(target, source, 1); |
| 6825 | } |
| 6826 | source++; |
| 6827 | } |
| 6828 | |
| 6829 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6830 | xmlBufferFree(target); |
| 6831 | } |
| 6832 | xmlXPathFreeObject(obj); |
| 6833 | } |
| 6834 | |
| 6835 | /** |
| 6836 | * xmlXPathTranslateFunction: |
| 6837 | * @ctxt: the XPath Parser context |
| 6838 | * @nargs: the number of arguments |
| 6839 | * |
| 6840 | * Implement the translate() XPath function |
| 6841 | * string translate(string, string, string) |
| 6842 | * The translate function returns the first argument string with |
| 6843 | * occurrences of characters in the second argument string replaced |
| 6844 | * by the character at the corresponding position in the third argument |
| 6845 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6846 | * BAr. If there is a character in the second argument string with no |
| 6847 | * character at a corresponding position in the third argument string |
| 6848 | * (because the second argument string is longer than the third argument |
| 6849 | * string), then occurrences of that character in the first argument |
| 6850 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6851 | * returns "AAA". If a character occurs more than once in second |
| 6852 | * argument string, then the first occurrence determines the replacement |
| 6853 | * character. If the third argument string is longer than the second |
| 6854 | * argument string, then excess characters are ignored. |
| 6855 | */ |
| 6856 | void |
| 6857 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6858 | xmlXPathObjectPtr str; |
| 6859 | xmlXPathObjectPtr from; |
| 6860 | xmlXPathObjectPtr to; |
| 6861 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6862 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6863 | xmlChar ch; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6864 | xmlChar *point; |
| 6865 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6866 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6867 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6869 | CAST_TO_STRING; |
| 6870 | to = valuePop(ctxt); |
| 6871 | CAST_TO_STRING; |
| 6872 | from = valuePop(ctxt); |
| 6873 | CAST_TO_STRING; |
| 6874 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6875 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6876 | target = xmlBufferCreate(); |
| 6877 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6878 | max = xmlUTF8Strlen(to->stringval); |
| 6879 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6880 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6881 | if (offset >= 0) { |
| 6882 | if (offset < max) { |
| 6883 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6884 | if (point) |
| 6885 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6886 | } |
| 6887 | } else |
| 6888 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6889 | |
| 6890 | /* Step to next character in input */ |
| 6891 | cptr++; |
| 6892 | if ( ch & 0x80 ) { |
| 6893 | /* if not simple ascii, verify proper format */ |
| 6894 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6895 | xmlGenericError(xmlGenericErrorContext, |
| 6896 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6897 | break; |
| 6898 | } |
| 6899 | /* then skip over remaining bytes for this char */ |
| 6900 | while ( (ch <<= 1) & 0x80 ) |
| 6901 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 6902 | xmlGenericError(xmlGenericErrorContext, |
| 6903 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6904 | break; |
| 6905 | } |
| 6906 | if (ch & 0x80) /* must have had error encountered */ |
| 6907 | break; |
| 6908 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6909 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6910 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6911 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6912 | xmlBufferFree(target); |
| 6913 | xmlXPathFreeObject(str); |
| 6914 | xmlXPathFreeObject(from); |
| 6915 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | /** |
| 6919 | * xmlXPathBooleanFunction: |
| 6920 | * @ctxt: the XPath Parser context |
| 6921 | * @nargs: the number of arguments |
| 6922 | * |
| 6923 | * Implement the boolean() XPath function |
| 6924 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6925 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6926 | * - a number is true if and only if it is neither positive or |
| 6927 | * negative zero nor NaN |
| 6928 | * - a node-set is true if and only if it is non-empty |
| 6929 | * - a string is true if and only if its length is non-zero |
| 6930 | */ |
| 6931 | void |
| 6932 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6933 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | |
| 6935 | CHECK_ARITY(1); |
| 6936 | cur = valuePop(ctxt); |
| 6937 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6938 | cur = xmlXPathConvertBoolean(cur); |
| 6939 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6940 | } |
| 6941 | |
| 6942 | /** |
| 6943 | * xmlXPathNotFunction: |
| 6944 | * @ctxt: the XPath Parser context |
| 6945 | * @nargs: the number of arguments |
| 6946 | * |
| 6947 | * Implement the not() XPath function |
| 6948 | * boolean not(boolean) |
| 6949 | * The not function returns true if its argument is false, |
| 6950 | * and false otherwise. |
| 6951 | */ |
| 6952 | void |
| 6953 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6954 | CHECK_ARITY(1); |
| 6955 | CAST_TO_BOOLEAN; |
| 6956 | CHECK_TYPE(XPATH_BOOLEAN); |
| 6957 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 6958 | } |
| 6959 | |
| 6960 | /** |
| 6961 | * xmlXPathTrueFunction: |
| 6962 | * @ctxt: the XPath Parser context |
| 6963 | * @nargs: the number of arguments |
| 6964 | * |
| 6965 | * Implement the true() XPath function |
| 6966 | * boolean true() |
| 6967 | */ |
| 6968 | void |
| 6969 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6970 | CHECK_ARITY(0); |
| 6971 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6972 | } |
| 6973 | |
| 6974 | /** |
| 6975 | * xmlXPathFalseFunction: |
| 6976 | * @ctxt: the XPath Parser context |
| 6977 | * @nargs: the number of arguments |
| 6978 | * |
| 6979 | * Implement the false() XPath function |
| 6980 | * boolean false() |
| 6981 | */ |
| 6982 | void |
| 6983 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6984 | CHECK_ARITY(0); |
| 6985 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6986 | } |
| 6987 | |
| 6988 | /** |
| 6989 | * xmlXPathLangFunction: |
| 6990 | * @ctxt: the XPath Parser context |
| 6991 | * @nargs: the number of arguments |
| 6992 | * |
| 6993 | * Implement the lang() XPath function |
| 6994 | * boolean lang(string) |
| 6995 | * The lang function returns true or false depending on whether the |
| 6996 | * language of the context node as specified by xml:lang attributes |
| 6997 | * is the same as or is a sublanguage of the language specified by |
| 6998 | * the argument string. The language of the context node is determined |
| 6999 | * by the value of the xml:lang attribute on the context node, or, if |
| 7000 | * the context node has no xml:lang attribute, by the value of the |
| 7001 | * xml:lang attribute on the nearest ancestor of the context node that |
| 7002 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 7003 | * returns false. If there is such an attribute, then lang returns |
| 7004 | * true if the attribute value is equal to the argument ignoring case, |
| 7005 | * or if there is some suffix starting with - such that the attribute |
| 7006 | * value is equal to the argument ignoring that suffix of the attribute |
| 7007 | * value and ignoring case. |
| 7008 | */ |
| 7009 | void |
| 7010 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7011 | xmlXPathObjectPtr val; |
| 7012 | const xmlChar *theLang; |
| 7013 | const xmlChar *lang; |
| 7014 | int ret = 0; |
| 7015 | int i; |
| 7016 | |
| 7017 | CHECK_ARITY(1); |
| 7018 | CAST_TO_STRING; |
| 7019 | CHECK_TYPE(XPATH_STRING); |
| 7020 | val = valuePop(ctxt); |
| 7021 | lang = val->stringval; |
| 7022 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 7023 | if ((theLang != NULL) && (lang != NULL)) { |
| 7024 | for (i = 0;lang[i] != 0;i++) |
| 7025 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 7026 | goto not_equal; |
| 7027 | ret = 1; |
| 7028 | } |
| 7029 | not_equal: |
William M. Brack | a59ddb5 | 2004-02-25 08:12:32 +0000 | [diff] [blame] | 7030 | xmlFree((void *)theLang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | xmlXPathFreeObject(val); |
| 7032 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 7033 | } |
| 7034 | |
| 7035 | /** |
| 7036 | * xmlXPathNumberFunction: |
| 7037 | * @ctxt: the XPath Parser context |
| 7038 | * @nargs: the number of arguments |
| 7039 | * |
| 7040 | * Implement the number() XPath function |
| 7041 | * number number(object?) |
| 7042 | */ |
| 7043 | void |
| 7044 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7045 | xmlXPathObjectPtr cur; |
| 7046 | double res; |
| 7047 | |
| 7048 | if (nargs == 0) { |
| 7049 | if (ctxt->context->node == NULL) { |
| 7050 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 7051 | } else { |
| 7052 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 7053 | |
| 7054 | res = xmlXPathStringEvalNumber(content); |
| 7055 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 7056 | xmlFree(content); |
| 7057 | } |
| 7058 | return; |
| 7059 | } |
| 7060 | |
| 7061 | CHECK_ARITY(1); |
| 7062 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7063 | cur = xmlXPathConvertNumber(cur); |
| 7064 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
| 7067 | /** |
| 7068 | * xmlXPathSumFunction: |
| 7069 | * @ctxt: the XPath Parser context |
| 7070 | * @nargs: the number of arguments |
| 7071 | * |
| 7072 | * Implement the sum() XPath function |
| 7073 | * number sum(node-set) |
| 7074 | * The sum function returns the sum of the values of the nodes in |
| 7075 | * the argument node-set. |
| 7076 | */ |
| 7077 | void |
| 7078 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7079 | xmlXPathObjectPtr cur; |
| 7080 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7081 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7082 | |
| 7083 | CHECK_ARITY(1); |
| 7084 | if ((ctxt->value == NULL) || |
| 7085 | ((ctxt->value->type != XPATH_NODESET) && |
| 7086 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7087 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7088 | cur = valuePop(ctxt); |
| 7089 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7090 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7091 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7092 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7093 | } |
| 7094 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7095 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7096 | xmlXPathFreeObject(cur); |
| 7097 | } |
| 7098 | |
| 7099 | /** |
| 7100 | * xmlXPathFloorFunction: |
| 7101 | * @ctxt: the XPath Parser context |
| 7102 | * @nargs: the number of arguments |
| 7103 | * |
| 7104 | * Implement the floor() XPath function |
| 7105 | * number floor(number) |
| 7106 | * The floor function returns the largest (closest to positive infinity) |
| 7107 | * number that is not greater than the argument and that is an integer. |
| 7108 | */ |
| 7109 | void |
| 7110 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7111 | double f; |
| 7112 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7113 | CHECK_ARITY(1); |
| 7114 | CAST_TO_NUMBER; |
| 7115 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7116 | |
| 7117 | f = (double)((int) ctxt->value->floatval); |
| 7118 | if (f != ctxt->value->floatval) { |
| 7119 | if (ctxt->value->floatval > 0) |
| 7120 | ctxt->value->floatval = f; |
| 7121 | else |
| 7122 | ctxt->value->floatval = f - 1; |
| 7123 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7124 | } |
| 7125 | |
| 7126 | /** |
| 7127 | * xmlXPathCeilingFunction: |
| 7128 | * @ctxt: the XPath Parser context |
| 7129 | * @nargs: the number of arguments |
| 7130 | * |
| 7131 | * Implement the ceiling() XPath function |
| 7132 | * number ceiling(number) |
| 7133 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7134 | * number that is not less than the argument and that is an integer. |
| 7135 | */ |
| 7136 | void |
| 7137 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7138 | double f; |
| 7139 | |
| 7140 | CHECK_ARITY(1); |
| 7141 | CAST_TO_NUMBER; |
| 7142 | CHECK_TYPE(XPATH_NUMBER); |
| 7143 | |
| 7144 | #if 0 |
| 7145 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7146 | #else |
| 7147 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7148 | if (f != ctxt->value->floatval) { |
| 7149 | if (ctxt->value->floatval > 0) |
| 7150 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7151 | else { |
| 7152 | if (ctxt->value->floatval < 0 && f == 0) |
| 7153 | ctxt->value->floatval = xmlXPathNZERO; |
| 7154 | else |
| 7155 | ctxt->value->floatval = f; |
| 7156 | } |
| 7157 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7158 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7159 | #endif |
| 7160 | } |
| 7161 | |
| 7162 | /** |
| 7163 | * xmlXPathRoundFunction: |
| 7164 | * @ctxt: the XPath Parser context |
| 7165 | * @nargs: the number of arguments |
| 7166 | * |
| 7167 | * Implement the round() XPath function |
| 7168 | * number round(number) |
| 7169 | * The round function returns the number that is closest to the |
| 7170 | * argument and that is an integer. If there are two such numbers, |
| 7171 | * then the one that is even is returned. |
| 7172 | */ |
| 7173 | void |
| 7174 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7175 | double f; |
| 7176 | |
| 7177 | CHECK_ARITY(1); |
| 7178 | CAST_TO_NUMBER; |
| 7179 | CHECK_TYPE(XPATH_NUMBER); |
| 7180 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7181 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7182 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7183 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7184 | (ctxt->value->floatval == 0.0)) |
| 7185 | return; |
| 7186 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7187 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7188 | if (ctxt->value->floatval < 0) { |
| 7189 | if (ctxt->value->floatval < f - 0.5) |
| 7190 | ctxt->value->floatval = f - 1; |
| 7191 | else |
| 7192 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7193 | if (ctxt->value->floatval == 0) |
| 7194 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7195 | } else { |
| 7196 | if (ctxt->value->floatval < f + 0.5) |
| 7197 | ctxt->value->floatval = f; |
| 7198 | else |
| 7199 | ctxt->value->floatval = f + 1; |
| 7200 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | /************************************************************************ |
| 7204 | * * |
| 7205 | * The Parser * |
| 7206 | * * |
| 7207 | ************************************************************************/ |
| 7208 | |
| 7209 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7210 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7211 | * implementation. |
| 7212 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7213 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7214 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7215 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7216 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7217 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7218 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7219 | |
| 7220 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7221 | * xmlXPathCurrentChar: |
| 7222 | * @ctxt: the XPath parser context |
| 7223 | * @cur: pointer to the beginning of the char |
| 7224 | * @len: pointer to the length of the char read |
| 7225 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7226 | * 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] | 7227 | * bytes in the input buffer. |
| 7228 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7229 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7230 | */ |
| 7231 | |
| 7232 | static int |
| 7233 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7234 | unsigned char c; |
| 7235 | unsigned int val; |
| 7236 | const xmlChar *cur; |
| 7237 | |
| 7238 | if (ctxt == NULL) |
| 7239 | return(0); |
| 7240 | cur = ctxt->cur; |
| 7241 | |
| 7242 | /* |
| 7243 | * We are supposed to handle UTF8, check it's valid |
| 7244 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7245 | * |
| 7246 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7247 | * 0000 0000-0000 007F 0xxxxxxx |
| 7248 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7249 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7250 | * |
| 7251 | * Check for the 0x110000 limit too |
| 7252 | */ |
| 7253 | c = *cur; |
| 7254 | if (c & 0x80) { |
| 7255 | if ((cur[1] & 0xc0) != 0x80) |
| 7256 | goto encoding_error; |
| 7257 | if ((c & 0xe0) == 0xe0) { |
| 7258 | |
| 7259 | if ((cur[2] & 0xc0) != 0x80) |
| 7260 | goto encoding_error; |
| 7261 | if ((c & 0xf0) == 0xf0) { |
| 7262 | if (((c & 0xf8) != 0xf0) || |
| 7263 | ((cur[3] & 0xc0) != 0x80)) |
| 7264 | goto encoding_error; |
| 7265 | /* 4-byte code */ |
| 7266 | *len = 4; |
| 7267 | val = (cur[0] & 0x7) << 18; |
| 7268 | val |= (cur[1] & 0x3f) << 12; |
| 7269 | val |= (cur[2] & 0x3f) << 6; |
| 7270 | val |= cur[3] & 0x3f; |
| 7271 | } else { |
| 7272 | /* 3-byte code */ |
| 7273 | *len = 3; |
| 7274 | val = (cur[0] & 0xf) << 12; |
| 7275 | val |= (cur[1] & 0x3f) << 6; |
| 7276 | val |= cur[2] & 0x3f; |
| 7277 | } |
| 7278 | } else { |
| 7279 | /* 2-byte code */ |
| 7280 | *len = 2; |
| 7281 | val = (cur[0] & 0x1f) << 6; |
| 7282 | val |= cur[1] & 0x3f; |
| 7283 | } |
| 7284 | if (!IS_CHAR(val)) { |
| 7285 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7286 | } |
| 7287 | return(val); |
| 7288 | } else { |
| 7289 | /* 1-byte code */ |
| 7290 | *len = 1; |
| 7291 | return((int) *cur); |
| 7292 | } |
| 7293 | encoding_error: |
| 7294 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7295 | * If we detect an UTF8 error that probably means that the |
| 7296 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7297 | * declaration header. Report the error and switch the encoding |
| 7298 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7299 | * encoding !) |
| 7300 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7301 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7302 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7306 | * xmlXPathParseNCName: |
| 7307 | * @ctxt: the XPath Parser context |
| 7308 | * |
| 7309 | * parse an XML namespace non qualified name. |
| 7310 | * |
| 7311 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7312 | * |
| 7313 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7314 | * CombiningChar | Extender |
| 7315 | * |
| 7316 | * Returns the namespace name or NULL |
| 7317 | */ |
| 7318 | |
| 7319 | xmlChar * |
| 7320 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7321 | const xmlChar *in; |
| 7322 | xmlChar *ret; |
| 7323 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7324 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7325 | /* |
| 7326 | * Accelerator for simple ASCII names |
| 7327 | */ |
| 7328 | in = ctxt->cur; |
| 7329 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7330 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7331 | (*in == '_')) { |
| 7332 | in++; |
| 7333 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7334 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7335 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7336 | (*in == '_') || (*in == '.') || |
| 7337 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7338 | in++; |
| 7339 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7340 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7341 | (*in == '@') || (*in == '*')) { |
| 7342 | count = in - ctxt->cur; |
| 7343 | if (count == 0) |
| 7344 | return(NULL); |
| 7345 | ret = xmlStrndup(ctxt->cur, count); |
| 7346 | ctxt->cur = in; |
| 7347 | return(ret); |
| 7348 | } |
| 7349 | } |
| 7350 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7351 | } |
| 7352 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7353 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7354 | /** |
| 7355 | * xmlXPathParseQName: |
| 7356 | * @ctxt: the XPath Parser context |
| 7357 | * @prefix: a xmlChar ** |
| 7358 | * |
| 7359 | * parse an XML qualified name |
| 7360 | * |
| 7361 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7362 | * |
| 7363 | * [NS 6] Prefix ::= NCName |
| 7364 | * |
| 7365 | * [NS 7] LocalPart ::= NCName |
| 7366 | * |
| 7367 | * Returns the function returns the local part, and prefix is updated |
| 7368 | * to get the Prefix if any. |
| 7369 | */ |
| 7370 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7371 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7372 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7373 | xmlChar *ret = NULL; |
| 7374 | |
| 7375 | *prefix = NULL; |
| 7376 | ret = xmlXPathParseNCName(ctxt); |
| 7377 | if (CUR == ':') { |
| 7378 | *prefix = ret; |
| 7379 | NEXT; |
| 7380 | ret = xmlXPathParseNCName(ctxt); |
| 7381 | } |
| 7382 | return(ret); |
| 7383 | } |
| 7384 | |
| 7385 | /** |
| 7386 | * xmlXPathParseName: |
| 7387 | * @ctxt: the XPath Parser context |
| 7388 | * |
| 7389 | * parse an XML name |
| 7390 | * |
| 7391 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7392 | * CombiningChar | Extender |
| 7393 | * |
| 7394 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7395 | * |
| 7396 | * Returns the namespace name or NULL |
| 7397 | */ |
| 7398 | |
| 7399 | xmlChar * |
| 7400 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7401 | const xmlChar *in; |
| 7402 | xmlChar *ret; |
| 7403 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7404 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7405 | /* |
| 7406 | * Accelerator for simple ASCII names |
| 7407 | */ |
| 7408 | in = ctxt->cur; |
| 7409 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7410 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7411 | (*in == '_') || (*in == ':')) { |
| 7412 | in++; |
| 7413 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7414 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7415 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7416 | (*in == '_') || (*in == '-') || |
| 7417 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7418 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7419 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7420 | count = in - ctxt->cur; |
| 7421 | ret = xmlStrndup(ctxt->cur, count); |
| 7422 | ctxt->cur = in; |
| 7423 | return(ret); |
| 7424 | } |
| 7425 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7426 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7429 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7430 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7431 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7432 | int len = 0, l; |
| 7433 | int c; |
| 7434 | |
| 7435 | /* |
| 7436 | * Handler for more complex cases |
| 7437 | */ |
| 7438 | c = CUR_CHAR(l); |
| 7439 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7440 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7441 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7442 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7443 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7444 | return(NULL); |
| 7445 | } |
| 7446 | |
| 7447 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7448 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7449 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7450 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7451 | (IS_COMBINING(c)) || |
| 7452 | (IS_EXTENDER(c)))) { |
| 7453 | COPY_BUF(l,buf,len,c); |
| 7454 | NEXTL(l); |
| 7455 | c = CUR_CHAR(l); |
| 7456 | if (len >= XML_MAX_NAMELEN) { |
| 7457 | /* |
| 7458 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7459 | * for the processing speed. |
| 7460 | */ |
| 7461 | xmlChar *buffer; |
| 7462 | int max = len * 2; |
| 7463 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7464 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7465 | if (buffer == NULL) { |
| 7466 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7467 | } |
| 7468 | memcpy(buffer, buf, len); |
| 7469 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7470 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7471 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7472 | (IS_COMBINING(c)) || |
| 7473 | (IS_EXTENDER(c))) { |
| 7474 | if (len + 10 > max) { |
| 7475 | max *= 2; |
| 7476 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7477 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7478 | if (buffer == NULL) { |
| 7479 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7480 | } |
| 7481 | } |
| 7482 | COPY_BUF(l,buffer,len,c); |
| 7483 | NEXTL(l); |
| 7484 | c = CUR_CHAR(l); |
| 7485 | } |
| 7486 | buffer[len] = 0; |
| 7487 | return(buffer); |
| 7488 | } |
| 7489 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7490 | if (len == 0) |
| 7491 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7492 | return(xmlStrndup(buf, len)); |
| 7493 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7494 | |
| 7495 | #define MAX_FRAC 20 |
| 7496 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7497 | /* |
| 7498 | * These are used as divisors for the fractional part of a number. |
| 7499 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 7500 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 7501 | */ |
| 7502 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7503 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7504 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7505 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7506 | 100000000000000.0, |
| 7507 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7508 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7509 | }; |
| 7510 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7511 | /** |
| 7512 | * xmlXPathStringEvalNumber: |
| 7513 | * @str: A string to scan |
| 7514 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7515 | * [30a] Float ::= Number ('e' Digits?)? |
| 7516 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7517 | * [30] Number ::= Digits ('.' Digits?)? |
| 7518 | * | '.' Digits |
| 7519 | * [31] Digits ::= [0-9]+ |
| 7520 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7521 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7522 | * In complement of the Number expression, this function also handles |
| 7523 | * negative values : '-' Number. |
| 7524 | * |
| 7525 | * Returns the double value. |
| 7526 | */ |
| 7527 | double |
| 7528 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7529 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7530 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7531 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7532 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7533 | int exponent = 0; |
| 7534 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7535 | #ifdef __GNUC__ |
| 7536 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7537 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7538 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7539 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7540 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7541 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7542 | return(xmlXPathNAN); |
| 7543 | } |
| 7544 | if (*cur == '-') { |
| 7545 | isneg = 1; |
| 7546 | cur++; |
| 7547 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7548 | |
| 7549 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7550 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7551 | * tmp/temp is a workaround against a gcc compiler bug |
| 7552 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7553 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7554 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7555 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7556 | ret = ret * 10; |
| 7557 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7558 | ok = 1; |
| 7559 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7560 | temp = (double) tmp; |
| 7561 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7562 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7563 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7564 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7565 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7566 | ret = ret * 10 + (*cur - '0'); |
| 7567 | ok = 1; |
| 7568 | cur++; |
| 7569 | } |
| 7570 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7571 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7572 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7573 | int v, frac = 0; |
| 7574 | double fraction = 0; |
| 7575 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7576 | cur++; |
| 7577 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7578 | return(xmlXPathNAN); |
| 7579 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7580 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7581 | v = (*cur - '0'); |
| 7582 | fraction = fraction * 10 + v; |
| 7583 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7584 | cur++; |
| 7585 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7586 | fraction /= my_pow10[frac]; |
| 7587 | ret = ret + fraction; |
| 7588 | while ((*cur >= '0') && (*cur <= '9')) |
| 7589 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7590 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7591 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7592 | cur++; |
| 7593 | if (*cur == '-') { |
| 7594 | is_exponent_negative = 1; |
| 7595 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7596 | } else if (*cur == '+') { |
| 7597 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7598 | } |
| 7599 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7600 | exponent = exponent * 10 + (*cur - '0'); |
| 7601 | cur++; |
| 7602 | } |
| 7603 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7604 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7605 | if (*cur != 0) return(xmlXPathNAN); |
| 7606 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7607 | if (is_exponent_negative) exponent = -exponent; |
| 7608 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7609 | return(ret); |
| 7610 | } |
| 7611 | |
| 7612 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7613 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7614 | * @ctxt: the XPath Parser context |
| 7615 | * |
| 7616 | * [30] Number ::= Digits ('.' Digits?)? |
| 7617 | * | '.' Digits |
| 7618 | * [31] Digits ::= [0-9]+ |
| 7619 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7620 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7621 | * |
| 7622 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7623 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7624 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7625 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7626 | double ret = 0.0; |
| 7627 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7628 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7629 | int exponent = 0; |
| 7630 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7631 | #ifdef __GNUC__ |
| 7632 | unsigned long tmp = 0; |
| 7633 | double temp; |
| 7634 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7635 | |
| 7636 | CHECK_ERROR; |
| 7637 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7638 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7639 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7640 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7641 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7642 | * tmp/temp is a workaround against a gcc compiler bug |
| 7643 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7644 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7645 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7646 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7647 | ret = ret * 10; |
| 7648 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7649 | ok = 1; |
| 7650 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7651 | temp = (double) tmp; |
| 7652 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7653 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7654 | #else |
| 7655 | ret = 0; |
| 7656 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7657 | ret = ret * 10 + (CUR - '0'); |
| 7658 | ok = 1; |
| 7659 | NEXT; |
| 7660 | } |
| 7661 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7662 | if (CUR == '.') { |
| 7663 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7664 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7665 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7666 | } |
| 7667 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7668 | mult /= 10; |
| 7669 | ret = ret + (CUR - '0') * mult; |
| 7670 | NEXT; |
| 7671 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7672 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7673 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7674 | NEXT; |
| 7675 | if (CUR == '-') { |
| 7676 | is_exponent_negative = 1; |
| 7677 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7678 | } else if (CUR == '+') { |
| 7679 | NEXT; |
| 7680 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7681 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7682 | exponent = exponent * 10 + (CUR - '0'); |
| 7683 | NEXT; |
| 7684 | } |
| 7685 | if (is_exponent_negative) |
| 7686 | exponent = -exponent; |
| 7687 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7688 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7689 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7690 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7691 | } |
| 7692 | |
| 7693 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7694 | * xmlXPathParseLiteral: |
| 7695 | * @ctxt: the XPath Parser context |
| 7696 | * |
| 7697 | * Parse a Literal |
| 7698 | * |
| 7699 | * [29] Literal ::= '"' [^"]* '"' |
| 7700 | * | "'" [^']* "'" |
| 7701 | * |
| 7702 | * Returns the value found or NULL in case of error |
| 7703 | */ |
| 7704 | static xmlChar * |
| 7705 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7706 | const xmlChar *q; |
| 7707 | xmlChar *ret = NULL; |
| 7708 | |
| 7709 | if (CUR == '"') { |
| 7710 | NEXT; |
| 7711 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7712 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7713 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7714 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7715 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7716 | } else { |
| 7717 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7718 | NEXT; |
| 7719 | } |
| 7720 | } else if (CUR == '\'') { |
| 7721 | NEXT; |
| 7722 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7723 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7724 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7725 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7726 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7727 | } else { |
| 7728 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7729 | NEXT; |
| 7730 | } |
| 7731 | } else { |
| 7732 | XP_ERROR0(XPATH_START_LITERAL_ERROR); |
| 7733 | } |
| 7734 | return(ret); |
| 7735 | } |
| 7736 | |
| 7737 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7738 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7739 | * @ctxt: the XPath Parser context |
| 7740 | * |
| 7741 | * Parse a Literal and push it on the stack. |
| 7742 | * |
| 7743 | * [29] Literal ::= '"' [^"]* '"' |
| 7744 | * | "'" [^']* "'" |
| 7745 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7746 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7747 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7748 | static void |
| 7749 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7750 | const xmlChar *q; |
| 7751 | xmlChar *ret = NULL; |
| 7752 | |
| 7753 | if (CUR == '"') { |
| 7754 | NEXT; |
| 7755 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7756 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7757 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7758 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7759 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7760 | } else { |
| 7761 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7762 | NEXT; |
| 7763 | } |
| 7764 | } else if (CUR == '\'') { |
| 7765 | NEXT; |
| 7766 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7767 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7768 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7769 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7770 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7771 | } else { |
| 7772 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7773 | NEXT; |
| 7774 | } |
| 7775 | } else { |
| 7776 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7777 | } |
| 7778 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7779 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7780 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7781 | xmlFree(ret); |
| 7782 | } |
| 7783 | |
| 7784 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7785 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7786 | * @ctxt: the XPath Parser context |
| 7787 | * |
| 7788 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7789 | * |
| 7790 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7791 | * to variable values. The value of a variable is an object, which can be |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7792 | * of any of the types that are possible for the value of an expression, |
| 7793 | * and may also be of additional types not specified here. |
| 7794 | * |
| 7795 | * Early evaluation is possible since: |
| 7796 | * The variable bindings [...] used to evaluate a subexpression are |
| 7797 | * always the same as those used to evaluate the containing expression. |
| 7798 | * |
| 7799 | * [36] VariableReference ::= '$' QName |
| 7800 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7801 | static void |
| 7802 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7803 | xmlChar *name; |
| 7804 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7805 | |
| 7806 | SKIP_BLANKS; |
| 7807 | if (CUR != '$') { |
| 7808 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7809 | } |
| 7810 | NEXT; |
| 7811 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7812 | if (name == NULL) { |
| 7813 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7814 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7815 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7816 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7817 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7818 | SKIP_BLANKS; |
| 7819 | } |
| 7820 | |
| 7821 | /** |
| 7822 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7823 | * @name: a name string |
| 7824 | * |
| 7825 | * Is the name given a NodeType one. |
| 7826 | * |
| 7827 | * [38] NodeType ::= 'comment' |
| 7828 | * | 'text' |
| 7829 | * | 'processing-instruction' |
| 7830 | * | 'node' |
| 7831 | * |
| 7832 | * Returns 1 if true 0 otherwise |
| 7833 | */ |
| 7834 | int |
| 7835 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7836 | if (name == NULL) |
| 7837 | return(0); |
| 7838 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7839 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7840 | return(1); |
| 7841 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7842 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7843 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7844 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7845 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7846 | return(1); |
| 7847 | return(0); |
| 7848 | } |
| 7849 | |
| 7850 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7851 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7852 | * @ctxt: the XPath Parser context |
| 7853 | * |
| 7854 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7855 | * [17] Argument ::= Expr |
| 7856 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7857 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7858 | * pushed on the stack |
| 7859 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7860 | static void |
| 7861 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7862 | xmlChar *name; |
| 7863 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7864 | int nbargs = 0; |
| 7865 | |
| 7866 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7867 | if (name == NULL) { |
| 7868 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7869 | } |
| 7870 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7871 | #ifdef DEBUG_EXPR |
| 7872 | if (prefix == NULL) |
| 7873 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7874 | name); |
| 7875 | else |
| 7876 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7877 | prefix, name); |
| 7878 | #endif |
| 7879 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7880 | if (CUR != '(') { |
| 7881 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7882 | } |
| 7883 | NEXT; |
| 7884 | SKIP_BLANKS; |
| 7885 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7886 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 7887 | if (CUR != ')') { |
| 7888 | while (CUR != 0) { |
| 7889 | int op1 = ctxt->comp->last; |
| 7890 | ctxt->comp->last = -1; |
| 7891 | xmlXPathCompileExpr(ctxt); |
| 7892 | CHECK_ERROR; |
| 7893 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 7894 | nbargs++; |
| 7895 | if (CUR == ')') break; |
| 7896 | if (CUR != ',') { |
| 7897 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7898 | } |
| 7899 | NEXT; |
| 7900 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7901 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7902 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7903 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 7904 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7905 | NEXT; |
| 7906 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7907 | } |
| 7908 | |
| 7909 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7910 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7911 | * @ctxt: the XPath Parser context |
| 7912 | * |
| 7913 | * [15] PrimaryExpr ::= VariableReference |
| 7914 | * | '(' Expr ')' |
| 7915 | * | Literal |
| 7916 | * | Number |
| 7917 | * | FunctionCall |
| 7918 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7919 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7920 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7921 | static void |
| 7922 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7923 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7924 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7925 | else if (CUR == '(') { |
| 7926 | NEXT; |
| 7927 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7928 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 7929 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7930 | if (CUR != ')') { |
| 7931 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7932 | } |
| 7933 | NEXT; |
| 7934 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7935 | } else if (IS_DIGIT_CH(CUR) || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7936 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7937 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7938 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7939 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7940 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7941 | } |
| 7942 | SKIP_BLANKS; |
| 7943 | } |
| 7944 | |
| 7945 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7946 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7947 | * @ctxt: the XPath Parser context |
| 7948 | * |
| 7949 | * [20] FilterExpr ::= PrimaryExpr |
| 7950 | * | FilterExpr Predicate |
| 7951 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7952 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7953 | * Square brackets are used to filter expressions in the same way that |
| 7954 | * they are used in location paths. It is an error if the expression to |
| 7955 | * be filtered does not evaluate to a node-set. The context node list |
| 7956 | * used for evaluating the expression in square brackets is the node-set |
| 7957 | * to be filtered listed in document order. |
| 7958 | */ |
| 7959 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7960 | static void |
| 7961 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 7962 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7963 | CHECK_ERROR; |
| 7964 | SKIP_BLANKS; |
| 7965 | |
| 7966 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7967 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7968 | SKIP_BLANKS; |
| 7969 | } |
| 7970 | |
| 7971 | |
| 7972 | } |
| 7973 | |
| 7974 | /** |
| 7975 | * xmlXPathScanName: |
| 7976 | * @ctxt: the XPath Parser context |
| 7977 | * |
| 7978 | * Trickery: parse an XML name but without consuming the input flow |
| 7979 | * Needed to avoid insanity in the parser state. |
| 7980 | * |
| 7981 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7982 | * CombiningChar | Extender |
| 7983 | * |
| 7984 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7985 | * |
| 7986 | * [6] Names ::= Name (S Name)* |
| 7987 | * |
| 7988 | * Returns the Name parsed or NULL |
| 7989 | */ |
| 7990 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7991 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7992 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
| 7993 | xmlChar buf[XML_MAX_NAMELEN]; |
| 7994 | int len = 0; |
| 7995 | |
| 7996 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7997 | if (!IS_LETTER_CH(CUR) && (CUR != '_') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7998 | (CUR != ':')) { |
| 7999 | return(NULL); |
| 8000 | } |
| 8001 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8002 | while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8003 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 8004 | (NXT(len) == '_') || (NXT(len) == ':') || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8005 | (IS_COMBINING_CH(NXT(len))) || |
| 8006 | (IS_EXTENDER_CH(NXT(len)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8007 | buf[len] = NXT(len); |
| 8008 | len++; |
| 8009 | if (len >= XML_MAX_NAMELEN) { |
| 8010 | xmlGenericError(xmlGenericErrorContext, |
| 8011 | "xmlScanName: reached XML_MAX_NAMELEN limit\n"); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8012 | while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8013 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 8014 | (NXT(len) == '_') || (NXT(len) == ':') || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8015 | (IS_COMBINING_CH(NXT(len))) || |
| 8016 | (IS_EXTENDER_CH(NXT(len)))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8017 | len++; |
| 8018 | break; |
| 8019 | } |
| 8020 | } |
| 8021 | return(xmlStrndup(buf, len)); |
| 8022 | } |
| 8023 | |
| 8024 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8025 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8026 | * @ctxt: the XPath Parser context |
| 8027 | * |
| 8028 | * [19] PathExpr ::= LocationPath |
| 8029 | * | FilterExpr |
| 8030 | * | FilterExpr '/' RelativeLocationPath |
| 8031 | * | FilterExpr '//' RelativeLocationPath |
| 8032 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8033 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8034 | * The / operator and // operators combine an arbitrary expression |
| 8035 | * and a relative location path. It is an error if the expression |
| 8036 | * does not evaluate to a node-set. |
| 8037 | * The / operator does composition in the same way as when / is |
| 8038 | * used in a location path. As in location paths, // is short for |
| 8039 | * /descendant-or-self::node()/. |
| 8040 | */ |
| 8041 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8042 | static void |
| 8043 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8044 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 8045 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 8046 | |
| 8047 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8048 | if ((CUR == '$') || (CUR == '(') || (IS_DIGIT_CH(CUR)) || |
| 8049 | (CUR == '\'') || (CUR == '"') || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8050 | lc = 0; |
| 8051 | } else if (CUR == '*') { |
| 8052 | /* relative or absolute location path */ |
| 8053 | lc = 1; |
| 8054 | } else if (CUR == '/') { |
| 8055 | /* relative or absolute location path */ |
| 8056 | lc = 1; |
| 8057 | } else if (CUR == '@') { |
| 8058 | /* relative abbreviated attribute location path */ |
| 8059 | lc = 1; |
| 8060 | } else if (CUR == '.') { |
| 8061 | /* relative abbreviated attribute location path */ |
| 8062 | lc = 1; |
| 8063 | } else { |
| 8064 | /* |
| 8065 | * Problem is finding if we have a name here whether it's: |
| 8066 | * - a nodetype |
| 8067 | * - a function call in which case it's followed by '(' |
| 8068 | * - an axis in which case it's followed by ':' |
| 8069 | * - a element name |
| 8070 | * We do an a priori analysis here rather than having to |
| 8071 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8072 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8073 | * read/write/debug. |
| 8074 | */ |
| 8075 | SKIP_BLANKS; |
| 8076 | name = xmlXPathScanName(ctxt); |
| 8077 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8078 | #ifdef DEBUG_STEP |
| 8079 | xmlGenericError(xmlGenericErrorContext, |
| 8080 | "PathExpr: Axis\n"); |
| 8081 | #endif |
| 8082 | lc = 1; |
| 8083 | xmlFree(name); |
| 8084 | } else if (name != NULL) { |
| 8085 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8086 | |
| 8087 | |
| 8088 | while (NXT(len) != 0) { |
| 8089 | if (NXT(len) == '/') { |
| 8090 | /* element name */ |
| 8091 | #ifdef DEBUG_STEP |
| 8092 | xmlGenericError(xmlGenericErrorContext, |
| 8093 | "PathExpr: AbbrRelLocation\n"); |
| 8094 | #endif |
| 8095 | lc = 1; |
| 8096 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8097 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8098 | /* ignore blanks */ |
| 8099 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8100 | } else if (NXT(len) == ':') { |
| 8101 | #ifdef DEBUG_STEP |
| 8102 | xmlGenericError(xmlGenericErrorContext, |
| 8103 | "PathExpr: AbbrRelLocation\n"); |
| 8104 | #endif |
| 8105 | lc = 1; |
| 8106 | break; |
| 8107 | } else if ((NXT(len) == '(')) { |
| 8108 | /* Note Type or Function */ |
| 8109 | if (xmlXPathIsNodeType(name)) { |
| 8110 | #ifdef DEBUG_STEP |
| 8111 | xmlGenericError(xmlGenericErrorContext, |
| 8112 | "PathExpr: Type search\n"); |
| 8113 | #endif |
| 8114 | lc = 1; |
| 8115 | } else { |
| 8116 | #ifdef DEBUG_STEP |
| 8117 | xmlGenericError(xmlGenericErrorContext, |
| 8118 | "PathExpr: function call\n"); |
| 8119 | #endif |
| 8120 | lc = 0; |
| 8121 | } |
| 8122 | break; |
| 8123 | } else if ((NXT(len) == '[')) { |
| 8124 | /* element name */ |
| 8125 | #ifdef DEBUG_STEP |
| 8126 | xmlGenericError(xmlGenericErrorContext, |
| 8127 | "PathExpr: AbbrRelLocation\n"); |
| 8128 | #endif |
| 8129 | lc = 1; |
| 8130 | break; |
| 8131 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8132 | (NXT(len) == '=')) { |
| 8133 | lc = 1; |
| 8134 | break; |
| 8135 | } else { |
| 8136 | lc = 1; |
| 8137 | break; |
| 8138 | } |
| 8139 | len++; |
| 8140 | } |
| 8141 | if (NXT(len) == 0) { |
| 8142 | #ifdef DEBUG_STEP |
| 8143 | xmlGenericError(xmlGenericErrorContext, |
| 8144 | "PathExpr: AbbrRelLocation\n"); |
| 8145 | #endif |
| 8146 | /* element name */ |
| 8147 | lc = 1; |
| 8148 | } |
| 8149 | xmlFree(name); |
| 8150 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8151 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8152 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8153 | } |
| 8154 | } |
| 8155 | |
| 8156 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8157 | if (CUR == '/') { |
| 8158 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8159 | } else { |
| 8160 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8161 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8162 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8163 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8164 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8165 | CHECK_ERROR; |
| 8166 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8167 | SKIP(2); |
| 8168 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8169 | |
| 8170 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8171 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8172 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8173 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8174 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8175 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8176 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8177 | } |
| 8178 | } |
| 8179 | SKIP_BLANKS; |
| 8180 | } |
| 8181 | |
| 8182 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8183 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8184 | * @ctxt: the XPath Parser context |
| 8185 | * |
| 8186 | * [18] UnionExpr ::= PathExpr |
| 8187 | * | UnionExpr '|' PathExpr |
| 8188 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8189 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8190 | */ |
| 8191 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8192 | static void |
| 8193 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8194 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8195 | CHECK_ERROR; |
| 8196 | SKIP_BLANKS; |
| 8197 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8198 | int op1 = ctxt->comp->last; |
| 8199 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8200 | |
| 8201 | NEXT; |
| 8202 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8203 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8204 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8205 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8207 | SKIP_BLANKS; |
| 8208 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8209 | } |
| 8210 | |
| 8211 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8212 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8213 | * @ctxt: the XPath Parser context |
| 8214 | * |
| 8215 | * [27] UnaryExpr ::= UnionExpr |
| 8216 | * | '-' UnaryExpr |
| 8217 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8218 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8219 | */ |
| 8220 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8221 | static void |
| 8222 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8223 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8224 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8225 | |
| 8226 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8227 | while (CUR == '-') { |
| 8228 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8229 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8230 | NEXT; |
| 8231 | SKIP_BLANKS; |
| 8232 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8233 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8234 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8235 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8236 | if (found) { |
| 8237 | if (minus) |
| 8238 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8239 | else |
| 8240 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8241 | } |
| 8242 | } |
| 8243 | |
| 8244 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8245 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8246 | * @ctxt: the XPath Parser context |
| 8247 | * |
| 8248 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8249 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8250 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8251 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8252 | * [34] MultiplyOperator ::= '*' |
| 8253 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8254 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8255 | */ |
| 8256 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8257 | static void |
| 8258 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8259 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8260 | CHECK_ERROR; |
| 8261 | SKIP_BLANKS; |
| 8262 | while ((CUR == '*') || |
| 8263 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8264 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8265 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8266 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8267 | |
| 8268 | if (CUR == '*') { |
| 8269 | op = 0; |
| 8270 | NEXT; |
| 8271 | } else if (CUR == 'd') { |
| 8272 | op = 1; |
| 8273 | SKIP(3); |
| 8274 | } else if (CUR == 'm') { |
| 8275 | op = 2; |
| 8276 | SKIP(3); |
| 8277 | } |
| 8278 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8279 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8280 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8281 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8282 | SKIP_BLANKS; |
| 8283 | } |
| 8284 | } |
| 8285 | |
| 8286 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8287 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8288 | * @ctxt: the XPath Parser context |
| 8289 | * |
| 8290 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8291 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8292 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8293 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8294 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8295 | */ |
| 8296 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8297 | static void |
| 8298 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8299 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8300 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8301 | CHECK_ERROR; |
| 8302 | SKIP_BLANKS; |
| 8303 | while ((CUR == '+') || (CUR == '-')) { |
| 8304 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8305 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8306 | |
| 8307 | if (CUR == '+') plus = 1; |
| 8308 | else plus = 0; |
| 8309 | NEXT; |
| 8310 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8311 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8313 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8314 | SKIP_BLANKS; |
| 8315 | } |
| 8316 | } |
| 8317 | |
| 8318 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8319 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8320 | * @ctxt: the XPath Parser context |
| 8321 | * |
| 8322 | * [24] RelationalExpr ::= AdditiveExpr |
| 8323 | * | RelationalExpr '<' AdditiveExpr |
| 8324 | * | RelationalExpr '>' AdditiveExpr |
| 8325 | * | RelationalExpr '<=' AdditiveExpr |
| 8326 | * | RelationalExpr '>=' AdditiveExpr |
| 8327 | * |
| 8328 | * A <= B > C is allowed ? Answer from James, yes with |
| 8329 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8330 | * which is basically what got implemented. |
| 8331 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8332 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | * on the stack |
| 8334 | */ |
| 8335 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8336 | static void |
| 8337 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8338 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8339 | CHECK_ERROR; |
| 8340 | SKIP_BLANKS; |
| 8341 | while ((CUR == '<') || |
| 8342 | (CUR == '>') || |
| 8343 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8344 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8345 | int inf, strict; |
| 8346 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8347 | |
| 8348 | if (CUR == '<') inf = 1; |
| 8349 | else inf = 0; |
| 8350 | if (NXT(1) == '=') strict = 0; |
| 8351 | else strict = 1; |
| 8352 | NEXT; |
| 8353 | if (!strict) NEXT; |
| 8354 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8355 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8356 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8357 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8358 | SKIP_BLANKS; |
| 8359 | } |
| 8360 | } |
| 8361 | |
| 8362 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8363 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8364 | * @ctxt: the XPath Parser context |
| 8365 | * |
| 8366 | * [23] EqualityExpr ::= RelationalExpr |
| 8367 | * | EqualityExpr '=' RelationalExpr |
| 8368 | * | EqualityExpr '!=' RelationalExpr |
| 8369 | * |
| 8370 | * A != B != C is allowed ? Answer from James, yes with |
| 8371 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8372 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8373 | * which is basically what got implemented. |
| 8374 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8375 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8376 | * |
| 8377 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8378 | static void |
| 8379 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8380 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8381 | CHECK_ERROR; |
| 8382 | SKIP_BLANKS; |
| 8383 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8384 | int eq; |
| 8385 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8386 | |
| 8387 | if (CUR == '=') eq = 1; |
| 8388 | else eq = 0; |
| 8389 | NEXT; |
| 8390 | if (!eq) NEXT; |
| 8391 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8392 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8393 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8394 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8395 | SKIP_BLANKS; |
| 8396 | } |
| 8397 | } |
| 8398 | |
| 8399 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8400 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8401 | * @ctxt: the XPath Parser context |
| 8402 | * |
| 8403 | * [22] AndExpr ::= EqualityExpr |
| 8404 | * | AndExpr 'and' EqualityExpr |
| 8405 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8406 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8407 | * |
| 8408 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8409 | static void |
| 8410 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8411 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8412 | CHECK_ERROR; |
| 8413 | SKIP_BLANKS; |
| 8414 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8415 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8416 | SKIP(3); |
| 8417 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8418 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8419 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8420 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8421 | SKIP_BLANKS; |
| 8422 | } |
| 8423 | } |
| 8424 | |
| 8425 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8426 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8427 | * @ctxt: the XPath Parser context |
| 8428 | * |
| 8429 | * [14] Expr ::= OrExpr |
| 8430 | * [21] OrExpr ::= AndExpr |
| 8431 | * | OrExpr 'or' AndExpr |
| 8432 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8433 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8434 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8435 | static void |
| 8436 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 8437 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8438 | CHECK_ERROR; |
| 8439 | SKIP_BLANKS; |
| 8440 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8441 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8442 | SKIP(2); |
| 8443 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8444 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8445 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8446 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8447 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8448 | SKIP_BLANKS; |
| 8449 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8450 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 8451 | /* more ops could be optimized too */ |
| 8452 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8453 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8454 | } |
| 8455 | |
| 8456 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8457 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8458 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8459 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8460 | * |
| 8461 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8462 | * [9] PredicateExpr ::= Expr |
| 8463 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8464 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8465 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8466 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8467 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8468 | int op1 = ctxt->comp->last; |
| 8469 | |
| 8470 | SKIP_BLANKS; |
| 8471 | if (CUR != '[') { |
| 8472 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8473 | } |
| 8474 | NEXT; |
| 8475 | SKIP_BLANKS; |
| 8476 | |
| 8477 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8478 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8479 | CHECK_ERROR; |
| 8480 | |
| 8481 | if (CUR != ']') { |
| 8482 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8483 | } |
| 8484 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8485 | if (filter) |
| 8486 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8487 | else |
| 8488 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8489 | |
| 8490 | NEXT; |
| 8491 | SKIP_BLANKS; |
| 8492 | } |
| 8493 | |
| 8494 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8495 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8496 | * @ctxt: the XPath Parser context |
| 8497 | * @test: pointer to a xmlXPathTestVal |
| 8498 | * @type: pointer to a xmlXPathTypeVal |
| 8499 | * @prefix: placeholder for a possible name prefix |
| 8500 | * |
| 8501 | * [7] NodeTest ::= NameTest |
| 8502 | * | NodeType '(' ')' |
| 8503 | * | 'processing-instruction' '(' Literal ')' |
| 8504 | * |
| 8505 | * [37] NameTest ::= '*' |
| 8506 | * | NCName ':' '*' |
| 8507 | * | QName |
| 8508 | * [38] NodeType ::= 'comment' |
| 8509 | * | 'text' |
| 8510 | * | 'processing-instruction' |
| 8511 | * | 'node' |
| 8512 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8513 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8514 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8515 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8516 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8517 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8518 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8519 | int blanks; |
| 8520 | |
| 8521 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8522 | STRANGE; |
| 8523 | return(NULL); |
| 8524 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8525 | *type = (xmlXPathTypeVal) 0; |
| 8526 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8527 | *prefix = NULL; |
| 8528 | SKIP_BLANKS; |
| 8529 | |
| 8530 | if ((name == NULL) && (CUR == '*')) { |
| 8531 | /* |
| 8532 | * All elements |
| 8533 | */ |
| 8534 | NEXT; |
| 8535 | *test = NODE_TEST_ALL; |
| 8536 | return(NULL); |
| 8537 | } |
| 8538 | |
| 8539 | if (name == NULL) |
| 8540 | name = xmlXPathParseNCName(ctxt); |
| 8541 | if (name == NULL) { |
| 8542 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8543 | } |
| 8544 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8545 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8546 | SKIP_BLANKS; |
| 8547 | if (CUR == '(') { |
| 8548 | NEXT; |
| 8549 | /* |
| 8550 | * NodeType or PI search |
| 8551 | */ |
| 8552 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8553 | *type = NODE_TYPE_COMMENT; |
| 8554 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8555 | *type = NODE_TYPE_NODE; |
| 8556 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8557 | *type = NODE_TYPE_PI; |
| 8558 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8559 | *type = NODE_TYPE_TEXT; |
| 8560 | else { |
| 8561 | if (name != NULL) |
| 8562 | xmlFree(name); |
| 8563 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8564 | } |
| 8565 | |
| 8566 | *test = NODE_TEST_TYPE; |
| 8567 | |
| 8568 | SKIP_BLANKS; |
| 8569 | if (*type == NODE_TYPE_PI) { |
| 8570 | /* |
| 8571 | * Specific case: search a PI by name. |
| 8572 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8573 | if (name != NULL) |
| 8574 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8575 | name = NULL; |
| 8576 | if (CUR != ')') { |
| 8577 | name = xmlXPathParseLiteral(ctxt); |
| 8578 | CHECK_ERROR 0; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8579 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8580 | SKIP_BLANKS; |
| 8581 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8582 | } |
| 8583 | if (CUR != ')') { |
| 8584 | if (name != NULL) |
| 8585 | xmlFree(name); |
| 8586 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
| 8587 | } |
| 8588 | NEXT; |
| 8589 | return(name); |
| 8590 | } |
| 8591 | *test = NODE_TEST_NAME; |
| 8592 | if ((!blanks) && (CUR == ':')) { |
| 8593 | NEXT; |
| 8594 | |
| 8595 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8596 | * Since currently the parser context don't have a |
| 8597 | * namespace list associated: |
| 8598 | * The namespace name for this prefix can be computed |
| 8599 | * only at evaluation time. The compilation is done |
| 8600 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8601 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8602 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8603 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8604 | if (name != NULL) |
| 8605 | xmlFree(name); |
| 8606 | if (*prefix == NULL) { |
| 8607 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8608 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8609 | #else |
| 8610 | *prefix = name; |
| 8611 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8612 | |
| 8613 | if (CUR == '*') { |
| 8614 | /* |
| 8615 | * All elements |
| 8616 | */ |
| 8617 | NEXT; |
| 8618 | *test = NODE_TEST_ALL; |
| 8619 | return(NULL); |
| 8620 | } |
| 8621 | |
| 8622 | name = xmlXPathParseNCName(ctxt); |
| 8623 | if (name == NULL) { |
| 8624 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8625 | } |
| 8626 | } |
| 8627 | return(name); |
| 8628 | } |
| 8629 | |
| 8630 | /** |
| 8631 | * xmlXPathIsAxisName: |
| 8632 | * @name: a preparsed name token |
| 8633 | * |
| 8634 | * [6] AxisName ::= 'ancestor' |
| 8635 | * | 'ancestor-or-self' |
| 8636 | * | 'attribute' |
| 8637 | * | 'child' |
| 8638 | * | 'descendant' |
| 8639 | * | 'descendant-or-self' |
| 8640 | * | 'following' |
| 8641 | * | 'following-sibling' |
| 8642 | * | 'namespace' |
| 8643 | * | 'parent' |
| 8644 | * | 'preceding' |
| 8645 | * | 'preceding-sibling' |
| 8646 | * | 'self' |
| 8647 | * |
| 8648 | * Returns the axis or 0 |
| 8649 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8650 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8651 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8652 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8653 | switch (name[0]) { |
| 8654 | case 'a': |
| 8655 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8656 | ret = AXIS_ANCESTOR; |
| 8657 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8658 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8659 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8660 | ret = AXIS_ATTRIBUTE; |
| 8661 | break; |
| 8662 | case 'c': |
| 8663 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8664 | ret = AXIS_CHILD; |
| 8665 | break; |
| 8666 | case 'd': |
| 8667 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8668 | ret = AXIS_DESCENDANT; |
| 8669 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8670 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8671 | break; |
| 8672 | case 'f': |
| 8673 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8674 | ret = AXIS_FOLLOWING; |
| 8675 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8676 | ret = AXIS_FOLLOWING_SIBLING; |
| 8677 | break; |
| 8678 | case 'n': |
| 8679 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8680 | ret = AXIS_NAMESPACE; |
| 8681 | break; |
| 8682 | case 'p': |
| 8683 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8684 | ret = AXIS_PARENT; |
| 8685 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8686 | ret = AXIS_PRECEDING; |
| 8687 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8688 | ret = AXIS_PRECEDING_SIBLING; |
| 8689 | break; |
| 8690 | case 's': |
| 8691 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8692 | ret = AXIS_SELF; |
| 8693 | break; |
| 8694 | } |
| 8695 | return(ret); |
| 8696 | } |
| 8697 | |
| 8698 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8699 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8700 | * @ctxt: the XPath Parser context |
| 8701 | * |
| 8702 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8703 | * | AbbreviatedStep |
| 8704 | * |
| 8705 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8706 | * |
| 8707 | * [5] AxisSpecifier ::= AxisName '::' |
| 8708 | * | AbbreviatedAxisSpecifier |
| 8709 | * |
| 8710 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8711 | * |
| 8712 | * Modified for XPtr range support as: |
| 8713 | * |
| 8714 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8715 | * | AbbreviatedStep |
| 8716 | * | 'range-to' '(' Expr ')' Predicate* |
| 8717 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8718 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8719 | * A location step of . is short for self::node(). This is |
| 8720 | * particularly useful in conjunction with //. For example, the |
| 8721 | * location path .//para is short for |
| 8722 | * self::node()/descendant-or-self::node()/child::para |
| 8723 | * and so will select all para descendant elements of the context |
| 8724 | * node. |
| 8725 | * Similarly, a location step of .. is short for parent::node(). |
| 8726 | * For example, ../title is short for parent::node()/child::title |
| 8727 | * and so will select the title children of the parent of the context |
| 8728 | * node. |
| 8729 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8730 | static void |
| 8731 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8732 | #ifdef LIBXML_XPTR_ENABLED |
| 8733 | int rangeto = 0; |
| 8734 | int op2 = -1; |
| 8735 | #endif |
| 8736 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8737 | SKIP_BLANKS; |
| 8738 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8739 | SKIP(2); |
| 8740 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8741 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8742 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8743 | } else if (CUR == '.') { |
| 8744 | NEXT; |
| 8745 | SKIP_BLANKS; |
| 8746 | } else { |
| 8747 | xmlChar *name = NULL; |
| 8748 | const xmlChar *prefix = NULL; |
| 8749 | xmlXPathTestVal test; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8750 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8751 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8752 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8753 | |
| 8754 | /* |
| 8755 | * The modification needed for XPointer change to the production |
| 8756 | */ |
| 8757 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8758 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8759 | name = xmlXPathParseNCName(ctxt); |
| 8760 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8761 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8762 | xmlFree(name); |
| 8763 | SKIP_BLANKS; |
| 8764 | if (CUR != '(') { |
| 8765 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8766 | } |
| 8767 | NEXT; |
| 8768 | SKIP_BLANKS; |
| 8769 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8770 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8771 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8772 | CHECK_ERROR; |
| 8773 | |
| 8774 | SKIP_BLANKS; |
| 8775 | if (CUR != ')') { |
| 8776 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8777 | } |
| 8778 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8779 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8780 | goto eval_predicates; |
| 8781 | } |
| 8782 | } |
| 8783 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8784 | if (CUR == '*') { |
| 8785 | axis = AXIS_CHILD; |
| 8786 | } else { |
| 8787 | if (name == NULL) |
| 8788 | name = xmlXPathParseNCName(ctxt); |
| 8789 | if (name != NULL) { |
| 8790 | axis = xmlXPathIsAxisName(name); |
| 8791 | if (axis != 0) { |
| 8792 | SKIP_BLANKS; |
| 8793 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8794 | SKIP(2); |
| 8795 | xmlFree(name); |
| 8796 | name = NULL; |
| 8797 | } else { |
| 8798 | /* an element name can conflict with an axis one :-\ */ |
| 8799 | axis = AXIS_CHILD; |
| 8800 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8801 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8802 | axis = AXIS_CHILD; |
| 8803 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8804 | } else if (CUR == '@') { |
| 8805 | NEXT; |
| 8806 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8807 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8808 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8809 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | CHECK_ERROR; |
| 8813 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8814 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8815 | if (test == 0) |
| 8816 | return; |
| 8817 | |
| 8818 | #ifdef DEBUG_STEP |
| 8819 | xmlGenericError(xmlGenericErrorContext, |
| 8820 | "Basis : computing new set\n"); |
| 8821 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8822 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8823 | #ifdef DEBUG_STEP |
| 8824 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8825 | if (ctxt->value == NULL) |
| 8826 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8827 | else if (ctxt->value->nodesetval == NULL) |
| 8828 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8829 | else |
| 8830 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8831 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8832 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8833 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8834 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8835 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8836 | op1 = ctxt->comp->last; |
| 8837 | ctxt->comp->last = -1; |
| 8838 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8839 | SKIP_BLANKS; |
| 8840 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8841 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8842 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8843 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8844 | #ifdef LIBXML_XPTR_ENABLED |
| 8845 | if (rangeto) { |
| 8846 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8847 | } else |
| 8848 | #endif |
| 8849 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8850 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8851 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8852 | } |
| 8853 | #ifdef DEBUG_STEP |
| 8854 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8855 | if (ctxt->value == NULL) |
| 8856 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8857 | else if (ctxt->value->nodesetval == NULL) |
| 8858 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8859 | else |
| 8860 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8861 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8862 | #endif |
| 8863 | } |
| 8864 | |
| 8865 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8866 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8867 | * @ctxt: the XPath Parser context |
| 8868 | * |
| 8869 | * [3] RelativeLocationPath ::= Step |
| 8870 | * | RelativeLocationPath '/' Step |
| 8871 | * | AbbreviatedRelativeLocationPath |
| 8872 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8873 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8874 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8875 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8876 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8877 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8878 | (xmlXPathParserContextPtr ctxt) { |
| 8879 | SKIP_BLANKS; |
| 8880 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8881 | SKIP(2); |
| 8882 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8883 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8884 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8885 | } else if (CUR == '/') { |
| 8886 | NEXT; |
| 8887 | SKIP_BLANKS; |
| 8888 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8889 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8890 | SKIP_BLANKS; |
| 8891 | while (CUR == '/') { |
| 8892 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8893 | SKIP(2); |
| 8894 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8895 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8896 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8897 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8898 | } else if (CUR == '/') { |
| 8899 | NEXT; |
| 8900 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8901 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8902 | } |
| 8903 | SKIP_BLANKS; |
| 8904 | } |
| 8905 | } |
| 8906 | |
| 8907 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8908 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8909 | * @ctxt: the XPath Parser context |
| 8910 | * |
| 8911 | * [1] LocationPath ::= RelativeLocationPath |
| 8912 | * | AbsoluteLocationPath |
| 8913 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 8914 | * | AbbreviatedAbsoluteLocationPath |
| 8915 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 8916 | * '//' RelativeLocationPath |
| 8917 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8918 | * Compile a location path |
| 8919 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8920 | * // is short for /descendant-or-self::node()/. For example, |
| 8921 | * //para is short for /descendant-or-self::node()/child::para and |
| 8922 | * so will select any para element in the document (even a para element |
| 8923 | * that is a document element will be selected by //para since the |
| 8924 | * document element node is a child of the root node); div//para is |
| 8925 | * short for div/descendant-or-self::node()/child::para and so will |
| 8926 | * select all para descendants of div children. |
| 8927 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8928 | static void |
| 8929 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8930 | SKIP_BLANKS; |
| 8931 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8932 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8933 | } else { |
| 8934 | while (CUR == '/') { |
| 8935 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8936 | SKIP(2); |
| 8937 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8938 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8939 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8940 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8941 | } else if (CUR == '/') { |
| 8942 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 8943 | SKIP_BLANKS; |
| 8944 | if ((CUR != 0 ) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8945 | ((IS_LETTER_CH(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 8946 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8947 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8948 | } |
| 8949 | } |
| 8950 | } |
| 8951 | } |
| 8952 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8953 | /************************************************************************ |
| 8954 | * * |
| 8955 | * XPath precompiled expression evaluation * |
| 8956 | * * |
| 8957 | ************************************************************************/ |
| 8958 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8959 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8960 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 8961 | |
| 8962 | /** |
| 8963 | * xmlXPathNodeCollectAndTest: |
| 8964 | * @ctxt: the XPath Parser context |
| 8965 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8966 | * @first: pointer to the first element in document order |
| 8967 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8968 | * |
| 8969 | * This is the function implementing a step: based on the current list |
| 8970 | * of nodes, it builds up a new list, looking at all nodes under that |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8971 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8972 | * |
| 8973 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8974 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8975 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8976 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8977 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8978 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8979 | xmlXPathStepOpPtr op, |
| 8980 | xmlNodePtr * first, xmlNodePtr * last) |
| 8981 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8982 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 8983 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 8984 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8985 | const xmlChar *prefix = op->value4; |
| 8986 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8987 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8988 | |
| 8989 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8990 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8991 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8992 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8993 | xmlNodeSetPtr ret, list; |
| 8994 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8995 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8996 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8997 | xmlNodePtr cur = NULL; |
| 8998 | xmlXPathObjectPtr obj; |
| 8999 | xmlNodeSetPtr nodelist; |
| 9000 | xmlNodePtr tmp; |
| 9001 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9002 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9003 | obj = valuePop(ctxt); |
| 9004 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9005 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9006 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9007 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 9008 | if (URI == NULL) |
| 9009 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9010 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9011 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9012 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | switch (axis) { |
| 9015 | case AXIS_ANCESTOR: |
| 9016 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9017 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9018 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9019 | first = NULL; |
| 9020 | next = xmlXPathNextAncestor; |
| 9021 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9022 | case AXIS_ANCESTOR_OR_SELF: |
| 9023 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9024 | xmlGenericError(xmlGenericErrorContext, |
| 9025 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9026 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9027 | first = NULL; |
| 9028 | next = xmlXPathNextAncestorOrSelf; |
| 9029 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9030 | case AXIS_ATTRIBUTE: |
| 9031 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9032 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9033 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9034 | first = NULL; |
| 9035 | last = NULL; |
| 9036 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9037 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9038 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9039 | case AXIS_CHILD: |
| 9040 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9041 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9042 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9043 | last = NULL; |
| 9044 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9045 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9046 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9047 | case AXIS_DESCENDANT: |
| 9048 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9049 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9050 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9051 | last = NULL; |
| 9052 | next = xmlXPathNextDescendant; |
| 9053 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9054 | case AXIS_DESCENDANT_OR_SELF: |
| 9055 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9056 | xmlGenericError(xmlGenericErrorContext, |
| 9057 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9058 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9059 | last = NULL; |
| 9060 | next = xmlXPathNextDescendantOrSelf; |
| 9061 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9062 | case AXIS_FOLLOWING: |
| 9063 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9064 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9065 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9066 | last = NULL; |
| 9067 | next = xmlXPathNextFollowing; |
| 9068 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9069 | case AXIS_FOLLOWING_SIBLING: |
| 9070 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9071 | xmlGenericError(xmlGenericErrorContext, |
| 9072 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9073 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9074 | last = NULL; |
| 9075 | next = xmlXPathNextFollowingSibling; |
| 9076 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9077 | case AXIS_NAMESPACE: |
| 9078 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9079 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9080 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9081 | first = NULL; |
| 9082 | last = NULL; |
| 9083 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9084 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9085 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9086 | case AXIS_PARENT: |
| 9087 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9088 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9089 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9090 | first = NULL; |
| 9091 | next = xmlXPathNextParent; |
| 9092 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9093 | case AXIS_PRECEDING: |
| 9094 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9095 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9096 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9097 | first = NULL; |
| 9098 | next = xmlXPathNextPrecedingInternal; |
| 9099 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9100 | case AXIS_PRECEDING_SIBLING: |
| 9101 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9102 | xmlGenericError(xmlGenericErrorContext, |
| 9103 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9104 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9105 | first = NULL; |
| 9106 | next = xmlXPathNextPrecedingSibling; |
| 9107 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9108 | case AXIS_SELF: |
| 9109 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9110 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9111 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9112 | first = NULL; |
| 9113 | last = NULL; |
| 9114 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9115 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9116 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9117 | } |
| 9118 | if (next == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9119 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9120 | |
| 9121 | nodelist = obj->nodesetval; |
| 9122 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9123 | xmlXPathFreeObject(obj); |
| 9124 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9125 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9126 | } |
| 9127 | addNode = xmlXPathNodeSetAddUnique; |
| 9128 | ret = NULL; |
| 9129 | #ifdef DEBUG_STEP |
| 9130 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9131 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9132 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9133 | case NODE_TEST_NONE: |
| 9134 | xmlGenericError(xmlGenericErrorContext, |
| 9135 | " searching for none !!!\n"); |
| 9136 | break; |
| 9137 | case NODE_TEST_TYPE: |
| 9138 | xmlGenericError(xmlGenericErrorContext, |
| 9139 | " searching for type %d\n", type); |
| 9140 | break; |
| 9141 | case NODE_TEST_PI: |
| 9142 | xmlGenericError(xmlGenericErrorContext, |
| 9143 | " searching for PI !!!\n"); |
| 9144 | break; |
| 9145 | case NODE_TEST_ALL: |
| 9146 | xmlGenericError(xmlGenericErrorContext, |
| 9147 | " searching for *\n"); |
| 9148 | break; |
| 9149 | case NODE_TEST_NS: |
| 9150 | xmlGenericError(xmlGenericErrorContext, |
| 9151 | " searching for namespace %s\n", |
| 9152 | prefix); |
| 9153 | break; |
| 9154 | case NODE_TEST_NAME: |
| 9155 | xmlGenericError(xmlGenericErrorContext, |
| 9156 | " searching for name %s\n", name); |
| 9157 | if (prefix != NULL) |
| 9158 | xmlGenericError(xmlGenericErrorContext, |
| 9159 | " with namespace %s\n", prefix); |
| 9160 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9161 | } |
| 9162 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9163 | #endif |
| 9164 | /* |
| 9165 | * 2.3 Node Tests |
| 9166 | * - For the attribute axis, the principal node type is attribute. |
| 9167 | * - For the namespace axis, the principal node type is namespace. |
| 9168 | * - For other axes, the principal node type is element. |
| 9169 | * |
| 9170 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9171 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9172 | * select all element children of the context node |
| 9173 | */ |
| 9174 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9175 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9176 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9177 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9178 | cur = NULL; |
| 9179 | list = xmlXPathNodeSetCreate(NULL); |
| 9180 | do { |
| 9181 | cur = next(ctxt, cur); |
| 9182 | if (cur == NULL) |
| 9183 | break; |
| 9184 | if ((first != NULL) && (*first == cur)) |
| 9185 | break; |
| 9186 | if (((t % 256) == 0) && |
| 9187 | (first != NULL) && (*first != NULL) && |
| 9188 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9189 | break; |
| 9190 | if ((last != NULL) && (*last == cur)) |
| 9191 | break; |
| 9192 | if (((t % 256) == 0) && |
| 9193 | (last != NULL) && (*last != NULL) && |
| 9194 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9195 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9196 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9197 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9198 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9199 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9200 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9201 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9202 | ctxt->context->node = tmp; |
| 9203 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9204 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9205 | if ((cur->type == type) || |
| 9206 | ((type == NODE_TYPE_NODE) && |
| 9207 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9208 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9209 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9210 | (cur->type == XML_NAMESPACE_DECL) || |
| 9211 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9212 | (cur->type == XML_PI_NODE) || |
| 9213 | (cur->type == XML_COMMENT_NODE) || |
| 9214 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9215 | (cur->type == XML_TEXT_NODE))) || |
| 9216 | ((type == NODE_TYPE_TEXT) && |
| 9217 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9218 | #ifdef DEBUG_STEP |
| 9219 | n++; |
| 9220 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9221 | addNode(list, cur); |
| 9222 | } |
| 9223 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9224 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9225 | if (cur->type == XML_PI_NODE) { |
| 9226 | if ((name != NULL) && |
| 9227 | (!xmlStrEqual(name, cur->name))) |
| 9228 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9229 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9230 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9231 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9232 | addNode(list, cur); |
| 9233 | } |
| 9234 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9235 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9236 | if (axis == AXIS_ATTRIBUTE) { |
| 9237 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9238 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9239 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9240 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9241 | addNode(list, cur); |
| 9242 | } |
| 9243 | } else if (axis == AXIS_NAMESPACE) { |
| 9244 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9245 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9246 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9247 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9248 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9249 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9250 | } |
| 9251 | } else { |
| 9252 | if (cur->type == XML_ELEMENT_NODE) { |
| 9253 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9254 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9255 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9256 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9257 | addNode(list, cur); |
| 9258 | } else if ((cur->ns != NULL) && |
| 9259 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9260 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9261 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9262 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9263 | addNode(list, cur); |
| 9264 | } |
| 9265 | } |
| 9266 | } |
| 9267 | break; |
| 9268 | case NODE_TEST_NS:{ |
| 9269 | TODO; |
| 9270 | break; |
| 9271 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9272 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9273 | switch (cur->type) { |
| 9274 | case XML_ELEMENT_NODE: |
| 9275 | if (xmlStrEqual(name, cur->name)) { |
| 9276 | if (prefix == NULL) { |
| 9277 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9278 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9279 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9280 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9281 | addNode(list, cur); |
| 9282 | } |
| 9283 | } else { |
| 9284 | if ((cur->ns != NULL) && |
| 9285 | (xmlStrEqual(URI, |
| 9286 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9287 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9288 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9289 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9290 | addNode(list, cur); |
| 9291 | } |
| 9292 | } |
| 9293 | } |
| 9294 | break; |
| 9295 | case XML_ATTRIBUTE_NODE:{ |
| 9296 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9297 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9298 | if (xmlStrEqual(name, attr->name)) { |
| 9299 | if (prefix == NULL) { |
| 9300 | if ((attr->ns == NULL) || |
| 9301 | (attr->ns->prefix == NULL)) { |
| 9302 | #ifdef DEBUG_STEP |
| 9303 | n++; |
| 9304 | #endif |
| 9305 | addNode(list, |
| 9306 | (xmlNodePtr) attr); |
| 9307 | } |
| 9308 | } else { |
| 9309 | if ((attr->ns != NULL) && |
| 9310 | (xmlStrEqual(URI, |
| 9311 | attr->ns-> |
| 9312 | href))) { |
| 9313 | #ifdef DEBUG_STEP |
| 9314 | n++; |
| 9315 | #endif |
| 9316 | addNode(list, |
| 9317 | (xmlNodePtr) attr); |
| 9318 | } |
| 9319 | } |
| 9320 | } |
| 9321 | break; |
| 9322 | } |
| 9323 | case XML_NAMESPACE_DECL: |
| 9324 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9325 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9326 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9327 | if ((ns->prefix != NULL) && (name != NULL) |
| 9328 | && (xmlStrEqual(ns->prefix, name))) { |
| 9329 | #ifdef DEBUG_STEP |
| 9330 | n++; |
| 9331 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9332 | xmlXPathNodeSetAddNs(list, |
| 9333 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9334 | } |
| 9335 | } |
| 9336 | break; |
| 9337 | default: |
| 9338 | break; |
| 9339 | } |
| 9340 | break; |
| 9341 | break; |
| 9342 | } |
| 9343 | } while (cur != NULL); |
| 9344 | |
| 9345 | /* |
| 9346 | * If there is some predicate filtering do it now |
| 9347 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9348 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9349 | xmlXPathObjectPtr obj2; |
| 9350 | |
| 9351 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9352 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9353 | CHECK_TYPE0(XPATH_NODESET); |
| 9354 | obj2 = valuePop(ctxt); |
| 9355 | list = obj2->nodesetval; |
| 9356 | obj2->nodesetval = NULL; |
| 9357 | xmlXPathFreeObject(obj2); |
| 9358 | } |
| 9359 | if (ret == NULL) { |
| 9360 | ret = list; |
| 9361 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9362 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9363 | xmlXPathFreeNodeSet(list); |
| 9364 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9365 | } |
| 9366 | ctxt->context->node = tmp; |
| 9367 | #ifdef DEBUG_STEP |
| 9368 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9369 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9370 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9371 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9372 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9373 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9374 | ctxt->value->boolval = 1; |
| 9375 | ctxt->value->user = obj->user; |
| 9376 | obj->user = NULL; |
| 9377 | obj->boolval = 0; |
| 9378 | } |
| 9379 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9380 | return(t); |
| 9381 | } |
| 9382 | |
| 9383 | /** |
| 9384 | * xmlXPathNodeCollectAndTestNth: |
| 9385 | * @ctxt: the XPath Parser context |
| 9386 | * @op: the XPath precompiled step operation |
| 9387 | * @indx: the index to collect |
| 9388 | * @first: pointer to the first element in document order |
| 9389 | * @last: pointer to the last element in document order |
| 9390 | * |
| 9391 | * This is the function implementing a step: based on the current list |
| 9392 | * of nodes, it builds up a new list, looking at all nodes under that |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9393 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9394 | * |
| 9395 | * Pushes the new NodeSet resulting from the search. |
| 9396 | * Returns the number of node traversed |
| 9397 | */ |
| 9398 | static int |
| 9399 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9400 | xmlXPathStepOpPtr op, int indx, |
| 9401 | xmlNodePtr * first, xmlNodePtr * last) |
| 9402 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9403 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9404 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9405 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9406 | const xmlChar *prefix = op->value4; |
| 9407 | const xmlChar *name = op->value5; |
| 9408 | const xmlChar *URI = NULL; |
| 9409 | int n = 0, t = 0; |
| 9410 | |
| 9411 | int i; |
| 9412 | xmlNodeSetPtr list; |
| 9413 | xmlXPathTraversalFunction next = NULL; |
| 9414 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9415 | xmlNodePtr cur = NULL; |
| 9416 | xmlXPathObjectPtr obj; |
| 9417 | xmlNodeSetPtr nodelist; |
| 9418 | xmlNodePtr tmp; |
| 9419 | |
| 9420 | CHECK_TYPE0(XPATH_NODESET); |
| 9421 | obj = valuePop(ctxt); |
| 9422 | addNode = xmlXPathNodeSetAdd; |
| 9423 | if (prefix != NULL) { |
| 9424 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 9425 | if (URI == NULL) |
| 9426 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 9427 | } |
| 9428 | #ifdef DEBUG_STEP_NTH |
| 9429 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9430 | if (first != NULL) { |
| 9431 | if (*first != NULL) |
| 9432 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9433 | (*first)->name); |
| 9434 | else |
| 9435 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9436 | } |
| 9437 | if (last != NULL) { |
| 9438 | if (*last != NULL) |
| 9439 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9440 | (*last)->name); |
| 9441 | else |
| 9442 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9443 | } |
| 9444 | #endif |
| 9445 | switch (axis) { |
| 9446 | case AXIS_ANCESTOR: |
| 9447 | #ifdef DEBUG_STEP_NTH |
| 9448 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9449 | #endif |
| 9450 | first = NULL; |
| 9451 | next = xmlXPathNextAncestor; |
| 9452 | break; |
| 9453 | case AXIS_ANCESTOR_OR_SELF: |
| 9454 | #ifdef DEBUG_STEP_NTH |
| 9455 | xmlGenericError(xmlGenericErrorContext, |
| 9456 | "axis 'ancestors-or-self' "); |
| 9457 | #endif |
| 9458 | first = NULL; |
| 9459 | next = xmlXPathNextAncestorOrSelf; |
| 9460 | break; |
| 9461 | case AXIS_ATTRIBUTE: |
| 9462 | #ifdef DEBUG_STEP_NTH |
| 9463 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 9464 | #endif |
| 9465 | first = NULL; |
| 9466 | last = NULL; |
| 9467 | next = xmlXPathNextAttribute; |
| 9468 | break; |
| 9469 | case AXIS_CHILD: |
| 9470 | #ifdef DEBUG_STEP_NTH |
| 9471 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9472 | #endif |
| 9473 | last = NULL; |
| 9474 | next = xmlXPathNextChild; |
| 9475 | break; |
| 9476 | case AXIS_DESCENDANT: |
| 9477 | #ifdef DEBUG_STEP_NTH |
| 9478 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9479 | #endif |
| 9480 | last = NULL; |
| 9481 | next = xmlXPathNextDescendant; |
| 9482 | break; |
| 9483 | case AXIS_DESCENDANT_OR_SELF: |
| 9484 | #ifdef DEBUG_STEP_NTH |
| 9485 | xmlGenericError(xmlGenericErrorContext, |
| 9486 | "axis 'descendant-or-self' "); |
| 9487 | #endif |
| 9488 | last = NULL; |
| 9489 | next = xmlXPathNextDescendantOrSelf; |
| 9490 | break; |
| 9491 | case AXIS_FOLLOWING: |
| 9492 | #ifdef DEBUG_STEP_NTH |
| 9493 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9494 | #endif |
| 9495 | last = NULL; |
| 9496 | next = xmlXPathNextFollowing; |
| 9497 | break; |
| 9498 | case AXIS_FOLLOWING_SIBLING: |
| 9499 | #ifdef DEBUG_STEP_NTH |
| 9500 | xmlGenericError(xmlGenericErrorContext, |
| 9501 | "axis 'following-siblings' "); |
| 9502 | #endif |
| 9503 | last = NULL; |
| 9504 | next = xmlXPathNextFollowingSibling; |
| 9505 | break; |
| 9506 | case AXIS_NAMESPACE: |
| 9507 | #ifdef DEBUG_STEP_NTH |
| 9508 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9509 | #endif |
| 9510 | last = NULL; |
| 9511 | first = NULL; |
| 9512 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9513 | break; |
| 9514 | case AXIS_PARENT: |
| 9515 | #ifdef DEBUG_STEP_NTH |
| 9516 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9517 | #endif |
| 9518 | first = NULL; |
| 9519 | next = xmlXPathNextParent; |
| 9520 | break; |
| 9521 | case AXIS_PRECEDING: |
| 9522 | #ifdef DEBUG_STEP_NTH |
| 9523 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9524 | #endif |
| 9525 | first = NULL; |
| 9526 | next = xmlXPathNextPrecedingInternal; |
| 9527 | break; |
| 9528 | case AXIS_PRECEDING_SIBLING: |
| 9529 | #ifdef DEBUG_STEP_NTH |
| 9530 | xmlGenericError(xmlGenericErrorContext, |
| 9531 | "axis 'preceding-sibling' "); |
| 9532 | #endif |
| 9533 | first = NULL; |
| 9534 | next = xmlXPathNextPrecedingSibling; |
| 9535 | break; |
| 9536 | case AXIS_SELF: |
| 9537 | #ifdef DEBUG_STEP_NTH |
| 9538 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9539 | #endif |
| 9540 | first = NULL; |
| 9541 | last = NULL; |
| 9542 | next = xmlXPathNextSelf; |
| 9543 | break; |
| 9544 | } |
| 9545 | if (next == NULL) |
| 9546 | return(0); |
| 9547 | |
| 9548 | nodelist = obj->nodesetval; |
| 9549 | if (nodelist == NULL) { |
| 9550 | xmlXPathFreeObject(obj); |
| 9551 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9552 | return(0); |
| 9553 | } |
| 9554 | addNode = xmlXPathNodeSetAddUnique; |
| 9555 | #ifdef DEBUG_STEP_NTH |
| 9556 | xmlGenericError(xmlGenericErrorContext, |
| 9557 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9558 | switch (test) { |
| 9559 | case NODE_TEST_NONE: |
| 9560 | xmlGenericError(xmlGenericErrorContext, |
| 9561 | " searching for none !!!\n"); |
| 9562 | break; |
| 9563 | case NODE_TEST_TYPE: |
| 9564 | xmlGenericError(xmlGenericErrorContext, |
| 9565 | " searching for type %d\n", type); |
| 9566 | break; |
| 9567 | case NODE_TEST_PI: |
| 9568 | xmlGenericError(xmlGenericErrorContext, |
| 9569 | " searching for PI !!!\n"); |
| 9570 | break; |
| 9571 | case NODE_TEST_ALL: |
| 9572 | xmlGenericError(xmlGenericErrorContext, |
| 9573 | " searching for *\n"); |
| 9574 | break; |
| 9575 | case NODE_TEST_NS: |
| 9576 | xmlGenericError(xmlGenericErrorContext, |
| 9577 | " searching for namespace %s\n", |
| 9578 | prefix); |
| 9579 | break; |
| 9580 | case NODE_TEST_NAME: |
| 9581 | xmlGenericError(xmlGenericErrorContext, |
| 9582 | " searching for name %s\n", name); |
| 9583 | if (prefix != NULL) |
| 9584 | xmlGenericError(xmlGenericErrorContext, |
| 9585 | " with namespace %s\n", prefix); |
| 9586 | break; |
| 9587 | } |
| 9588 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9589 | #endif |
| 9590 | /* |
| 9591 | * 2.3 Node Tests |
| 9592 | * - For the attribute axis, the principal node type is attribute. |
| 9593 | * - For the namespace axis, the principal node type is namespace. |
| 9594 | * - For other axes, the principal node type is element. |
| 9595 | * |
| 9596 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9597 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9598 | * select all element children of the context node |
| 9599 | */ |
| 9600 | tmp = ctxt->context->node; |
| 9601 | list = xmlXPathNodeSetCreate(NULL); |
| 9602 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9603 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9604 | |
| 9605 | cur = NULL; |
| 9606 | n = 0; |
| 9607 | do { |
| 9608 | cur = next(ctxt, cur); |
| 9609 | if (cur == NULL) |
| 9610 | break; |
| 9611 | if ((first != NULL) && (*first == cur)) |
| 9612 | break; |
| 9613 | if (((t % 256) == 0) && |
| 9614 | (first != NULL) && (*first != NULL) && |
| 9615 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9616 | break; |
| 9617 | if ((last != NULL) && (*last == cur)) |
| 9618 | break; |
| 9619 | if (((t % 256) == 0) && |
| 9620 | (last != NULL) && (*last != NULL) && |
| 9621 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9622 | break; |
| 9623 | t++; |
| 9624 | switch (test) { |
| 9625 | case NODE_TEST_NONE: |
| 9626 | ctxt->context->node = tmp; |
| 9627 | STRANGE return(0); |
| 9628 | case NODE_TEST_TYPE: |
| 9629 | if ((cur->type == type) || |
| 9630 | ((type == NODE_TYPE_NODE) && |
| 9631 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9632 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9633 | (cur->type == XML_ELEMENT_NODE) || |
| 9634 | (cur->type == XML_PI_NODE) || |
| 9635 | (cur->type == XML_COMMENT_NODE) || |
| 9636 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 9637 | (cur->type == XML_TEXT_NODE))) || |
| 9638 | ((type == NODE_TYPE_TEXT) && |
| 9639 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9640 | n++; |
| 9641 | if (n == indx) |
| 9642 | addNode(list, cur); |
| 9643 | } |
| 9644 | break; |
| 9645 | case NODE_TEST_PI: |
| 9646 | if (cur->type == XML_PI_NODE) { |
| 9647 | if ((name != NULL) && |
| 9648 | (!xmlStrEqual(name, cur->name))) |
| 9649 | break; |
| 9650 | n++; |
| 9651 | if (n == indx) |
| 9652 | addNode(list, cur); |
| 9653 | } |
| 9654 | break; |
| 9655 | case NODE_TEST_ALL: |
| 9656 | if (axis == AXIS_ATTRIBUTE) { |
| 9657 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9658 | n++; |
| 9659 | if (n == indx) |
| 9660 | addNode(list, cur); |
| 9661 | } |
| 9662 | } else if (axis == AXIS_NAMESPACE) { |
| 9663 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9664 | n++; |
| 9665 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9666 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9667 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9668 | } |
| 9669 | } else { |
| 9670 | if (cur->type == XML_ELEMENT_NODE) { |
| 9671 | if (prefix == NULL) { |
| 9672 | n++; |
| 9673 | if (n == indx) |
| 9674 | addNode(list, cur); |
| 9675 | } else if ((cur->ns != NULL) && |
| 9676 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9677 | n++; |
| 9678 | if (n == indx) |
| 9679 | addNode(list, cur); |
| 9680 | } |
| 9681 | } |
| 9682 | } |
| 9683 | break; |
| 9684 | case NODE_TEST_NS:{ |
| 9685 | TODO; |
| 9686 | break; |
| 9687 | } |
| 9688 | case NODE_TEST_NAME: |
| 9689 | switch (cur->type) { |
| 9690 | case XML_ELEMENT_NODE: |
| 9691 | if (xmlStrEqual(name, cur->name)) { |
| 9692 | if (prefix == NULL) { |
| 9693 | if (cur->ns == NULL) { |
| 9694 | n++; |
| 9695 | if (n == indx) |
| 9696 | addNode(list, cur); |
| 9697 | } |
| 9698 | } else { |
| 9699 | if ((cur->ns != NULL) && |
| 9700 | (xmlStrEqual(URI, |
| 9701 | cur->ns->href))) { |
| 9702 | n++; |
| 9703 | if (n == indx) |
| 9704 | addNode(list, cur); |
| 9705 | } |
| 9706 | } |
| 9707 | } |
| 9708 | break; |
| 9709 | case XML_ATTRIBUTE_NODE:{ |
| 9710 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9711 | |
| 9712 | if (xmlStrEqual(name, attr->name)) { |
| 9713 | if (prefix == NULL) { |
| 9714 | if ((attr->ns == NULL) || |
| 9715 | (attr->ns->prefix == NULL)) { |
| 9716 | n++; |
| 9717 | if (n == indx) |
| 9718 | addNode(list, cur); |
| 9719 | } |
| 9720 | } else { |
| 9721 | if ((attr->ns != NULL) && |
| 9722 | (xmlStrEqual(URI, |
| 9723 | attr->ns-> |
| 9724 | href))) { |
| 9725 | n++; |
| 9726 | if (n == indx) |
| 9727 | addNode(list, cur); |
| 9728 | } |
| 9729 | } |
| 9730 | } |
| 9731 | break; |
| 9732 | } |
| 9733 | case XML_NAMESPACE_DECL: |
| 9734 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9735 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9736 | |
| 9737 | if ((ns->prefix != NULL) && (name != NULL) |
| 9738 | && (xmlStrEqual(ns->prefix, name))) { |
| 9739 | n++; |
| 9740 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9741 | xmlXPathNodeSetAddNs(list, |
| 9742 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9743 | } |
| 9744 | } |
| 9745 | break; |
| 9746 | default: |
| 9747 | break; |
| 9748 | } |
| 9749 | break; |
| 9750 | break; |
| 9751 | } |
| 9752 | } while (n < indx); |
| 9753 | } |
| 9754 | ctxt->context->node = tmp; |
| 9755 | #ifdef DEBUG_STEP_NTH |
| 9756 | xmlGenericError(xmlGenericErrorContext, |
| 9757 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9758 | t, list->nodeNr); |
| 9759 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9760 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9761 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9762 | ctxt->value->boolval = 1; |
| 9763 | ctxt->value->user = obj->user; |
| 9764 | obj->user = NULL; |
| 9765 | obj->boolval = 0; |
| 9766 | } |
| 9767 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9768 | return(t); |
| 9769 | } |
| 9770 | |
| 9771 | /** |
| 9772 | * xmlXPathCompOpEvalFirst: |
| 9773 | * @ctxt: the XPath parser context with the compiled expression |
| 9774 | * @op: an XPath compiled operation |
| 9775 | * @first: the first elem found so far |
| 9776 | * |
| 9777 | * Evaluate the Precompiled XPath operation searching only the first |
| 9778 | * element in document order |
| 9779 | * |
| 9780 | * Returns the number of examined objects. |
| 9781 | */ |
| 9782 | static int |
| 9783 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9784 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9785 | { |
| 9786 | int total = 0, cur; |
| 9787 | xmlXPathCompExprPtr comp; |
| 9788 | xmlXPathObjectPtr arg1, arg2; |
| 9789 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9790 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9791 | comp = ctxt->comp; |
| 9792 | switch (op->op) { |
| 9793 | case XPATH_OP_END: |
| 9794 | return (0); |
| 9795 | case XPATH_OP_UNION: |
| 9796 | total = |
| 9797 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9798 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9799 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9800 | if ((ctxt->value != NULL) |
| 9801 | && (ctxt->value->type == XPATH_NODESET) |
| 9802 | && (ctxt->value->nodesetval != NULL) |
| 9803 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9804 | /* |
| 9805 | * limit tree traversing to first node in the result |
| 9806 | */ |
| 9807 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9808 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9809 | } |
| 9810 | cur = |
| 9811 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9812 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9813 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9814 | CHECK_TYPE0(XPATH_NODESET); |
| 9815 | arg2 = valuePop(ctxt); |
| 9816 | |
| 9817 | CHECK_TYPE0(XPATH_NODESET); |
| 9818 | arg1 = valuePop(ctxt); |
| 9819 | |
| 9820 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9821 | arg2->nodesetval); |
| 9822 | valuePush(ctxt, arg1); |
| 9823 | xmlXPathFreeObject(arg2); |
| 9824 | /* optimizer */ |
| 9825 | if (total > cur) |
| 9826 | xmlXPathCompSwap(op); |
| 9827 | return (total + cur); |
| 9828 | case XPATH_OP_ROOT: |
| 9829 | xmlXPathRoot(ctxt); |
| 9830 | return (0); |
| 9831 | case XPATH_OP_NODE: |
| 9832 | if (op->ch1 != -1) |
| 9833 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9834 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9835 | if (op->ch2 != -1) |
| 9836 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9837 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9838 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9839 | return (total); |
| 9840 | case XPATH_OP_RESET: |
| 9841 | if (op->ch1 != -1) |
| 9842 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9843 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9844 | if (op->ch2 != -1) |
| 9845 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9846 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9847 | ctxt->context->node = NULL; |
| 9848 | return (total); |
| 9849 | case XPATH_OP_COLLECT:{ |
| 9850 | if (op->ch1 == -1) |
| 9851 | return (total); |
| 9852 | |
| 9853 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9854 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9855 | |
| 9856 | /* |
| 9857 | * Optimization for [n] selection where n is a number |
| 9858 | */ |
| 9859 | if ((op->ch2 != -1) && |
| 9860 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9861 | (comp->steps[op->ch2].ch1 == -1) && |
| 9862 | (comp->steps[op->ch2].ch2 != -1) && |
| 9863 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9864 | XPATH_OP_VALUE)) { |
| 9865 | xmlXPathObjectPtr val; |
| 9866 | |
| 9867 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9868 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9869 | int indx = (int) val->floatval; |
| 9870 | |
| 9871 | if (val->floatval == (float) indx) { |
| 9872 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 9873 | first, NULL); |
| 9874 | return (total); |
| 9875 | } |
| 9876 | } |
| 9877 | } |
| 9878 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 9879 | return (total); |
| 9880 | } |
| 9881 | case XPATH_OP_VALUE: |
| 9882 | valuePush(ctxt, |
| 9883 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9884 | return (0); |
| 9885 | case XPATH_OP_SORT: |
| 9886 | if (op->ch1 != -1) |
| 9887 | total += |
| 9888 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9889 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9890 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9891 | if ((ctxt->value != NULL) |
| 9892 | && (ctxt->value->type == XPATH_NODESET) |
| 9893 | && (ctxt->value->nodesetval != NULL)) |
| 9894 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9895 | return (total); |
| 9896 | default: |
| 9897 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9898 | } |
| 9899 | } |
| 9900 | |
| 9901 | /** |
| 9902 | * xmlXPathCompOpEvalLast: |
| 9903 | * @ctxt: the XPath parser context with the compiled expression |
| 9904 | * @op: an XPath compiled operation |
| 9905 | * @last: the last elem found so far |
| 9906 | * |
| 9907 | * Evaluate the Precompiled XPath operation searching only the last |
| 9908 | * element in document order |
| 9909 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9910 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9911 | */ |
| 9912 | static int |
| 9913 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 9914 | xmlNodePtr * last) |
| 9915 | { |
| 9916 | int total = 0, cur; |
| 9917 | xmlXPathCompExprPtr comp; |
| 9918 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 9919 | xmlNodePtr bak; |
| 9920 | xmlDocPtr bakd; |
| 9921 | int pp; |
| 9922 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9923 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9924 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9925 | comp = ctxt->comp; |
| 9926 | switch (op->op) { |
| 9927 | case XPATH_OP_END: |
| 9928 | return (0); |
| 9929 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 9930 | bakd = ctxt->context->doc; |
| 9931 | bak = ctxt->context->node; |
| 9932 | pp = ctxt->context->proximityPosition; |
| 9933 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9934 | total = |
| 9935 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9936 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9937 | if ((ctxt->value != NULL) |
| 9938 | && (ctxt->value->type == XPATH_NODESET) |
| 9939 | && (ctxt->value->nodesetval != NULL) |
| 9940 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9941 | /* |
| 9942 | * limit tree traversing to first node in the result |
| 9943 | */ |
| 9944 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9945 | *last = |
| 9946 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 9947 | nodesetval->nodeNr - |
| 9948 | 1]; |
| 9949 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 9950 | ctxt->context->doc = bakd; |
| 9951 | ctxt->context->node = bak; |
| 9952 | ctxt->context->proximityPosition = pp; |
| 9953 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9954 | cur = |
| 9955 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9956 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9957 | if ((ctxt->value != NULL) |
| 9958 | && (ctxt->value->type == XPATH_NODESET) |
| 9959 | && (ctxt->value->nodesetval != NULL) |
| 9960 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9961 | } |
| 9962 | CHECK_TYPE0(XPATH_NODESET); |
| 9963 | arg2 = valuePop(ctxt); |
| 9964 | |
| 9965 | CHECK_TYPE0(XPATH_NODESET); |
| 9966 | arg1 = valuePop(ctxt); |
| 9967 | |
| 9968 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9969 | arg2->nodesetval); |
| 9970 | valuePush(ctxt, arg1); |
| 9971 | xmlXPathFreeObject(arg2); |
| 9972 | /* optimizer */ |
| 9973 | if (total > cur) |
| 9974 | xmlXPathCompSwap(op); |
| 9975 | return (total + cur); |
| 9976 | case XPATH_OP_ROOT: |
| 9977 | xmlXPathRoot(ctxt); |
| 9978 | return (0); |
| 9979 | case XPATH_OP_NODE: |
| 9980 | if (op->ch1 != -1) |
| 9981 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9982 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9983 | if (op->ch2 != -1) |
| 9984 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9985 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9986 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9987 | return (total); |
| 9988 | case XPATH_OP_RESET: |
| 9989 | if (op->ch1 != -1) |
| 9990 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9991 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9992 | if (op->ch2 != -1) |
| 9993 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9994 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9995 | ctxt->context->node = NULL; |
| 9996 | return (total); |
| 9997 | case XPATH_OP_COLLECT:{ |
| 9998 | if (op->ch1 == -1) |
| 9999 | return (0); |
| 10000 | |
| 10001 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10002 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10003 | |
| 10004 | /* |
| 10005 | * Optimization for [n] selection where n is a number |
| 10006 | */ |
| 10007 | if ((op->ch2 != -1) && |
| 10008 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10009 | (comp->steps[op->ch2].ch1 == -1) && |
| 10010 | (comp->steps[op->ch2].ch2 != -1) && |
| 10011 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10012 | XPATH_OP_VALUE)) { |
| 10013 | xmlXPathObjectPtr val; |
| 10014 | |
| 10015 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10016 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10017 | int indx = (int) val->floatval; |
| 10018 | |
| 10019 | if (val->floatval == (float) indx) { |
| 10020 | total += |
| 10021 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10022 | indx, NULL, |
| 10023 | last); |
| 10024 | return (total); |
| 10025 | } |
| 10026 | } |
| 10027 | } |
| 10028 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 10029 | return (total); |
| 10030 | } |
| 10031 | case XPATH_OP_VALUE: |
| 10032 | valuePush(ctxt, |
| 10033 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10034 | return (0); |
| 10035 | case XPATH_OP_SORT: |
| 10036 | if (op->ch1 != -1) |
| 10037 | total += |
| 10038 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 10039 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10040 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10041 | if ((ctxt->value != NULL) |
| 10042 | && (ctxt->value->type == XPATH_NODESET) |
| 10043 | && (ctxt->value->nodesetval != NULL)) |
| 10044 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10045 | return (total); |
| 10046 | default: |
| 10047 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10048 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10049 | } |
| 10050 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10051 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10052 | * xmlXPathCompOpEval: |
| 10053 | * @ctxt: the XPath parser context with the compiled expression |
| 10054 | * @op: an XPath compiled operation |
| 10055 | * |
| 10056 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10057 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10058 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10059 | static int |
| 10060 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 10061 | { |
| 10062 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10063 | int equal, ret; |
| 10064 | xmlXPathCompExprPtr comp; |
| 10065 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10066 | xmlNodePtr bak; |
| 10067 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10068 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10069 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10070 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10071 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10072 | comp = ctxt->comp; |
| 10073 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10074 | case XPATH_OP_END: |
| 10075 | return (0); |
| 10076 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10077 | bakd = ctxt->context->doc; |
| 10078 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10079 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10080 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10081 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10082 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10083 | xmlXPathBooleanFunction(ctxt, 1); |
| 10084 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10085 | return (total); |
| 10086 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10087 | ctxt->context->doc = bakd; |
| 10088 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10089 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10090 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10091 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10092 | if (ctxt->error) { |
| 10093 | xmlXPathFreeObject(arg2); |
| 10094 | return(0); |
| 10095 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10096 | xmlXPathBooleanFunction(ctxt, 1); |
| 10097 | arg1 = valuePop(ctxt); |
| 10098 | arg1->boolval &= arg2->boolval; |
| 10099 | valuePush(ctxt, arg1); |
| 10100 | xmlXPathFreeObject(arg2); |
| 10101 | return (total); |
| 10102 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10103 | bakd = ctxt->context->doc; |
| 10104 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10105 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10106 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10107 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10108 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10109 | xmlXPathBooleanFunction(ctxt, 1); |
| 10110 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10111 | return (total); |
| 10112 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10113 | ctxt->context->doc = bakd; |
| 10114 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10115 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10116 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10117 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10118 | if (ctxt->error) { |
| 10119 | xmlXPathFreeObject(arg2); |
| 10120 | return(0); |
| 10121 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10122 | xmlXPathBooleanFunction(ctxt, 1); |
| 10123 | arg1 = valuePop(ctxt); |
| 10124 | arg1->boolval |= arg2->boolval; |
| 10125 | valuePush(ctxt, arg1); |
| 10126 | xmlXPathFreeObject(arg2); |
| 10127 | return (total); |
| 10128 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10129 | bakd = ctxt->context->doc; |
| 10130 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10131 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10132 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10133 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10134 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10135 | ctxt->context->doc = bakd; |
| 10136 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10137 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10138 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10139 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10140 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10141 | if (op->value) |
| 10142 | equal = xmlXPathEqualValues(ctxt); |
| 10143 | else |
| 10144 | equal = xmlXPathNotEqualValues(ctxt); |
| 10145 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10146 | return (total); |
| 10147 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10148 | bakd = ctxt->context->doc; |
| 10149 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10150 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10151 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10152 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10153 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10154 | ctxt->context->doc = bakd; |
| 10155 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10156 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10157 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10158 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10159 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10160 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 10161 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 10162 | return (total); |
| 10163 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10164 | bakd = ctxt->context->doc; |
| 10165 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10166 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10167 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10168 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10169 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10170 | if (op->ch2 != -1) { |
| 10171 | ctxt->context->doc = bakd; |
| 10172 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10173 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10174 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10175 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10176 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10177 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10178 | if (op->value == 0) |
| 10179 | xmlXPathSubValues(ctxt); |
| 10180 | else if (op->value == 1) |
| 10181 | xmlXPathAddValues(ctxt); |
| 10182 | else if (op->value == 2) |
| 10183 | xmlXPathValueFlipSign(ctxt); |
| 10184 | else if (op->value == 3) { |
| 10185 | CAST_TO_NUMBER; |
| 10186 | CHECK_TYPE0(XPATH_NUMBER); |
| 10187 | } |
| 10188 | return (total); |
| 10189 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10190 | bakd = ctxt->context->doc; |
| 10191 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10192 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10193 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10194 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10195 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10196 | ctxt->context->doc = bakd; |
| 10197 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10198 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10199 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10200 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10201 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10202 | if (op->value == 0) |
| 10203 | xmlXPathMultValues(ctxt); |
| 10204 | else if (op->value == 1) |
| 10205 | xmlXPathDivValues(ctxt); |
| 10206 | else if (op->value == 2) |
| 10207 | xmlXPathModValues(ctxt); |
| 10208 | return (total); |
| 10209 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10210 | bakd = ctxt->context->doc; |
| 10211 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10212 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10213 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10214 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10215 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10216 | ctxt->context->doc = bakd; |
| 10217 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10218 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10219 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10220 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10221 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10222 | CHECK_TYPE0(XPATH_NODESET); |
| 10223 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10224 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10225 | CHECK_TYPE0(XPATH_NODESET); |
| 10226 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10227 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10228 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10229 | arg2->nodesetval); |
| 10230 | valuePush(ctxt, arg1); |
| 10231 | xmlXPathFreeObject(arg2); |
| 10232 | return (total); |
| 10233 | case XPATH_OP_ROOT: |
| 10234 | xmlXPathRoot(ctxt); |
| 10235 | return (total); |
| 10236 | case XPATH_OP_NODE: |
| 10237 | if (op->ch1 != -1) |
| 10238 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10239 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10240 | if (op->ch2 != -1) |
| 10241 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10242 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10243 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10244 | return (total); |
| 10245 | case XPATH_OP_RESET: |
| 10246 | if (op->ch1 != -1) |
| 10247 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10248 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10249 | if (op->ch2 != -1) |
| 10250 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10251 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10252 | ctxt->context->node = NULL; |
| 10253 | return (total); |
| 10254 | case XPATH_OP_COLLECT:{ |
| 10255 | if (op->ch1 == -1) |
| 10256 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10257 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10258 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10259 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10260 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10261 | /* |
| 10262 | * Optimization for [n] selection where n is a number |
| 10263 | */ |
| 10264 | if ((op->ch2 != -1) && |
| 10265 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10266 | (comp->steps[op->ch2].ch1 == -1) && |
| 10267 | (comp->steps[op->ch2].ch2 != -1) && |
| 10268 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10269 | XPATH_OP_VALUE)) { |
| 10270 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 10271 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10272 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10273 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10274 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10275 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10276 | if (val->floatval == (float) indx) { |
| 10277 | total += |
| 10278 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10279 | indx, NULL, |
| 10280 | NULL); |
| 10281 | return (total); |
| 10282 | } |
| 10283 | } |
| 10284 | } |
| 10285 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 10286 | return (total); |
| 10287 | } |
| 10288 | case XPATH_OP_VALUE: |
| 10289 | valuePush(ctxt, |
| 10290 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10291 | return (total); |
| 10292 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10293 | xmlXPathObjectPtr val; |
| 10294 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10295 | if (op->ch1 != -1) |
| 10296 | total += |
| 10297 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10298 | if (op->value5 == NULL) { |
| 10299 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 10300 | if (val == NULL) { |
| 10301 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10302 | return(0); |
| 10303 | } |
| 10304 | valuePush(ctxt, val); |
| 10305 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10306 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10307 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10308 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10309 | if (URI == NULL) { |
| 10310 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10311 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10312 | op->value4, op->value5); |
| 10313 | return (total); |
| 10314 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10315 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 10316 | op->value4, URI); |
| 10317 | if (val == NULL) { |
| 10318 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10319 | return(0); |
| 10320 | } |
| 10321 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10322 | } |
| 10323 | return (total); |
| 10324 | } |
| 10325 | case XPATH_OP_FUNCTION:{ |
| 10326 | xmlXPathFunction func; |
| 10327 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10328 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10329 | |
| 10330 | if (op->ch1 != -1) |
| 10331 | total += |
| 10332 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10333 | if (ctxt->valueNr < op->value) { |
| 10334 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10335 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10336 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10337 | return (total); |
| 10338 | } |
| 10339 | for (i = 0; i < op->value; i++) |
| 10340 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 10341 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10342 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10343 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10344 | return (total); |
| 10345 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10346 | if (op->cache != NULL) |
| 10347 | func = (xmlXPathFunction) op->cache; |
| 10348 | else { |
| 10349 | const xmlChar *URI = NULL; |
| 10350 | |
| 10351 | if (op->value5 == NULL) |
| 10352 | func = |
| 10353 | xmlXPathFunctionLookup(ctxt->context, |
| 10354 | op->value4); |
| 10355 | else { |
| 10356 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10357 | if (URI == NULL) { |
| 10358 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10359 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10360 | op->value4, op->value5); |
| 10361 | return (total); |
| 10362 | } |
| 10363 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 10364 | op->value4, URI); |
| 10365 | } |
| 10366 | if (func == NULL) { |
| 10367 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10368 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10369 | op->value4); |
| 10370 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10371 | } |
| 10372 | op->cache = (void *) func; |
| 10373 | op->cacheURI = (void *) URI; |
| 10374 | } |
| 10375 | oldFunc = ctxt->context->function; |
| 10376 | oldFuncURI = ctxt->context->functionURI; |
| 10377 | ctxt->context->function = op->value4; |
| 10378 | ctxt->context->functionURI = op->cacheURI; |
| 10379 | func(ctxt, op->value); |
| 10380 | ctxt->context->function = oldFunc; |
| 10381 | ctxt->context->functionURI = oldFuncURI; |
| 10382 | return (total); |
| 10383 | } |
| 10384 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10385 | bakd = ctxt->context->doc; |
| 10386 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10387 | if (op->ch1 != -1) |
| 10388 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10389 | ctxt->context->doc = bakd; |
| 10390 | ctxt->context->node = bak; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10391 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10392 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10393 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10394 | ctxt->context->doc = bakd; |
| 10395 | ctxt->context->node = bak; |
| 10396 | CHECK_ERROR0; |
| 10397 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10398 | return (total); |
| 10399 | case XPATH_OP_PREDICATE: |
| 10400 | case XPATH_OP_FILTER:{ |
| 10401 | xmlXPathObjectPtr res; |
| 10402 | xmlXPathObjectPtr obj, tmp; |
| 10403 | xmlNodeSetPtr newset = NULL; |
| 10404 | xmlNodeSetPtr oldset; |
| 10405 | xmlNodePtr oldnode; |
| 10406 | int i; |
| 10407 | |
| 10408 | /* |
| 10409 | * Optimization for ()[1] selection i.e. the first elem |
| 10410 | */ |
| 10411 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10412 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10413 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 10414 | xmlXPathObjectPtr val; |
| 10415 | |
| 10416 | val = comp->steps[op->ch2].value4; |
| 10417 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 10418 | (val->floatval == 1.0)) { |
| 10419 | xmlNodePtr first = NULL; |
| 10420 | |
| 10421 | total += |
| 10422 | xmlXPathCompOpEvalFirst(ctxt, |
| 10423 | &comp->steps[op->ch1], |
| 10424 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10425 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10426 | /* |
| 10427 | * The nodeset should be in document order, |
| 10428 | * Keep only the first value |
| 10429 | */ |
| 10430 | if ((ctxt->value != NULL) && |
| 10431 | (ctxt->value->type == XPATH_NODESET) && |
| 10432 | (ctxt->value->nodesetval != NULL) && |
| 10433 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 10434 | ctxt->value->nodesetval->nodeNr = 1; |
| 10435 | return (total); |
| 10436 | } |
| 10437 | } |
| 10438 | /* |
| 10439 | * Optimization for ()[last()] selection i.e. the last elem |
| 10440 | */ |
| 10441 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10442 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10443 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10444 | int f = comp->steps[op->ch2].ch1; |
| 10445 | |
| 10446 | if ((f != -1) && |
| 10447 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10448 | (comp->steps[f].value5 == NULL) && |
| 10449 | (comp->steps[f].value == 0) && |
| 10450 | (comp->steps[f].value4 != NULL) && |
| 10451 | (xmlStrEqual |
| 10452 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10453 | xmlNodePtr last = NULL; |
| 10454 | |
| 10455 | total += |
| 10456 | xmlXPathCompOpEvalLast(ctxt, |
| 10457 | &comp->steps[op->ch1], |
| 10458 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10459 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10460 | /* |
| 10461 | * The nodeset should be in document order, |
| 10462 | * Keep only the last value |
| 10463 | */ |
| 10464 | if ((ctxt->value != NULL) && |
| 10465 | (ctxt->value->type == XPATH_NODESET) && |
| 10466 | (ctxt->value->nodesetval != NULL) && |
| 10467 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10468 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10469 | ctxt->value->nodesetval->nodeTab[0] = |
| 10470 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10471 | value-> |
| 10472 | nodesetval-> |
| 10473 | nodeNr - |
| 10474 | 1]; |
| 10475 | ctxt->value->nodesetval->nodeNr = 1; |
| 10476 | } |
| 10477 | return (total); |
| 10478 | } |
| 10479 | } |
| 10480 | |
| 10481 | if (op->ch1 != -1) |
| 10482 | total += |
| 10483 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10484 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10485 | if (op->ch2 == -1) |
| 10486 | return (total); |
| 10487 | if (ctxt->value == NULL) |
| 10488 | return (total); |
| 10489 | |
| 10490 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10491 | |
| 10492 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10493 | /* |
| 10494 | * Hum are we filtering the result of an XPointer expression |
| 10495 | */ |
| 10496 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10497 | xmlLocationSetPtr newlocset = NULL; |
| 10498 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10499 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10500 | /* |
| 10501 | * Extract the old locset, and then evaluate the result of the |
| 10502 | * expression for all the element in the locset. use it to grow |
| 10503 | * up a new locset. |
| 10504 | */ |
| 10505 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10506 | obj = valuePop(ctxt); |
| 10507 | oldlocset = obj->user; |
| 10508 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10509 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10510 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10511 | ctxt->context->contextSize = 0; |
| 10512 | ctxt->context->proximityPosition = 0; |
| 10513 | if (op->ch2 != -1) |
| 10514 | total += |
| 10515 | xmlXPathCompOpEval(ctxt, |
| 10516 | &comp->steps[op->ch2]); |
| 10517 | res = valuePop(ctxt); |
| 10518 | if (res != NULL) |
| 10519 | xmlXPathFreeObject(res); |
| 10520 | valuePush(ctxt, obj); |
| 10521 | CHECK_ERROR0; |
| 10522 | return (total); |
| 10523 | } |
| 10524 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10525 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10526 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10527 | /* |
| 10528 | * Run the evaluation with a node list made of a |
| 10529 | * single item in the nodelocset. |
| 10530 | */ |
| 10531 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10532 | ctxt->context->contextSize = oldlocset->locNr; |
| 10533 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10534 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10535 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10536 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10537 | if (op->ch2 != -1) |
| 10538 | total += |
| 10539 | xmlXPathCompOpEval(ctxt, |
| 10540 | &comp->steps[op->ch2]); |
| 10541 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10542 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10543 | /* |
| 10544 | * The result of the evaluation need to be tested to |
| 10545 | * decided whether the filter succeeded or not |
| 10546 | */ |
| 10547 | res = valuePop(ctxt); |
| 10548 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10549 | xmlXPtrLocationSetAdd(newlocset, |
| 10550 | xmlXPathObjectCopy |
| 10551 | (oldlocset->locTab[i])); |
| 10552 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10553 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10554 | /* |
| 10555 | * Cleanup |
| 10556 | */ |
| 10557 | if (res != NULL) |
| 10558 | xmlXPathFreeObject(res); |
| 10559 | if (ctxt->value == tmp) { |
| 10560 | res = valuePop(ctxt); |
| 10561 | xmlXPathFreeObject(res); |
| 10562 | } |
| 10563 | |
| 10564 | ctxt->context->node = NULL; |
| 10565 | } |
| 10566 | |
| 10567 | /* |
| 10568 | * The result is used as the new evaluation locset. |
| 10569 | */ |
| 10570 | xmlXPathFreeObject(obj); |
| 10571 | ctxt->context->node = NULL; |
| 10572 | ctxt->context->contextSize = -1; |
| 10573 | ctxt->context->proximityPosition = -1; |
| 10574 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10575 | ctxt->context->node = oldnode; |
| 10576 | return (total); |
| 10577 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10578 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10579 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10580 | /* |
| 10581 | * Extract the old set, and then evaluate the result of the |
| 10582 | * expression for all the element in the set. use it to grow |
| 10583 | * up a new set. |
| 10584 | */ |
| 10585 | CHECK_TYPE0(XPATH_NODESET); |
| 10586 | obj = valuePop(ctxt); |
| 10587 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10588 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10589 | oldnode = ctxt->context->node; |
| 10590 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10591 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10592 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10593 | ctxt->context->contextSize = 0; |
| 10594 | ctxt->context->proximityPosition = 0; |
| 10595 | if (op->ch2 != -1) |
| 10596 | total += |
| 10597 | xmlXPathCompOpEval(ctxt, |
| 10598 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10599 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10600 | res = valuePop(ctxt); |
| 10601 | if (res != NULL) |
| 10602 | xmlXPathFreeObject(res); |
| 10603 | valuePush(ctxt, obj); |
| 10604 | ctxt->context->node = oldnode; |
| 10605 | CHECK_ERROR0; |
| 10606 | } else { |
| 10607 | /* |
| 10608 | * Initialize the new set. |
| 10609 | */ |
| 10610 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10611 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10612 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10613 | /* |
| 10614 | * Run the evaluation with a node list made of |
| 10615 | * a single item in the nodeset. |
| 10616 | */ |
| 10617 | ctxt->context->node = oldset->nodeTab[i]; |
| 10618 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10619 | valuePush(ctxt, tmp); |
| 10620 | ctxt->context->contextSize = oldset->nodeNr; |
| 10621 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10622 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10623 | if (op->ch2 != -1) |
| 10624 | total += |
| 10625 | xmlXPathCompOpEval(ctxt, |
| 10626 | &comp->steps[op->ch2]); |
| 10627 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10628 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10629 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10630 | * The result of the evaluation needs to be tested to |
| 10631 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10632 | */ |
| 10633 | res = valuePop(ctxt); |
| 10634 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10635 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10636 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10637 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10638 | /* |
| 10639 | * Cleanup |
| 10640 | */ |
| 10641 | if (res != NULL) |
| 10642 | xmlXPathFreeObject(res); |
| 10643 | if (ctxt->value == tmp) { |
| 10644 | res = valuePop(ctxt); |
| 10645 | xmlXPathFreeObject(res); |
| 10646 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10647 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10648 | ctxt->context->node = NULL; |
| 10649 | } |
| 10650 | |
| 10651 | /* |
| 10652 | * The result is used as the new evaluation set. |
| 10653 | */ |
| 10654 | xmlXPathFreeObject(obj); |
| 10655 | ctxt->context->node = NULL; |
| 10656 | ctxt->context->contextSize = -1; |
| 10657 | ctxt->context->proximityPosition = -1; |
| 10658 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10659 | } |
| 10660 | ctxt->context->node = oldnode; |
| 10661 | return (total); |
| 10662 | } |
| 10663 | case XPATH_OP_SORT: |
| 10664 | if (op->ch1 != -1) |
| 10665 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10666 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10667 | if ((ctxt->value != NULL) && |
| 10668 | (ctxt->value->type == XPATH_NODESET) && |
| 10669 | (ctxt->value->nodesetval != NULL)) |
| 10670 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10671 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10672 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10673 | case XPATH_OP_RANGETO:{ |
| 10674 | xmlXPathObjectPtr range; |
| 10675 | xmlXPathObjectPtr res, obj; |
| 10676 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10677 | xmlLocationSetPtr newlocset = NULL; |
| 10678 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10679 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10680 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10681 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10682 | if (op->ch1 != -1) |
| 10683 | total += |
| 10684 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10685 | if (op->ch2 == -1) |
| 10686 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10687 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10688 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10689 | /* |
| 10690 | * Extract the old locset, and then evaluate the result of the |
| 10691 | * expression for all the element in the locset. use it to grow |
| 10692 | * up a new locset. |
| 10693 | */ |
| 10694 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10695 | obj = valuePop(ctxt); |
| 10696 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10697 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10698 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10699 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10700 | ctxt->context->contextSize = 0; |
| 10701 | ctxt->context->proximityPosition = 0; |
| 10702 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 10703 | res = valuePop(ctxt); |
| 10704 | if (res != NULL) |
| 10705 | xmlXPathFreeObject(res); |
| 10706 | valuePush(ctxt, obj); |
| 10707 | CHECK_ERROR0; |
| 10708 | return (total); |
| 10709 | } |
| 10710 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10711 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10712 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10713 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10714 | * Run the evaluation with a node list made of a |
| 10715 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10716 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10717 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10718 | ctxt->context->contextSize = oldlocset->locNr; |
| 10719 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10720 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10721 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10722 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10723 | if (op->ch2 != -1) |
| 10724 | total += |
| 10725 | xmlXPathCompOpEval(ctxt, |
| 10726 | &comp->steps[op->ch2]); |
| 10727 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10728 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10729 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10730 | if (res->type == XPATH_LOCATIONSET) { |
| 10731 | xmlLocationSetPtr rloc = |
| 10732 | (xmlLocationSetPtr)res->user; |
| 10733 | for (j=0; j<rloc->locNr; j++) { |
| 10734 | range = xmlXPtrNewRange( |
| 10735 | oldlocset->locTab[i]->user, |
| 10736 | oldlocset->locTab[i]->index, |
| 10737 | rloc->locTab[j]->user2, |
| 10738 | rloc->locTab[j]->index2); |
| 10739 | if (range != NULL) { |
| 10740 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10741 | } |
| 10742 | } |
| 10743 | } else { |
| 10744 | range = xmlXPtrNewRangeNodeObject( |
| 10745 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 10746 | if (range != NULL) { |
| 10747 | xmlXPtrLocationSetAdd(newlocset,range); |
| 10748 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10749 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10750 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10751 | /* |
| 10752 | * Cleanup |
| 10753 | */ |
| 10754 | if (res != NULL) |
| 10755 | xmlXPathFreeObject(res); |
| 10756 | if (ctxt->value == tmp) { |
| 10757 | res = valuePop(ctxt); |
| 10758 | xmlXPathFreeObject(res); |
| 10759 | } |
| 10760 | |
| 10761 | ctxt->context->node = NULL; |
| 10762 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10763 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10764 | CHECK_TYPE0(XPATH_NODESET); |
| 10765 | obj = valuePop(ctxt); |
| 10766 | oldset = obj->nodesetval; |
| 10767 | ctxt->context->node = NULL; |
| 10768 | |
| 10769 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10770 | |
| 10771 | if (oldset != NULL) { |
| 10772 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10773 | /* |
| 10774 | * Run the evaluation with a node list made of a single item |
| 10775 | * in the nodeset. |
| 10776 | */ |
| 10777 | ctxt->context->node = oldset->nodeTab[i]; |
| 10778 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10779 | valuePush(ctxt, tmp); |
| 10780 | |
| 10781 | if (op->ch2 != -1) |
| 10782 | total += |
| 10783 | xmlXPathCompOpEval(ctxt, |
| 10784 | &comp->steps[op->ch2]); |
| 10785 | CHECK_ERROR0; |
| 10786 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10787 | res = valuePop(ctxt); |
| 10788 | range = |
| 10789 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10790 | res); |
| 10791 | if (range != NULL) { |
| 10792 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10793 | } |
| 10794 | |
| 10795 | /* |
| 10796 | * Cleanup |
| 10797 | */ |
| 10798 | if (res != NULL) |
| 10799 | xmlXPathFreeObject(res); |
| 10800 | if (ctxt->value == tmp) { |
| 10801 | res = valuePop(ctxt); |
| 10802 | xmlXPathFreeObject(res); |
| 10803 | } |
| 10804 | |
| 10805 | ctxt->context->node = NULL; |
| 10806 | } |
| 10807 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10808 | } |
| 10809 | |
| 10810 | /* |
| 10811 | * The result is used as the new evaluation set. |
| 10812 | */ |
| 10813 | xmlXPathFreeObject(obj); |
| 10814 | ctxt->context->node = NULL; |
| 10815 | ctxt->context->contextSize = -1; |
| 10816 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10817 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10818 | return (total); |
| 10819 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10820 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10821 | } |
| 10822 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10823 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10824 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10825 | } |
| 10826 | |
| 10827 | /** |
| 10828 | * xmlXPathRunEval: |
| 10829 | * @ctxt: the XPath parser context with the compiled expression |
| 10830 | * |
| 10831 | * Evaluate the Precompiled XPath expression in the given context. |
| 10832 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10833 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10834 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 10835 | xmlXPathCompExprPtr comp; |
| 10836 | |
| 10837 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 10838 | return; |
| 10839 | |
| 10840 | if (ctxt->valueTab == NULL) { |
| 10841 | /* Allocate the value stack */ |
| 10842 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 10843 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 10844 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 10845 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10846 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10847 | } |
| 10848 | ctxt->valueNr = 0; |
| 10849 | ctxt->valueMax = 10; |
| 10850 | ctxt->value = NULL; |
| 10851 | } |
| 10852 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 10853 | if(comp->last < 0) { |
| 10854 | xmlGenericError(xmlGenericErrorContext, |
| 10855 | "xmlXPathRunEval: last is less than zero\n"); |
| 10856 | return; |
| 10857 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10858 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 10859 | } |
| 10860 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10861 | /************************************************************************ |
| 10862 | * * |
| 10863 | * Public interfaces * |
| 10864 | * * |
| 10865 | ************************************************************************/ |
| 10866 | |
| 10867 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10868 | * xmlXPathEvalPredicate: |
| 10869 | * @ctxt: the XPath context |
| 10870 | * @res: the Predicate Expression evaluation result |
| 10871 | * |
| 10872 | * Evaluate a predicate result for the current node. |
| 10873 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10874 | * the result to a boolean. If the result is a number, the result will |
| 10875 | * be converted to true if the number is equal to the position of the |
| 10876 | * context node in the context node list (as returned by the position |
| 10877 | * function) and will be converted to false otherwise; if the result |
| 10878 | * is not a number, then the result will be converted as if by a call |
| 10879 | * to the boolean function. |
| 10880 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10881 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10882 | */ |
| 10883 | int |
| 10884 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
| 10885 | if (res == NULL) return(0); |
| 10886 | switch (res->type) { |
| 10887 | case XPATH_BOOLEAN: |
| 10888 | return(res->boolval); |
| 10889 | case XPATH_NUMBER: |
| 10890 | return(res->floatval == ctxt->proximityPosition); |
| 10891 | case XPATH_NODESET: |
| 10892 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10893 | if (res->nodesetval == NULL) |
| 10894 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10895 | return(res->nodesetval->nodeNr != 0); |
| 10896 | case XPATH_STRING: |
| 10897 | return((res->stringval != NULL) && |
| 10898 | (xmlStrlen(res->stringval) != 0)); |
| 10899 | default: |
| 10900 | STRANGE |
| 10901 | } |
| 10902 | return(0); |
| 10903 | } |
| 10904 | |
| 10905 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10906 | * xmlXPathEvaluatePredicateResult: |
| 10907 | * @ctxt: the XPath Parser context |
| 10908 | * @res: the Predicate Expression evaluation result |
| 10909 | * |
| 10910 | * Evaluate a predicate result for the current node. |
| 10911 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10912 | * the result to a boolean. If the result is a number, the result will |
| 10913 | * be converted to true if the number is equal to the position of the |
| 10914 | * context node in the context node list (as returned by the position |
| 10915 | * function) and will be converted to false otherwise; if the result |
| 10916 | * is not a number, then the result will be converted as if by a call |
| 10917 | * to the boolean function. |
| 10918 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10919 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10920 | */ |
| 10921 | int |
| 10922 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 10923 | xmlXPathObjectPtr res) { |
| 10924 | if (res == NULL) return(0); |
| 10925 | switch (res->type) { |
| 10926 | case XPATH_BOOLEAN: |
| 10927 | return(res->boolval); |
| 10928 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 10929 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 10930 | return((res->floatval == ctxt->context->proximityPosition) && |
| 10931 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 10932 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10933 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 10934 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10935 | case XPATH_NODESET: |
| 10936 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 10937 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10938 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10939 | return(res->nodesetval->nodeNr != 0); |
| 10940 | case XPATH_STRING: |
| 10941 | return((res->stringval != NULL) && |
| 10942 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10943 | #ifdef LIBXML_XPTR_ENABLED |
| 10944 | case XPATH_LOCATIONSET:{ |
| 10945 | xmlLocationSetPtr ptr = res->user; |
| 10946 | if (ptr == NULL) |
| 10947 | return(0); |
| 10948 | return (ptr->locNr != 0); |
| 10949 | } |
| 10950 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10951 | default: |
| 10952 | STRANGE |
| 10953 | } |
| 10954 | return(0); |
| 10955 | } |
| 10956 | |
| 10957 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 10958 | * xmlXPathCtxtCompile: |
| 10959 | * @ctxt: an XPath context |
| 10960 | * @str: the XPath expression |
| 10961 | * |
| 10962 | * Compile an XPath expression |
| 10963 | * |
| 10964 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 10965 | * the caller has to free the object. |
| 10966 | */ |
| 10967 | xmlXPathCompExprPtr |
| 10968 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 10969 | xmlXPathParserContextPtr pctxt; |
| 10970 | xmlXPathCompExprPtr comp; |
| 10971 | |
| 10972 | xmlXPathInit(); |
| 10973 | |
| 10974 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 10975 | xmlXPathCompileExpr(pctxt); |
| 10976 | |
| 10977 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 10978 | { |
| 10979 | xmlXPathFreeParserContext(pctxt); |
| 10980 | return (0); |
| 10981 | } |
| 10982 | |
| 10983 | if (*pctxt->cur != 0) { |
| 10984 | /* |
| 10985 | * aleksey: in some cases this line prints *second* error message |
| 10986 | * (see bug #78858) and probably this should be fixed. |
| 10987 | * However, we are not sure that all error messages are printed |
| 10988 | * out in other places. It's not critical so we leave it as-is for now |
| 10989 | */ |
| 10990 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 10991 | comp = NULL; |
| 10992 | } else { |
| 10993 | comp = pctxt->comp; |
| 10994 | pctxt->comp = NULL; |
| 10995 | } |
| 10996 | xmlXPathFreeParserContext(pctxt); |
| 10997 | if (comp != NULL) { |
| 10998 | comp->expr = xmlStrdup(str); |
| 10999 | #ifdef DEBUG_EVAL_COUNTS |
| 11000 | comp->string = xmlStrdup(str); |
| 11001 | comp->nb = 0; |
| 11002 | #endif |
| 11003 | } |
| 11004 | return(comp); |
| 11005 | } |
| 11006 | |
| 11007 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11008 | * xmlXPathCompile: |
| 11009 | * @str: the XPath expression |
| 11010 | * |
| 11011 | * Compile an XPath expression |
| 11012 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 11013 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11014 | * the caller has to free the object. |
| 11015 | */ |
| 11016 | xmlXPathCompExprPtr |
| 11017 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11018 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11019 | } |
| 11020 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11021 | /** |
| 11022 | * xmlXPathCompiledEval: |
| 11023 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11024 | * @ctx: the XPath context |
| 11025 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11026 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11027 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11028 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11029 | * the caller has to free the object. |
| 11030 | */ |
| 11031 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11032 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11033 | xmlXPathParserContextPtr ctxt; |
| 11034 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11035 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11036 | #ifndef LIBXML_THREAD_ENABLED |
| 11037 | static int reentance = 0; |
| 11038 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11039 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11040 | if ((comp == NULL) || (ctx == NULL)) |
| 11041 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11042 | xmlXPathInit(); |
| 11043 | |
| 11044 | CHECK_CONTEXT(ctx) |
| 11045 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11046 | #ifndef LIBXML_THREAD_ENABLED |
| 11047 | reentance++; |
| 11048 | if (reentance > 1) |
| 11049 | xmlXPathDisableOptimizer = 1; |
| 11050 | #endif |
| 11051 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11052 | #ifdef DEBUG_EVAL_COUNTS |
| 11053 | comp->nb++; |
| 11054 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 11055 | fprintf(stderr, "100 x %s\n", comp->string); |
| 11056 | comp->nb = 0; |
| 11057 | } |
| 11058 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11059 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 11060 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11061 | |
| 11062 | if (ctxt->value == NULL) { |
| 11063 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11064 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11065 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11066 | } else { |
| 11067 | res = valuePop(ctxt); |
| 11068 | } |
| 11069 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11070 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11071 | do { |
| 11072 | tmp = valuePop(ctxt); |
| 11073 | if (tmp != NULL) { |
| 11074 | if (tmp != init) |
| 11075 | stack++; |
| 11076 | xmlXPathFreeObject(tmp); |
| 11077 | } |
| 11078 | } while (tmp != NULL); |
| 11079 | if ((stack != 0) && (res != NULL)) { |
| 11080 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11081 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11082 | stack); |
| 11083 | } |
| 11084 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11085 | xmlXPathFreeObject(res); |
| 11086 | res = NULL; |
| 11087 | } |
| 11088 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11089 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11090 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11091 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11092 | #ifndef LIBXML_THREAD_ENABLED |
| 11093 | reentance--; |
| 11094 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11095 | return(res); |
| 11096 | } |
| 11097 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11098 | /** |
| 11099 | * xmlXPathEvalExpr: |
| 11100 | * @ctxt: the XPath Parser context |
| 11101 | * |
| 11102 | * Parse and evaluate an XPath expression in the given context, |
| 11103 | * then push the result on the context stack |
| 11104 | */ |
| 11105 | void |
| 11106 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
| 11107 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 11108 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11109 | xmlXPathRunEval(ctxt); |
| 11110 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11111 | |
| 11112 | /** |
| 11113 | * xmlXPathEval: |
| 11114 | * @str: the XPath expression |
| 11115 | * @ctx: the XPath context |
| 11116 | * |
| 11117 | * Evaluate the XPath Location Path in the given context. |
| 11118 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11119 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11120 | * the caller has to free the object. |
| 11121 | */ |
| 11122 | xmlXPathObjectPtr |
| 11123 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 11124 | xmlXPathParserContextPtr ctxt; |
| 11125 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11126 | int stack = 0; |
| 11127 | |
| 11128 | xmlXPathInit(); |
| 11129 | |
| 11130 | CHECK_CONTEXT(ctx) |
| 11131 | |
| 11132 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 11133 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11134 | |
| 11135 | if (ctxt->value == NULL) { |
| 11136 | xmlGenericError(xmlGenericErrorContext, |
| 11137 | "xmlXPathEval: evaluation failed\n"); |
| 11138 | res = NULL; |
| 11139 | } else if (*ctxt->cur != 0) { |
| 11140 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11141 | res = NULL; |
| 11142 | } else { |
| 11143 | res = valuePop(ctxt); |
| 11144 | } |
| 11145 | |
| 11146 | do { |
| 11147 | tmp = valuePop(ctxt); |
| 11148 | if (tmp != NULL) { |
| 11149 | if (tmp != init) |
| 11150 | stack++; |
| 11151 | xmlXPathFreeObject(tmp); |
| 11152 | } |
| 11153 | } while (tmp != NULL); |
| 11154 | if ((stack != 0) && (res != NULL)) { |
| 11155 | xmlGenericError(xmlGenericErrorContext, |
| 11156 | "xmlXPathEval: %d object left on the stack\n", |
| 11157 | stack); |
| 11158 | } |
| 11159 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11160 | xmlXPathFreeObject(res); |
| 11161 | res = NULL; |
| 11162 | } |
| 11163 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11164 | xmlXPathFreeParserContext(ctxt); |
| 11165 | return(res); |
| 11166 | } |
| 11167 | |
| 11168 | /** |
| 11169 | * xmlXPathEvalExpression: |
| 11170 | * @str: the XPath expression |
| 11171 | * @ctxt: the XPath context |
| 11172 | * |
| 11173 | * Evaluate the XPath expression in the given context. |
| 11174 | * |
| 11175 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 11176 | * the caller has to free the object. |
| 11177 | */ |
| 11178 | xmlXPathObjectPtr |
| 11179 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 11180 | xmlXPathParserContextPtr pctxt; |
| 11181 | xmlXPathObjectPtr res, tmp; |
| 11182 | int stack = 0; |
| 11183 | |
| 11184 | xmlXPathInit(); |
| 11185 | |
| 11186 | CHECK_CONTEXT(ctxt) |
| 11187 | |
| 11188 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11189 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11190 | |
| 11191 | if (*pctxt->cur != 0) { |
| 11192 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11193 | res = NULL; |
| 11194 | } else { |
| 11195 | res = valuePop(pctxt); |
| 11196 | } |
| 11197 | do { |
| 11198 | tmp = valuePop(pctxt); |
| 11199 | if (tmp != NULL) { |
| 11200 | xmlXPathFreeObject(tmp); |
| 11201 | stack++; |
| 11202 | } |
| 11203 | } while (tmp != NULL); |
| 11204 | if ((stack != 0) && (res != NULL)) { |
| 11205 | xmlGenericError(xmlGenericErrorContext, |
| 11206 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 11207 | stack); |
| 11208 | } |
| 11209 | xmlXPathFreeParserContext(pctxt); |
| 11210 | return(res); |
| 11211 | } |
| 11212 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11213 | /************************************************************************ |
| 11214 | * * |
| 11215 | * Extra functions not pertaining to the XPath spec * |
| 11216 | * * |
| 11217 | ************************************************************************/ |
| 11218 | /** |
| 11219 | * xmlXPathEscapeUriFunction: |
| 11220 | * @ctxt: the XPath Parser context |
| 11221 | * @nargs: the number of arguments |
| 11222 | * |
| 11223 | * Implement the escape-uri() XPath function |
| 11224 | * string escape-uri(string $str, bool $escape-reserved) |
| 11225 | * |
| 11226 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 11227 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 11228 | * or part of a URI. The effect of the function is to replace any special |
| 11229 | * character in the string by an escape sequence of the form %xx%yy..., |
| 11230 | * where xxyy... is the hexadecimal representation of the octets used to |
| 11231 | * represent the character in UTF-8. |
| 11232 | * |
| 11233 | * The set of characters that are escaped depends on the setting of the |
| 11234 | * boolean argument $escape-reserved. |
| 11235 | * |
| 11236 | * If $escape-reserved is true, all characters are escaped other than lower |
| 11237 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 11238 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 11239 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 11240 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 11241 | * A-F). |
| 11242 | * |
| 11243 | * If $escape-reserved is false, the behavior differs in that characters |
| 11244 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 11245 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 11246 | * |
| 11247 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 11248 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 11249 | * compared using string comparison functions, this function must always use |
| 11250 | * the upper-case letters A-F. |
| 11251 | * |
| 11252 | * Generally, $escape-reserved should be set to true when escaping a string |
| 11253 | * that is to form a single part of a URI, and to false when escaping an |
| 11254 | * entire URI or URI reference. |
| 11255 | * |
| 11256 | * In the case of non-ascii characters, the string is encoded according to |
| 11257 | * utf-8 and then converted according to RFC 2396. |
| 11258 | * |
| 11259 | * Examples |
| 11260 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 11261 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 11262 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 11263 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 11264 | * |
| 11265 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 11266 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11267 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 11268 | xmlXPathObjectPtr str; |
| 11269 | int escape_reserved; |
| 11270 | xmlBufferPtr target; |
| 11271 | xmlChar *cptr; |
| 11272 | xmlChar escape[4]; |
| 11273 | |
| 11274 | CHECK_ARITY(2); |
| 11275 | |
| 11276 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 11277 | |
| 11278 | CAST_TO_STRING; |
| 11279 | str = valuePop(ctxt); |
| 11280 | |
| 11281 | target = xmlBufferCreate(); |
| 11282 | |
| 11283 | escape[0] = '%'; |
| 11284 | escape[3] = 0; |
| 11285 | |
| 11286 | if (target) { |
| 11287 | for (cptr = str->stringval; *cptr; cptr++) { |
| 11288 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 11289 | (*cptr >= 'a' && *cptr <= 'z') || |
| 11290 | (*cptr >= '0' && *cptr <= '9') || |
| 11291 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 11292 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 11293 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 11294 | (*cptr == '%' && |
| 11295 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 11296 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 11297 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 11298 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 11299 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 11300 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 11301 | (!escape_reserved && |
| 11302 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 11303 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 11304 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 11305 | *cptr == ','))) { |
| 11306 | xmlBufferAdd(target, cptr, 1); |
| 11307 | } else { |
| 11308 | if ((*cptr >> 4) < 10) |
| 11309 | escape[1] = '0' + (*cptr >> 4); |
| 11310 | else |
| 11311 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 11312 | if ((*cptr & 0xF) < 10) |
| 11313 | escape[2] = '0' + (*cptr & 0xF); |
| 11314 | else |
| 11315 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 11316 | |
| 11317 | xmlBufferAdd(target, &escape[0], 3); |
| 11318 | } |
| 11319 | } |
| 11320 | } |
| 11321 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 11322 | xmlBufferFree(target); |
| 11323 | xmlXPathFreeObject(str); |
| 11324 | } |
| 11325 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11326 | /** |
| 11327 | * xmlXPathRegisterAllFunctions: |
| 11328 | * @ctxt: the XPath context |
| 11329 | * |
| 11330 | * Registers all default XPath functions in this context |
| 11331 | */ |
| 11332 | void |
| 11333 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 11334 | { |
| 11335 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 11336 | xmlXPathBooleanFunction); |
| 11337 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 11338 | xmlXPathCeilingFunction); |
| 11339 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 11340 | xmlXPathCountFunction); |
| 11341 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 11342 | xmlXPathConcatFunction); |
| 11343 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 11344 | xmlXPathContainsFunction); |
| 11345 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 11346 | xmlXPathIdFunction); |
| 11347 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 11348 | xmlXPathFalseFunction); |
| 11349 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 11350 | xmlXPathFloorFunction); |
| 11351 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 11352 | xmlXPathLastFunction); |
| 11353 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 11354 | xmlXPathLangFunction); |
| 11355 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 11356 | xmlXPathLocalNameFunction); |
| 11357 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 11358 | xmlXPathNotFunction); |
| 11359 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 11360 | xmlXPathNameFunction); |
| 11361 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 11362 | xmlXPathNamespaceURIFunction); |
| 11363 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 11364 | xmlXPathNormalizeFunction); |
| 11365 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 11366 | xmlXPathNumberFunction); |
| 11367 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 11368 | xmlXPathPositionFunction); |
| 11369 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 11370 | xmlXPathRoundFunction); |
| 11371 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 11372 | xmlXPathStringFunction); |
| 11373 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 11374 | xmlXPathStringLengthFunction); |
| 11375 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 11376 | xmlXPathStartsWithFunction); |
| 11377 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 11378 | xmlXPathSubstringFunction); |
| 11379 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 11380 | xmlXPathSubstringBeforeFunction); |
| 11381 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 11382 | xmlXPathSubstringAfterFunction); |
| 11383 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 11384 | xmlXPathSumFunction); |
| 11385 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 11386 | xmlXPathTrueFunction); |
| 11387 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 11388 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11389 | |
| 11390 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 11391 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 11392 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11393 | } |
| 11394 | |
| 11395 | #endif /* LIBXML_XPATH_ENABLED */ |