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 | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 423 | #ifdef DEBUG_EVAL_COUNTS |
| 424 | int nb; |
| 425 | xmlChar *string; |
| 426 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | /************************************************************************ |
| 430 | * * |
| 431 | * Parser Type functions * |
| 432 | * * |
| 433 | ************************************************************************/ |
| 434 | |
| 435 | /** |
| 436 | * xmlXPathNewCompExpr: |
| 437 | * |
| 438 | * Create a new Xpath component |
| 439 | * |
| 440 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 441 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 442 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 443 | xmlXPathNewCompExpr(void) { |
| 444 | xmlXPathCompExprPtr cur; |
| 445 | |
| 446 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 447 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 448 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 449 | return(NULL); |
| 450 | } |
| 451 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 452 | cur->maxStep = 10; |
| 453 | cur->nbStep = 0; |
| 454 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 455 | sizeof(xmlXPathStepOp)); |
| 456 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 457 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 458 | xmlFree(cur); |
| 459 | return(NULL); |
| 460 | } |
| 461 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 462 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 463 | #ifdef DEBUG_EVAL_COUNTS |
| 464 | cur->nb = 0; |
| 465 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 466 | return(cur); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * xmlXPathFreeCompExpr: |
| 471 | * @comp: an XPATH comp |
| 472 | * |
| 473 | * Free up the memory allocated by @comp |
| 474 | */ |
| 475 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 476 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 477 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 478 | xmlXPathStepOpPtr op; |
| 479 | int i; |
| 480 | |
| 481 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 482 | return; |
| 483 | for (i = 0; i < comp->nbStep; i++) { |
| 484 | op = &comp->steps[i]; |
| 485 | if (op->value4 != NULL) { |
| 486 | if (op->op == XPATH_OP_VALUE) |
| 487 | xmlXPathFreeObject(op->value4); |
| 488 | else |
| 489 | xmlFree(op->value4); |
| 490 | } |
| 491 | if (op->value5 != NULL) |
| 492 | xmlFree(op->value5); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 493 | } |
| 494 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 495 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 496 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 497 | #ifdef DEBUG_EVAL_COUNTS |
| 498 | if (comp->string != NULL) { |
| 499 | xmlFree(comp->string); |
| 500 | } |
| 501 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 502 | if (comp->expr != NULL) { |
| 503 | xmlFree(comp->expr); |
| 504 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 505 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 506 | xmlFree(comp); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * xmlXPathCompExprAdd: |
| 511 | * @comp: the compiled expression |
| 512 | * @ch1: first child index |
| 513 | * @ch2: second child index |
| 514 | * @op: an op |
| 515 | * @value: the first int value |
| 516 | * @value2: the second int value |
| 517 | * @value3: the third int value |
| 518 | * @value4: the first string value |
| 519 | * @value5: the second string value |
| 520 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 521 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 522 | * |
| 523 | * Returns -1 in case of failure, the index otherwise |
| 524 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 525 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 526 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 527 | xmlXPathOp op, int value, |
| 528 | int value2, int value3, void *value4, void *value5) { |
| 529 | if (comp->nbStep >= comp->maxStep) { |
| 530 | xmlXPathStepOp *real; |
| 531 | |
| 532 | comp->maxStep *= 2; |
| 533 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 534 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 535 | if (real == NULL) { |
| 536 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 537 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 538 | return(-1); |
| 539 | } |
| 540 | comp->steps = real; |
| 541 | } |
| 542 | comp->last = comp->nbStep; |
| 543 | comp->steps[comp->nbStep].ch1 = ch1; |
| 544 | comp->steps[comp->nbStep].ch2 = ch2; |
| 545 | comp->steps[comp->nbStep].op = op; |
| 546 | comp->steps[comp->nbStep].value = value; |
| 547 | comp->steps[comp->nbStep].value2 = value2; |
| 548 | comp->steps[comp->nbStep].value3 = value3; |
| 549 | comp->steps[comp->nbStep].value4 = value4; |
| 550 | comp->steps[comp->nbStep].value5 = value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 551 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 552 | return(comp->nbStep++); |
| 553 | } |
| 554 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 555 | /** |
| 556 | * xmlXPathCompSwap: |
| 557 | * @comp: the compiled expression |
| 558 | * @op: operation index |
| 559 | * |
| 560 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 561 | */ |
| 562 | static void |
| 563 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 564 | int tmp; |
| 565 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 566 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 567 | /* |
| 568 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 569 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 570 | * application |
| 571 | */ |
| 572 | if (xmlXPathDisableOptimizer) |
| 573 | return; |
| 574 | #endif |
| 575 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 576 | tmp = op->ch1; |
| 577 | op->ch1 = op->ch2; |
| 578 | op->ch2 = tmp; |
| 579 | } |
| 580 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 581 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 582 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 583 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 584 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 585 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 586 | (op), (val), (val2), (val3), (val4), (val5)) |
| 587 | |
| 588 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 589 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 590 | |
| 591 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 592 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 593 | |
| 594 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 595 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 596 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 597 | |
| 598 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 599 | * * |
| 600 | * Debugging related functions * |
| 601 | * * |
| 602 | ************************************************************************/ |
| 603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 604 | #define STRANGE \ |
| 605 | xmlGenericError(xmlGenericErrorContext, \ |
| 606 | "Internal error at %s:%d\n", \ |
| 607 | __FILE__, __LINE__); |
| 608 | |
| 609 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 610 | static void |
| 611 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 612 | int i; |
| 613 | char shift[100]; |
| 614 | |
| 615 | for (i = 0;((i < depth) && (i < 25));i++) |
| 616 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 617 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 618 | if (cur == NULL) { |
| 619 | fprintf(output, shift); |
| 620 | fprintf(output, "Node is NULL !\n"); |
| 621 | return; |
| 622 | |
| 623 | } |
| 624 | |
| 625 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 626 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 627 | fprintf(output, shift); |
| 628 | fprintf(output, " /\n"); |
| 629 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 630 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 631 | else |
| 632 | xmlDebugDumpOneNode(output, cur, depth); |
| 633 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 634 | static void |
| 635 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 636 | xmlNodePtr tmp; |
| 637 | int i; |
| 638 | char shift[100]; |
| 639 | |
| 640 | for (i = 0;((i < depth) && (i < 25));i++) |
| 641 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 642 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 643 | if (cur == NULL) { |
| 644 | fprintf(output, shift); |
| 645 | fprintf(output, "Node is NULL !\n"); |
| 646 | return; |
| 647 | |
| 648 | } |
| 649 | |
| 650 | while (cur != NULL) { |
| 651 | tmp = cur; |
| 652 | cur = cur->next; |
| 653 | xmlDebugDumpOneNode(output, tmp, depth); |
| 654 | } |
| 655 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 656 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 657 | static void |
| 658 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 659 | int i; |
| 660 | char shift[100]; |
| 661 | |
| 662 | for (i = 0;((i < depth) && (i < 25));i++) |
| 663 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 664 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 665 | |
| 666 | if (cur == NULL) { |
| 667 | fprintf(output, shift); |
| 668 | fprintf(output, "NodeSet is NULL !\n"); |
| 669 | return; |
| 670 | |
| 671 | } |
| 672 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 673 | if (cur != NULL) { |
| 674 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 675 | for (i = 0;i < cur->nodeNr;i++) { |
| 676 | fprintf(output, shift); |
| 677 | fprintf(output, "%d", i + 1); |
| 678 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 679 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 683 | static void |
| 684 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 685 | int i; |
| 686 | char shift[100]; |
| 687 | |
| 688 | for (i = 0;((i < depth) && (i < 25));i++) |
| 689 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 690 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 691 | |
| 692 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 693 | fprintf(output, shift); |
| 694 | fprintf(output, "Value Tree is NULL !\n"); |
| 695 | return; |
| 696 | |
| 697 | } |
| 698 | |
| 699 | fprintf(output, shift); |
| 700 | fprintf(output, "%d", i + 1); |
| 701 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 702 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 703 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 704 | static void |
| 705 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 706 | int i; |
| 707 | char shift[100]; |
| 708 | |
| 709 | for (i = 0;((i < depth) && (i < 25));i++) |
| 710 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 711 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 712 | |
| 713 | if (cur == NULL) { |
| 714 | fprintf(output, shift); |
| 715 | fprintf(output, "LocationSet is NULL !\n"); |
| 716 | return; |
| 717 | |
| 718 | } |
| 719 | |
| 720 | for (i = 0;i < cur->locNr;i++) { |
| 721 | fprintf(output, shift); |
| 722 | fprintf(output, "%d : ", i + 1); |
| 723 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 724 | } |
| 725 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 726 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 727 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 728 | /** |
| 729 | * xmlXPathDebugDumpObject: |
| 730 | * @output: the FILE * to dump the output |
| 731 | * @cur: the object to inspect |
| 732 | * @depth: indentation level |
| 733 | * |
| 734 | * Dump the content of the object for debugging purposes |
| 735 | */ |
| 736 | void |
| 737 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 738 | int i; |
| 739 | char shift[100]; |
| 740 | |
| 741 | for (i = 0;((i < depth) && (i < 25));i++) |
| 742 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 743 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 744 | |
| 745 | fprintf(output, shift); |
| 746 | |
| 747 | if (cur == NULL) { |
| 748 | fprintf(output, "Object is empty (NULL)\n"); |
| 749 | return; |
| 750 | } |
| 751 | switch(cur->type) { |
| 752 | case XPATH_UNDEFINED: |
| 753 | fprintf(output, "Object is uninitialized\n"); |
| 754 | break; |
| 755 | case XPATH_NODESET: |
| 756 | fprintf(output, "Object is a Node Set :\n"); |
| 757 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 758 | break; |
| 759 | case XPATH_XSLT_TREE: |
| 760 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 761 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 762 | break; |
| 763 | case XPATH_BOOLEAN: |
| 764 | fprintf(output, "Object is a Boolean : "); |
| 765 | if (cur->boolval) fprintf(output, "true\n"); |
| 766 | else fprintf(output, "false\n"); |
| 767 | break; |
| 768 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 769 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 770 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 771 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 772 | break; |
| 773 | case -1: |
| 774 | fprintf(output, "Object is a number : -Infinity\n"); |
| 775 | break; |
| 776 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 777 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 778 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 779 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 780 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 781 | } else { |
| 782 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 783 | } |
| 784 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 785 | break; |
| 786 | case XPATH_STRING: |
| 787 | fprintf(output, "Object is a string : "); |
| 788 | xmlDebugDumpString(output, cur->stringval); |
| 789 | fprintf(output, "\n"); |
| 790 | break; |
| 791 | case XPATH_POINT: |
| 792 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 793 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 794 | fprintf(output, "\n"); |
| 795 | break; |
| 796 | case XPATH_RANGE: |
| 797 | if ((cur->user2 == NULL) || |
| 798 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 799 | fprintf(output, "Object is a collapsed range :\n"); |
| 800 | fprintf(output, shift); |
| 801 | if (cur->index >= 0) |
| 802 | fprintf(output, "index %d in ", cur->index); |
| 803 | fprintf(output, "node\n"); |
| 804 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 805 | depth + 1); |
| 806 | } else { |
| 807 | fprintf(output, "Object is a range :\n"); |
| 808 | fprintf(output, shift); |
| 809 | fprintf(output, "From "); |
| 810 | if (cur->index >= 0) |
| 811 | fprintf(output, "index %d in ", cur->index); |
| 812 | fprintf(output, "node\n"); |
| 813 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 814 | depth + 1); |
| 815 | fprintf(output, shift); |
| 816 | fprintf(output, "To "); |
| 817 | if (cur->index2 >= 0) |
| 818 | fprintf(output, "index %d in ", cur->index2); |
| 819 | fprintf(output, "node\n"); |
| 820 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 821 | depth + 1); |
| 822 | fprintf(output, "\n"); |
| 823 | } |
| 824 | break; |
| 825 | case XPATH_LOCATIONSET: |
| 826 | #if defined(LIBXML_XPTR_ENABLED) |
| 827 | fprintf(output, "Object is a Location Set:\n"); |
| 828 | xmlXPathDebugDumpLocationSet(output, |
| 829 | (xmlLocationSetPtr) cur->user, depth); |
| 830 | #endif |
| 831 | break; |
| 832 | case XPATH_USERS: |
| 833 | fprintf(output, "Object is user defined\n"); |
| 834 | break; |
| 835 | } |
| 836 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 837 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 838 | static void |
| 839 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 840 | xmlXPathStepOpPtr op, int depth) { |
| 841 | int i; |
| 842 | char shift[100]; |
| 843 | |
| 844 | for (i = 0;((i < depth) && (i < 25));i++) |
| 845 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 846 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 847 | |
| 848 | fprintf(output, shift); |
| 849 | if (op == NULL) { |
| 850 | fprintf(output, "Step is NULL\n"); |
| 851 | return; |
| 852 | } |
| 853 | switch (op->op) { |
| 854 | case XPATH_OP_END: |
| 855 | fprintf(output, "END"); break; |
| 856 | case XPATH_OP_AND: |
| 857 | fprintf(output, "AND"); break; |
| 858 | case XPATH_OP_OR: |
| 859 | fprintf(output, "OR"); break; |
| 860 | case XPATH_OP_EQUAL: |
| 861 | if (op->value) |
| 862 | fprintf(output, "EQUAL ="); |
| 863 | else |
| 864 | fprintf(output, "EQUAL !="); |
| 865 | break; |
| 866 | case XPATH_OP_CMP: |
| 867 | if (op->value) |
| 868 | fprintf(output, "CMP <"); |
| 869 | else |
| 870 | fprintf(output, "CMP >"); |
| 871 | if (!op->value2) |
| 872 | fprintf(output, "="); |
| 873 | break; |
| 874 | case XPATH_OP_PLUS: |
| 875 | if (op->value == 0) |
| 876 | fprintf(output, "PLUS -"); |
| 877 | else if (op->value == 1) |
| 878 | fprintf(output, "PLUS +"); |
| 879 | else if (op->value == 2) |
| 880 | fprintf(output, "PLUS unary -"); |
| 881 | else if (op->value == 3) |
| 882 | fprintf(output, "PLUS unary - -"); |
| 883 | break; |
| 884 | case XPATH_OP_MULT: |
| 885 | if (op->value == 0) |
| 886 | fprintf(output, "MULT *"); |
| 887 | else if (op->value == 1) |
| 888 | fprintf(output, "MULT div"); |
| 889 | else |
| 890 | fprintf(output, "MULT mod"); |
| 891 | break; |
| 892 | case XPATH_OP_UNION: |
| 893 | fprintf(output, "UNION"); break; |
| 894 | case XPATH_OP_ROOT: |
| 895 | fprintf(output, "ROOT"); break; |
| 896 | case XPATH_OP_NODE: |
| 897 | fprintf(output, "NODE"); break; |
| 898 | case XPATH_OP_RESET: |
| 899 | fprintf(output, "RESET"); break; |
| 900 | case XPATH_OP_SORT: |
| 901 | fprintf(output, "SORT"); break; |
| 902 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 903 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 904 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 905 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 906 | const xmlChar *prefix = op->value4; |
| 907 | const xmlChar *name = op->value5; |
| 908 | |
| 909 | fprintf(output, "COLLECT "); |
| 910 | switch (axis) { |
| 911 | case AXIS_ANCESTOR: |
| 912 | fprintf(output, " 'ancestors' "); break; |
| 913 | case AXIS_ANCESTOR_OR_SELF: |
| 914 | fprintf(output, " 'ancestors-or-self' "); break; |
| 915 | case AXIS_ATTRIBUTE: |
| 916 | fprintf(output, " 'attributes' "); break; |
| 917 | case AXIS_CHILD: |
| 918 | fprintf(output, " 'child' "); break; |
| 919 | case AXIS_DESCENDANT: |
| 920 | fprintf(output, " 'descendant' "); break; |
| 921 | case AXIS_DESCENDANT_OR_SELF: |
| 922 | fprintf(output, " 'descendant-or-self' "); break; |
| 923 | case AXIS_FOLLOWING: |
| 924 | fprintf(output, " 'following' "); break; |
| 925 | case AXIS_FOLLOWING_SIBLING: |
| 926 | fprintf(output, " 'following-siblings' "); break; |
| 927 | case AXIS_NAMESPACE: |
| 928 | fprintf(output, " 'namespace' "); break; |
| 929 | case AXIS_PARENT: |
| 930 | fprintf(output, " 'parent' "); break; |
| 931 | case AXIS_PRECEDING: |
| 932 | fprintf(output, " 'preceding' "); break; |
| 933 | case AXIS_PRECEDING_SIBLING: |
| 934 | fprintf(output, " 'preceding-sibling' "); break; |
| 935 | case AXIS_SELF: |
| 936 | fprintf(output, " 'self' "); break; |
| 937 | } |
| 938 | switch (test) { |
| 939 | case NODE_TEST_NONE: |
| 940 | fprintf(output, "'none' "); break; |
| 941 | case NODE_TEST_TYPE: |
| 942 | fprintf(output, "'type' "); break; |
| 943 | case NODE_TEST_PI: |
| 944 | fprintf(output, "'PI' "); break; |
| 945 | case NODE_TEST_ALL: |
| 946 | fprintf(output, "'all' "); break; |
| 947 | case NODE_TEST_NS: |
| 948 | fprintf(output, "'namespace' "); break; |
| 949 | case NODE_TEST_NAME: |
| 950 | fprintf(output, "'name' "); break; |
| 951 | } |
| 952 | switch (type) { |
| 953 | case NODE_TYPE_NODE: |
| 954 | fprintf(output, "'node' "); break; |
| 955 | case NODE_TYPE_COMMENT: |
| 956 | fprintf(output, "'comment' "); break; |
| 957 | case NODE_TYPE_TEXT: |
| 958 | fprintf(output, "'text' "); break; |
| 959 | case NODE_TYPE_PI: |
| 960 | fprintf(output, "'PI' "); break; |
| 961 | } |
| 962 | if (prefix != NULL) |
| 963 | fprintf(output, "%s:", prefix); |
| 964 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 965 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 966 | break; |
| 967 | |
| 968 | } |
| 969 | case XPATH_OP_VALUE: { |
| 970 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 971 | |
| 972 | fprintf(output, "ELEM "); |
| 973 | xmlXPathDebugDumpObject(output, object, 0); |
| 974 | goto finish; |
| 975 | } |
| 976 | case XPATH_OP_VARIABLE: { |
| 977 | const xmlChar *prefix = op->value5; |
| 978 | const xmlChar *name = op->value4; |
| 979 | |
| 980 | if (prefix != NULL) |
| 981 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 982 | else |
| 983 | fprintf(output, "VARIABLE %s", name); |
| 984 | break; |
| 985 | } |
| 986 | case XPATH_OP_FUNCTION: { |
| 987 | int nbargs = op->value; |
| 988 | const xmlChar *prefix = op->value5; |
| 989 | const xmlChar *name = op->value4; |
| 990 | |
| 991 | if (prefix != NULL) |
| 992 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 993 | prefix, name, nbargs); |
| 994 | else |
| 995 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 996 | break; |
| 997 | } |
| 998 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 999 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1000 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1001 | #ifdef LIBXML_XPTR_ENABLED |
| 1002 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1003 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1004 | default: |
| 1005 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1006 | } |
| 1007 | fprintf(output, "\n"); |
| 1008 | finish: |
| 1009 | if (op->ch1 >= 0) |
| 1010 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1011 | if (op->ch2 >= 0) |
| 1012 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1013 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1014 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1015 | /** |
| 1016 | * xmlXPathDebugDumpCompExpr: |
| 1017 | * @output: the FILE * for the output |
| 1018 | * @comp: the precompiled XPath expression |
| 1019 | * @depth: the indentation level. |
| 1020 | * |
| 1021 | * Dumps the tree of the compiled XPath expression. |
| 1022 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1023 | void |
| 1024 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1025 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1026 | int i; |
| 1027 | char shift[100]; |
| 1028 | |
| 1029 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1030 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1031 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1032 | |
| 1033 | fprintf(output, shift); |
| 1034 | |
| 1035 | if (comp == NULL) { |
| 1036 | fprintf(output, "Compiled Expression is NULL\n"); |
| 1037 | return; |
| 1038 | } |
| 1039 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1040 | comp->nbStep); |
| 1041 | i = comp->last; |
| 1042 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1043 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1044 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1045 | |
| 1046 | /************************************************************************ |
| 1047 | * * |
| 1048 | * Parser stacks related functions and macros * |
| 1049 | * * |
| 1050 | ************************************************************************/ |
| 1051 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1052 | /** |
| 1053 | * valuePop: |
| 1054 | * @ctxt: an XPath evaluation context |
| 1055 | * |
| 1056 | * Pops the top XPath object from the value stack |
| 1057 | * |
| 1058 | * Returns the XPath object just removed |
| 1059 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1060 | extern xmlXPathObjectPtr |
| 1061 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1062 | { |
| 1063 | xmlXPathObjectPtr ret; |
| 1064 | |
| 1065 | if (ctxt->valueNr <= 0) |
| 1066 | return (0); |
| 1067 | ctxt->valueNr--; |
| 1068 | if (ctxt->valueNr > 0) |
| 1069 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1070 | else |
| 1071 | ctxt->value = NULL; |
| 1072 | ret = ctxt->valueTab[ctxt->valueNr]; |
| 1073 | ctxt->valueTab[ctxt->valueNr] = 0; |
| 1074 | return (ret); |
| 1075 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1076 | /** |
| 1077 | * valuePush: |
| 1078 | * @ctxt: an XPath evaluation context |
| 1079 | * @value: the XPath object |
| 1080 | * |
| 1081 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1082 | * |
| 1083 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1084 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1085 | extern int |
| 1086 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1087 | { |
| 1088 | if (ctxt->valueNr >= ctxt->valueMax) { |
| 1089 | ctxt->valueMax *= 2; |
| 1090 | ctxt->valueTab = |
| 1091 | (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1092 | ctxt->valueMax * |
| 1093 | sizeof(ctxt->valueTab[0])); |
| 1094 | if (ctxt->valueTab == NULL) { |
| 1095 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1096 | return (0); |
| 1097 | } |
| 1098 | } |
| 1099 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1100 | ctxt->value = value; |
| 1101 | return (ctxt->valueNr++); |
| 1102 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1103 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1104 | /** |
| 1105 | * xmlXPathPopBoolean: |
| 1106 | * @ctxt: an XPath parser context |
| 1107 | * |
| 1108 | * Pops a boolean from the stack, handling conversion if needed. |
| 1109 | * Check error with #xmlXPathCheckError. |
| 1110 | * |
| 1111 | * Returns the boolean |
| 1112 | */ |
| 1113 | int |
| 1114 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1115 | xmlXPathObjectPtr obj; |
| 1116 | int ret; |
| 1117 | |
| 1118 | obj = valuePop(ctxt); |
| 1119 | if (obj == NULL) { |
| 1120 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1121 | return(0); |
| 1122 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1123 | if (obj->type != XPATH_BOOLEAN) |
| 1124 | ret = xmlXPathCastToBoolean(obj); |
| 1125 | else |
| 1126 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1127 | xmlXPathFreeObject(obj); |
| 1128 | return(ret); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * xmlXPathPopNumber: |
| 1133 | * @ctxt: an XPath parser context |
| 1134 | * |
| 1135 | * Pops a number from the stack, handling conversion if needed. |
| 1136 | * Check error with #xmlXPathCheckError. |
| 1137 | * |
| 1138 | * Returns the number |
| 1139 | */ |
| 1140 | double |
| 1141 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1142 | xmlXPathObjectPtr obj; |
| 1143 | double ret; |
| 1144 | |
| 1145 | obj = valuePop(ctxt); |
| 1146 | if (obj == NULL) { |
| 1147 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1148 | return(0); |
| 1149 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1150 | if (obj->type != XPATH_NUMBER) |
| 1151 | ret = xmlXPathCastToNumber(obj); |
| 1152 | else |
| 1153 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1154 | xmlXPathFreeObject(obj); |
| 1155 | return(ret); |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * xmlXPathPopString: |
| 1160 | * @ctxt: an XPath parser context |
| 1161 | * |
| 1162 | * Pops a string from the stack, handling conversion if needed. |
| 1163 | * Check error with #xmlXPathCheckError. |
| 1164 | * |
| 1165 | * Returns the string |
| 1166 | */ |
| 1167 | xmlChar * |
| 1168 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1169 | xmlXPathObjectPtr obj; |
| 1170 | xmlChar * ret; |
| 1171 | |
| 1172 | obj = valuePop(ctxt); |
| 1173 | if (obj == NULL) { |
| 1174 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1175 | return(NULL); |
| 1176 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1177 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1178 | /* TODO: needs refactoring somewhere else */ |
| 1179 | if (obj->stringval == ret) |
| 1180 | obj->stringval = NULL; |
| 1181 | xmlXPathFreeObject(obj); |
| 1182 | return(ret); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * xmlXPathPopNodeSet: |
| 1187 | * @ctxt: an XPath parser context |
| 1188 | * |
| 1189 | * Pops a node-set from the stack, handling conversion if needed. |
| 1190 | * Check error with #xmlXPathCheckError. |
| 1191 | * |
| 1192 | * Returns the node-set |
| 1193 | */ |
| 1194 | xmlNodeSetPtr |
| 1195 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1196 | xmlXPathObjectPtr obj; |
| 1197 | xmlNodeSetPtr ret; |
| 1198 | |
| 1199 | if (ctxt->value == NULL) { |
| 1200 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1201 | return(NULL); |
| 1202 | } |
| 1203 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1204 | xmlXPathSetTypeError(ctxt); |
| 1205 | return(NULL); |
| 1206 | } |
| 1207 | obj = valuePop(ctxt); |
| 1208 | ret = obj->nodesetval; |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1209 | /* to fix memory leak of not clearing obj->user */ |
| 1210 | if (obj->boolval && obj->user != NULL) |
| 1211 | xmlFreeNodeList((xmlNodePtr) obj->user); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1212 | xmlXPathFreeNodeSetList(obj); |
| 1213 | return(ret); |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * xmlXPathPopExternal: |
| 1218 | * @ctxt: an XPath parser context |
| 1219 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1220 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1221 | * Check error with #xmlXPathCheckError. |
| 1222 | * |
| 1223 | * Returns the object |
| 1224 | */ |
| 1225 | void * |
| 1226 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1227 | xmlXPathObjectPtr obj; |
| 1228 | void * ret; |
| 1229 | |
| 1230 | if (ctxt->value == NULL) { |
| 1231 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1232 | return(NULL); |
| 1233 | } |
| 1234 | if (ctxt->value->type != XPATH_USERS) { |
| 1235 | xmlXPathSetTypeError(ctxt); |
| 1236 | return(NULL); |
| 1237 | } |
| 1238 | obj = valuePop(ctxt); |
| 1239 | ret = obj->user; |
| 1240 | xmlXPathFreeObject(obj); |
| 1241 | return(ret); |
| 1242 | } |
| 1243 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1244 | /* |
| 1245 | * Macros for accessing the content. Those should be used only by the parser, |
| 1246 | * and not exported. |
| 1247 | * |
| 1248 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1249 | * |
| 1250 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1251 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1252 | * in ISO-Latin or UTF-8. |
| 1253 | * This should be used internally by the parser |
| 1254 | * only to compare to ASCII values otherwise it would break when |
| 1255 | * running with UTF-8 encoding. |
| 1256 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1257 | * to compare on ASCII based substring. |
| 1258 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1259 | * strings within the parser. |
| 1260 | * CURRENT Returns the current char value, with the full decoding of |
| 1261 | * UTF-8 if we are using this mode. It returns an int. |
| 1262 | * NEXT Skip to the next character, this does the proper decoding |
| 1263 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1264 | * It returns the pointer to the current xmlChar. |
| 1265 | */ |
| 1266 | |
| 1267 | #define CUR (*ctxt->cur) |
| 1268 | #define SKIP(val) ctxt->cur += (val) |
| 1269 | #define NXT(val) ctxt->cur[(val)] |
| 1270 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1271 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1272 | |
| 1273 | #define COPY_BUF(l,b,i,v) \ |
| 1274 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1275 | else i += xmlCopyChar(l,&b[i],v) |
| 1276 | |
| 1277 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1278 | |
| 1279 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1280 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1281 | |
| 1282 | #define CURRENT (*ctxt->cur) |
| 1283 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1284 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1285 | |
| 1286 | #ifndef DBL_DIG |
| 1287 | #define DBL_DIG 16 |
| 1288 | #endif |
| 1289 | #ifndef DBL_EPSILON |
| 1290 | #define DBL_EPSILON 1E-9 |
| 1291 | #endif |
| 1292 | |
| 1293 | #define UPPER_DOUBLE 1E9 |
| 1294 | #define LOWER_DOUBLE 1E-5 |
| 1295 | |
| 1296 | #define INTEGER_DIGITS DBL_DIG |
| 1297 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1298 | #define EXPONENT_DIGITS (3 + 2) |
| 1299 | |
| 1300 | /** |
| 1301 | * xmlXPathFormatNumber: |
| 1302 | * @number: number to format |
| 1303 | * @buffer: output buffer |
| 1304 | * @buffersize: size of output buffer |
| 1305 | * |
| 1306 | * Convert the number into a string representation. |
| 1307 | */ |
| 1308 | static void |
| 1309 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1310 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1311 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1312 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1313 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1314 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1315 | break; |
| 1316 | case -1: |
| 1317 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1318 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1321 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1322 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1323 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1324 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1325 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1326 | } else if (number == ((int) number)) { |
| 1327 | char work[30]; |
| 1328 | char *ptr, *cur; |
| 1329 | int res, value = (int) number; |
| 1330 | |
| 1331 | ptr = &buffer[0]; |
| 1332 | if (value < 0) { |
| 1333 | *ptr++ = '-'; |
| 1334 | value = -value; |
| 1335 | } |
| 1336 | if (value == 0) { |
| 1337 | *ptr++ = '0'; |
| 1338 | } else { |
| 1339 | cur = &work[0]; |
| 1340 | while (value != 0) { |
| 1341 | res = value % 10; |
| 1342 | value = value / 10; |
| 1343 | *cur++ = '0' + res; |
| 1344 | } |
| 1345 | cur--; |
| 1346 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1347 | *ptr++ = *cur--; |
| 1348 | } |
| 1349 | } |
| 1350 | if (ptr - buffer < buffersize) { |
| 1351 | *ptr = 0; |
| 1352 | } else if (buffersize > 0) { |
| 1353 | ptr--; |
| 1354 | *ptr = 0; |
| 1355 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1356 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1357 | /* 3 is sign, decimal point, and terminating zero */ |
| 1358 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1359 | int integer_place, fraction_place; |
| 1360 | char *ptr; |
| 1361 | char *after_fraction; |
| 1362 | double absolute_value; |
| 1363 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1364 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1365 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1366 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1367 | /* |
| 1368 | * First choose format - scientific or regular floating point. |
| 1369 | * In either case, result is in work, and after_fraction points |
| 1370 | * just past the fractional part. |
| 1371 | */ |
| 1372 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1373 | (absolute_value < LOWER_DOUBLE)) && |
| 1374 | (absolute_value != 0.0) ) { |
| 1375 | /* Use scientific notation */ |
| 1376 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1377 | fraction_place = DBL_DIG - 1; |
| 1378 | snprintf(work, sizeof(work),"%*.*e", |
| 1379 | integer_place, fraction_place, number); |
| 1380 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1381 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1382 | else { |
| 1383 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1384 | if (absolute_value > 0.0) |
| 1385 | integer_place = 1 + (int)log10(absolute_value); |
| 1386 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1387 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1388 | fraction_place = (integer_place > 0) |
| 1389 | ? DBL_DIG - integer_place |
| 1390 | : DBL_DIG; |
| 1391 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1392 | fraction_place, number); |
| 1393 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1396 | /* Remove fractional trailing zeroes */ |
| 1397 | ptr = after_fraction; |
| 1398 | while (*(--ptr) == '0') |
| 1399 | ; |
| 1400 | if (*ptr != '.') |
| 1401 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1402 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1403 | |
| 1404 | /* Finally copy result back to caller */ |
| 1405 | size = strlen(work) + 1; |
| 1406 | if (size > buffersize) { |
| 1407 | work[buffersize - 1] = 0; |
| 1408 | size = buffersize; |
| 1409 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1410 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1411 | } |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1416 | |
| 1417 | /************************************************************************ |
| 1418 | * * |
| 1419 | * Routines to handle NodeSets * |
| 1420 | * * |
| 1421 | ************************************************************************/ |
| 1422 | |
| 1423 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1424 | * xmlXPathOrderDocElems: |
| 1425 | * @doc: an input document |
| 1426 | * |
| 1427 | * Call this routine to speed up XPath computation on static documents. |
| 1428 | * This stamps all the element nodes with the document order |
| 1429 | * 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] | 1430 | * field, the value stored is actually - the node number (starting at -1) |
| 1431 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1432 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1433 | * 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] | 1434 | * of error. |
| 1435 | */ |
| 1436 | long |
| 1437 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1438 | long count = 0; |
| 1439 | xmlNodePtr cur; |
| 1440 | |
| 1441 | if (doc == NULL) |
| 1442 | return(-1); |
| 1443 | cur = doc->children; |
| 1444 | while (cur != NULL) { |
| 1445 | if (cur->type == XML_ELEMENT_NODE) { |
| 1446 | cur->content = (void *) (-(++count)); |
| 1447 | if (cur->children != NULL) { |
| 1448 | cur = cur->children; |
| 1449 | continue; |
| 1450 | } |
| 1451 | } |
| 1452 | if (cur->next != NULL) { |
| 1453 | cur = cur->next; |
| 1454 | continue; |
| 1455 | } |
| 1456 | do { |
| 1457 | cur = cur->parent; |
| 1458 | if (cur == NULL) |
| 1459 | break; |
| 1460 | if (cur == (xmlNodePtr) doc) { |
| 1461 | cur = NULL; |
| 1462 | break; |
| 1463 | } |
| 1464 | if (cur->next != NULL) { |
| 1465 | cur = cur->next; |
| 1466 | break; |
| 1467 | } |
| 1468 | } while (cur != NULL); |
| 1469 | } |
| 1470 | return(count); |
| 1471 | } |
| 1472 | |
| 1473 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1474 | * xmlXPathCmpNodes: |
| 1475 | * @node1: the first node |
| 1476 | * @node2: the second node |
| 1477 | * |
| 1478 | * Compare two nodes w.r.t document order |
| 1479 | * |
| 1480 | * 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] | 1481 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1482 | */ |
| 1483 | int |
| 1484 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1485 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1486 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1487 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1488 | xmlNodePtr cur, root; |
| 1489 | |
| 1490 | if ((node1 == NULL) || (node2 == NULL)) |
| 1491 | return(-2); |
| 1492 | /* |
| 1493 | * a couple of optimizations which will avoid computations in most cases |
| 1494 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1495 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1496 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1497 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1498 | node1 = node1->parent; |
| 1499 | } |
| 1500 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1501 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1502 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1503 | node2 = node2->parent; |
| 1504 | } |
| 1505 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1506 | if (attr1 == attr2) { |
| 1507 | /* not required, but we keep attributes in order */ |
| 1508 | if (attr1 != 0) { |
| 1509 | cur = attrNode2->prev; |
| 1510 | while (cur != NULL) { |
| 1511 | if (cur == attrNode1) |
| 1512 | return (1); |
| 1513 | cur = cur->prev; |
| 1514 | } |
| 1515 | return (-1); |
| 1516 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1517 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1518 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1519 | if (attr2 == 1) |
| 1520 | return(1); |
| 1521 | return(-1); |
| 1522 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1523 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1524 | (node2->type == XML_NAMESPACE_DECL)) |
| 1525 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1526 | if (node1 == node2->prev) |
| 1527 | return(1); |
| 1528 | if (node1 == node2->next) |
| 1529 | return(-1); |
| 1530 | |
| 1531 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1532 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1533 | */ |
| 1534 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1535 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1536 | (0 > (long) node1->content) && |
| 1537 | (0 > (long) node2->content) && |
| 1538 | (node1->doc == node2->doc)) { |
| 1539 | long l1, l2; |
| 1540 | |
| 1541 | l1 = -((long) node1->content); |
| 1542 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1543 | if (l1 < l2) |
| 1544 | return(1); |
| 1545 | if (l1 > l2) |
| 1546 | return(-1); |
| 1547 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1548 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1549 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1550 | * compute depth to root |
| 1551 | */ |
| 1552 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1553 | if (cur == node1) |
| 1554 | return(1); |
| 1555 | depth2++; |
| 1556 | } |
| 1557 | root = cur; |
| 1558 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1559 | if (cur == node2) |
| 1560 | return(-1); |
| 1561 | depth1++; |
| 1562 | } |
| 1563 | /* |
| 1564 | * Distinct document (or distinct entities :-( ) case. |
| 1565 | */ |
| 1566 | if (root != cur) { |
| 1567 | return(-2); |
| 1568 | } |
| 1569 | /* |
| 1570 | * get the nearest common ancestor. |
| 1571 | */ |
| 1572 | while (depth1 > depth2) { |
| 1573 | depth1--; |
| 1574 | node1 = node1->parent; |
| 1575 | } |
| 1576 | while (depth2 > depth1) { |
| 1577 | depth2--; |
| 1578 | node2 = node2->parent; |
| 1579 | } |
| 1580 | while (node1->parent != node2->parent) { |
| 1581 | node1 = node1->parent; |
| 1582 | node2 = node2->parent; |
| 1583 | /* should not happen but just in case ... */ |
| 1584 | if ((node1 == NULL) || (node2 == NULL)) |
| 1585 | return(-2); |
| 1586 | } |
| 1587 | /* |
| 1588 | * Find who's first. |
| 1589 | */ |
| 1590 | if (node1 == node2->next) |
| 1591 | return(-1); |
| 1592 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1593 | if (cur == node2) |
| 1594 | return(1); |
| 1595 | return(-1); /* assume there is no sibling list corruption */ |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * xmlXPathNodeSetSort: |
| 1600 | * @set: the node set |
| 1601 | * |
| 1602 | * Sort the node set in document order |
| 1603 | */ |
| 1604 | void |
| 1605 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1606 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | xmlNodePtr tmp; |
| 1608 | |
| 1609 | if (set == NULL) |
| 1610 | return; |
| 1611 | |
| 1612 | /* Use Shell's sort to sort the node-set */ |
| 1613 | len = set->nodeNr; |
| 1614 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1615 | for (i = incr; i < len; i++) { |
| 1616 | j = i - incr; |
| 1617 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1618 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1619 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1620 | tmp = set->nodeTab[j]; |
| 1621 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1622 | set->nodeTab[j + incr] = tmp; |
| 1623 | j -= incr; |
| 1624 | } else |
| 1625 | break; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | #define XML_NODESET_DEFAULT 10 |
| 1632 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1633 | * xmlXPathNodeSetDupNs: |
| 1634 | * @node: the parent node of the namespace XPath node |
| 1635 | * @ns: the libxml namespace declaration node. |
| 1636 | * |
| 1637 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1638 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1639 | * parent node in the XPath semantic. |
| 1640 | * |
| 1641 | * Returns the newly created object. |
| 1642 | */ |
| 1643 | static xmlNodePtr |
| 1644 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1645 | xmlNsPtr cur; |
| 1646 | |
| 1647 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1648 | return(NULL); |
| 1649 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1650 | return((xmlNodePtr) ns); |
| 1651 | |
| 1652 | /* |
| 1653 | * Allocate a new Namespace and fill the fields. |
| 1654 | */ |
| 1655 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1656 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1657 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1658 | return(NULL); |
| 1659 | } |
| 1660 | memset(cur, 0, sizeof(xmlNs)); |
| 1661 | cur->type = XML_NAMESPACE_DECL; |
| 1662 | if (ns->href != NULL) |
| 1663 | cur->href = xmlStrdup(ns->href); |
| 1664 | if (ns->prefix != NULL) |
| 1665 | cur->prefix = xmlStrdup(ns->prefix); |
| 1666 | cur->next = (xmlNsPtr) node; |
| 1667 | return((xmlNodePtr) cur); |
| 1668 | } |
| 1669 | |
| 1670 | /** |
| 1671 | * xmlXPathNodeSetFreeNs: |
| 1672 | * @ns: the XPath namespace node found in a nodeset. |
| 1673 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1674 | * 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] | 1675 | * 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] | 1676 | * 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] | 1677 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1678 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1679 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1680 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1681 | return; |
| 1682 | |
| 1683 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1684 | if (ns->href != NULL) |
| 1685 | xmlFree((xmlChar *)ns->href); |
| 1686 | if (ns->prefix != NULL) |
| 1687 | xmlFree((xmlChar *)ns->prefix); |
| 1688 | xmlFree(ns); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1693 | * xmlXPathNodeSetCreate: |
| 1694 | * @val: an initial xmlNodePtr, or NULL |
| 1695 | * |
| 1696 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1697 | * |
| 1698 | * Returns the newly created object. |
| 1699 | */ |
| 1700 | xmlNodeSetPtr |
| 1701 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1702 | xmlNodeSetPtr ret; |
| 1703 | |
| 1704 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1705 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1706 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1707 | return(NULL); |
| 1708 | } |
| 1709 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1710 | if (val != NULL) { |
| 1711 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1712 | sizeof(xmlNodePtr)); |
| 1713 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1714 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 1715 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1716 | return(NULL); |
| 1717 | } |
| 1718 | memset(ret->nodeTab, 0 , |
| 1719 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1720 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1721 | if (val->type == XML_NAMESPACE_DECL) { |
| 1722 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1723 | |
| 1724 | ret->nodeTab[ret->nodeNr++] = |
| 1725 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1726 | } else |
| 1727 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1728 | } |
| 1729 | return(ret); |
| 1730 | } |
| 1731 | |
| 1732 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1733 | * xmlXPathNodeSetContains: |
| 1734 | * @cur: the node-set |
| 1735 | * @val: the node |
| 1736 | * |
| 1737 | * checks whether @cur contains @val |
| 1738 | * |
| 1739 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1740 | */ |
| 1741 | int |
| 1742 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1743 | int i; |
| 1744 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1745 | if (val->type == XML_NAMESPACE_DECL) { |
| 1746 | for (i = 0; i < cur->nodeNr; i++) { |
| 1747 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1748 | xmlNsPtr ns1, ns2; |
| 1749 | |
| 1750 | ns1 = (xmlNsPtr) val; |
| 1751 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1752 | if (ns1 == ns2) |
| 1753 | return(1); |
| 1754 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1755 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1756 | return(1); |
| 1757 | } |
| 1758 | } |
| 1759 | } else { |
| 1760 | for (i = 0; i < cur->nodeNr; i++) { |
| 1761 | if (cur->nodeTab[i] == val) |
| 1762 | return(1); |
| 1763 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1764 | } |
| 1765 | return(0); |
| 1766 | } |
| 1767 | |
| 1768 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1769 | * xmlXPathNodeSetAddNs: |
| 1770 | * @cur: the initial node set |
| 1771 | * @node: the hosting node |
| 1772 | * @ns: a the namespace node |
| 1773 | * |
| 1774 | * add a new namespace node to an existing NodeSet |
| 1775 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1776 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1777 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1778 | int i; |
| 1779 | |
| 1780 | if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) || |
| 1781 | (node->type != XML_ELEMENT_NODE)) |
| 1782 | return; |
| 1783 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1784 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1785 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1786 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1787 | */ |
| 1788 | for (i = 0;i < cur->nodeNr;i++) { |
| 1789 | if ((cur->nodeTab[i] != NULL) && |
| 1790 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1791 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1792 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1793 | return; |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * grow the nodeTab if needed |
| 1798 | */ |
| 1799 | if (cur->nodeMax == 0) { |
| 1800 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1801 | sizeof(xmlNodePtr)); |
| 1802 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1803 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1804 | return; |
| 1805 | } |
| 1806 | memset(cur->nodeTab, 0 , |
| 1807 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1808 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1809 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1810 | xmlNodePtr *temp; |
| 1811 | |
| 1812 | cur->nodeMax *= 2; |
| 1813 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1814 | sizeof(xmlNodePtr)); |
| 1815 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1816 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1817 | return; |
| 1818 | } |
| 1819 | cur->nodeTab = temp; |
| 1820 | } |
| 1821 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1822 | } |
| 1823 | |
| 1824 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1825 | * xmlXPathNodeSetAdd: |
| 1826 | * @cur: the initial node set |
| 1827 | * @val: a new xmlNodePtr |
| 1828 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1829 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1830 | */ |
| 1831 | void |
| 1832 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1833 | int i; |
| 1834 | |
| 1835 | if (val == NULL) return; |
| 1836 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1837 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1838 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1839 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1840 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1841 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1842 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1843 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1844 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1845 | */ |
| 1846 | for (i = 0;i < cur->nodeNr;i++) |
| 1847 | if (cur->nodeTab[i] == val) return; |
| 1848 | |
| 1849 | /* |
| 1850 | * grow the nodeTab if needed |
| 1851 | */ |
| 1852 | if (cur->nodeMax == 0) { |
| 1853 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1854 | sizeof(xmlNodePtr)); |
| 1855 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1856 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | memset(cur->nodeTab, 0 , |
| 1860 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1861 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1862 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1863 | xmlNodePtr *temp; |
| 1864 | |
| 1865 | cur->nodeMax *= 2; |
| 1866 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1867 | sizeof(xmlNodePtr)); |
| 1868 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1869 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1870 | return; |
| 1871 | } |
| 1872 | cur->nodeTab = temp; |
| 1873 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1874 | if (val->type == XML_NAMESPACE_DECL) { |
| 1875 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1876 | |
| 1877 | cur->nodeTab[cur->nodeNr++] = |
| 1878 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1879 | } else |
| 1880 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * xmlXPathNodeSetAddUnique: |
| 1885 | * @cur: the initial node set |
| 1886 | * @val: a new xmlNodePtr |
| 1887 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1888 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1889 | * when we are sure the node is not already in the set. |
| 1890 | */ |
| 1891 | void |
| 1892 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1893 | if (val == NULL) return; |
| 1894 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1895 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1896 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1897 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1898 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1899 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1900 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1901 | /* |
| 1902 | * grow the nodeTab if needed |
| 1903 | */ |
| 1904 | if (cur->nodeMax == 0) { |
| 1905 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1906 | sizeof(xmlNodePtr)); |
| 1907 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1908 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | memset(cur->nodeTab, 0 , |
| 1912 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1913 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1914 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1915 | xmlNodePtr *temp; |
| 1916 | |
| 1917 | cur->nodeMax *= 2; |
| 1918 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1919 | sizeof(xmlNodePtr)); |
| 1920 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1921 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
| 1924 | cur->nodeTab = temp; |
| 1925 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1926 | if (val->type == XML_NAMESPACE_DECL) { |
| 1927 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1928 | |
| 1929 | cur->nodeTab[cur->nodeNr++] = |
| 1930 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1931 | } else |
| 1932 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * xmlXPathNodeSetMerge: |
| 1937 | * @val1: the first NodeSet or NULL |
| 1938 | * @val2: the second NodeSet |
| 1939 | * |
| 1940 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 1941 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 1942 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1943 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | */ |
| 1945 | xmlNodeSetPtr |
| 1946 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1947 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1948 | |
| 1949 | if (val2 == NULL) return(val1); |
| 1950 | if (val1 == NULL) { |
| 1951 | val1 = xmlXPathNodeSetCreate(NULL); |
| 1952 | } |
| 1953 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1954 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1955 | initNr = val1->nodeNr; |
| 1956 | |
| 1957 | for (i = 0;i < val2->nodeNr;i++) { |
| 1958 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1959 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1961 | skip = 0; |
| 1962 | for (j = 0; j < initNr; j++) { |
| 1963 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 1964 | skip = 1; |
| 1965 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1966 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 1967 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 1968 | xmlNsPtr ns1, ns2; |
| 1969 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 1970 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 1971 | if ((ns1->next == ns2->next) && |
| 1972 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 1973 | skip = 1; |
| 1974 | break; |
| 1975 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | if (skip) |
| 1979 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | |
| 1981 | /* |
| 1982 | * grow the nodeTab if needed |
| 1983 | */ |
| 1984 | if (val1->nodeMax == 0) { |
| 1985 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1986 | sizeof(xmlNodePtr)); |
| 1987 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1988 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1989 | return(NULL); |
| 1990 | } |
| 1991 | memset(val1->nodeTab, 0 , |
| 1992 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1993 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 1994 | } else if (val1->nodeNr == val1->nodeMax) { |
| 1995 | xmlNodePtr *temp; |
| 1996 | |
| 1997 | val1->nodeMax *= 2; |
| 1998 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 1999 | sizeof(xmlNodePtr)); |
| 2000 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2001 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2002 | return(NULL); |
| 2003 | } |
| 2004 | val1->nodeTab = temp; |
| 2005 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2006 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2007 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2008 | |
| 2009 | val1->nodeTab[val1->nodeNr++] = |
| 2010 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2011 | } else |
| 2012 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | return(val1); |
| 2016 | } |
| 2017 | |
| 2018 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2019 | * xmlXPathNodeSetMergeUnique: |
| 2020 | * @val1: the first NodeSet or NULL |
| 2021 | * @val2: the second NodeSet |
| 2022 | * |
| 2023 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2024 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2025 | * |
| 2026 | * Returns @val1 once extended or NULL in case of error. |
| 2027 | */ |
| 2028 | static xmlNodeSetPtr |
| 2029 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2030 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2031 | |
| 2032 | if (val2 == NULL) return(val1); |
| 2033 | if (val1 == NULL) { |
| 2034 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2035 | } |
| 2036 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2037 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2038 | |
| 2039 | for (i = 0;i < val2->nodeNr;i++) { |
| 2040 | /* |
| 2041 | * grow the nodeTab if needed |
| 2042 | */ |
| 2043 | if (val1->nodeMax == 0) { |
| 2044 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2045 | sizeof(xmlNodePtr)); |
| 2046 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2047 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2048 | return(NULL); |
| 2049 | } |
| 2050 | memset(val1->nodeTab, 0 , |
| 2051 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2052 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2053 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2054 | xmlNodePtr *temp; |
| 2055 | |
| 2056 | val1->nodeMax *= 2; |
| 2057 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2058 | sizeof(xmlNodePtr)); |
| 2059 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2060 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2061 | return(NULL); |
| 2062 | } |
| 2063 | val1->nodeTab = temp; |
| 2064 | } |
| 2065 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2066 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2067 | |
| 2068 | val1->nodeTab[val1->nodeNr++] = |
| 2069 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2070 | } else |
| 2071 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2072 | } |
| 2073 | |
| 2074 | return(val1); |
| 2075 | } |
| 2076 | |
| 2077 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2078 | * xmlXPathNodeSetDel: |
| 2079 | * @cur: the initial node set |
| 2080 | * @val: an xmlNodePtr |
| 2081 | * |
| 2082 | * Removes an xmlNodePtr from an existing NodeSet |
| 2083 | */ |
| 2084 | void |
| 2085 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2086 | int i; |
| 2087 | |
| 2088 | if (cur == NULL) return; |
| 2089 | if (val == NULL) return; |
| 2090 | |
| 2091 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2092 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | for (i = 0;i < cur->nodeNr;i++) |
| 2095 | if (cur->nodeTab[i] == val) break; |
| 2096 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2097 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2098 | #ifdef DEBUG |
| 2099 | xmlGenericError(xmlGenericErrorContext, |
| 2100 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2101 | val->name); |
| 2102 | #endif |
| 2103 | return; |
| 2104 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2105 | if ((cur->nodeTab[i] != NULL) && |
| 2106 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2107 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2108 | cur->nodeNr--; |
| 2109 | for (;i < cur->nodeNr;i++) |
| 2110 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2111 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2112 | } |
| 2113 | |
| 2114 | /** |
| 2115 | * xmlXPathNodeSetRemove: |
| 2116 | * @cur: the initial node set |
| 2117 | * @val: the index to remove |
| 2118 | * |
| 2119 | * Removes an entry from an existing NodeSet list. |
| 2120 | */ |
| 2121 | void |
| 2122 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2123 | if (cur == NULL) return; |
| 2124 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2125 | if ((cur->nodeTab[val] != NULL) && |
| 2126 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2127 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2128 | cur->nodeNr--; |
| 2129 | for (;val < cur->nodeNr;val++) |
| 2130 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2131 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2132 | } |
| 2133 | |
| 2134 | /** |
| 2135 | * xmlXPathFreeNodeSet: |
| 2136 | * @obj: the xmlNodeSetPtr to free |
| 2137 | * |
| 2138 | * Free the NodeSet compound (not the actual nodes !). |
| 2139 | */ |
| 2140 | void |
| 2141 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2142 | if (obj == NULL) return; |
| 2143 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2144 | int i; |
| 2145 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2146 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2147 | for (i = 0;i < obj->nodeNr;i++) |
| 2148 | if ((obj->nodeTab[i] != NULL) && |
| 2149 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2150 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2151 | xmlFree(obj->nodeTab); |
| 2152 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2153 | xmlFree(obj); |
| 2154 | } |
| 2155 | |
| 2156 | /** |
| 2157 | * xmlXPathFreeValueTree: |
| 2158 | * @obj: the xmlNodeSetPtr to free |
| 2159 | * |
| 2160 | * Free the NodeSet compound and the actual tree, this is different |
| 2161 | * from xmlXPathFreeNodeSet() |
| 2162 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2163 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2164 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2165 | int i; |
| 2166 | |
| 2167 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2170 | for (i = 0;i < obj->nodeNr;i++) { |
| 2171 | if (obj->nodeTab[i] != NULL) { |
| 2172 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2173 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2174 | } else { |
| 2175 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2176 | } |
| 2177 | } |
| 2178 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | xmlFree(obj->nodeTab); |
| 2180 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2181 | xmlFree(obj); |
| 2182 | } |
| 2183 | |
| 2184 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2185 | /** |
| 2186 | * xmlGenericErrorContextNodeSet: |
| 2187 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2188 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2189 | * |
| 2190 | * Quick display of a NodeSet |
| 2191 | */ |
| 2192 | void |
| 2193 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2194 | int i; |
| 2195 | |
| 2196 | if (output == NULL) output = xmlGenericErrorContext; |
| 2197 | if (obj == NULL) { |
| 2198 | fprintf(output, "NodeSet == NULL !\n"); |
| 2199 | return; |
| 2200 | } |
| 2201 | if (obj->nodeNr == 0) { |
| 2202 | fprintf(output, "NodeSet is empty\n"); |
| 2203 | return; |
| 2204 | } |
| 2205 | if (obj->nodeTab == NULL) { |
| 2206 | fprintf(output, " nodeTab == NULL !\n"); |
| 2207 | return; |
| 2208 | } |
| 2209 | for (i = 0; i < obj->nodeNr; i++) { |
| 2210 | if (obj->nodeTab[i] == NULL) { |
| 2211 | fprintf(output, " NULL !\n"); |
| 2212 | return; |
| 2213 | } |
| 2214 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2215 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2216 | fprintf(output, " /"); |
| 2217 | else if (obj->nodeTab[i]->name == NULL) |
| 2218 | fprintf(output, " noname!"); |
| 2219 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2220 | } |
| 2221 | fprintf(output, "\n"); |
| 2222 | } |
| 2223 | #endif |
| 2224 | |
| 2225 | /** |
| 2226 | * xmlXPathNewNodeSet: |
| 2227 | * @val: the NodePtr value |
| 2228 | * |
| 2229 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2230 | * it with the single Node @val |
| 2231 | * |
| 2232 | * Returns the newly created object. |
| 2233 | */ |
| 2234 | xmlXPathObjectPtr |
| 2235 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2236 | xmlXPathObjectPtr ret; |
| 2237 | |
| 2238 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2239 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2240 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2241 | return(NULL); |
| 2242 | } |
| 2243 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2244 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2245 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2246 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2247 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2248 | return(ret); |
| 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * xmlXPathNewValueTree: |
| 2253 | * @val: the NodePtr value |
| 2254 | * |
| 2255 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2256 | * it with the tree root @val |
| 2257 | * |
| 2258 | * Returns the newly created object. |
| 2259 | */ |
| 2260 | xmlXPathObjectPtr |
| 2261 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2262 | xmlXPathObjectPtr ret; |
| 2263 | |
| 2264 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2265 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2266 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2267 | return(NULL); |
| 2268 | } |
| 2269 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2270 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2271 | ret->boolval = 1; |
| 2272 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2273 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2274 | return(ret); |
| 2275 | } |
| 2276 | |
| 2277 | /** |
| 2278 | * xmlXPathNewNodeSetList: |
| 2279 | * @val: an existing NodeSet |
| 2280 | * |
| 2281 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2282 | * it with the Nodeset @val |
| 2283 | * |
| 2284 | * Returns the newly created object. |
| 2285 | */ |
| 2286 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2287 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2288 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2289 | xmlXPathObjectPtr ret; |
| 2290 | int i; |
| 2291 | |
| 2292 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2293 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2294 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2295 | ret = xmlXPathNewNodeSet(NULL); |
| 2296 | else { |
| 2297 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2298 | for (i = 1; i < val->nodeNr; ++i) |
| 2299 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2300 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2301 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2302 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * xmlXPathWrapNodeSet: |
| 2307 | * @val: the NodePtr value |
| 2308 | * |
| 2309 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2310 | * |
| 2311 | * Returns the newly created object. |
| 2312 | */ |
| 2313 | xmlXPathObjectPtr |
| 2314 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2315 | xmlXPathObjectPtr ret; |
| 2316 | |
| 2317 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2318 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2319 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2320 | return(NULL); |
| 2321 | } |
| 2322 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2323 | ret->type = XPATH_NODESET; |
| 2324 | ret->nodesetval = val; |
| 2325 | return(ret); |
| 2326 | } |
| 2327 | |
| 2328 | /** |
| 2329 | * xmlXPathFreeNodeSetList: |
| 2330 | * @obj: an existing NodeSetList object |
| 2331 | * |
| 2332 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2333 | * the list contrary to xmlXPathFreeObject(). |
| 2334 | */ |
| 2335 | void |
| 2336 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2337 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2338 | xmlFree(obj); |
| 2339 | } |
| 2340 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2341 | /** |
| 2342 | * xmlXPathDifference: |
| 2343 | * @nodes1: a node-set |
| 2344 | * @nodes2: a node-set |
| 2345 | * |
| 2346 | * Implements the EXSLT - Sets difference() function: |
| 2347 | * node-set set:difference (node-set, node-set) |
| 2348 | * |
| 2349 | * Returns the difference between the two node sets, or nodes1 if |
| 2350 | * nodes2 is empty |
| 2351 | */ |
| 2352 | xmlNodeSetPtr |
| 2353 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2354 | xmlNodeSetPtr ret; |
| 2355 | int i, l1; |
| 2356 | xmlNodePtr cur; |
| 2357 | |
| 2358 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2359 | return(nodes1); |
| 2360 | |
| 2361 | ret = xmlXPathNodeSetCreate(NULL); |
| 2362 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2363 | return(ret); |
| 2364 | |
| 2365 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2366 | |
| 2367 | for (i = 0; i < l1; i++) { |
| 2368 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2369 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2370 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2371 | } |
| 2372 | return(ret); |
| 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * xmlXPathIntersection: |
| 2377 | * @nodes1: a node-set |
| 2378 | * @nodes2: a node-set |
| 2379 | * |
| 2380 | * Implements the EXSLT - Sets intersection() function: |
| 2381 | * node-set set:intersection (node-set, node-set) |
| 2382 | * |
| 2383 | * Returns a node set comprising the nodes that are within both the |
| 2384 | * node sets passed as arguments |
| 2385 | */ |
| 2386 | xmlNodeSetPtr |
| 2387 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2388 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2389 | int i, l1; |
| 2390 | xmlNodePtr cur; |
| 2391 | |
| 2392 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2393 | return(ret); |
| 2394 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2395 | return(ret); |
| 2396 | |
| 2397 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2398 | |
| 2399 | for (i = 0; i < l1; i++) { |
| 2400 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2401 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2402 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2403 | } |
| 2404 | return(ret); |
| 2405 | } |
| 2406 | |
| 2407 | /** |
| 2408 | * xmlXPathDistinctSorted: |
| 2409 | * @nodes: a node-set, sorted by document order |
| 2410 | * |
| 2411 | * Implements the EXSLT - Sets distinct() function: |
| 2412 | * node-set set:distinct (node-set) |
| 2413 | * |
| 2414 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2415 | * it is empty |
| 2416 | */ |
| 2417 | xmlNodeSetPtr |
| 2418 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2419 | xmlNodeSetPtr ret; |
| 2420 | xmlHashTablePtr hash; |
| 2421 | int i, l; |
| 2422 | xmlChar * strval; |
| 2423 | xmlNodePtr cur; |
| 2424 | |
| 2425 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2426 | return(nodes); |
| 2427 | |
| 2428 | ret = xmlXPathNodeSetCreate(NULL); |
| 2429 | l = xmlXPathNodeSetGetLength(nodes); |
| 2430 | hash = xmlHashCreate (l); |
| 2431 | for (i = 0; i < l; i++) { |
| 2432 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2433 | strval = xmlXPathCastNodeToString(cur); |
| 2434 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2435 | xmlHashAddEntry(hash, strval, strval); |
| 2436 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2437 | } else { |
| 2438 | xmlFree(strval); |
| 2439 | } |
| 2440 | } |
| 2441 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2442 | return(ret); |
| 2443 | } |
| 2444 | |
| 2445 | /** |
| 2446 | * xmlXPathDistinct: |
| 2447 | * @nodes: a node-set |
| 2448 | * |
| 2449 | * Implements the EXSLT - Sets distinct() function: |
| 2450 | * node-set set:distinct (node-set) |
| 2451 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2452 | * is called with the sorted node-set |
| 2453 | * |
| 2454 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2455 | * it is empty |
| 2456 | */ |
| 2457 | xmlNodeSetPtr |
| 2458 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2459 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2460 | return(nodes); |
| 2461 | |
| 2462 | xmlXPathNodeSetSort(nodes); |
| 2463 | return(xmlXPathDistinctSorted(nodes)); |
| 2464 | } |
| 2465 | |
| 2466 | /** |
| 2467 | * xmlXPathHasSameNodes: |
| 2468 | * @nodes1: a node-set |
| 2469 | * @nodes2: a node-set |
| 2470 | * |
| 2471 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2472 | * boolean set:has-same-node(node-set, node-set) |
| 2473 | * |
| 2474 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2475 | * otherwise |
| 2476 | */ |
| 2477 | int |
| 2478 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2479 | int i, l; |
| 2480 | xmlNodePtr cur; |
| 2481 | |
| 2482 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2483 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2484 | return(0); |
| 2485 | |
| 2486 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2487 | for (i = 0; i < l; i++) { |
| 2488 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2489 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2490 | return(1); |
| 2491 | } |
| 2492 | return(0); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
| 2496 | * xmlXPathNodeLeadingSorted: |
| 2497 | * @nodes: a node-set, sorted by document order |
| 2498 | * @node: a node |
| 2499 | * |
| 2500 | * Implements the EXSLT - Sets leading() function: |
| 2501 | * node-set set:leading (node-set, node-set) |
| 2502 | * |
| 2503 | * Returns the nodes in @nodes that precede @node in document order, |
| 2504 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2505 | * doesn't contain @node |
| 2506 | */ |
| 2507 | xmlNodeSetPtr |
| 2508 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2509 | int i, l; |
| 2510 | xmlNodePtr cur; |
| 2511 | xmlNodeSetPtr ret; |
| 2512 | |
| 2513 | if (node == NULL) |
| 2514 | return(nodes); |
| 2515 | |
| 2516 | ret = xmlXPathNodeSetCreate(NULL); |
| 2517 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2518 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2519 | return(ret); |
| 2520 | |
| 2521 | l = xmlXPathNodeSetGetLength(nodes); |
| 2522 | for (i = 0; i < l; i++) { |
| 2523 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2524 | if (cur == node) |
| 2525 | break; |
| 2526 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2527 | } |
| 2528 | return(ret); |
| 2529 | } |
| 2530 | |
| 2531 | /** |
| 2532 | * xmlXPathNodeLeading: |
| 2533 | * @nodes: a node-set |
| 2534 | * @node: a node |
| 2535 | * |
| 2536 | * Implements the EXSLT - Sets leading() function: |
| 2537 | * node-set set:leading (node-set, node-set) |
| 2538 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2539 | * is called. |
| 2540 | * |
| 2541 | * Returns the nodes in @nodes that precede @node in document order, |
| 2542 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2543 | * doesn't contain @node |
| 2544 | */ |
| 2545 | xmlNodeSetPtr |
| 2546 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2547 | xmlXPathNodeSetSort(nodes); |
| 2548 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2549 | } |
| 2550 | |
| 2551 | /** |
| 2552 | * xmlXPathLeadingSorted: |
| 2553 | * @nodes1: a node-set, sorted by document order |
| 2554 | * @nodes2: a node-set, sorted by document order |
| 2555 | * |
| 2556 | * Implements the EXSLT - Sets leading() function: |
| 2557 | * node-set set:leading (node-set, node-set) |
| 2558 | * |
| 2559 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2560 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2561 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2562 | */ |
| 2563 | xmlNodeSetPtr |
| 2564 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2565 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2566 | return(nodes1); |
| 2567 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2568 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2569 | } |
| 2570 | |
| 2571 | /** |
| 2572 | * xmlXPathLeading: |
| 2573 | * @nodes1: a node-set |
| 2574 | * @nodes2: a node-set |
| 2575 | * |
| 2576 | * Implements the EXSLT - Sets leading() function: |
| 2577 | * node-set set:leading (node-set, node-set) |
| 2578 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2579 | * #exslSetsLeadingSorted is called. |
| 2580 | * |
| 2581 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2582 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2583 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2584 | */ |
| 2585 | xmlNodeSetPtr |
| 2586 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2587 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2588 | return(nodes1); |
| 2589 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2590 | return(xmlXPathNodeSetCreate(NULL)); |
| 2591 | xmlXPathNodeSetSort(nodes1); |
| 2592 | xmlXPathNodeSetSort(nodes2); |
| 2593 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2594 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2595 | } |
| 2596 | |
| 2597 | /** |
| 2598 | * xmlXPathNodeTrailingSorted: |
| 2599 | * @nodes: a node-set, sorted by document order |
| 2600 | * @node: a node |
| 2601 | * |
| 2602 | * Implements the EXSLT - Sets trailing() function: |
| 2603 | * node-set set:trailing (node-set, node-set) |
| 2604 | * |
| 2605 | * Returns the nodes in @nodes that follow @node in document order, |
| 2606 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2607 | * doesn't contain @node |
| 2608 | */ |
| 2609 | xmlNodeSetPtr |
| 2610 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2611 | int i, l; |
| 2612 | xmlNodePtr cur; |
| 2613 | xmlNodeSetPtr ret; |
| 2614 | |
| 2615 | if (node == NULL) |
| 2616 | return(nodes); |
| 2617 | |
| 2618 | ret = xmlXPathNodeSetCreate(NULL); |
| 2619 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2620 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2621 | return(ret); |
| 2622 | |
| 2623 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2624 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2625 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2626 | if (cur == node) |
| 2627 | break; |
| 2628 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2629 | } |
| 2630 | return(ret); |
| 2631 | } |
| 2632 | |
| 2633 | /** |
| 2634 | * xmlXPathNodeTrailing: |
| 2635 | * @nodes: a node-set |
| 2636 | * @node: a node |
| 2637 | * |
| 2638 | * Implements the EXSLT - Sets trailing() function: |
| 2639 | * node-set set:trailing (node-set, node-set) |
| 2640 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2641 | * is called. |
| 2642 | * |
| 2643 | * Returns the nodes in @nodes that follow @node in document order, |
| 2644 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2645 | * doesn't contain @node |
| 2646 | */ |
| 2647 | xmlNodeSetPtr |
| 2648 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2649 | xmlXPathNodeSetSort(nodes); |
| 2650 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2651 | } |
| 2652 | |
| 2653 | /** |
| 2654 | * xmlXPathTrailingSorted: |
| 2655 | * @nodes1: a node-set, sorted by document order |
| 2656 | * @nodes2: a node-set, sorted by document order |
| 2657 | * |
| 2658 | * Implements the EXSLT - Sets trailing() function: |
| 2659 | * node-set set:trailing (node-set, node-set) |
| 2660 | * |
| 2661 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2662 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2663 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2664 | */ |
| 2665 | xmlNodeSetPtr |
| 2666 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2667 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2668 | return(nodes1); |
| 2669 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2670 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2671 | } |
| 2672 | |
| 2673 | /** |
| 2674 | * xmlXPathTrailing: |
| 2675 | * @nodes1: a node-set |
| 2676 | * @nodes2: a node-set |
| 2677 | * |
| 2678 | * Implements the EXSLT - Sets trailing() function: |
| 2679 | * node-set set:trailing (node-set, node-set) |
| 2680 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2681 | * #xmlXPathTrailingSorted is called. |
| 2682 | * |
| 2683 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2684 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2685 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2686 | */ |
| 2687 | xmlNodeSetPtr |
| 2688 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2689 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2690 | return(nodes1); |
| 2691 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2692 | return(xmlXPathNodeSetCreate(NULL)); |
| 2693 | xmlXPathNodeSetSort(nodes1); |
| 2694 | xmlXPathNodeSetSort(nodes2); |
| 2695 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2696 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2697 | } |
| 2698 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2699 | /************************************************************************ |
| 2700 | * * |
| 2701 | * Routines to handle extra functions * |
| 2702 | * * |
| 2703 | ************************************************************************/ |
| 2704 | |
| 2705 | /** |
| 2706 | * xmlXPathRegisterFunc: |
| 2707 | * @ctxt: the XPath context |
| 2708 | * @name: the function name |
| 2709 | * @f: the function implementation or NULL |
| 2710 | * |
| 2711 | * Register a new function. If @f is NULL it unregisters the function |
| 2712 | * |
| 2713 | * Returns 0 in case of success, -1 in case of error |
| 2714 | */ |
| 2715 | int |
| 2716 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2717 | xmlXPathFunction f) { |
| 2718 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * xmlXPathRegisterFuncNS: |
| 2723 | * @ctxt: the XPath context |
| 2724 | * @name: the function name |
| 2725 | * @ns_uri: the function namespace URI |
| 2726 | * @f: the function implementation or NULL |
| 2727 | * |
| 2728 | * Register a new function. If @f is NULL it unregisters the function |
| 2729 | * |
| 2730 | * Returns 0 in case of success, -1 in case of error |
| 2731 | */ |
| 2732 | int |
| 2733 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2734 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2735 | if (ctxt == NULL) |
| 2736 | return(-1); |
| 2737 | if (name == NULL) |
| 2738 | return(-1); |
| 2739 | |
| 2740 | if (ctxt->funcHash == NULL) |
| 2741 | ctxt->funcHash = xmlHashCreate(0); |
| 2742 | if (ctxt->funcHash == NULL) |
| 2743 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2744 | if (f == NULL) |
| 2745 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2746 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
| 2747 | } |
| 2748 | |
| 2749 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2750 | * xmlXPathRegisterFuncLookup: |
| 2751 | * @ctxt: the XPath context |
| 2752 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2753 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2754 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2755 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2756 | */ |
| 2757 | void |
| 2758 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2759 | xmlXPathFuncLookupFunc f, |
| 2760 | void *funcCtxt) { |
| 2761 | if (ctxt == NULL) |
| 2762 | return; |
| 2763 | ctxt->funcLookupFunc = (void *) f; |
| 2764 | ctxt->funcLookupData = funcCtxt; |
| 2765 | } |
| 2766 | |
| 2767 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2768 | * xmlXPathFunctionLookup: |
| 2769 | * @ctxt: the XPath context |
| 2770 | * @name: the function name |
| 2771 | * |
| 2772 | * Search in the Function array of the context for the given |
| 2773 | * function. |
| 2774 | * |
| 2775 | * Returns the xmlXPathFunction or NULL if not found |
| 2776 | */ |
| 2777 | xmlXPathFunction |
| 2778 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2779 | if (ctxt == NULL) |
| 2780 | return (NULL); |
| 2781 | |
| 2782 | if (ctxt->funcLookupFunc != NULL) { |
| 2783 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2784 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2785 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2786 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2787 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2788 | if (ret != NULL) |
| 2789 | return(ret); |
| 2790 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2791 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2792 | } |
| 2793 | |
| 2794 | /** |
| 2795 | * xmlXPathFunctionLookupNS: |
| 2796 | * @ctxt: the XPath context |
| 2797 | * @name: the function name |
| 2798 | * @ns_uri: the function namespace URI |
| 2799 | * |
| 2800 | * Search in the Function array of the context for the given |
| 2801 | * function. |
| 2802 | * |
| 2803 | * Returns the xmlXPathFunction or NULL if not found |
| 2804 | */ |
| 2805 | xmlXPathFunction |
| 2806 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2807 | const xmlChar *ns_uri) { |
| 2808 | if (ctxt == NULL) |
| 2809 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2810 | if (name == NULL) |
| 2811 | return(NULL); |
| 2812 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2813 | if (ctxt->funcLookupFunc != NULL) { |
| 2814 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2815 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2816 | |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2817 | f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2818 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2819 | if (ret != NULL) |
| 2820 | return(ret); |
| 2821 | } |
| 2822 | |
| 2823 | if (ctxt->funcHash == NULL) |
| 2824 | return(NULL); |
| 2825 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2826 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
| 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * xmlXPathRegisteredFuncsCleanup: |
| 2831 | * @ctxt: the XPath context |
| 2832 | * |
| 2833 | * Cleanup the XPath context data associated to registered functions |
| 2834 | */ |
| 2835 | void |
| 2836 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2837 | if (ctxt == NULL) |
| 2838 | return; |
| 2839 | |
| 2840 | xmlHashFree(ctxt->funcHash, NULL); |
| 2841 | ctxt->funcHash = NULL; |
| 2842 | } |
| 2843 | |
| 2844 | /************************************************************************ |
| 2845 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2846 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2847 | * * |
| 2848 | ************************************************************************/ |
| 2849 | |
| 2850 | /** |
| 2851 | * xmlXPathRegisterVariable: |
| 2852 | * @ctxt: the XPath context |
| 2853 | * @name: the variable name |
| 2854 | * @value: the variable value or NULL |
| 2855 | * |
| 2856 | * Register a new variable value. If @value is NULL it unregisters |
| 2857 | * the variable |
| 2858 | * |
| 2859 | * Returns 0 in case of success, -1 in case of error |
| 2860 | */ |
| 2861 | int |
| 2862 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2863 | xmlXPathObjectPtr value) { |
| 2864 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2865 | } |
| 2866 | |
| 2867 | /** |
| 2868 | * xmlXPathRegisterVariableNS: |
| 2869 | * @ctxt: the XPath context |
| 2870 | * @name: the variable name |
| 2871 | * @ns_uri: the variable namespace URI |
| 2872 | * @value: the variable value or NULL |
| 2873 | * |
| 2874 | * Register a new variable value. If @value is NULL it unregisters |
| 2875 | * the variable |
| 2876 | * |
| 2877 | * Returns 0 in case of success, -1 in case of error |
| 2878 | */ |
| 2879 | int |
| 2880 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2881 | const xmlChar *ns_uri, |
| 2882 | xmlXPathObjectPtr value) { |
| 2883 | if (ctxt == NULL) |
| 2884 | return(-1); |
| 2885 | if (name == NULL) |
| 2886 | return(-1); |
| 2887 | |
| 2888 | if (ctxt->varHash == NULL) |
| 2889 | ctxt->varHash = xmlHashCreate(0); |
| 2890 | if (ctxt->varHash == NULL) |
| 2891 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2892 | if (value == NULL) |
| 2893 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 2894 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2895 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2896 | (void *) value, |
| 2897 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * xmlXPathRegisterVariableLookup: |
| 2902 | * @ctxt: the XPath context |
| 2903 | * @f: the lookup function |
| 2904 | * @data: the lookup data |
| 2905 | * |
| 2906 | * register an external mechanism to do variable lookup |
| 2907 | */ |
| 2908 | void |
| 2909 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 2910 | xmlXPathVariableLookupFunc f, void *data) { |
| 2911 | if (ctxt == NULL) |
| 2912 | return; |
| 2913 | ctxt->varLookupFunc = (void *) f; |
| 2914 | ctxt->varLookupData = data; |
| 2915 | } |
| 2916 | |
| 2917 | /** |
| 2918 | * xmlXPathVariableLookup: |
| 2919 | * @ctxt: the XPath context |
| 2920 | * @name: the variable name |
| 2921 | * |
| 2922 | * Search in the Variable array of the context for the given |
| 2923 | * variable value. |
| 2924 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2925 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2926 | */ |
| 2927 | xmlXPathObjectPtr |
| 2928 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 2929 | if (ctxt == NULL) |
| 2930 | return(NULL); |
| 2931 | |
| 2932 | if (ctxt->varLookupFunc != NULL) { |
| 2933 | xmlXPathObjectPtr ret; |
| 2934 | |
| 2935 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 2936 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 2937 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2938 | } |
| 2939 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 2940 | } |
| 2941 | |
| 2942 | /** |
| 2943 | * xmlXPathVariableLookupNS: |
| 2944 | * @ctxt: the XPath context |
| 2945 | * @name: the variable name |
| 2946 | * @ns_uri: the variable namespace URI |
| 2947 | * |
| 2948 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2949 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2950 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2951 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2952 | */ |
| 2953 | xmlXPathObjectPtr |
| 2954 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2955 | const xmlChar *ns_uri) { |
| 2956 | if (ctxt == NULL) |
| 2957 | return(NULL); |
| 2958 | |
| 2959 | if (ctxt->varLookupFunc != NULL) { |
| 2960 | xmlXPathObjectPtr ret; |
| 2961 | |
| 2962 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 2963 | (ctxt->varLookupData, name, ns_uri); |
| 2964 | if (ret != NULL) return(ret); |
| 2965 | } |
| 2966 | |
| 2967 | if (ctxt->varHash == NULL) |
| 2968 | return(NULL); |
| 2969 | if (name == NULL) |
| 2970 | return(NULL); |
| 2971 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 2972 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 2973 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /** |
| 2977 | * xmlXPathRegisteredVariablesCleanup: |
| 2978 | * @ctxt: the XPath context |
| 2979 | * |
| 2980 | * Cleanup the XPath context data associated to registered variables |
| 2981 | */ |
| 2982 | void |
| 2983 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 2984 | if (ctxt == NULL) |
| 2985 | return; |
| 2986 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2987 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2988 | ctxt->varHash = NULL; |
| 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * xmlXPathRegisterNs: |
| 2993 | * @ctxt: the XPath context |
| 2994 | * @prefix: the namespace prefix |
| 2995 | * @ns_uri: the namespace name |
| 2996 | * |
| 2997 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 2998 | * the namespace |
| 2999 | * |
| 3000 | * Returns 0 in case of success, -1 in case of error |
| 3001 | */ |
| 3002 | int |
| 3003 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3004 | const xmlChar *ns_uri) { |
| 3005 | if (ctxt == NULL) |
| 3006 | return(-1); |
| 3007 | if (prefix == NULL) |
| 3008 | return(-1); |
| 3009 | |
| 3010 | if (ctxt->nsHash == NULL) |
| 3011 | ctxt->nsHash = xmlHashCreate(10); |
| 3012 | if (ctxt->nsHash == NULL) |
| 3013 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3014 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3015 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3016 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3017 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3018 | (xmlHashDeallocator)xmlFree)); |
| 3019 | } |
| 3020 | |
| 3021 | /** |
| 3022 | * xmlXPathNsLookup: |
| 3023 | * @ctxt: the XPath context |
| 3024 | * @prefix: the namespace prefix value |
| 3025 | * |
| 3026 | * Search in the namespace declaration array of the context for the given |
| 3027 | * namespace name associated to the given prefix |
| 3028 | * |
| 3029 | * Returns the value or NULL if not found |
| 3030 | */ |
| 3031 | const xmlChar * |
| 3032 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3033 | if (ctxt == NULL) |
| 3034 | return(NULL); |
| 3035 | if (prefix == NULL) |
| 3036 | return(NULL); |
| 3037 | |
| 3038 | #ifdef XML_XML_NAMESPACE |
| 3039 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3040 | return(XML_XML_NAMESPACE); |
| 3041 | #endif |
| 3042 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3043 | if (ctxt->namespaces != NULL) { |
| 3044 | int i; |
| 3045 | |
| 3046 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3047 | if ((ctxt->namespaces[i] != NULL) && |
| 3048 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3049 | return(ctxt->namespaces[i]->href); |
| 3050 | } |
| 3051 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3052 | |
| 3053 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3054 | } |
| 3055 | |
| 3056 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3057 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | * @ctxt: the XPath context |
| 3059 | * |
| 3060 | * Cleanup the XPath context data associated to registered variables |
| 3061 | */ |
| 3062 | void |
| 3063 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3064 | if (ctxt == NULL) |
| 3065 | return; |
| 3066 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3067 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3068 | ctxt->nsHash = NULL; |
| 3069 | } |
| 3070 | |
| 3071 | /************************************************************************ |
| 3072 | * * |
| 3073 | * Routines to handle Values * |
| 3074 | * * |
| 3075 | ************************************************************************/ |
| 3076 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3077 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3078 | |
| 3079 | /** |
| 3080 | * xmlXPathNewFloat: |
| 3081 | * @val: the double value |
| 3082 | * |
| 3083 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3084 | * |
| 3085 | * Returns the newly created object. |
| 3086 | */ |
| 3087 | xmlXPathObjectPtr |
| 3088 | xmlXPathNewFloat(double val) { |
| 3089 | xmlXPathObjectPtr ret; |
| 3090 | |
| 3091 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3092 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3093 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3094 | return(NULL); |
| 3095 | } |
| 3096 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3097 | ret->type = XPATH_NUMBER; |
| 3098 | ret->floatval = val; |
| 3099 | return(ret); |
| 3100 | } |
| 3101 | |
| 3102 | /** |
| 3103 | * xmlXPathNewBoolean: |
| 3104 | * @val: the boolean value |
| 3105 | * |
| 3106 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3107 | * |
| 3108 | * Returns the newly created object. |
| 3109 | */ |
| 3110 | xmlXPathObjectPtr |
| 3111 | xmlXPathNewBoolean(int val) { |
| 3112 | xmlXPathObjectPtr ret; |
| 3113 | |
| 3114 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3115 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3116 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | return(NULL); |
| 3118 | } |
| 3119 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3120 | ret->type = XPATH_BOOLEAN; |
| 3121 | ret->boolval = (val != 0); |
| 3122 | return(ret); |
| 3123 | } |
| 3124 | |
| 3125 | /** |
| 3126 | * xmlXPathNewString: |
| 3127 | * @val: the xmlChar * value |
| 3128 | * |
| 3129 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3130 | * |
| 3131 | * Returns the newly created object. |
| 3132 | */ |
| 3133 | xmlXPathObjectPtr |
| 3134 | xmlXPathNewString(const xmlChar *val) { |
| 3135 | xmlXPathObjectPtr ret; |
| 3136 | |
| 3137 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3138 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3139 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3140 | return(NULL); |
| 3141 | } |
| 3142 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3143 | ret->type = XPATH_STRING; |
| 3144 | if (val != NULL) |
| 3145 | ret->stringval = xmlStrdup(val); |
| 3146 | else |
| 3147 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3148 | return(ret); |
| 3149 | } |
| 3150 | |
| 3151 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3152 | * xmlXPathWrapString: |
| 3153 | * @val: the xmlChar * value |
| 3154 | * |
| 3155 | * Wraps the @val string into an XPath object. |
| 3156 | * |
| 3157 | * Returns the newly created object. |
| 3158 | */ |
| 3159 | xmlXPathObjectPtr |
| 3160 | xmlXPathWrapString (xmlChar *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 string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3166 | return(NULL); |
| 3167 | } |
| 3168 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3169 | ret->type = XPATH_STRING; |
| 3170 | ret->stringval = val; |
| 3171 | return(ret); |
| 3172 | } |
| 3173 | |
| 3174 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3175 | * xmlXPathNewCString: |
| 3176 | * @val: the char * 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 | xmlXPathNewCString(const char *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 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3194 | return(ret); |
| 3195 | } |
| 3196 | |
| 3197 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3198 | * xmlXPathWrapCString: |
| 3199 | * @val: the char * value |
| 3200 | * |
| 3201 | * Wraps a string into an XPath object. |
| 3202 | * |
| 3203 | * Returns the newly created object. |
| 3204 | */ |
| 3205 | xmlXPathObjectPtr |
| 3206 | xmlXPathWrapCString (char * val) { |
| 3207 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3208 | } |
| 3209 | |
| 3210 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3211 | * xmlXPathWrapExternal: |
| 3212 | * @val: the user data |
| 3213 | * |
| 3214 | * Wraps the @val data into an XPath object. |
| 3215 | * |
| 3216 | * Returns the newly created object. |
| 3217 | */ |
| 3218 | xmlXPathObjectPtr |
| 3219 | xmlXPathWrapExternal (void *val) { |
| 3220 | xmlXPathObjectPtr ret; |
| 3221 | |
| 3222 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3223 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3224 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3225 | return(NULL); |
| 3226 | } |
| 3227 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3228 | ret->type = XPATH_USERS; |
| 3229 | ret->user = val; |
| 3230 | return(ret); |
| 3231 | } |
| 3232 | |
| 3233 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3234 | * xmlXPathObjectCopy: |
| 3235 | * @val: the original object |
| 3236 | * |
| 3237 | * allocate a new copy of a given object |
| 3238 | * |
| 3239 | * Returns the newly created object. |
| 3240 | */ |
| 3241 | xmlXPathObjectPtr |
| 3242 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3243 | xmlXPathObjectPtr ret; |
| 3244 | |
| 3245 | if (val == NULL) |
| 3246 | return(NULL); |
| 3247 | |
| 3248 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3249 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3250 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3251 | return(NULL); |
| 3252 | } |
| 3253 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3254 | switch (val->type) { |
| 3255 | case XPATH_BOOLEAN: |
| 3256 | case XPATH_NUMBER: |
| 3257 | case XPATH_POINT: |
| 3258 | case XPATH_RANGE: |
| 3259 | break; |
| 3260 | case XPATH_STRING: |
| 3261 | ret->stringval = xmlStrdup(val->stringval); |
| 3262 | break; |
| 3263 | case XPATH_XSLT_TREE: |
| 3264 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3265 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3266 | xmlNodePtr cur, tmp; |
| 3267 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3268 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3269 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3270 | top = xmlNewDoc(NULL); |
| 3271 | top->name = (char *) |
| 3272 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3273 | ret->user = top; |
| 3274 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3275 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3276 | cur = val->nodesetval->nodeTab[0]->children; |
| 3277 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3278 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3279 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3280 | cur = cur->next; |
| 3281 | } |
| 3282 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3283 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3284 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3285 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3286 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3287 | break; |
| 3288 | case XPATH_NODESET: |
| 3289 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3290 | /* Do not deallocate the copied tree value */ |
| 3291 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3292 | break; |
| 3293 | case XPATH_LOCATIONSET: |
| 3294 | #ifdef LIBXML_XPTR_ENABLED |
| 3295 | { |
| 3296 | xmlLocationSetPtr loc = val->user; |
| 3297 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3298 | break; |
| 3299 | } |
| 3300 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3301 | case XPATH_USERS: |
| 3302 | ret->user = val->user; |
| 3303 | break; |
| 3304 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3305 | xmlGenericError(xmlGenericErrorContext, |
| 3306 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3307 | val->type); |
| 3308 | break; |
| 3309 | } |
| 3310 | return(ret); |
| 3311 | } |
| 3312 | |
| 3313 | /** |
| 3314 | * xmlXPathFreeObject: |
| 3315 | * @obj: the object to free |
| 3316 | * |
| 3317 | * Free up an xmlXPathObjectPtr object. |
| 3318 | */ |
| 3319 | void |
| 3320 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3321 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3322 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3323 | if (obj->boolval) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3324 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3325 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3326 | xmlFreeNodeList((xmlNodePtr) obj->user); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3327 | } else if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3328 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3329 | } else { |
| 3330 | if (obj->nodesetval != NULL) |
| 3331 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3332 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3333 | #ifdef LIBXML_XPTR_ENABLED |
| 3334 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3335 | if (obj->user != NULL) |
| 3336 | xmlXPtrFreeLocationSet(obj->user); |
| 3337 | #endif |
| 3338 | } else if (obj->type == XPATH_STRING) { |
| 3339 | if (obj->stringval != NULL) |
| 3340 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3343 | xmlFree(obj); |
| 3344 | } |
| 3345 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3346 | |
| 3347 | /************************************************************************ |
| 3348 | * * |
| 3349 | * Type Casting Routines * |
| 3350 | * * |
| 3351 | ************************************************************************/ |
| 3352 | |
| 3353 | /** |
| 3354 | * xmlXPathCastBooleanToString: |
| 3355 | * @val: a boolean |
| 3356 | * |
| 3357 | * Converts a boolean to its string value. |
| 3358 | * |
| 3359 | * Returns a newly allocated string. |
| 3360 | */ |
| 3361 | xmlChar * |
| 3362 | xmlXPathCastBooleanToString (int val) { |
| 3363 | xmlChar *ret; |
| 3364 | if (val) |
| 3365 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3366 | else |
| 3367 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3368 | return(ret); |
| 3369 | } |
| 3370 | |
| 3371 | /** |
| 3372 | * xmlXPathCastNumberToString: |
| 3373 | * @val: a number |
| 3374 | * |
| 3375 | * Converts a number to its string value. |
| 3376 | * |
| 3377 | * Returns a newly allocated string. |
| 3378 | */ |
| 3379 | xmlChar * |
| 3380 | xmlXPathCastNumberToString (double val) { |
| 3381 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3382 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3383 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3384 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3385 | break; |
| 3386 | case -1: |
| 3387 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3388 | break; |
| 3389 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3390 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3391 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3392 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3393 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3394 | } else { |
| 3395 | /* could be improved */ |
| 3396 | char buf[100]; |
| 3397 | xmlXPathFormatNumber(val, buf, 100); |
| 3398 | ret = xmlStrdup((const xmlChar *) buf); |
| 3399 | } |
| 3400 | } |
| 3401 | return(ret); |
| 3402 | } |
| 3403 | |
| 3404 | /** |
| 3405 | * xmlXPathCastNodeToString: |
| 3406 | * @node: a node |
| 3407 | * |
| 3408 | * Converts a node to its string value. |
| 3409 | * |
| 3410 | * Returns a newly allocated string. |
| 3411 | */ |
| 3412 | xmlChar * |
| 3413 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3414 | return(xmlNodeGetContent(node)); |
| 3415 | } |
| 3416 | |
| 3417 | /** |
| 3418 | * xmlXPathCastNodeSetToString: |
| 3419 | * @ns: a node-set |
| 3420 | * |
| 3421 | * Converts a node-set to its string value. |
| 3422 | * |
| 3423 | * Returns a newly allocated string. |
| 3424 | */ |
| 3425 | xmlChar * |
| 3426 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3427 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3428 | return(xmlStrdup((const xmlChar *) "")); |
| 3429 | |
| 3430 | xmlXPathNodeSetSort(ns); |
| 3431 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3432 | } |
| 3433 | |
| 3434 | /** |
| 3435 | * xmlXPathCastToString: |
| 3436 | * @val: an XPath object |
| 3437 | * |
| 3438 | * Converts an existing object to its string() equivalent |
| 3439 | * |
| 3440 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3441 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3442 | * string object). |
| 3443 | */ |
| 3444 | xmlChar * |
| 3445 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3446 | xmlChar *ret = NULL; |
| 3447 | |
| 3448 | if (val == NULL) |
| 3449 | return(xmlStrdup((const xmlChar *) "")); |
| 3450 | switch (val->type) { |
| 3451 | case XPATH_UNDEFINED: |
| 3452 | #ifdef DEBUG_EXPR |
| 3453 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3454 | #endif |
| 3455 | ret = xmlStrdup((const xmlChar *) ""); |
| 3456 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3457 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3458 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3459 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3460 | break; |
| 3461 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3462 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3463 | case XPATH_BOOLEAN: |
| 3464 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3465 | break; |
| 3466 | case XPATH_NUMBER: { |
| 3467 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3468 | break; |
| 3469 | } |
| 3470 | case XPATH_USERS: |
| 3471 | case XPATH_POINT: |
| 3472 | case XPATH_RANGE: |
| 3473 | case XPATH_LOCATIONSET: |
| 3474 | TODO |
| 3475 | ret = xmlStrdup((const xmlChar *) ""); |
| 3476 | break; |
| 3477 | } |
| 3478 | return(ret); |
| 3479 | } |
| 3480 | |
| 3481 | /** |
| 3482 | * xmlXPathConvertString: |
| 3483 | * @val: an XPath object |
| 3484 | * |
| 3485 | * Converts an existing object to its string() equivalent |
| 3486 | * |
| 3487 | * Returns the new object, the old one is freed (or the operation |
| 3488 | * is done directly on @val) |
| 3489 | */ |
| 3490 | xmlXPathObjectPtr |
| 3491 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3492 | xmlChar *res = NULL; |
| 3493 | |
| 3494 | if (val == NULL) |
| 3495 | return(xmlXPathNewCString("")); |
| 3496 | |
| 3497 | switch (val->type) { |
| 3498 | case XPATH_UNDEFINED: |
| 3499 | #ifdef DEBUG_EXPR |
| 3500 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3501 | #endif |
| 3502 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3503 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3504 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3505 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3506 | break; |
| 3507 | case XPATH_STRING: |
| 3508 | return(val); |
| 3509 | case XPATH_BOOLEAN: |
| 3510 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3511 | break; |
| 3512 | case XPATH_NUMBER: |
| 3513 | res = xmlXPathCastNumberToString(val->floatval); |
| 3514 | break; |
| 3515 | case XPATH_USERS: |
| 3516 | case XPATH_POINT: |
| 3517 | case XPATH_RANGE: |
| 3518 | case XPATH_LOCATIONSET: |
| 3519 | TODO; |
| 3520 | break; |
| 3521 | } |
| 3522 | xmlXPathFreeObject(val); |
| 3523 | if (res == NULL) |
| 3524 | return(xmlXPathNewCString("")); |
| 3525 | return(xmlXPathWrapString(res)); |
| 3526 | } |
| 3527 | |
| 3528 | /** |
| 3529 | * xmlXPathCastBooleanToNumber: |
| 3530 | * @val: a boolean |
| 3531 | * |
| 3532 | * Converts a boolean to its number value |
| 3533 | * |
| 3534 | * Returns the number value |
| 3535 | */ |
| 3536 | double |
| 3537 | xmlXPathCastBooleanToNumber(int val) { |
| 3538 | if (val) |
| 3539 | return(1.0); |
| 3540 | return(0.0); |
| 3541 | } |
| 3542 | |
| 3543 | /** |
| 3544 | * xmlXPathCastStringToNumber: |
| 3545 | * @val: a string |
| 3546 | * |
| 3547 | * Converts a string to its number value |
| 3548 | * |
| 3549 | * Returns the number value |
| 3550 | */ |
| 3551 | double |
| 3552 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3553 | return(xmlXPathStringEvalNumber(val)); |
| 3554 | } |
| 3555 | |
| 3556 | /** |
| 3557 | * xmlXPathCastNodeToNumber: |
| 3558 | * @node: a node |
| 3559 | * |
| 3560 | * Converts a node to its number value |
| 3561 | * |
| 3562 | * Returns the number value |
| 3563 | */ |
| 3564 | double |
| 3565 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3566 | xmlChar *strval; |
| 3567 | double ret; |
| 3568 | |
| 3569 | if (node == NULL) |
| 3570 | return(xmlXPathNAN); |
| 3571 | strval = xmlXPathCastNodeToString(node); |
| 3572 | if (strval == NULL) |
| 3573 | return(xmlXPathNAN); |
| 3574 | ret = xmlXPathCastStringToNumber(strval); |
| 3575 | xmlFree(strval); |
| 3576 | |
| 3577 | return(ret); |
| 3578 | } |
| 3579 | |
| 3580 | /** |
| 3581 | * xmlXPathCastNodeSetToNumber: |
| 3582 | * @ns: a node-set |
| 3583 | * |
| 3584 | * Converts a node-set to its number value |
| 3585 | * |
| 3586 | * Returns the number value |
| 3587 | */ |
| 3588 | double |
| 3589 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3590 | xmlChar *str; |
| 3591 | double ret; |
| 3592 | |
| 3593 | if (ns == NULL) |
| 3594 | return(xmlXPathNAN); |
| 3595 | str = xmlXPathCastNodeSetToString(ns); |
| 3596 | ret = xmlXPathCastStringToNumber(str); |
| 3597 | xmlFree(str); |
| 3598 | return(ret); |
| 3599 | } |
| 3600 | |
| 3601 | /** |
| 3602 | * xmlXPathCastToNumber: |
| 3603 | * @val: an XPath object |
| 3604 | * |
| 3605 | * Converts an XPath object to its number value |
| 3606 | * |
| 3607 | * Returns the number value |
| 3608 | */ |
| 3609 | double |
| 3610 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3611 | double ret = 0.0; |
| 3612 | |
| 3613 | if (val == NULL) |
| 3614 | return(xmlXPathNAN); |
| 3615 | switch (val->type) { |
| 3616 | case XPATH_UNDEFINED: |
| 3617 | #ifdef DEGUB_EXPR |
| 3618 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3619 | #endif |
| 3620 | ret = xmlXPathNAN; |
| 3621 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3622 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3623 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3624 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3625 | break; |
| 3626 | case XPATH_STRING: |
| 3627 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3628 | break; |
| 3629 | case XPATH_NUMBER: |
| 3630 | ret = val->floatval; |
| 3631 | break; |
| 3632 | case XPATH_BOOLEAN: |
| 3633 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3634 | break; |
| 3635 | case XPATH_USERS: |
| 3636 | case XPATH_POINT: |
| 3637 | case XPATH_RANGE: |
| 3638 | case XPATH_LOCATIONSET: |
| 3639 | TODO; |
| 3640 | ret = xmlXPathNAN; |
| 3641 | break; |
| 3642 | } |
| 3643 | return(ret); |
| 3644 | } |
| 3645 | |
| 3646 | /** |
| 3647 | * xmlXPathConvertNumber: |
| 3648 | * @val: an XPath object |
| 3649 | * |
| 3650 | * Converts an existing object to its number() equivalent |
| 3651 | * |
| 3652 | * Returns the new object, the old one is freed (or the operation |
| 3653 | * is done directly on @val) |
| 3654 | */ |
| 3655 | xmlXPathObjectPtr |
| 3656 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3657 | xmlXPathObjectPtr ret; |
| 3658 | |
| 3659 | if (val == NULL) |
| 3660 | return(xmlXPathNewFloat(0.0)); |
| 3661 | if (val->type == XPATH_NUMBER) |
| 3662 | return(val); |
| 3663 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3664 | xmlXPathFreeObject(val); |
| 3665 | return(ret); |
| 3666 | } |
| 3667 | |
| 3668 | /** |
| 3669 | * xmlXPathCastNumberToBoolean: |
| 3670 | * @val: a number |
| 3671 | * |
| 3672 | * Converts a number to its boolean value |
| 3673 | * |
| 3674 | * Returns the boolean value |
| 3675 | */ |
| 3676 | int |
| 3677 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3678 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3679 | return(0); |
| 3680 | return(1); |
| 3681 | } |
| 3682 | |
| 3683 | /** |
| 3684 | * xmlXPathCastStringToBoolean: |
| 3685 | * @val: a string |
| 3686 | * |
| 3687 | * Converts a string to its boolean value |
| 3688 | * |
| 3689 | * Returns the boolean value |
| 3690 | */ |
| 3691 | int |
| 3692 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3693 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3694 | return(0); |
| 3695 | return(1); |
| 3696 | } |
| 3697 | |
| 3698 | /** |
| 3699 | * xmlXPathCastNodeSetToBoolean: |
| 3700 | * @ns: a node-set |
| 3701 | * |
| 3702 | * Converts a node-set to its boolean value |
| 3703 | * |
| 3704 | * Returns the boolean value |
| 3705 | */ |
| 3706 | int |
| 3707 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3708 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3709 | return(0); |
| 3710 | return(1); |
| 3711 | } |
| 3712 | |
| 3713 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3714 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3715 | * @val: an XPath object |
| 3716 | * |
| 3717 | * Converts an XPath object to its boolean value |
| 3718 | * |
| 3719 | * Returns the boolean value |
| 3720 | */ |
| 3721 | int |
| 3722 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3723 | int ret = 0; |
| 3724 | |
| 3725 | if (val == NULL) |
| 3726 | return(0); |
| 3727 | switch (val->type) { |
| 3728 | case XPATH_UNDEFINED: |
| 3729 | #ifdef DEBUG_EXPR |
| 3730 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3731 | #endif |
| 3732 | ret = 0; |
| 3733 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3734 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3735 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3736 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3737 | break; |
| 3738 | case XPATH_STRING: |
| 3739 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3740 | break; |
| 3741 | case XPATH_NUMBER: |
| 3742 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3743 | break; |
| 3744 | case XPATH_BOOLEAN: |
| 3745 | ret = val->boolval; |
| 3746 | break; |
| 3747 | case XPATH_USERS: |
| 3748 | case XPATH_POINT: |
| 3749 | case XPATH_RANGE: |
| 3750 | case XPATH_LOCATIONSET: |
| 3751 | TODO; |
| 3752 | ret = 0; |
| 3753 | break; |
| 3754 | } |
| 3755 | return(ret); |
| 3756 | } |
| 3757 | |
| 3758 | |
| 3759 | /** |
| 3760 | * xmlXPathConvertBoolean: |
| 3761 | * @val: an XPath object |
| 3762 | * |
| 3763 | * Converts an existing object to its boolean() equivalent |
| 3764 | * |
| 3765 | * Returns the new object, the old one is freed (or the operation |
| 3766 | * is done directly on @val) |
| 3767 | */ |
| 3768 | xmlXPathObjectPtr |
| 3769 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3770 | xmlXPathObjectPtr ret; |
| 3771 | |
| 3772 | if (val == NULL) |
| 3773 | return(xmlXPathNewBoolean(0)); |
| 3774 | if (val->type == XPATH_BOOLEAN) |
| 3775 | return(val); |
| 3776 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3777 | xmlXPathFreeObject(val); |
| 3778 | return(ret); |
| 3779 | } |
| 3780 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3781 | /************************************************************************ |
| 3782 | * * |
| 3783 | * Routines to handle XPath contexts * |
| 3784 | * * |
| 3785 | ************************************************************************/ |
| 3786 | |
| 3787 | /** |
| 3788 | * xmlXPathNewContext: |
| 3789 | * @doc: the XML document |
| 3790 | * |
| 3791 | * Create a new xmlXPathContext |
| 3792 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3793 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3794 | */ |
| 3795 | xmlXPathContextPtr |
| 3796 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3797 | xmlXPathContextPtr ret; |
| 3798 | |
| 3799 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3800 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3801 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3802 | return(NULL); |
| 3803 | } |
| 3804 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3805 | ret->doc = doc; |
| 3806 | ret->node = NULL; |
| 3807 | |
| 3808 | ret->varHash = NULL; |
| 3809 | |
| 3810 | ret->nb_types = 0; |
| 3811 | ret->max_types = 0; |
| 3812 | ret->types = NULL; |
| 3813 | |
| 3814 | ret->funcHash = xmlHashCreate(0); |
| 3815 | |
| 3816 | ret->nb_axis = 0; |
| 3817 | ret->max_axis = 0; |
| 3818 | ret->axis = NULL; |
| 3819 | |
| 3820 | ret->nsHash = NULL; |
| 3821 | ret->user = NULL; |
| 3822 | |
| 3823 | ret->contextSize = -1; |
| 3824 | ret->proximityPosition = -1; |
| 3825 | |
| 3826 | xmlXPathRegisterAllFunctions(ret); |
| 3827 | |
| 3828 | return(ret); |
| 3829 | } |
| 3830 | |
| 3831 | /** |
| 3832 | * xmlXPathFreeContext: |
| 3833 | * @ctxt: the context to free |
| 3834 | * |
| 3835 | * Free up an xmlXPathContext |
| 3836 | */ |
| 3837 | void |
| 3838 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
| 3839 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3840 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3841 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3842 | xmlFree(ctxt); |
| 3843 | } |
| 3844 | |
| 3845 | /************************************************************************ |
| 3846 | * * |
| 3847 | * Routines to handle XPath parser contexts * |
| 3848 | * * |
| 3849 | ************************************************************************/ |
| 3850 | |
| 3851 | #define CHECK_CTXT(ctxt) \ |
| 3852 | if (ctxt == NULL) { \ |
| 3853 | xmlGenericError(xmlGenericErrorContext, \ |
| 3854 | "%s:%d Internal error: ctxt == NULL\n", \ |
| 3855 | __FILE__, __LINE__); \ |
| 3856 | } \ |
| 3857 | |
| 3858 | |
| 3859 | #define CHECK_CONTEXT(ctxt) \ |
| 3860 | if (ctxt == NULL) { \ |
| 3861 | xmlGenericError(xmlGenericErrorContext, \ |
| 3862 | "%s:%d Internal error: no context\n", \ |
| 3863 | __FILE__, __LINE__); \ |
| 3864 | } \ |
| 3865 | else if (ctxt->doc == NULL) { \ |
| 3866 | xmlGenericError(xmlGenericErrorContext, \ |
| 3867 | "%s:%d Internal error: no document\n", \ |
| 3868 | __FILE__, __LINE__); \ |
| 3869 | } \ |
| 3870 | else if (ctxt->doc->children == NULL) { \ |
| 3871 | xmlGenericError(xmlGenericErrorContext, \ |
| 3872 | "%s:%d Internal error: document without root\n", \ |
| 3873 | __FILE__, __LINE__); \ |
| 3874 | } \ |
| 3875 | |
| 3876 | |
| 3877 | /** |
| 3878 | * xmlXPathNewParserContext: |
| 3879 | * @str: the XPath expression |
| 3880 | * @ctxt: the XPath context |
| 3881 | * |
| 3882 | * Create a new xmlXPathParserContext |
| 3883 | * |
| 3884 | * Returns the xmlXPathParserContext just allocated. |
| 3885 | */ |
| 3886 | xmlXPathParserContextPtr |
| 3887 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3888 | xmlXPathParserContextPtr ret; |
| 3889 | |
| 3890 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3891 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3892 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3893 | return(NULL); |
| 3894 | } |
| 3895 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3896 | ret->cur = ret->base = str; |
| 3897 | ret->context = ctxt; |
| 3898 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3899 | ret->comp = xmlXPathNewCompExpr(); |
| 3900 | if (ret->comp == NULL) { |
| 3901 | xmlFree(ret->valueTab); |
| 3902 | xmlFree(ret); |
| 3903 | return(NULL); |
| 3904 | } |
| 3905 | |
| 3906 | return(ret); |
| 3907 | } |
| 3908 | |
| 3909 | /** |
| 3910 | * xmlXPathCompParserContext: |
| 3911 | * @comp: the XPath compiled expression |
| 3912 | * @ctxt: the XPath context |
| 3913 | * |
| 3914 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 3915 | * |
| 3916 | * Returns the xmlXPathParserContext just allocated. |
| 3917 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3918 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3919 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 3920 | xmlXPathParserContextPtr ret; |
| 3921 | |
| 3922 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3923 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3924 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3925 | return(NULL); |
| 3926 | } |
| 3927 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3928 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3929 | /* Allocate the value stack */ |
| 3930 | ret->valueTab = (xmlXPathObjectPtr *) |
| 3931 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3932 | if (ret->valueTab == NULL) { |
| 3933 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3934 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3935 | return(NULL); |
| 3936 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | ret->valueNr = 0; |
| 3938 | ret->valueMax = 10; |
| 3939 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3940 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 3941 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3942 | ret->comp = comp; |
| 3943 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3944 | return(ret); |
| 3945 | } |
| 3946 | |
| 3947 | /** |
| 3948 | * xmlXPathFreeParserContext: |
| 3949 | * @ctxt: the context to free |
| 3950 | * |
| 3951 | * Free up an xmlXPathParserContext |
| 3952 | */ |
| 3953 | void |
| 3954 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 3955 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3956 | xmlFree(ctxt->valueTab); |
| 3957 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3958 | if (ctxt->comp) |
| 3959 | xmlXPathFreeCompExpr(ctxt->comp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3960 | xmlFree(ctxt); |
| 3961 | } |
| 3962 | |
| 3963 | /************************************************************************ |
| 3964 | * * |
| 3965 | * The implicit core function library * |
| 3966 | * * |
| 3967 | ************************************************************************/ |
| 3968 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3969 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3970 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 3971 | * @node: a node pointer |
| 3972 | * |
| 3973 | * Function computing the beginning of the string value of the node, |
| 3974 | * used to speed up comparisons |
| 3975 | * |
| 3976 | * Returns an int usable as a hash |
| 3977 | */ |
| 3978 | static unsigned int |
| 3979 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 3980 | int len = 2; |
| 3981 | const xmlChar * string = NULL; |
| 3982 | xmlNodePtr tmp = NULL; |
| 3983 | unsigned int ret = 0; |
| 3984 | |
| 3985 | if (node == NULL) |
| 3986 | return(0); |
| 3987 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3988 | if (node->type == XML_DOCUMENT_NODE) { |
| 3989 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 3990 | if (tmp == NULL) |
| 3991 | node = node->children; |
| 3992 | else |
| 3993 | node = tmp; |
| 3994 | |
| 3995 | if (node == NULL) |
| 3996 | return(0); |
| 3997 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 3998 | |
| 3999 | switch (node->type) { |
| 4000 | case XML_COMMENT_NODE: |
| 4001 | case XML_PI_NODE: |
| 4002 | case XML_CDATA_SECTION_NODE: |
| 4003 | case XML_TEXT_NODE: |
| 4004 | string = node->content; |
| 4005 | if (string == NULL) |
| 4006 | return(0); |
| 4007 | if (string[0] == 0) |
| 4008 | return(0); |
| 4009 | return(((unsigned int) string[0]) + |
| 4010 | (((unsigned int) string[1]) << 8)); |
| 4011 | case XML_NAMESPACE_DECL: |
| 4012 | string = ((xmlNsPtr)node)->href; |
| 4013 | if (string == NULL) |
| 4014 | return(0); |
| 4015 | if (string[0] == 0) |
| 4016 | return(0); |
| 4017 | return(((unsigned int) string[0]) + |
| 4018 | (((unsigned int) string[1]) << 8)); |
| 4019 | case XML_ATTRIBUTE_NODE: |
| 4020 | tmp = ((xmlAttrPtr) node)->children; |
| 4021 | break; |
| 4022 | case XML_ELEMENT_NODE: |
| 4023 | tmp = node->children; |
| 4024 | break; |
| 4025 | default: |
| 4026 | return(0); |
| 4027 | } |
| 4028 | while (tmp != NULL) { |
| 4029 | switch (tmp->type) { |
| 4030 | case XML_COMMENT_NODE: |
| 4031 | case XML_PI_NODE: |
| 4032 | case XML_CDATA_SECTION_NODE: |
| 4033 | case XML_TEXT_NODE: |
| 4034 | string = tmp->content; |
| 4035 | break; |
| 4036 | case XML_NAMESPACE_DECL: |
| 4037 | string = ((xmlNsPtr)tmp)->href; |
| 4038 | break; |
| 4039 | default: |
| 4040 | break; |
| 4041 | } |
| 4042 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4043 | if (len == 1) { |
| 4044 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4045 | } |
| 4046 | if (string[1] == 0) { |
| 4047 | len = 1; |
| 4048 | ret = (unsigned int) string[0]; |
| 4049 | } else { |
| 4050 | return(((unsigned int) string[0]) + |
| 4051 | (((unsigned int) string[1]) << 8)); |
| 4052 | } |
| 4053 | } |
| 4054 | /* |
| 4055 | * Skip to next node |
| 4056 | */ |
| 4057 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4058 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4059 | tmp = tmp->children; |
| 4060 | continue; |
| 4061 | } |
| 4062 | } |
| 4063 | if (tmp == node) |
| 4064 | break; |
| 4065 | |
| 4066 | if (tmp->next != NULL) { |
| 4067 | tmp = tmp->next; |
| 4068 | continue; |
| 4069 | } |
| 4070 | |
| 4071 | do { |
| 4072 | tmp = tmp->parent; |
| 4073 | if (tmp == NULL) |
| 4074 | break; |
| 4075 | if (tmp == node) { |
| 4076 | tmp = NULL; |
| 4077 | break; |
| 4078 | } |
| 4079 | if (tmp->next != NULL) { |
| 4080 | tmp = tmp->next; |
| 4081 | break; |
| 4082 | } |
| 4083 | } while (tmp != NULL); |
| 4084 | } |
| 4085 | return(ret); |
| 4086 | } |
| 4087 | |
| 4088 | /** |
| 4089 | * xmlXPathStringHash: |
| 4090 | * @string: a string |
| 4091 | * |
| 4092 | * Function computing the beginning of the string value of the node, |
| 4093 | * used to speed up comparisons |
| 4094 | * |
| 4095 | * Returns an int usable as a hash |
| 4096 | */ |
| 4097 | static unsigned int |
| 4098 | xmlXPathStringHash(const xmlChar * string) { |
| 4099 | if (string == NULL) |
| 4100 | return((unsigned int) 0); |
| 4101 | if (string[0] == 0) |
| 4102 | return(0); |
| 4103 | return(((unsigned int) string[0]) + |
| 4104 | (((unsigned int) string[1]) << 8)); |
| 4105 | } |
| 4106 | |
| 4107 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4108 | * xmlXPathCompareNodeSetFloat: |
| 4109 | * @ctxt: the XPath Parser context |
| 4110 | * @inf: less than (1) or greater than (0) |
| 4111 | * @strict: is the comparison strict |
| 4112 | * @arg: the node set |
| 4113 | * @f: the value |
| 4114 | * |
| 4115 | * Implement the compare operation between a nodeset and a number |
| 4116 | * @ns < @val (1, 1, ... |
| 4117 | * @ns <= @val (1, 0, ... |
| 4118 | * @ns > @val (0, 1, ... |
| 4119 | * @ns >= @val (0, 0, ... |
| 4120 | * |
| 4121 | * If one object to be compared is a node-set and the other is a number, |
| 4122 | * then the comparison will be true if and only if there is a node in the |
| 4123 | * node-set such that the result of performing the comparison on the number |
| 4124 | * to be compared and on the result of converting the string-value of that |
| 4125 | * node to a number using the number function is true. |
| 4126 | * |
| 4127 | * Returns 0 or 1 depending on the results of the test. |
| 4128 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4129 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4131 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4132 | int i, ret = 0; |
| 4133 | xmlNodeSetPtr ns; |
| 4134 | xmlChar *str2; |
| 4135 | |
| 4136 | if ((f == NULL) || (arg == NULL) || |
| 4137 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4138 | xmlXPathFreeObject(arg); |
| 4139 | xmlXPathFreeObject(f); |
| 4140 | return(0); |
| 4141 | } |
| 4142 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4143 | if (ns != NULL) { |
| 4144 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4145 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4146 | if (str2 != NULL) { |
| 4147 | valuePush(ctxt, |
| 4148 | xmlXPathNewString(str2)); |
| 4149 | xmlFree(str2); |
| 4150 | xmlXPathNumberFunction(ctxt, 1); |
| 4151 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4152 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4153 | if (ret) |
| 4154 | break; |
| 4155 | } |
| 4156 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4157 | } |
| 4158 | xmlXPathFreeObject(arg); |
| 4159 | xmlXPathFreeObject(f); |
| 4160 | return(ret); |
| 4161 | } |
| 4162 | |
| 4163 | /** |
| 4164 | * xmlXPathCompareNodeSetString: |
| 4165 | * @ctxt: the XPath Parser context |
| 4166 | * @inf: less than (1) or greater than (0) |
| 4167 | * @strict: is the comparison strict |
| 4168 | * @arg: the node set |
| 4169 | * @s: the value |
| 4170 | * |
| 4171 | * Implement the compare operation between a nodeset and a string |
| 4172 | * @ns < @val (1, 1, ... |
| 4173 | * @ns <= @val (1, 0, ... |
| 4174 | * @ns > @val (0, 1, ... |
| 4175 | * @ns >= @val (0, 0, ... |
| 4176 | * |
| 4177 | * If one object to be compared is a node-set and the other is a string, |
| 4178 | * then the comparison will be true if and only if there is a node in |
| 4179 | * the node-set such that the result of performing the comparison on the |
| 4180 | * string-value of the node and the other string is true. |
| 4181 | * |
| 4182 | * Returns 0 or 1 depending on the results of the test. |
| 4183 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4184 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4185 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4186 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4187 | int i, ret = 0; |
| 4188 | xmlNodeSetPtr ns; |
| 4189 | xmlChar *str2; |
| 4190 | |
| 4191 | if ((s == NULL) || (arg == NULL) || |
| 4192 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4193 | xmlXPathFreeObject(arg); |
| 4194 | xmlXPathFreeObject(s); |
| 4195 | return(0); |
| 4196 | } |
| 4197 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4198 | if (ns != NULL) { |
| 4199 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4200 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4201 | if (str2 != NULL) { |
| 4202 | valuePush(ctxt, |
| 4203 | xmlXPathNewString(str2)); |
| 4204 | xmlFree(str2); |
| 4205 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4206 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4207 | if (ret) |
| 4208 | break; |
| 4209 | } |
| 4210 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4211 | } |
| 4212 | xmlXPathFreeObject(arg); |
| 4213 | xmlXPathFreeObject(s); |
| 4214 | return(ret); |
| 4215 | } |
| 4216 | |
| 4217 | /** |
| 4218 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4219 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4220 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4221 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4222 | * @arg2: the second node set object |
| 4223 | * |
| 4224 | * Implement the compare operation on nodesets: |
| 4225 | * |
| 4226 | * If both objects to be compared are node-sets, then the comparison |
| 4227 | * will be true if and only if there is a node in the first node-set |
| 4228 | * and a node in the second node-set such that the result of performing |
| 4229 | * the comparison on the string-values of the two nodes is true. |
| 4230 | * .... |
| 4231 | * When neither object to be compared is a node-set and the operator |
| 4232 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4233 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4234 | * .... |
| 4235 | * The number function converts its argument to a number as follows: |
| 4236 | * - a string that consists of optional whitespace followed by an |
| 4237 | * optional minus sign followed by a Number followed by whitespace |
| 4238 | * is converted to the IEEE 754 number that is nearest (according |
| 4239 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4240 | * represented by the string; any other string is converted to NaN |
| 4241 | * |
| 4242 | * Conclusion all nodes need to be converted first to their string value |
| 4243 | * and then the comparison must be done when possible |
| 4244 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4245 | static int |
| 4246 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4247 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4248 | int i, j, init = 0; |
| 4249 | double val1; |
| 4250 | double *values2; |
| 4251 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4252 | xmlNodeSetPtr ns1; |
| 4253 | xmlNodeSetPtr ns2; |
| 4254 | |
| 4255 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4256 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4257 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4258 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4259 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4260 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4261 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4262 | xmlXPathFreeObject(arg1); |
| 4263 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4264 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4266 | |
| 4267 | ns1 = arg1->nodesetval; |
| 4268 | ns2 = arg2->nodesetval; |
| 4269 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4270 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4271 | xmlXPathFreeObject(arg1); |
| 4272 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4273 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4274 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4275 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4276 | xmlXPathFreeObject(arg1); |
| 4277 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4278 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4279 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4280 | |
| 4281 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4282 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4283 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4284 | xmlXPathFreeObject(arg1); |
| 4285 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4286 | return(0); |
| 4287 | } |
| 4288 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4289 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4290 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4291 | continue; |
| 4292 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4293 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4294 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4295 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4296 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4297 | continue; |
| 4298 | if (inf && strict) |
| 4299 | ret = (val1 < values2[j]); |
| 4300 | else if (inf && !strict) |
| 4301 | ret = (val1 <= values2[j]); |
| 4302 | else if (!inf && strict) |
| 4303 | ret = (val1 > values2[j]); |
| 4304 | else if (!inf && !strict) |
| 4305 | ret = (val1 >= values2[j]); |
| 4306 | if (ret) |
| 4307 | break; |
| 4308 | } |
| 4309 | if (ret) |
| 4310 | break; |
| 4311 | init = 1; |
| 4312 | } |
| 4313 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4314 | xmlXPathFreeObject(arg1); |
| 4315 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4316 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | /** |
| 4320 | * xmlXPathCompareNodeSetValue: |
| 4321 | * @ctxt: the XPath Parser context |
| 4322 | * @inf: less than (1) or greater than (0) |
| 4323 | * @strict: is the comparison strict |
| 4324 | * @arg: the node set |
| 4325 | * @val: the value |
| 4326 | * |
| 4327 | * Implement the compare operation between a nodeset and a value |
| 4328 | * @ns < @val (1, 1, ... |
| 4329 | * @ns <= @val (1, 0, ... |
| 4330 | * @ns > @val (0, 1, ... |
| 4331 | * @ns >= @val (0, 0, ... |
| 4332 | * |
| 4333 | * If one object to be compared is a node-set and the other is a boolean, |
| 4334 | * then the comparison will be true if and only if the result of performing |
| 4335 | * the comparison on the boolean and on the result of converting |
| 4336 | * the node-set to a boolean using the boolean function is true. |
| 4337 | * |
| 4338 | * Returns 0 or 1 depending on the results of the test. |
| 4339 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4340 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4341 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4342 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4343 | if ((val == NULL) || (arg == NULL) || |
| 4344 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4345 | return(0); |
| 4346 | |
| 4347 | switch(val->type) { |
| 4348 | case XPATH_NUMBER: |
| 4349 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4350 | case XPATH_NODESET: |
| 4351 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4352 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4353 | case XPATH_STRING: |
| 4354 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4355 | case XPATH_BOOLEAN: |
| 4356 | valuePush(ctxt, arg); |
| 4357 | xmlXPathBooleanFunction(ctxt, 1); |
| 4358 | valuePush(ctxt, val); |
| 4359 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4360 | default: |
| 4361 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4362 | } |
| 4363 | return(0); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4367 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4368 | * @arg: the nodeset object argument |
| 4369 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4370 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4371 | * |
| 4372 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4373 | * If one object to be compared is a node-set and the other is a string, |
| 4374 | * then the comparison will be true if and only if there is a node in |
| 4375 | * the node-set such that the result of performing the comparison on the |
| 4376 | * string-value of the node and the other string is true. |
| 4377 | * |
| 4378 | * Returns 0 or 1 depending on the results of the test. |
| 4379 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4380 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4381 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4382 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4383 | int i; |
| 4384 | xmlNodeSetPtr ns; |
| 4385 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4386 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4387 | |
| 4388 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4389 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4390 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4391 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4392 | /* |
| 4393 | * A NULL nodeset compared with a string is always false |
| 4394 | * (since there is no node equal, and no node not equal) |
| 4395 | */ |
| 4396 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4397 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4398 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4399 | for (i = 0; i < ns->nodeNr; i++) { |
| 4400 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4401 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4402 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4403 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4404 | if (neq) |
| 4405 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4406 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4407 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4408 | if (neq) |
| 4409 | continue; |
| 4410 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4411 | } else if (neq) { |
| 4412 | if (str2 != NULL) |
| 4413 | xmlFree(str2); |
| 4414 | return (1); |
| 4415 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4416 | if (str2 != NULL) |
| 4417 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4418 | } else if (neq) |
| 4419 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4420 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4421 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4425 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | * @arg: the nodeset object argument |
| 4427 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4428 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4429 | * |
| 4430 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4431 | * If one object to be compared is a node-set and the other is a number, |
| 4432 | * then the comparison will be true if and only if there is a node in |
| 4433 | * the node-set such that the result of performing the comparison on the |
| 4434 | * number to be compared and on the result of converting the string-value |
| 4435 | * of that node to a number using the number function is true. |
| 4436 | * |
| 4437 | * Returns 0 or 1 depending on the results of the test. |
| 4438 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4439 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4440 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4441 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4442 | int i, ret=0; |
| 4443 | xmlNodeSetPtr ns; |
| 4444 | xmlChar *str2; |
| 4445 | xmlXPathObjectPtr val; |
| 4446 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | |
| 4448 | if ((arg == NULL) || |
| 4449 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4450 | return(0); |
| 4451 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4452 | ns = arg->nodesetval; |
| 4453 | if (ns != NULL) { |
| 4454 | for (i=0;i<ns->nodeNr;i++) { |
| 4455 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4456 | if (str2 != NULL) { |
| 4457 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4458 | xmlFree(str2); |
| 4459 | xmlXPathNumberFunction(ctxt, 1); |
| 4460 | val = valuePop(ctxt); |
| 4461 | v = val->floatval; |
| 4462 | xmlXPathFreeObject(val); |
| 4463 | if (!xmlXPathIsNaN(v)) { |
| 4464 | if ((!neq) && (v==f)) { |
| 4465 | ret = 1; |
| 4466 | break; |
| 4467 | } else if ((neq) && (v!=f)) { |
| 4468 | ret = 1; |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4481 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4482 | * @arg1: first nodeset object argument |
| 4483 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4484 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4485 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4486 | * Implement the equal / not equal operation on XPath nodesets: |
| 4487 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4488 | * If both objects to be compared are node-sets, then the comparison |
| 4489 | * will be true if and only if there is a node in the first node-set and |
| 4490 | * a node in the second node-set such that the result of performing the |
| 4491 | * comparison on the string-values of the two nodes is true. |
| 4492 | * |
| 4493 | * (needless to say, this is a costly operation) |
| 4494 | * |
| 4495 | * Returns 0 or 1 depending on the results of the test. |
| 4496 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4497 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4498 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4499 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4500 | unsigned int *hashs1; |
| 4501 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4502 | xmlChar **values1; |
| 4503 | xmlChar **values2; |
| 4504 | int ret = 0; |
| 4505 | xmlNodeSetPtr ns1; |
| 4506 | xmlNodeSetPtr ns2; |
| 4507 | |
| 4508 | if ((arg1 == NULL) || |
| 4509 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4510 | return(0); |
| 4511 | if ((arg2 == NULL) || |
| 4512 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4513 | return(0); |
| 4514 | |
| 4515 | ns1 = arg1->nodesetval; |
| 4516 | ns2 = arg2->nodesetval; |
| 4517 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4518 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4519 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4520 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4521 | return(0); |
| 4522 | |
| 4523 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4524 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4525 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4526 | if (neq == 0) |
| 4527 | for (i = 0;i < ns1->nodeNr;i++) |
| 4528 | for (j = 0;j < ns2->nodeNr;j++) |
| 4529 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4530 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4531 | |
| 4532 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4533 | if (values1 == NULL) { |
| 4534 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4535 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4536 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4537 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4538 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4539 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4540 | xmlFree(values1); |
| 4541 | return(0); |
| 4542 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4543 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4544 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4545 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4546 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4547 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4548 | xmlFree(values1); |
| 4549 | return(0); |
| 4550 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4551 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4552 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4553 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4554 | xmlFree(hashs1); |
| 4555 | xmlFree(values1); |
| 4556 | xmlFree(values2); |
| 4557 | return(0); |
| 4558 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4559 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4560 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4561 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4562 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4563 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4564 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4565 | if (hashs1[i] != hashs2[j]) { |
| 4566 | if (neq) { |
| 4567 | ret = 1; |
| 4568 | break; |
| 4569 | } |
| 4570 | } |
| 4571 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4572 | if (values1[i] == NULL) |
| 4573 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4574 | if (values2[j] == NULL) |
| 4575 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4576 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4577 | if (ret) |
| 4578 | break; |
| 4579 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4580 | } |
| 4581 | if (ret) |
| 4582 | break; |
| 4583 | } |
| 4584 | for (i = 0;i < ns1->nodeNr;i++) |
| 4585 | if (values1[i] != NULL) |
| 4586 | xmlFree(values1[i]); |
| 4587 | for (j = 0;j < ns2->nodeNr;j++) |
| 4588 | if (values2[j] != NULL) |
| 4589 | xmlFree(values2[j]); |
| 4590 | xmlFree(values1); |
| 4591 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4592 | xmlFree(hashs1); |
| 4593 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | return(ret); |
| 4595 | } |
| 4596 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4597 | static int |
| 4598 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 4599 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4601 | /* |
| 4602 | *At this point we are assured neither arg1 nor arg2 |
| 4603 | *is a nodeset, so we can just pick the appropriate routine. |
| 4604 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4605 | switch (arg1->type) { |
| 4606 | case XPATH_UNDEFINED: |
| 4607 | #ifdef DEBUG_EXPR |
| 4608 | xmlGenericError(xmlGenericErrorContext, |
| 4609 | "Equal: undefined\n"); |
| 4610 | #endif |
| 4611 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4612 | case XPATH_BOOLEAN: |
| 4613 | switch (arg2->type) { |
| 4614 | case XPATH_UNDEFINED: |
| 4615 | #ifdef DEBUG_EXPR |
| 4616 | xmlGenericError(xmlGenericErrorContext, |
| 4617 | "Equal: undefined\n"); |
| 4618 | #endif |
| 4619 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4620 | case XPATH_BOOLEAN: |
| 4621 | #ifdef DEBUG_EXPR |
| 4622 | xmlGenericError(xmlGenericErrorContext, |
| 4623 | "Equal: %d boolean %d \n", |
| 4624 | arg1->boolval, arg2->boolval); |
| 4625 | #endif |
| 4626 | ret = (arg1->boolval == arg2->boolval); |
| 4627 | break; |
| 4628 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4629 | ret = (arg1->boolval == |
| 4630 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4631 | break; |
| 4632 | case XPATH_STRING: |
| 4633 | if ((arg2->stringval == NULL) || |
| 4634 | (arg2->stringval[0] == 0)) ret = 0; |
| 4635 | else |
| 4636 | ret = 1; |
| 4637 | ret = (arg1->boolval == ret); |
| 4638 | break; |
| 4639 | case XPATH_USERS: |
| 4640 | case XPATH_POINT: |
| 4641 | case XPATH_RANGE: |
| 4642 | case XPATH_LOCATIONSET: |
| 4643 | TODO |
| 4644 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4645 | case XPATH_NODESET: |
| 4646 | case XPATH_XSLT_TREE: |
| 4647 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4648 | } |
| 4649 | break; |
| 4650 | case XPATH_NUMBER: |
| 4651 | switch (arg2->type) { |
| 4652 | case XPATH_UNDEFINED: |
| 4653 | #ifdef DEBUG_EXPR |
| 4654 | xmlGenericError(xmlGenericErrorContext, |
| 4655 | "Equal: undefined\n"); |
| 4656 | #endif |
| 4657 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4658 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4659 | ret = (arg2->boolval== |
| 4660 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4661 | break; |
| 4662 | case XPATH_STRING: |
| 4663 | valuePush(ctxt, arg2); |
| 4664 | xmlXPathNumberFunction(ctxt, 1); |
| 4665 | arg2 = valuePop(ctxt); |
| 4666 | /* no break on purpose */ |
| 4667 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4668 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4669 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4670 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4671 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4672 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4673 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4674 | ret = 1; |
| 4675 | else |
| 4676 | ret = 0; |
| 4677 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4678 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4679 | ret = 1; |
| 4680 | else |
| 4681 | ret = 0; |
| 4682 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4683 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4684 | ret = 1; |
| 4685 | else |
| 4686 | ret = 0; |
| 4687 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4688 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4689 | ret = 1; |
| 4690 | else |
| 4691 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4692 | } else { |
| 4693 | ret = (arg1->floatval == arg2->floatval); |
| 4694 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4695 | break; |
| 4696 | case XPATH_USERS: |
| 4697 | case XPATH_POINT: |
| 4698 | case XPATH_RANGE: |
| 4699 | case XPATH_LOCATIONSET: |
| 4700 | TODO |
| 4701 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4702 | case XPATH_NODESET: |
| 4703 | case XPATH_XSLT_TREE: |
| 4704 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4705 | } |
| 4706 | break; |
| 4707 | case XPATH_STRING: |
| 4708 | switch (arg2->type) { |
| 4709 | case XPATH_UNDEFINED: |
| 4710 | #ifdef DEBUG_EXPR |
| 4711 | xmlGenericError(xmlGenericErrorContext, |
| 4712 | "Equal: undefined\n"); |
| 4713 | #endif |
| 4714 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4715 | case XPATH_BOOLEAN: |
| 4716 | if ((arg1->stringval == NULL) || |
| 4717 | (arg1->stringval[0] == 0)) ret = 0; |
| 4718 | else |
| 4719 | ret = 1; |
| 4720 | ret = (arg2->boolval == ret); |
| 4721 | break; |
| 4722 | case XPATH_STRING: |
| 4723 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4724 | break; |
| 4725 | case XPATH_NUMBER: |
| 4726 | valuePush(ctxt, arg1); |
| 4727 | xmlXPathNumberFunction(ctxt, 1); |
| 4728 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4729 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4730 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4731 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4732 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4733 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4734 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4735 | ret = 1; |
| 4736 | else |
| 4737 | ret = 0; |
| 4738 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4739 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4740 | ret = 1; |
| 4741 | else |
| 4742 | ret = 0; |
| 4743 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4744 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4745 | ret = 1; |
| 4746 | else |
| 4747 | ret = 0; |
| 4748 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4749 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4750 | ret = 1; |
| 4751 | else |
| 4752 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4753 | } else { |
| 4754 | ret = (arg1->floatval == arg2->floatval); |
| 4755 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4756 | break; |
| 4757 | case XPATH_USERS: |
| 4758 | case XPATH_POINT: |
| 4759 | case XPATH_RANGE: |
| 4760 | case XPATH_LOCATIONSET: |
| 4761 | TODO |
| 4762 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4763 | case XPATH_NODESET: |
| 4764 | case XPATH_XSLT_TREE: |
| 4765 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4766 | } |
| 4767 | break; |
| 4768 | case XPATH_USERS: |
| 4769 | case XPATH_POINT: |
| 4770 | case XPATH_RANGE: |
| 4771 | case XPATH_LOCATIONSET: |
| 4772 | TODO |
| 4773 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4774 | case XPATH_NODESET: |
| 4775 | case XPATH_XSLT_TREE: |
| 4776 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4777 | } |
| 4778 | xmlXPathFreeObject(arg1); |
| 4779 | xmlXPathFreeObject(arg2); |
| 4780 | return(ret); |
| 4781 | } |
| 4782 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4783 | /** |
| 4784 | * xmlXPathEqualValues: |
| 4785 | * @ctxt: the XPath Parser context |
| 4786 | * |
| 4787 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4788 | * |
| 4789 | * Returns 0 or 1 depending on the results of the test. |
| 4790 | */ |
| 4791 | int |
| 4792 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4793 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4794 | int ret = 0; |
| 4795 | |
| 4796 | arg2 = valuePop(ctxt); |
| 4797 | arg1 = valuePop(ctxt); |
| 4798 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4799 | if (arg1 != NULL) |
| 4800 | xmlXPathFreeObject(arg1); |
| 4801 | else |
| 4802 | xmlXPathFreeObject(arg2); |
| 4803 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4804 | } |
| 4805 | |
| 4806 | if (arg1 == arg2) { |
| 4807 | #ifdef DEBUG_EXPR |
| 4808 | xmlGenericError(xmlGenericErrorContext, |
| 4809 | "Equal: by pointer\n"); |
| 4810 | #endif |
| 4811 | return(1); |
| 4812 | } |
| 4813 | |
| 4814 | /* |
| 4815 | *If either argument is a nodeset, it's a 'special case' |
| 4816 | */ |
| 4817 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4818 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4819 | /* |
| 4820 | *Hack it to assure arg1 is the nodeset |
| 4821 | */ |
| 4822 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4823 | argtmp = arg2; |
| 4824 | arg2 = arg1; |
| 4825 | arg1 = argtmp; |
| 4826 | } |
| 4827 | switch (arg2->type) { |
| 4828 | case XPATH_UNDEFINED: |
| 4829 | #ifdef DEBUG_EXPR |
| 4830 | xmlGenericError(xmlGenericErrorContext, |
| 4831 | "Equal: undefined\n"); |
| 4832 | #endif |
| 4833 | break; |
| 4834 | case XPATH_NODESET: |
| 4835 | case XPATH_XSLT_TREE: |
| 4836 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 4837 | break; |
| 4838 | case XPATH_BOOLEAN: |
| 4839 | if ((arg1->nodesetval == NULL) || |
| 4840 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4841 | else |
| 4842 | ret = 1; |
| 4843 | ret = (ret == arg2->boolval); |
| 4844 | break; |
| 4845 | case XPATH_NUMBER: |
| 4846 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 4847 | break; |
| 4848 | case XPATH_STRING: |
| 4849 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 4850 | break; |
| 4851 | case XPATH_USERS: |
| 4852 | case XPATH_POINT: |
| 4853 | case XPATH_RANGE: |
| 4854 | case XPATH_LOCATIONSET: |
| 4855 | TODO |
| 4856 | break; |
| 4857 | } |
| 4858 | xmlXPathFreeObject(arg1); |
| 4859 | xmlXPathFreeObject(arg2); |
| 4860 | return(ret); |
| 4861 | } |
| 4862 | |
| 4863 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4864 | } |
| 4865 | |
| 4866 | /** |
| 4867 | * xmlXPathNotEqualValues: |
| 4868 | * @ctxt: the XPath Parser context |
| 4869 | * |
| 4870 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4871 | * |
| 4872 | * Returns 0 or 1 depending on the results of the test. |
| 4873 | */ |
| 4874 | int |
| 4875 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4876 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4877 | int ret = 0; |
| 4878 | |
| 4879 | arg2 = valuePop(ctxt); |
| 4880 | arg1 = valuePop(ctxt); |
| 4881 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4882 | if (arg1 != NULL) |
| 4883 | xmlXPathFreeObject(arg1); |
| 4884 | else |
| 4885 | xmlXPathFreeObject(arg2); |
| 4886 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4887 | } |
| 4888 | |
| 4889 | if (arg1 == arg2) { |
| 4890 | #ifdef DEBUG_EXPR |
| 4891 | xmlGenericError(xmlGenericErrorContext, |
| 4892 | "NotEqual: by pointer\n"); |
| 4893 | #endif |
| 4894 | return(0); |
| 4895 | } |
| 4896 | |
| 4897 | /* |
| 4898 | *If either argument is a nodeset, it's a 'special case' |
| 4899 | */ |
| 4900 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4901 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4902 | /* |
| 4903 | *Hack it to assure arg1 is the nodeset |
| 4904 | */ |
| 4905 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4906 | argtmp = arg2; |
| 4907 | arg2 = arg1; |
| 4908 | arg1 = argtmp; |
| 4909 | } |
| 4910 | switch (arg2->type) { |
| 4911 | case XPATH_UNDEFINED: |
| 4912 | #ifdef DEBUG_EXPR |
| 4913 | xmlGenericError(xmlGenericErrorContext, |
| 4914 | "NotEqual: undefined\n"); |
| 4915 | #endif |
| 4916 | break; |
| 4917 | case XPATH_NODESET: |
| 4918 | case XPATH_XSLT_TREE: |
| 4919 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 4920 | break; |
| 4921 | case XPATH_BOOLEAN: |
| 4922 | if ((arg1->nodesetval == NULL) || |
| 4923 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4924 | else |
| 4925 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4926 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4927 | break; |
| 4928 | case XPATH_NUMBER: |
| 4929 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 4930 | break; |
| 4931 | case XPATH_STRING: |
| 4932 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 4933 | break; |
| 4934 | case XPATH_USERS: |
| 4935 | case XPATH_POINT: |
| 4936 | case XPATH_RANGE: |
| 4937 | case XPATH_LOCATIONSET: |
| 4938 | TODO |
| 4939 | break; |
| 4940 | } |
| 4941 | xmlXPathFreeObject(arg1); |
| 4942 | xmlXPathFreeObject(arg2); |
| 4943 | return(ret); |
| 4944 | } |
| 4945 | |
| 4946 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4947 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4948 | |
| 4949 | /** |
| 4950 | * xmlXPathCompareValues: |
| 4951 | * @ctxt: the XPath Parser context |
| 4952 | * @inf: less than (1) or greater than (0) |
| 4953 | * @strict: is the comparison strict |
| 4954 | * |
| 4955 | * Implement the compare operation on XPath objects: |
| 4956 | * @arg1 < @arg2 (1, 1, ... |
| 4957 | * @arg1 <= @arg2 (1, 0, ... |
| 4958 | * @arg1 > @arg2 (0, 1, ... |
| 4959 | * @arg1 >= @arg2 (0, 0, ... |
| 4960 | * |
| 4961 | * When neither object to be compared is a node-set and the operator is |
| 4962 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 4963 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 4964 | * comparison will be true if and only if the first number is less than the |
| 4965 | * second number. The <= comparison will be true if and only if the first |
| 4966 | * number is less than or equal to the second number. The > comparison |
| 4967 | * will be true if and only if the first number is greater than the second |
| 4968 | * number. The >= comparison will be true if and only if the first number |
| 4969 | * is greater than or equal to the second number. |
| 4970 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4971 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4972 | */ |
| 4973 | int |
| 4974 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4975 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4976 | xmlXPathObjectPtr arg1, arg2; |
| 4977 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4978 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4979 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4980 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4981 | if (arg1 != NULL) |
| 4982 | xmlXPathFreeObject(arg1); |
| 4983 | else |
| 4984 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4985 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4986 | } |
| 4987 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4988 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4989 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4990 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 4991 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4992 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4993 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4994 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 4995 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 4996 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4997 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 4998 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 4999 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | return(ret); |
| 5003 | } |
| 5004 | |
| 5005 | if (arg1->type != XPATH_NUMBER) { |
| 5006 | valuePush(ctxt, arg1); |
| 5007 | xmlXPathNumberFunction(ctxt, 1); |
| 5008 | arg1 = valuePop(ctxt); |
| 5009 | } |
| 5010 | if (arg1->type != XPATH_NUMBER) { |
| 5011 | xmlXPathFreeObject(arg1); |
| 5012 | xmlXPathFreeObject(arg2); |
| 5013 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5014 | } |
| 5015 | if (arg2->type != XPATH_NUMBER) { |
| 5016 | valuePush(ctxt, arg2); |
| 5017 | xmlXPathNumberFunction(ctxt, 1); |
| 5018 | arg2 = valuePop(ctxt); |
| 5019 | } |
| 5020 | if (arg2->type != XPATH_NUMBER) { |
| 5021 | xmlXPathFreeObject(arg1); |
| 5022 | xmlXPathFreeObject(arg2); |
| 5023 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5024 | } |
| 5025 | /* |
| 5026 | * Add tests for infinity and nan |
| 5027 | * => feedback on 3.4 for Inf and NaN |
| 5028 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5029 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5030 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5031 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5032 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5033 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5034 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5035 | if (inf && strict) { |
| 5036 | if ((arg1i == -1 && arg2i != -1) || |
| 5037 | (arg2i == 1 && arg1i != 1)) { |
| 5038 | ret = 1; |
| 5039 | } else if (arg1i == 0 && arg2i == 0) { |
| 5040 | ret = (arg1->floatval < arg2->floatval); |
| 5041 | } else { |
| 5042 | ret = 0; |
| 5043 | } |
| 5044 | } |
| 5045 | else if (inf && !strict) { |
| 5046 | if (arg1i == -1 || arg2i == 1) { |
| 5047 | ret = 1; |
| 5048 | } else if (arg1i == 0 && arg2i == 0) { |
| 5049 | ret = (arg1->floatval <= arg2->floatval); |
| 5050 | } else { |
| 5051 | ret = 0; |
| 5052 | } |
| 5053 | } |
| 5054 | else if (!inf && strict) { |
| 5055 | if ((arg1i == 1 && arg2i != 1) || |
| 5056 | (arg2i == -1 && arg1i != -1)) { |
| 5057 | ret = 1; |
| 5058 | } else if (arg1i == 0 && arg2i == 0) { |
| 5059 | ret = (arg1->floatval > arg2->floatval); |
| 5060 | } else { |
| 5061 | ret = 0; |
| 5062 | } |
| 5063 | } |
| 5064 | else if (!inf && !strict) { |
| 5065 | if (arg1i == 1 || arg2i == -1) { |
| 5066 | ret = 1; |
| 5067 | } else if (arg1i == 0 && arg2i == 0) { |
| 5068 | ret = (arg1->floatval >= arg2->floatval); |
| 5069 | } else { |
| 5070 | ret = 0; |
| 5071 | } |
| 5072 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5073 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5074 | xmlXPathFreeObject(arg1); |
| 5075 | xmlXPathFreeObject(arg2); |
| 5076 | return(ret); |
| 5077 | } |
| 5078 | |
| 5079 | /** |
| 5080 | * xmlXPathValueFlipSign: |
| 5081 | * @ctxt: the XPath Parser context |
| 5082 | * |
| 5083 | * Implement the unary - operation on an XPath object |
| 5084 | * The numeric operators convert their operands to numbers as if |
| 5085 | * by calling the number function. |
| 5086 | */ |
| 5087 | void |
| 5088 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5089 | CAST_TO_NUMBER; |
| 5090 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5091 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5092 | ctxt->value->floatval=xmlXPathNAN; |
| 5093 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5094 | ctxt->value->floatval=xmlXPathNINF; |
| 5095 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5096 | ctxt->value->floatval=xmlXPathPINF; |
| 5097 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5098 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5099 | ctxt->value->floatval = xmlXPathNZERO; |
| 5100 | else |
| 5101 | ctxt->value->floatval = 0; |
| 5102 | } |
| 5103 | else |
| 5104 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | /** |
| 5108 | * xmlXPathAddValues: |
| 5109 | * @ctxt: the XPath Parser context |
| 5110 | * |
| 5111 | * Implement the add operation on XPath objects: |
| 5112 | * The numeric operators convert their operands to numbers as if |
| 5113 | * by calling the number function. |
| 5114 | */ |
| 5115 | void |
| 5116 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5117 | xmlXPathObjectPtr arg; |
| 5118 | double val; |
| 5119 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5120 | arg = valuePop(ctxt); |
| 5121 | if (arg == NULL) |
| 5122 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5123 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5124 | xmlXPathFreeObject(arg); |
| 5125 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5126 | CAST_TO_NUMBER; |
| 5127 | CHECK_TYPE(XPATH_NUMBER); |
| 5128 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | /** |
| 5132 | * xmlXPathSubValues: |
| 5133 | * @ctxt: the XPath Parser context |
| 5134 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5135 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5136 | * The numeric operators convert their operands to numbers as if |
| 5137 | * by calling the number function. |
| 5138 | */ |
| 5139 | void |
| 5140 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5141 | xmlXPathObjectPtr arg; |
| 5142 | double val; |
| 5143 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5144 | arg = valuePop(ctxt); |
| 5145 | if (arg == NULL) |
| 5146 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5147 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5148 | xmlXPathFreeObject(arg); |
| 5149 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5150 | CAST_TO_NUMBER; |
| 5151 | CHECK_TYPE(XPATH_NUMBER); |
| 5152 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | /** |
| 5156 | * xmlXPathMultValues: |
| 5157 | * @ctxt: the XPath Parser context |
| 5158 | * |
| 5159 | * Implement the multiply operation on XPath objects: |
| 5160 | * The numeric operators convert their operands to numbers as if |
| 5161 | * by calling the number function. |
| 5162 | */ |
| 5163 | void |
| 5164 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5165 | xmlXPathObjectPtr arg; |
| 5166 | double val; |
| 5167 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5168 | arg = valuePop(ctxt); |
| 5169 | if (arg == NULL) |
| 5170 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5171 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5172 | xmlXPathFreeObject(arg); |
| 5173 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5174 | CAST_TO_NUMBER; |
| 5175 | CHECK_TYPE(XPATH_NUMBER); |
| 5176 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | /** |
| 5180 | * xmlXPathDivValues: |
| 5181 | * @ctxt: the XPath Parser context |
| 5182 | * |
| 5183 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5184 | * The numeric operators convert their operands to numbers as if |
| 5185 | * by calling the number function. |
| 5186 | */ |
| 5187 | void |
| 5188 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5189 | xmlXPathObjectPtr arg; |
| 5190 | double val; |
| 5191 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5192 | arg = valuePop(ctxt); |
| 5193 | if (arg == NULL) |
| 5194 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5195 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5196 | xmlXPathFreeObject(arg); |
| 5197 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5198 | CAST_TO_NUMBER; |
| 5199 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5200 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5201 | ctxt->value->floatval = xmlXPathNAN; |
| 5202 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5203 | if (ctxt->value->floatval == 0) |
| 5204 | ctxt->value->floatval = xmlXPathNAN; |
| 5205 | else if (ctxt->value->floatval > 0) |
| 5206 | ctxt->value->floatval = xmlXPathNINF; |
| 5207 | else if (ctxt->value->floatval < 0) |
| 5208 | ctxt->value->floatval = xmlXPathPINF; |
| 5209 | } |
| 5210 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5211 | if (ctxt->value->floatval == 0) |
| 5212 | ctxt->value->floatval = xmlXPathNAN; |
| 5213 | else if (ctxt->value->floatval > 0) |
| 5214 | ctxt->value->floatval = xmlXPathPINF; |
| 5215 | else if (ctxt->value->floatval < 0) |
| 5216 | ctxt->value->floatval = xmlXPathNINF; |
| 5217 | } else |
| 5218 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | /** |
| 5222 | * xmlXPathModValues: |
| 5223 | * @ctxt: the XPath Parser context |
| 5224 | * |
| 5225 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5226 | * The numeric operators convert their operands to numbers as if |
| 5227 | * by calling the number function. |
| 5228 | */ |
| 5229 | void |
| 5230 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5231 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5232 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5233 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5234 | arg = valuePop(ctxt); |
| 5235 | if (arg == NULL) |
| 5236 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5237 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5238 | xmlXPathFreeObject(arg); |
| 5239 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5240 | CAST_TO_NUMBER; |
| 5241 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5242 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5243 | if (arg2 == 0) |
| 5244 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5245 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5246 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5247 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | /************************************************************************ |
| 5251 | * * |
| 5252 | * The traversal functions * |
| 5253 | * * |
| 5254 | ************************************************************************/ |
| 5255 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5256 | /* |
| 5257 | * A traversal function enumerates nodes along an axis. |
| 5258 | * Initially it must be called with NULL, and it indicates |
| 5259 | * termination on the axis by returning NULL. |
| 5260 | */ |
| 5261 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5262 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5263 | |
| 5264 | /** |
| 5265 | * xmlXPathNextSelf: |
| 5266 | * @ctxt: the XPath Parser context |
| 5267 | * @cur: the current node in the traversal |
| 5268 | * |
| 5269 | * Traversal function for the "self" direction |
| 5270 | * The self axis contains just the context node itself |
| 5271 | * |
| 5272 | * Returns the next element following that axis |
| 5273 | */ |
| 5274 | xmlNodePtr |
| 5275 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5276 | if (cur == NULL) |
| 5277 | return(ctxt->context->node); |
| 5278 | return(NULL); |
| 5279 | } |
| 5280 | |
| 5281 | /** |
| 5282 | * xmlXPathNextChild: |
| 5283 | * @ctxt: the XPath Parser context |
| 5284 | * @cur: the current node in the traversal |
| 5285 | * |
| 5286 | * Traversal function for the "child" direction |
| 5287 | * The child axis contains the children of the context node in document order. |
| 5288 | * |
| 5289 | * Returns the next element following that axis |
| 5290 | */ |
| 5291 | xmlNodePtr |
| 5292 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5293 | if (cur == NULL) { |
| 5294 | if (ctxt->context->node == NULL) return(NULL); |
| 5295 | switch (ctxt->context->node->type) { |
| 5296 | case XML_ELEMENT_NODE: |
| 5297 | case XML_TEXT_NODE: |
| 5298 | case XML_CDATA_SECTION_NODE: |
| 5299 | case XML_ENTITY_REF_NODE: |
| 5300 | case XML_ENTITY_NODE: |
| 5301 | case XML_PI_NODE: |
| 5302 | case XML_COMMENT_NODE: |
| 5303 | case XML_NOTATION_NODE: |
| 5304 | case XML_DTD_NODE: |
| 5305 | return(ctxt->context->node->children); |
| 5306 | case XML_DOCUMENT_NODE: |
| 5307 | case XML_DOCUMENT_TYPE_NODE: |
| 5308 | case XML_DOCUMENT_FRAG_NODE: |
| 5309 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5310 | #ifdef LIBXML_DOCB_ENABLED |
| 5311 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5312 | #endif |
| 5313 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5314 | case XML_ELEMENT_DECL: |
| 5315 | case XML_ATTRIBUTE_DECL: |
| 5316 | case XML_ENTITY_DECL: |
| 5317 | case XML_ATTRIBUTE_NODE: |
| 5318 | case XML_NAMESPACE_DECL: |
| 5319 | case XML_XINCLUDE_START: |
| 5320 | case XML_XINCLUDE_END: |
| 5321 | return(NULL); |
| 5322 | } |
| 5323 | return(NULL); |
| 5324 | } |
| 5325 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5326 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5327 | return(NULL); |
| 5328 | return(cur->next); |
| 5329 | } |
| 5330 | |
| 5331 | /** |
| 5332 | * xmlXPathNextDescendant: |
| 5333 | * @ctxt: the XPath Parser context |
| 5334 | * @cur: the current node in the traversal |
| 5335 | * |
| 5336 | * Traversal function for the "descendant" direction |
| 5337 | * the descendant axis contains the descendants of the context node in document |
| 5338 | * order; a descendant is a child or a child of a child and so on. |
| 5339 | * |
| 5340 | * Returns the next element following that axis |
| 5341 | */ |
| 5342 | xmlNodePtr |
| 5343 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5344 | if (cur == NULL) { |
| 5345 | if (ctxt->context->node == NULL) |
| 5346 | return(NULL); |
| 5347 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5348 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5349 | return(NULL); |
| 5350 | |
| 5351 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5352 | return(ctxt->context->doc->children); |
| 5353 | return(ctxt->context->node->children); |
| 5354 | } |
| 5355 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5356 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5357 | /* |
| 5358 | * Do not descend on entities declarations |
| 5359 | */ |
| 5360 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5361 | cur = cur->children; |
| 5362 | /* |
| 5363 | * Skip DTDs |
| 5364 | */ |
| 5365 | if (cur->type != XML_DTD_NODE) |
| 5366 | return(cur); |
| 5367 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | if (cur == ctxt->context->node) return(NULL); |
| 5371 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5372 | while (cur->next != NULL) { |
| 5373 | cur = cur->next; |
| 5374 | if ((cur->type != XML_ENTITY_DECL) && |
| 5375 | (cur->type != XML_DTD_NODE)) |
| 5376 | return(cur); |
| 5377 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5378 | |
| 5379 | do { |
| 5380 | cur = cur->parent; |
| 5381 | if (cur == NULL) return(NULL); |
| 5382 | if (cur == ctxt->context->node) return(NULL); |
| 5383 | if (cur->next != NULL) { |
| 5384 | cur = cur->next; |
| 5385 | return(cur); |
| 5386 | } |
| 5387 | } while (cur != NULL); |
| 5388 | return(cur); |
| 5389 | } |
| 5390 | |
| 5391 | /** |
| 5392 | * xmlXPathNextDescendantOrSelf: |
| 5393 | * @ctxt: the XPath Parser context |
| 5394 | * @cur: the current node in the traversal |
| 5395 | * |
| 5396 | * Traversal function for the "descendant-or-self" direction |
| 5397 | * the descendant-or-self axis contains the context node and the descendants |
| 5398 | * of the context node in document order; thus the context node is the first |
| 5399 | * node on the axis, and the first child of the context node is the second node |
| 5400 | * on the axis |
| 5401 | * |
| 5402 | * Returns the next element following that axis |
| 5403 | */ |
| 5404 | xmlNodePtr |
| 5405 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5406 | if (cur == NULL) { |
| 5407 | if (ctxt->context->node == NULL) |
| 5408 | return(NULL); |
| 5409 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5410 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5411 | return(NULL); |
| 5412 | return(ctxt->context->node); |
| 5413 | } |
| 5414 | |
| 5415 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5416 | } |
| 5417 | |
| 5418 | /** |
| 5419 | * xmlXPathNextParent: |
| 5420 | * @ctxt: the XPath Parser context |
| 5421 | * @cur: the current node in the traversal |
| 5422 | * |
| 5423 | * Traversal function for the "parent" direction |
| 5424 | * The parent axis contains the parent of the context node, if there is one. |
| 5425 | * |
| 5426 | * Returns the next element following that axis |
| 5427 | */ |
| 5428 | xmlNodePtr |
| 5429 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5430 | /* |
| 5431 | * the parent of an attribute or namespace node is the element |
| 5432 | * to which the attribute or namespace node is attached |
| 5433 | * Namespace handling !!! |
| 5434 | */ |
| 5435 | if (cur == NULL) { |
| 5436 | if (ctxt->context->node == NULL) return(NULL); |
| 5437 | switch (ctxt->context->node->type) { |
| 5438 | case XML_ELEMENT_NODE: |
| 5439 | case XML_TEXT_NODE: |
| 5440 | case XML_CDATA_SECTION_NODE: |
| 5441 | case XML_ENTITY_REF_NODE: |
| 5442 | case XML_ENTITY_NODE: |
| 5443 | case XML_PI_NODE: |
| 5444 | case XML_COMMENT_NODE: |
| 5445 | case XML_NOTATION_NODE: |
| 5446 | case XML_DTD_NODE: |
| 5447 | case XML_ELEMENT_DECL: |
| 5448 | case XML_ATTRIBUTE_DECL: |
| 5449 | case XML_XINCLUDE_START: |
| 5450 | case XML_XINCLUDE_END: |
| 5451 | case XML_ENTITY_DECL: |
| 5452 | if (ctxt->context->node->parent == NULL) |
| 5453 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5454 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5455 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5456 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5457 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5458 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5459 | return(ctxt->context->node->parent); |
| 5460 | case XML_ATTRIBUTE_NODE: { |
| 5461 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5462 | |
| 5463 | return(att->parent); |
| 5464 | } |
| 5465 | case XML_DOCUMENT_NODE: |
| 5466 | case XML_DOCUMENT_TYPE_NODE: |
| 5467 | case XML_DOCUMENT_FRAG_NODE: |
| 5468 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5469 | #ifdef LIBXML_DOCB_ENABLED |
| 5470 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5471 | #endif |
| 5472 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5473 | case XML_NAMESPACE_DECL: { |
| 5474 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5475 | |
| 5476 | if ((ns->next != NULL) && |
| 5477 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5478 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5479 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5480 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | return(NULL); |
| 5484 | } |
| 5485 | |
| 5486 | /** |
| 5487 | * xmlXPathNextAncestor: |
| 5488 | * @ctxt: the XPath Parser context |
| 5489 | * @cur: the current node in the traversal |
| 5490 | * |
| 5491 | * Traversal function for the "ancestor" direction |
| 5492 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5493 | * of the context node consist of the parent of context node and the parent's |
| 5494 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5495 | * parent is the first node on the axis, and the parent's parent is the second |
| 5496 | * node on the axis |
| 5497 | * |
| 5498 | * Returns the next element following that axis |
| 5499 | */ |
| 5500 | xmlNodePtr |
| 5501 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5502 | /* |
| 5503 | * the parent of an attribute or namespace node is the element |
| 5504 | * to which the attribute or namespace node is attached |
| 5505 | * !!!!!!!!!!!!! |
| 5506 | */ |
| 5507 | if (cur == NULL) { |
| 5508 | if (ctxt->context->node == NULL) return(NULL); |
| 5509 | switch (ctxt->context->node->type) { |
| 5510 | case XML_ELEMENT_NODE: |
| 5511 | case XML_TEXT_NODE: |
| 5512 | case XML_CDATA_SECTION_NODE: |
| 5513 | case XML_ENTITY_REF_NODE: |
| 5514 | case XML_ENTITY_NODE: |
| 5515 | case XML_PI_NODE: |
| 5516 | case XML_COMMENT_NODE: |
| 5517 | case XML_DTD_NODE: |
| 5518 | case XML_ELEMENT_DECL: |
| 5519 | case XML_ATTRIBUTE_DECL: |
| 5520 | case XML_ENTITY_DECL: |
| 5521 | case XML_NOTATION_NODE: |
| 5522 | case XML_XINCLUDE_START: |
| 5523 | case XML_XINCLUDE_END: |
| 5524 | if (ctxt->context->node->parent == NULL) |
| 5525 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5526 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5527 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5528 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5529 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5530 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5531 | return(ctxt->context->node->parent); |
| 5532 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5533 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5534 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5535 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5536 | } |
| 5537 | case XML_DOCUMENT_NODE: |
| 5538 | case XML_DOCUMENT_TYPE_NODE: |
| 5539 | case XML_DOCUMENT_FRAG_NODE: |
| 5540 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5541 | #ifdef LIBXML_DOCB_ENABLED |
| 5542 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5543 | #endif |
| 5544 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5545 | case XML_NAMESPACE_DECL: { |
| 5546 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5547 | |
| 5548 | if ((ns->next != NULL) && |
| 5549 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5550 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5551 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5552 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5553 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5554 | } |
| 5555 | return(NULL); |
| 5556 | } |
| 5557 | if (cur == ctxt->context->doc->children) |
| 5558 | return((xmlNodePtr) ctxt->context->doc); |
| 5559 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5560 | return(NULL); |
| 5561 | switch (cur->type) { |
| 5562 | case XML_ELEMENT_NODE: |
| 5563 | case XML_TEXT_NODE: |
| 5564 | case XML_CDATA_SECTION_NODE: |
| 5565 | case XML_ENTITY_REF_NODE: |
| 5566 | case XML_ENTITY_NODE: |
| 5567 | case XML_PI_NODE: |
| 5568 | case XML_COMMENT_NODE: |
| 5569 | case XML_NOTATION_NODE: |
| 5570 | case XML_DTD_NODE: |
| 5571 | case XML_ELEMENT_DECL: |
| 5572 | case XML_ATTRIBUTE_DECL: |
| 5573 | case XML_ENTITY_DECL: |
| 5574 | case XML_XINCLUDE_START: |
| 5575 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5576 | if (cur->parent == NULL) |
| 5577 | return(NULL); |
| 5578 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5579 | ((cur->parent->name[0] == ' ') || |
| 5580 | (xmlStrEqual(cur->parent->name, |
| 5581 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5582 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5583 | return(cur->parent); |
| 5584 | case XML_ATTRIBUTE_NODE: { |
| 5585 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5586 | |
| 5587 | return(att->parent); |
| 5588 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5589 | case XML_NAMESPACE_DECL: { |
| 5590 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5591 | |
| 5592 | if ((ns->next != NULL) && |
| 5593 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5594 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5595 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5596 | return(NULL); |
| 5597 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5598 | case XML_DOCUMENT_NODE: |
| 5599 | case XML_DOCUMENT_TYPE_NODE: |
| 5600 | case XML_DOCUMENT_FRAG_NODE: |
| 5601 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5602 | #ifdef LIBXML_DOCB_ENABLED |
| 5603 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5604 | #endif |
| 5605 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | } |
| 5607 | return(NULL); |
| 5608 | } |
| 5609 | |
| 5610 | /** |
| 5611 | * xmlXPathNextAncestorOrSelf: |
| 5612 | * @ctxt: the XPath Parser context |
| 5613 | * @cur: the current node in the traversal |
| 5614 | * |
| 5615 | * Traversal function for the "ancestor-or-self" direction |
| 5616 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5617 | * the context node in reverse document order; thus the context node is |
| 5618 | * the first node on the axis, and the context node's parent the second; |
| 5619 | * parent here is defined the same as with the parent axis. |
| 5620 | * |
| 5621 | * Returns the next element following that axis |
| 5622 | */ |
| 5623 | xmlNodePtr |
| 5624 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5625 | if (cur == NULL) |
| 5626 | return(ctxt->context->node); |
| 5627 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5628 | } |
| 5629 | |
| 5630 | /** |
| 5631 | * xmlXPathNextFollowingSibling: |
| 5632 | * @ctxt: the XPath Parser context |
| 5633 | * @cur: the current node in the traversal |
| 5634 | * |
| 5635 | * Traversal function for the "following-sibling" direction |
| 5636 | * The following-sibling axis contains the following siblings of the context |
| 5637 | * node in document order. |
| 5638 | * |
| 5639 | * Returns the next element following that axis |
| 5640 | */ |
| 5641 | xmlNodePtr |
| 5642 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5643 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5644 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5645 | return(NULL); |
| 5646 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5647 | return(NULL); |
| 5648 | if (cur == NULL) |
| 5649 | return(ctxt->context->node->next); |
| 5650 | return(cur->next); |
| 5651 | } |
| 5652 | |
| 5653 | /** |
| 5654 | * xmlXPathNextPrecedingSibling: |
| 5655 | * @ctxt: the XPath Parser context |
| 5656 | * @cur: the current node in the traversal |
| 5657 | * |
| 5658 | * Traversal function for the "preceding-sibling" direction |
| 5659 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5660 | * node in reverse document order; the first preceding sibling is first on the |
| 5661 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5662 | * |
| 5663 | * Returns the next element following that axis |
| 5664 | */ |
| 5665 | xmlNodePtr |
| 5666 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5667 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5668 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5669 | return(NULL); |
| 5670 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5671 | return(NULL); |
| 5672 | if (cur == NULL) |
| 5673 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5674 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5675 | cur = cur->prev; |
| 5676 | if (cur == NULL) |
| 5677 | return(ctxt->context->node->prev); |
| 5678 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5679 | return(cur->prev); |
| 5680 | } |
| 5681 | |
| 5682 | /** |
| 5683 | * xmlXPathNextFollowing: |
| 5684 | * @ctxt: the XPath Parser context |
| 5685 | * @cur: the current node in the traversal |
| 5686 | * |
| 5687 | * Traversal function for the "following" direction |
| 5688 | * The following axis contains all nodes in the same document as the context |
| 5689 | * node that are after the context node in document order, excluding any |
| 5690 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5691 | * are ordered in document order |
| 5692 | * |
| 5693 | * Returns the next element following that axis |
| 5694 | */ |
| 5695 | xmlNodePtr |
| 5696 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5697 | if (cur != NULL && cur->children != NULL) |
| 5698 | return cur->children ; |
| 5699 | if (cur == NULL) cur = ctxt->context->node; |
| 5700 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5701 | if (cur->next != NULL) return(cur->next) ; |
| 5702 | do { |
| 5703 | cur = cur->parent; |
| 5704 | if (cur == NULL) return(NULL); |
| 5705 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5706 | if (cur->next != NULL) return(cur->next); |
| 5707 | } while (cur != NULL); |
| 5708 | return(cur); |
| 5709 | } |
| 5710 | |
| 5711 | /* |
| 5712 | * xmlXPathIsAncestor: |
| 5713 | * @ancestor: the ancestor node |
| 5714 | * @node: the current node |
| 5715 | * |
| 5716 | * Check that @ancestor is a @node's ancestor |
| 5717 | * |
| 5718 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5719 | */ |
| 5720 | static int |
| 5721 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5722 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5723 | /* nodes need to be in the same document */ |
| 5724 | if (ancestor->doc != node->doc) return(0); |
| 5725 | /* avoid searching if ancestor or node is the root node */ |
| 5726 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5727 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5728 | while (node->parent != NULL) { |
| 5729 | if (node->parent == ancestor) |
| 5730 | return(1); |
| 5731 | node = node->parent; |
| 5732 | } |
| 5733 | return(0); |
| 5734 | } |
| 5735 | |
| 5736 | /** |
| 5737 | * xmlXPathNextPreceding: |
| 5738 | * @ctxt: the XPath Parser context |
| 5739 | * @cur: the current node in the traversal |
| 5740 | * |
| 5741 | * Traversal function for the "preceding" direction |
| 5742 | * the preceding axis contains all nodes in the same document as the context |
| 5743 | * node that are before the context node in document order, excluding any |
| 5744 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5745 | * ordered in reverse document order |
| 5746 | * |
| 5747 | * Returns the next element following that axis |
| 5748 | */ |
| 5749 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5750 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5751 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5752 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5753 | cur = ctxt->context->node; |
| 5754 | if (cur == NULL) |
| 5755 | return (NULL); |
| 5756 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5757 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | do { |
| 5759 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5760 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5761 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5765 | if (cur == NULL) |
| 5766 | return (NULL); |
| 5767 | if (cur == ctxt->context->doc->children) |
| 5768 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5769 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5770 | return (cur); |
| 5771 | } |
| 5772 | |
| 5773 | /** |
| 5774 | * xmlXPathNextPrecedingInternal: |
| 5775 | * @ctxt: the XPath Parser context |
| 5776 | * @cur: the current node in the traversal |
| 5777 | * |
| 5778 | * Traversal function for the "preceding" direction |
| 5779 | * the preceding axis contains all nodes in the same document as the context |
| 5780 | * node that are before the context node in document order, excluding any |
| 5781 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5782 | * ordered in reverse document order |
| 5783 | * This is a faster implementation but internal only since it requires a |
| 5784 | * state kept in the parser context: ctxt->ancestor. |
| 5785 | * |
| 5786 | * Returns the next element following that axis |
| 5787 | */ |
| 5788 | static xmlNodePtr |
| 5789 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5790 | xmlNodePtr cur) |
| 5791 | { |
| 5792 | if (cur == NULL) { |
| 5793 | cur = ctxt->context->node; |
| 5794 | if (cur == NULL) |
| 5795 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 5796 | if (cur->type == XML_NAMESPACE_DECL) |
| 5797 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5798 | ctxt->ancestor = cur->parent; |
| 5799 | } |
| 5800 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5801 | cur = cur->prev; |
| 5802 | while (cur->prev == NULL) { |
| 5803 | cur = cur->parent; |
| 5804 | if (cur == NULL) |
| 5805 | return (NULL); |
| 5806 | if (cur == ctxt->context->doc->children) |
| 5807 | return (NULL); |
| 5808 | if (cur != ctxt->ancestor) |
| 5809 | return (cur); |
| 5810 | ctxt->ancestor = cur->parent; |
| 5811 | } |
| 5812 | cur = cur->prev; |
| 5813 | while (cur->last != NULL) |
| 5814 | cur = cur->last; |
| 5815 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | /** |
| 5819 | * xmlXPathNextNamespace: |
| 5820 | * @ctxt: the XPath Parser context |
| 5821 | * @cur: the current attribute in the traversal |
| 5822 | * |
| 5823 | * Traversal function for the "namespace" direction |
| 5824 | * the namespace axis contains the namespace nodes of the context node; |
| 5825 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5826 | * be empty unless the context node is an element |
| 5827 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5828 | * We keep the XML namespace node at the end of the list. |
| 5829 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5830 | * Returns the next element following that axis |
| 5831 | */ |
| 5832 | xmlNodePtr |
| 5833 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 5834 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5835 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5836 | if (ctxt->context->tmpNsList != NULL) |
| 5837 | xmlFree(ctxt->context->tmpNsList); |
| 5838 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5839 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5840 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5841 | if (ctxt->context->tmpNsList != NULL) { |
| 5842 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 5843 | ctxt->context->tmpNsNr++; |
| 5844 | } |
| 5845 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5846 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5847 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5848 | if (ctxt->context->tmpNsNr > 0) { |
| 5849 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 5850 | } else { |
| 5851 | if (ctxt->context->tmpNsList != NULL) |
| 5852 | xmlFree(ctxt->context->tmpNsList); |
| 5853 | ctxt->context->tmpNsList = NULL; |
| 5854 | return(NULL); |
| 5855 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | /** |
| 5859 | * xmlXPathNextAttribute: |
| 5860 | * @ctxt: the XPath Parser context |
| 5861 | * @cur: the current attribute in the traversal |
| 5862 | * |
| 5863 | * Traversal function for the "attribute" direction |
| 5864 | * TODO: support DTD inherited default attributes |
| 5865 | * |
| 5866 | * Returns the next element following that axis |
| 5867 | */ |
| 5868 | xmlNodePtr |
| 5869 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5870 | if (ctxt->context->node == NULL) |
| 5871 | return(NULL); |
| 5872 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 5873 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | if (cur == NULL) { |
| 5875 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5876 | return(NULL); |
| 5877 | return((xmlNodePtr)ctxt->context->node->properties); |
| 5878 | } |
| 5879 | return((xmlNodePtr)cur->next); |
| 5880 | } |
| 5881 | |
| 5882 | /************************************************************************ |
| 5883 | * * |
| 5884 | * NodeTest Functions * |
| 5885 | * * |
| 5886 | ************************************************************************/ |
| 5887 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | #define IS_FUNCTION 200 |
| 5889 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5890 | |
| 5891 | /************************************************************************ |
| 5892 | * * |
| 5893 | * Implicit tree core function library * |
| 5894 | * * |
| 5895 | ************************************************************************/ |
| 5896 | |
| 5897 | /** |
| 5898 | * xmlXPathRoot: |
| 5899 | * @ctxt: the XPath Parser context |
| 5900 | * |
| 5901 | * Initialize the context to the root of the document |
| 5902 | */ |
| 5903 | void |
| 5904 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
| 5905 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 5906 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5907 | } |
| 5908 | |
| 5909 | /************************************************************************ |
| 5910 | * * |
| 5911 | * The explicit core function library * |
| 5912 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 5913 | * * |
| 5914 | ************************************************************************/ |
| 5915 | |
| 5916 | |
| 5917 | /** |
| 5918 | * xmlXPathLastFunction: |
| 5919 | * @ctxt: the XPath Parser context |
| 5920 | * @nargs: the number of arguments |
| 5921 | * |
| 5922 | * Implement the last() XPath function |
| 5923 | * number last() |
| 5924 | * The last function returns the number of nodes in the context node list. |
| 5925 | */ |
| 5926 | void |
| 5927 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5928 | CHECK_ARITY(0); |
| 5929 | if (ctxt->context->contextSize >= 0) { |
| 5930 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 5931 | #ifdef DEBUG_EXPR |
| 5932 | xmlGenericError(xmlGenericErrorContext, |
| 5933 | "last() : %d\n", ctxt->context->contextSize); |
| 5934 | #endif |
| 5935 | } else { |
| 5936 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | /** |
| 5941 | * xmlXPathPositionFunction: |
| 5942 | * @ctxt: the XPath Parser context |
| 5943 | * @nargs: the number of arguments |
| 5944 | * |
| 5945 | * Implement the position() XPath function |
| 5946 | * number position() |
| 5947 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5948 | * 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] | 5949 | * will be equal to last(). |
| 5950 | */ |
| 5951 | void |
| 5952 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5953 | CHECK_ARITY(0); |
| 5954 | if (ctxt->context->proximityPosition >= 0) { |
| 5955 | valuePush(ctxt, |
| 5956 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 5957 | #ifdef DEBUG_EXPR |
| 5958 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 5959 | ctxt->context->proximityPosition); |
| 5960 | #endif |
| 5961 | } else { |
| 5962 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | /** |
| 5967 | * xmlXPathCountFunction: |
| 5968 | * @ctxt: the XPath Parser context |
| 5969 | * @nargs: the number of arguments |
| 5970 | * |
| 5971 | * Implement the count() XPath function |
| 5972 | * number count(node-set) |
| 5973 | */ |
| 5974 | void |
| 5975 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 5976 | xmlXPathObjectPtr cur; |
| 5977 | |
| 5978 | CHECK_ARITY(1); |
| 5979 | if ((ctxt->value == NULL) || |
| 5980 | ((ctxt->value->type != XPATH_NODESET) && |
| 5981 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 5982 | XP_ERROR(XPATH_INVALID_TYPE); |
| 5983 | cur = valuePop(ctxt); |
| 5984 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5985 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 5986 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5987 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5988 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 5989 | } else { |
| 5990 | if ((cur->nodesetval->nodeNr != 1) || |
| 5991 | (cur->nodesetval->nodeTab == NULL)) { |
| 5992 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 5993 | } else { |
| 5994 | xmlNodePtr tmp; |
| 5995 | int i = 0; |
| 5996 | |
| 5997 | tmp = cur->nodesetval->nodeTab[0]; |
| 5998 | if (tmp != NULL) { |
| 5999 | tmp = tmp->children; |
| 6000 | while (tmp != NULL) { |
| 6001 | tmp = tmp->next; |
| 6002 | i++; |
| 6003 | } |
| 6004 | } |
| 6005 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6006 | } |
| 6007 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6008 | xmlXPathFreeObject(cur); |
| 6009 | } |
| 6010 | |
| 6011 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6012 | * xmlXPathGetElementsByIds: |
| 6013 | * @doc: the document |
| 6014 | * @ids: a whitespace separated list of IDs |
| 6015 | * |
| 6016 | * Selects elements by their unique ID. |
| 6017 | * |
| 6018 | * Returns a node-set of selected elements. |
| 6019 | */ |
| 6020 | static xmlNodeSetPtr |
| 6021 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6022 | xmlNodeSetPtr ret; |
| 6023 | const xmlChar *cur = ids; |
| 6024 | xmlChar *ID; |
| 6025 | xmlAttrPtr attr; |
| 6026 | xmlNodePtr elem = NULL; |
| 6027 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6028 | if (ids == NULL) return(NULL); |
| 6029 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6030 | ret = xmlXPathNodeSetCreate(NULL); |
| 6031 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6032 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6033 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6034 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6035 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6036 | |
| 6037 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6038 | if (ID != NULL) { |
| 6039 | if (xmlValidateNCName(ID, 1) == 0) { |
| 6040 | attr = xmlGetID(doc, ID); |
| 6041 | if (attr != NULL) { |
| 6042 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6043 | elem = attr->parent; |
| 6044 | else if (attr->type == XML_ELEMENT_NODE) |
| 6045 | elem = (xmlNodePtr) attr; |
| 6046 | else |
| 6047 | elem = NULL; |
| 6048 | if (elem != NULL) |
| 6049 | xmlXPathNodeSetAdd(ret, elem); |
| 6050 | } |
| 6051 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6052 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6053 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6054 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6055 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6056 | ids = cur; |
| 6057 | } |
| 6058 | return(ret); |
| 6059 | } |
| 6060 | |
| 6061 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6062 | * xmlXPathIdFunction: |
| 6063 | * @ctxt: the XPath Parser context |
| 6064 | * @nargs: the number of arguments |
| 6065 | * |
| 6066 | * Implement the id() XPath function |
| 6067 | * node-set id(object) |
| 6068 | * The id function selects elements by their unique ID |
| 6069 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6070 | * then the result is the union of the result of applying id to the |
| 6071 | * string value of each of the nodes in the argument node-set. When the |
| 6072 | * argument to id is of any other type, the argument is converted to a |
| 6073 | * string as if by a call to the string function; the string is split |
| 6074 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6075 | * of characters matching the production S); the result is a node-set |
| 6076 | * containing the elements in the same document as the context node that |
| 6077 | * have a unique ID equal to any of the tokens in the list. |
| 6078 | */ |
| 6079 | void |
| 6080 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6081 | xmlChar *tokens; |
| 6082 | xmlNodeSetPtr ret; |
| 6083 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6084 | |
| 6085 | CHECK_ARITY(1); |
| 6086 | obj = valuePop(ctxt); |
| 6087 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6088 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6089 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6090 | int i; |
| 6091 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6092 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6093 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6094 | if (obj->nodesetval != NULL) { |
| 6095 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6096 | tokens = |
| 6097 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6098 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6099 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6100 | xmlXPathFreeNodeSet(ns); |
| 6101 | if (tokens != NULL) |
| 6102 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6107 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6108 | return; |
| 6109 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6110 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6111 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6112 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6113 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6114 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | xmlXPathFreeObject(obj); |
| 6116 | return; |
| 6117 | } |
| 6118 | |
| 6119 | /** |
| 6120 | * xmlXPathLocalNameFunction: |
| 6121 | * @ctxt: the XPath Parser context |
| 6122 | * @nargs: the number of arguments |
| 6123 | * |
| 6124 | * Implement the local-name() XPath function |
| 6125 | * string local-name(node-set?) |
| 6126 | * The local-name function returns a string containing the local part |
| 6127 | * of the name of the node in the argument node-set that is first in |
| 6128 | * document order. If the node-set is empty or the first node has no |
| 6129 | * name, an empty string is returned. If the argument is omitted it |
| 6130 | * defaults to the context node. |
| 6131 | */ |
| 6132 | void |
| 6133 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6134 | xmlXPathObjectPtr cur; |
| 6135 | |
| 6136 | if (nargs == 0) { |
| 6137 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6138 | nargs = 1; |
| 6139 | } |
| 6140 | |
| 6141 | CHECK_ARITY(1); |
| 6142 | if ((ctxt->value == NULL) || |
| 6143 | ((ctxt->value->type != XPATH_NODESET) && |
| 6144 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6145 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6146 | cur = valuePop(ctxt); |
| 6147 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6148 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6149 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6150 | } else { |
| 6151 | int i = 0; /* Should be first in document order !!!!! */ |
| 6152 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6153 | case XML_ELEMENT_NODE: |
| 6154 | case XML_ATTRIBUTE_NODE: |
| 6155 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6156 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6157 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6158 | else |
| 6159 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6160 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6161 | break; |
| 6162 | case XML_NAMESPACE_DECL: |
| 6163 | valuePush(ctxt, xmlXPathNewString( |
| 6164 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6165 | break; |
| 6166 | default: |
| 6167 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6168 | } |
| 6169 | } |
| 6170 | xmlXPathFreeObject(cur); |
| 6171 | } |
| 6172 | |
| 6173 | /** |
| 6174 | * xmlXPathNamespaceURIFunction: |
| 6175 | * @ctxt: the XPath Parser context |
| 6176 | * @nargs: the number of arguments |
| 6177 | * |
| 6178 | * Implement the namespace-uri() XPath function |
| 6179 | * string namespace-uri(node-set?) |
| 6180 | * The namespace-uri function returns a string containing the |
| 6181 | * namespace URI of the expanded name of the node in the argument |
| 6182 | * node-set that is first in document order. If the node-set is empty, |
| 6183 | * the first node has no name, or the expanded name has no namespace |
| 6184 | * URI, an empty string is returned. If the argument is omitted it |
| 6185 | * defaults to the context node. |
| 6186 | */ |
| 6187 | void |
| 6188 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6189 | xmlXPathObjectPtr cur; |
| 6190 | |
| 6191 | if (nargs == 0) { |
| 6192 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6193 | nargs = 1; |
| 6194 | } |
| 6195 | CHECK_ARITY(1); |
| 6196 | if ((ctxt->value == NULL) || |
| 6197 | ((ctxt->value->type != XPATH_NODESET) && |
| 6198 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6199 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6200 | cur = valuePop(ctxt); |
| 6201 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6202 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6203 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6204 | } else { |
| 6205 | int i = 0; /* Should be first in document order !!!!! */ |
| 6206 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6207 | case XML_ELEMENT_NODE: |
| 6208 | case XML_ATTRIBUTE_NODE: |
| 6209 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6210 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6211 | else |
| 6212 | valuePush(ctxt, xmlXPathNewString( |
| 6213 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6214 | break; |
| 6215 | default: |
| 6216 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6217 | } |
| 6218 | } |
| 6219 | xmlXPathFreeObject(cur); |
| 6220 | } |
| 6221 | |
| 6222 | /** |
| 6223 | * xmlXPathNameFunction: |
| 6224 | * @ctxt: the XPath Parser context |
| 6225 | * @nargs: the number of arguments |
| 6226 | * |
| 6227 | * Implement the name() XPath function |
| 6228 | * string name(node-set?) |
| 6229 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6230 | * 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] | 6231 | * order. The QName must represent the name with respect to the namespace |
| 6232 | * declarations in effect on the node whose name is being represented. |
| 6233 | * Typically, this will be the form in which the name occurred in the XML |
| 6234 | * source. This need not be the case if there are namespace declarations |
| 6235 | * in effect on the node that associate multiple prefixes with the same |
| 6236 | * namespace. However, an implementation may include information about |
| 6237 | * the original prefix in its representation of nodes; in this case, an |
| 6238 | * implementation can ensure that the returned string is always the same |
| 6239 | * as the QName used in the XML source. If the argument it omitted it |
| 6240 | * defaults to the context node. |
| 6241 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6242 | * returned. |
| 6243 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6244 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6245 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6246 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6247 | xmlXPathObjectPtr cur; |
| 6248 | |
| 6249 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6250 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6251 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6255 | if ((ctxt->value == NULL) || |
| 6256 | ((ctxt->value->type != XPATH_NODESET) && |
| 6257 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6258 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | cur = valuePop(ctxt); |
| 6260 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6261 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6262 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6264 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6265 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6266 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6267 | case XML_ELEMENT_NODE: |
| 6268 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6269 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6270 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6271 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6272 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6273 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6274 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6275 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6276 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6277 | xmlChar *fullname; |
| 6278 | |
| 6279 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6280 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6281 | NULL, 0); |
| 6282 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6283 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6284 | if (fullname == NULL) { |
| 6285 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6286 | } |
| 6287 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6288 | } |
| 6289 | break; |
| 6290 | default: |
| 6291 | valuePush(ctxt, |
| 6292 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6293 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6294 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6295 | } |
| 6296 | xmlXPathFreeObject(cur); |
| 6297 | } |
| 6298 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6299 | |
| 6300 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6301 | * xmlXPathStringFunction: |
| 6302 | * @ctxt: the XPath Parser context |
| 6303 | * @nargs: the number of arguments |
| 6304 | * |
| 6305 | * Implement the string() XPath function |
| 6306 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6307 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6308 | * - A node-set is converted to a string by returning the value of |
| 6309 | * the node in the node-set that is first in document order. |
| 6310 | * If the node-set is empty, an empty string is returned. |
| 6311 | * - A number is converted to a string as follows |
| 6312 | * + NaN is converted to the string NaN |
| 6313 | * + positive zero is converted to the string 0 |
| 6314 | * + negative zero is converted to the string 0 |
| 6315 | * + positive infinity is converted to the string Infinity |
| 6316 | * + negative infinity is converted to the string -Infinity |
| 6317 | * + if the number is an integer, the number is represented in |
| 6318 | * decimal form as a Number with no decimal point and no leading |
| 6319 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6320 | * + otherwise, the number is represented in decimal form as a |
| 6321 | * Number including a decimal point with at least one digit |
| 6322 | * before the decimal point and at least one digit after the |
| 6323 | * decimal point, preceded by a minus sign (-) if the number |
| 6324 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6325 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6326 | * before the decimal point; beyond the one required digit |
| 6327 | * after the decimal point there must be as many, but only as |
| 6328 | * many, more digits as are needed to uniquely distinguish the |
| 6329 | * number from all other IEEE 754 numeric values. |
| 6330 | * - The boolean false value is converted to the string false. |
| 6331 | * The boolean true value is converted to the string true. |
| 6332 | * |
| 6333 | * If the argument is omitted, it defaults to a node-set with the |
| 6334 | * context node as its only member. |
| 6335 | */ |
| 6336 | void |
| 6337 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6338 | xmlXPathObjectPtr cur; |
| 6339 | |
| 6340 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6341 | valuePush(ctxt, |
| 6342 | xmlXPathWrapString( |
| 6343 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6344 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
| 6347 | CHECK_ARITY(1); |
| 6348 | cur = valuePop(ctxt); |
| 6349 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6350 | cur = xmlXPathConvertString(cur); |
| 6351 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | /** |
| 6355 | * xmlXPathStringLengthFunction: |
| 6356 | * @ctxt: the XPath Parser context |
| 6357 | * @nargs: the number of arguments |
| 6358 | * |
| 6359 | * Implement the string-length() XPath function |
| 6360 | * number string-length(string?) |
| 6361 | * The string-length returns the number of characters in the string |
| 6362 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6363 | * the context node converted to a string, in other words the value |
| 6364 | * of the context node. |
| 6365 | */ |
| 6366 | void |
| 6367 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6368 | xmlXPathObjectPtr cur; |
| 6369 | |
| 6370 | if (nargs == 0) { |
| 6371 | if (ctxt->context->node == NULL) { |
| 6372 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6373 | } else { |
| 6374 | xmlChar *content; |
| 6375 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6376 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6377 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6378 | xmlFree(content); |
| 6379 | } |
| 6380 | return; |
| 6381 | } |
| 6382 | CHECK_ARITY(1); |
| 6383 | CAST_TO_STRING; |
| 6384 | CHECK_TYPE(XPATH_STRING); |
| 6385 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6386 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6387 | xmlXPathFreeObject(cur); |
| 6388 | } |
| 6389 | |
| 6390 | /** |
| 6391 | * xmlXPathConcatFunction: |
| 6392 | * @ctxt: the XPath Parser context |
| 6393 | * @nargs: the number of arguments |
| 6394 | * |
| 6395 | * Implement the concat() XPath function |
| 6396 | * string concat(string, string, string*) |
| 6397 | * The concat function returns the concatenation of its arguments. |
| 6398 | */ |
| 6399 | void |
| 6400 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6401 | xmlXPathObjectPtr cur, newobj; |
| 6402 | xmlChar *tmp; |
| 6403 | |
| 6404 | if (nargs < 2) { |
| 6405 | CHECK_ARITY(2); |
| 6406 | } |
| 6407 | |
| 6408 | CAST_TO_STRING; |
| 6409 | cur = valuePop(ctxt); |
| 6410 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6411 | xmlXPathFreeObject(cur); |
| 6412 | return; |
| 6413 | } |
| 6414 | nargs--; |
| 6415 | |
| 6416 | while (nargs > 0) { |
| 6417 | CAST_TO_STRING; |
| 6418 | newobj = valuePop(ctxt); |
| 6419 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6420 | xmlXPathFreeObject(newobj); |
| 6421 | xmlXPathFreeObject(cur); |
| 6422 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6423 | } |
| 6424 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6425 | newobj->stringval = cur->stringval; |
| 6426 | cur->stringval = tmp; |
| 6427 | |
| 6428 | xmlXPathFreeObject(newobj); |
| 6429 | nargs--; |
| 6430 | } |
| 6431 | valuePush(ctxt, cur); |
| 6432 | } |
| 6433 | |
| 6434 | /** |
| 6435 | * xmlXPathContainsFunction: |
| 6436 | * @ctxt: the XPath Parser context |
| 6437 | * @nargs: the number of arguments |
| 6438 | * |
| 6439 | * Implement the contains() XPath function |
| 6440 | * boolean contains(string, string) |
| 6441 | * The contains function returns true if the first argument string |
| 6442 | * contains the second argument string, and otherwise returns false. |
| 6443 | */ |
| 6444 | void |
| 6445 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6446 | xmlXPathObjectPtr hay, needle; |
| 6447 | |
| 6448 | CHECK_ARITY(2); |
| 6449 | CAST_TO_STRING; |
| 6450 | CHECK_TYPE(XPATH_STRING); |
| 6451 | needle = valuePop(ctxt); |
| 6452 | CAST_TO_STRING; |
| 6453 | hay = valuePop(ctxt); |
| 6454 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6455 | xmlXPathFreeObject(hay); |
| 6456 | xmlXPathFreeObject(needle); |
| 6457 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6458 | } |
| 6459 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6460 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6461 | else |
| 6462 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6463 | xmlXPathFreeObject(hay); |
| 6464 | xmlXPathFreeObject(needle); |
| 6465 | } |
| 6466 | |
| 6467 | /** |
| 6468 | * xmlXPathStartsWithFunction: |
| 6469 | * @ctxt: the XPath Parser context |
| 6470 | * @nargs: the number of arguments |
| 6471 | * |
| 6472 | * Implement the starts-with() XPath function |
| 6473 | * boolean starts-with(string, string) |
| 6474 | * The starts-with function returns true if the first argument string |
| 6475 | * starts with the second argument string, and otherwise returns false. |
| 6476 | */ |
| 6477 | void |
| 6478 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6479 | xmlXPathObjectPtr hay, needle; |
| 6480 | int n; |
| 6481 | |
| 6482 | CHECK_ARITY(2); |
| 6483 | CAST_TO_STRING; |
| 6484 | CHECK_TYPE(XPATH_STRING); |
| 6485 | needle = valuePop(ctxt); |
| 6486 | CAST_TO_STRING; |
| 6487 | hay = valuePop(ctxt); |
| 6488 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6489 | xmlXPathFreeObject(hay); |
| 6490 | xmlXPathFreeObject(needle); |
| 6491 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6492 | } |
| 6493 | n = xmlStrlen(needle->stringval); |
| 6494 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6495 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6496 | else |
| 6497 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6498 | xmlXPathFreeObject(hay); |
| 6499 | xmlXPathFreeObject(needle); |
| 6500 | } |
| 6501 | |
| 6502 | /** |
| 6503 | * xmlXPathSubstringFunction: |
| 6504 | * @ctxt: the XPath Parser context |
| 6505 | * @nargs: the number of arguments |
| 6506 | * |
| 6507 | * Implement the substring() XPath function |
| 6508 | * string substring(string, number, number?) |
| 6509 | * The substring function returns the substring of the first argument |
| 6510 | * starting at the position specified in the second argument with |
| 6511 | * length specified in the third argument. For example, |
| 6512 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6513 | * specified, it returns the substring starting at the position specified |
| 6514 | * in the second argument and continuing to the end of the string. For |
| 6515 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6516 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6517 | * numeric position: the position of the first character is 1, the position |
| 6518 | * of the second character is 2 and so on. The returned substring contains |
| 6519 | * those characters for which the position of the character is greater than |
| 6520 | * or equal to the second argument and, if the third argument is specified, |
| 6521 | * less than the sum of the second and third arguments; the comparisons |
| 6522 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6523 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6524 | * - substring("12345", 0, 3) returns "12" |
| 6525 | * - substring("12345", 0 div 0, 3) returns "" |
| 6526 | * - substring("12345", 1, 0 div 0) returns "" |
| 6527 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6528 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6529 | */ |
| 6530 | void |
| 6531 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6532 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6533 | double le=0, in; |
| 6534 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6535 | xmlChar *ret; |
| 6536 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | if (nargs < 2) { |
| 6538 | CHECK_ARITY(2); |
| 6539 | } |
| 6540 | if (nargs > 3) { |
| 6541 | CHECK_ARITY(3); |
| 6542 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6543 | /* |
| 6544 | * take care of possible last (position) argument |
| 6545 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6546 | if (nargs == 3) { |
| 6547 | CAST_TO_NUMBER; |
| 6548 | CHECK_TYPE(XPATH_NUMBER); |
| 6549 | len = valuePop(ctxt); |
| 6550 | le = len->floatval; |
| 6551 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6552 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6553 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6554 | CAST_TO_NUMBER; |
| 6555 | CHECK_TYPE(XPATH_NUMBER); |
| 6556 | start = valuePop(ctxt); |
| 6557 | in = start->floatval; |
| 6558 | xmlXPathFreeObject(start); |
| 6559 | CAST_TO_STRING; |
| 6560 | CHECK_TYPE(XPATH_STRING); |
| 6561 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6562 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6563 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6564 | /* |
| 6565 | * If last pos not present, calculate last position |
| 6566 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6567 | if (nargs != 3) { |
| 6568 | le = (double)m; |
| 6569 | if (in < 1.0) |
| 6570 | in = 1.0; |
| 6571 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6572 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6573 | /* Need to check for the special cases where either |
| 6574 | * the index is NaN, the length is NaN, or both |
| 6575 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6576 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6577 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6578 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6579 | * To meet the requirements of the spec, the arguments |
| 6580 | * must be converted to integer format before |
| 6581 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6582 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6583 | * First we go to integer form, rounding up |
| 6584 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6585 | */ |
| 6586 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6587 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6589 | if (xmlXPathIsInf(le) == 1) { |
| 6590 | l = m; |
| 6591 | if (i < 1) |
| 6592 | i = 1; |
| 6593 | } |
| 6594 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 6595 | l = 0; |
| 6596 | else { |
| 6597 | l = (int) le; |
| 6598 | if (((double)l)+0.5 <= le) l++; |
| 6599 | } |
| 6600 | |
| 6601 | /* Now we normalize inidices */ |
| 6602 | i -= 1; |
| 6603 | l += i; |
| 6604 | if (i < 0) |
| 6605 | i = 0; |
| 6606 | if (l > m) |
| 6607 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6608 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6609 | /* number of chars to copy */ |
| 6610 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6611 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6612 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6613 | } |
| 6614 | else { |
| 6615 | ret = NULL; |
| 6616 | } |
| 6617 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | if (ret == NULL) |
| 6619 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6620 | else { |
| 6621 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6622 | xmlFree(ret); |
| 6623 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6624 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | xmlXPathFreeObject(str); |
| 6626 | } |
| 6627 | |
| 6628 | /** |
| 6629 | * xmlXPathSubstringBeforeFunction: |
| 6630 | * @ctxt: the XPath Parser context |
| 6631 | * @nargs: the number of arguments |
| 6632 | * |
| 6633 | * Implement the substring-before() XPath function |
| 6634 | * string substring-before(string, string) |
| 6635 | * The substring-before function returns the substring of the first |
| 6636 | * argument string that precedes the first occurrence of the second |
| 6637 | * argument string in the first argument string, or the empty string |
| 6638 | * if the first argument string does not contain the second argument |
| 6639 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6640 | */ |
| 6641 | void |
| 6642 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6643 | xmlXPathObjectPtr str; |
| 6644 | xmlXPathObjectPtr find; |
| 6645 | xmlBufferPtr target; |
| 6646 | const xmlChar *point; |
| 6647 | int offset; |
| 6648 | |
| 6649 | CHECK_ARITY(2); |
| 6650 | CAST_TO_STRING; |
| 6651 | find = valuePop(ctxt); |
| 6652 | CAST_TO_STRING; |
| 6653 | str = valuePop(ctxt); |
| 6654 | |
| 6655 | target = xmlBufferCreate(); |
| 6656 | if (target) { |
| 6657 | point = xmlStrstr(str->stringval, find->stringval); |
| 6658 | if (point) { |
| 6659 | offset = (int)(point - str->stringval); |
| 6660 | xmlBufferAdd(target, str->stringval, offset); |
| 6661 | } |
| 6662 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6663 | xmlBufferFree(target); |
| 6664 | } |
| 6665 | |
| 6666 | xmlXPathFreeObject(str); |
| 6667 | xmlXPathFreeObject(find); |
| 6668 | } |
| 6669 | |
| 6670 | /** |
| 6671 | * xmlXPathSubstringAfterFunction: |
| 6672 | * @ctxt: the XPath Parser context |
| 6673 | * @nargs: the number of arguments |
| 6674 | * |
| 6675 | * Implement the substring-after() XPath function |
| 6676 | * string substring-after(string, string) |
| 6677 | * The substring-after function returns the substring of the first |
| 6678 | * argument string that follows the first occurrence of the second |
| 6679 | * argument string in the first argument string, or the empty stringi |
| 6680 | * if the first argument string does not contain the second argument |
| 6681 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6682 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6683 | */ |
| 6684 | void |
| 6685 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6686 | xmlXPathObjectPtr str; |
| 6687 | xmlXPathObjectPtr find; |
| 6688 | xmlBufferPtr target; |
| 6689 | const xmlChar *point; |
| 6690 | int offset; |
| 6691 | |
| 6692 | CHECK_ARITY(2); |
| 6693 | CAST_TO_STRING; |
| 6694 | find = valuePop(ctxt); |
| 6695 | CAST_TO_STRING; |
| 6696 | str = valuePop(ctxt); |
| 6697 | |
| 6698 | target = xmlBufferCreate(); |
| 6699 | if (target) { |
| 6700 | point = xmlStrstr(str->stringval, find->stringval); |
| 6701 | if (point) { |
| 6702 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6703 | xmlBufferAdd(target, &str->stringval[offset], |
| 6704 | xmlStrlen(str->stringval) - offset); |
| 6705 | } |
| 6706 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6707 | xmlBufferFree(target); |
| 6708 | } |
| 6709 | |
| 6710 | xmlXPathFreeObject(str); |
| 6711 | xmlXPathFreeObject(find); |
| 6712 | } |
| 6713 | |
| 6714 | /** |
| 6715 | * xmlXPathNormalizeFunction: |
| 6716 | * @ctxt: the XPath Parser context |
| 6717 | * @nargs: the number of arguments |
| 6718 | * |
| 6719 | * Implement the normalize-space() XPath function |
| 6720 | * string normalize-space(string?) |
| 6721 | * The normalize-space function returns the argument string with white |
| 6722 | * space normalized by stripping leading and trailing whitespace |
| 6723 | * and replacing sequences of whitespace characters by a single |
| 6724 | * space. Whitespace characters are the same allowed by the S production |
| 6725 | * in XML. If the argument is omitted, it defaults to the context |
| 6726 | * node converted to a string, in other words the value of the context node. |
| 6727 | */ |
| 6728 | void |
| 6729 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6730 | xmlXPathObjectPtr obj = NULL; |
| 6731 | xmlChar *source = NULL; |
| 6732 | xmlBufferPtr target; |
| 6733 | xmlChar blank; |
| 6734 | |
| 6735 | if (nargs == 0) { |
| 6736 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6737 | valuePush(ctxt, |
| 6738 | xmlXPathWrapString( |
| 6739 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6740 | nargs = 1; |
| 6741 | } |
| 6742 | |
| 6743 | CHECK_ARITY(1); |
| 6744 | CAST_TO_STRING; |
| 6745 | CHECK_TYPE(XPATH_STRING); |
| 6746 | obj = valuePop(ctxt); |
| 6747 | source = obj->stringval; |
| 6748 | |
| 6749 | target = xmlBufferCreate(); |
| 6750 | if (target && source) { |
| 6751 | |
| 6752 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6753 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6754 | source++; |
| 6755 | |
| 6756 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6757 | blank = 0; |
| 6758 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6759 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6760 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6761 | } else { |
| 6762 | if (blank) { |
| 6763 | xmlBufferAdd(target, &blank, 1); |
| 6764 | blank = 0; |
| 6765 | } |
| 6766 | xmlBufferAdd(target, source, 1); |
| 6767 | } |
| 6768 | source++; |
| 6769 | } |
| 6770 | |
| 6771 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6772 | xmlBufferFree(target); |
| 6773 | } |
| 6774 | xmlXPathFreeObject(obj); |
| 6775 | } |
| 6776 | |
| 6777 | /** |
| 6778 | * xmlXPathTranslateFunction: |
| 6779 | * @ctxt: the XPath Parser context |
| 6780 | * @nargs: the number of arguments |
| 6781 | * |
| 6782 | * Implement the translate() XPath function |
| 6783 | * string translate(string, string, string) |
| 6784 | * The translate function returns the first argument string with |
| 6785 | * occurrences of characters in the second argument string replaced |
| 6786 | * by the character at the corresponding position in the third argument |
| 6787 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6788 | * BAr. If there is a character in the second argument string with no |
| 6789 | * character at a corresponding position in the third argument string |
| 6790 | * (because the second argument string is longer than the third argument |
| 6791 | * string), then occurrences of that character in the first argument |
| 6792 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6793 | * returns "AAA". If a character occurs more than once in second |
| 6794 | * argument string, then the first occurrence determines the replacement |
| 6795 | * character. If the third argument string is longer than the second |
| 6796 | * argument string, then excess characters are ignored. |
| 6797 | */ |
| 6798 | void |
| 6799 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6800 | xmlXPathObjectPtr str; |
| 6801 | xmlXPathObjectPtr from; |
| 6802 | xmlXPathObjectPtr to; |
| 6803 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6804 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6805 | xmlChar ch; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6806 | xmlChar *point; |
| 6807 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6808 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6809 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6810 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6811 | CAST_TO_STRING; |
| 6812 | to = valuePop(ctxt); |
| 6813 | CAST_TO_STRING; |
| 6814 | from = valuePop(ctxt); |
| 6815 | CAST_TO_STRING; |
| 6816 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6818 | target = xmlBufferCreate(); |
| 6819 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6820 | max = xmlUTF8Strlen(to->stringval); |
| 6821 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6822 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6823 | if (offset >= 0) { |
| 6824 | if (offset < max) { |
| 6825 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6826 | if (point) |
| 6827 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6828 | } |
| 6829 | } else |
| 6830 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6831 | |
| 6832 | /* Step to next character in input */ |
| 6833 | cptr++; |
| 6834 | if ( ch & 0x80 ) { |
| 6835 | /* if not simple ascii, verify proper format */ |
| 6836 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6837 | xmlGenericError(xmlGenericErrorContext, |
| 6838 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6839 | break; |
| 6840 | } |
| 6841 | /* then skip over remaining bytes for this char */ |
| 6842 | while ( (ch <<= 1) & 0x80 ) |
| 6843 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 6844 | xmlGenericError(xmlGenericErrorContext, |
| 6845 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6846 | break; |
| 6847 | } |
| 6848 | if (ch & 0x80) /* must have had error encountered */ |
| 6849 | break; |
| 6850 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6851 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6852 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6853 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6854 | xmlBufferFree(target); |
| 6855 | xmlXPathFreeObject(str); |
| 6856 | xmlXPathFreeObject(from); |
| 6857 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | /** |
| 6861 | * xmlXPathBooleanFunction: |
| 6862 | * @ctxt: the XPath Parser context |
| 6863 | * @nargs: the number of arguments |
| 6864 | * |
| 6865 | * Implement the boolean() XPath function |
| 6866 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6867 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | * - a number is true if and only if it is neither positive or |
| 6869 | * negative zero nor NaN |
| 6870 | * - a node-set is true if and only if it is non-empty |
| 6871 | * - a string is true if and only if its length is non-zero |
| 6872 | */ |
| 6873 | void |
| 6874 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6875 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6876 | |
| 6877 | CHECK_ARITY(1); |
| 6878 | cur = valuePop(ctxt); |
| 6879 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6880 | cur = xmlXPathConvertBoolean(cur); |
| 6881 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | /** |
| 6885 | * xmlXPathNotFunction: |
| 6886 | * @ctxt: the XPath Parser context |
| 6887 | * @nargs: the number of arguments |
| 6888 | * |
| 6889 | * Implement the not() XPath function |
| 6890 | * boolean not(boolean) |
| 6891 | * The not function returns true if its argument is false, |
| 6892 | * and false otherwise. |
| 6893 | */ |
| 6894 | void |
| 6895 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6896 | CHECK_ARITY(1); |
| 6897 | CAST_TO_BOOLEAN; |
| 6898 | CHECK_TYPE(XPATH_BOOLEAN); |
| 6899 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 6900 | } |
| 6901 | |
| 6902 | /** |
| 6903 | * xmlXPathTrueFunction: |
| 6904 | * @ctxt: the XPath Parser context |
| 6905 | * @nargs: the number of arguments |
| 6906 | * |
| 6907 | * Implement the true() XPath function |
| 6908 | * boolean true() |
| 6909 | */ |
| 6910 | void |
| 6911 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6912 | CHECK_ARITY(0); |
| 6913 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6914 | } |
| 6915 | |
| 6916 | /** |
| 6917 | * xmlXPathFalseFunction: |
| 6918 | * @ctxt: the XPath Parser context |
| 6919 | * @nargs: the number of arguments |
| 6920 | * |
| 6921 | * Implement the false() XPath function |
| 6922 | * boolean false() |
| 6923 | */ |
| 6924 | void |
| 6925 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6926 | CHECK_ARITY(0); |
| 6927 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6928 | } |
| 6929 | |
| 6930 | /** |
| 6931 | * xmlXPathLangFunction: |
| 6932 | * @ctxt: the XPath Parser context |
| 6933 | * @nargs: the number of arguments |
| 6934 | * |
| 6935 | * Implement the lang() XPath function |
| 6936 | * boolean lang(string) |
| 6937 | * The lang function returns true or false depending on whether the |
| 6938 | * language of the context node as specified by xml:lang attributes |
| 6939 | * is the same as or is a sublanguage of the language specified by |
| 6940 | * the argument string. The language of the context node is determined |
| 6941 | * by the value of the xml:lang attribute on the context node, or, if |
| 6942 | * the context node has no xml:lang attribute, by the value of the |
| 6943 | * xml:lang attribute on the nearest ancestor of the context node that |
| 6944 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 6945 | * returns false. If there is such an attribute, then lang returns |
| 6946 | * true if the attribute value is equal to the argument ignoring case, |
| 6947 | * or if there is some suffix starting with - such that the attribute |
| 6948 | * value is equal to the argument ignoring that suffix of the attribute |
| 6949 | * value and ignoring case. |
| 6950 | */ |
| 6951 | void |
| 6952 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6953 | xmlXPathObjectPtr val; |
| 6954 | const xmlChar *theLang; |
| 6955 | const xmlChar *lang; |
| 6956 | int ret = 0; |
| 6957 | int i; |
| 6958 | |
| 6959 | CHECK_ARITY(1); |
| 6960 | CAST_TO_STRING; |
| 6961 | CHECK_TYPE(XPATH_STRING); |
| 6962 | val = valuePop(ctxt); |
| 6963 | lang = val->stringval; |
| 6964 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 6965 | if ((theLang != NULL) && (lang != NULL)) { |
| 6966 | for (i = 0;lang[i] != 0;i++) |
| 6967 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 6968 | goto not_equal; |
| 6969 | ret = 1; |
| 6970 | } |
| 6971 | not_equal: |
| 6972 | xmlXPathFreeObject(val); |
| 6973 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 6974 | } |
| 6975 | |
| 6976 | /** |
| 6977 | * xmlXPathNumberFunction: |
| 6978 | * @ctxt: the XPath Parser context |
| 6979 | * @nargs: the number of arguments |
| 6980 | * |
| 6981 | * Implement the number() XPath function |
| 6982 | * number number(object?) |
| 6983 | */ |
| 6984 | void |
| 6985 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6986 | xmlXPathObjectPtr cur; |
| 6987 | double res; |
| 6988 | |
| 6989 | if (nargs == 0) { |
| 6990 | if (ctxt->context->node == NULL) { |
| 6991 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 6992 | } else { |
| 6993 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 6994 | |
| 6995 | res = xmlXPathStringEvalNumber(content); |
| 6996 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 6997 | xmlFree(content); |
| 6998 | } |
| 6999 | return; |
| 7000 | } |
| 7001 | |
| 7002 | CHECK_ARITY(1); |
| 7003 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7004 | cur = xmlXPathConvertNumber(cur); |
| 7005 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7006 | } |
| 7007 | |
| 7008 | /** |
| 7009 | * xmlXPathSumFunction: |
| 7010 | * @ctxt: the XPath Parser context |
| 7011 | * @nargs: the number of arguments |
| 7012 | * |
| 7013 | * Implement the sum() XPath function |
| 7014 | * number sum(node-set) |
| 7015 | * The sum function returns the sum of the values of the nodes in |
| 7016 | * the argument node-set. |
| 7017 | */ |
| 7018 | void |
| 7019 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7020 | xmlXPathObjectPtr cur; |
| 7021 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7022 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7023 | |
| 7024 | CHECK_ARITY(1); |
| 7025 | if ((ctxt->value == NULL) || |
| 7026 | ((ctxt->value->type != XPATH_NODESET) && |
| 7027 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7028 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7029 | cur = valuePop(ctxt); |
| 7030 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7031 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7032 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7033 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7034 | } |
| 7035 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7036 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7037 | xmlXPathFreeObject(cur); |
| 7038 | } |
| 7039 | |
| 7040 | /** |
| 7041 | * xmlXPathFloorFunction: |
| 7042 | * @ctxt: the XPath Parser context |
| 7043 | * @nargs: the number of arguments |
| 7044 | * |
| 7045 | * Implement the floor() XPath function |
| 7046 | * number floor(number) |
| 7047 | * The floor function returns the largest (closest to positive infinity) |
| 7048 | * number that is not greater than the argument and that is an integer. |
| 7049 | */ |
| 7050 | void |
| 7051 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7052 | double f; |
| 7053 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7054 | CHECK_ARITY(1); |
| 7055 | CAST_TO_NUMBER; |
| 7056 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7057 | |
| 7058 | f = (double)((int) ctxt->value->floatval); |
| 7059 | if (f != ctxt->value->floatval) { |
| 7060 | if (ctxt->value->floatval > 0) |
| 7061 | ctxt->value->floatval = f; |
| 7062 | else |
| 7063 | ctxt->value->floatval = f - 1; |
| 7064 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
| 7067 | /** |
| 7068 | * xmlXPathCeilingFunction: |
| 7069 | * @ctxt: the XPath Parser context |
| 7070 | * @nargs: the number of arguments |
| 7071 | * |
| 7072 | * Implement the ceiling() XPath function |
| 7073 | * number ceiling(number) |
| 7074 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7075 | * number that is not less than the argument and that is an integer. |
| 7076 | */ |
| 7077 | void |
| 7078 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7079 | double f; |
| 7080 | |
| 7081 | CHECK_ARITY(1); |
| 7082 | CAST_TO_NUMBER; |
| 7083 | CHECK_TYPE(XPATH_NUMBER); |
| 7084 | |
| 7085 | #if 0 |
| 7086 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7087 | #else |
| 7088 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7089 | if (f != ctxt->value->floatval) { |
| 7090 | if (ctxt->value->floatval > 0) |
| 7091 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7092 | else { |
| 7093 | if (ctxt->value->floatval < 0 && f == 0) |
| 7094 | ctxt->value->floatval = xmlXPathNZERO; |
| 7095 | else |
| 7096 | ctxt->value->floatval = f; |
| 7097 | } |
| 7098 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7099 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | #endif |
| 7101 | } |
| 7102 | |
| 7103 | /** |
| 7104 | * xmlXPathRoundFunction: |
| 7105 | * @ctxt: the XPath Parser context |
| 7106 | * @nargs: the number of arguments |
| 7107 | * |
| 7108 | * Implement the round() XPath function |
| 7109 | * number round(number) |
| 7110 | * The round function returns the number that is closest to the |
| 7111 | * argument and that is an integer. If there are two such numbers, |
| 7112 | * then the one that is even is returned. |
| 7113 | */ |
| 7114 | void |
| 7115 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7116 | double f; |
| 7117 | |
| 7118 | CHECK_ARITY(1); |
| 7119 | CAST_TO_NUMBER; |
| 7120 | CHECK_TYPE(XPATH_NUMBER); |
| 7121 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7122 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7123 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7124 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7125 | (ctxt->value->floatval == 0.0)) |
| 7126 | return; |
| 7127 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7128 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7129 | if (ctxt->value->floatval < 0) { |
| 7130 | if (ctxt->value->floatval < f - 0.5) |
| 7131 | ctxt->value->floatval = f - 1; |
| 7132 | else |
| 7133 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7134 | if (ctxt->value->floatval == 0) |
| 7135 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7136 | } else { |
| 7137 | if (ctxt->value->floatval < f + 0.5) |
| 7138 | ctxt->value->floatval = f; |
| 7139 | else |
| 7140 | ctxt->value->floatval = f + 1; |
| 7141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | /************************************************************************ |
| 7145 | * * |
| 7146 | * The Parser * |
| 7147 | * * |
| 7148 | ************************************************************************/ |
| 7149 | |
| 7150 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7151 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7152 | * implementation. |
| 7153 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7154 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7155 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7156 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7157 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7158 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7159 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7160 | |
| 7161 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7162 | * xmlXPathCurrentChar: |
| 7163 | * @ctxt: the XPath parser context |
| 7164 | * @cur: pointer to the beginning of the char |
| 7165 | * @len: pointer to the length of the char read |
| 7166 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7167 | * 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] | 7168 | * bytes in the input buffer. |
| 7169 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7170 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7171 | */ |
| 7172 | |
| 7173 | static int |
| 7174 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7175 | unsigned char c; |
| 7176 | unsigned int val; |
| 7177 | const xmlChar *cur; |
| 7178 | |
| 7179 | if (ctxt == NULL) |
| 7180 | return(0); |
| 7181 | cur = ctxt->cur; |
| 7182 | |
| 7183 | /* |
| 7184 | * We are supposed to handle UTF8, check it's valid |
| 7185 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7186 | * |
| 7187 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7188 | * 0000 0000-0000 007F 0xxxxxxx |
| 7189 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7190 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7191 | * |
| 7192 | * Check for the 0x110000 limit too |
| 7193 | */ |
| 7194 | c = *cur; |
| 7195 | if (c & 0x80) { |
| 7196 | if ((cur[1] & 0xc0) != 0x80) |
| 7197 | goto encoding_error; |
| 7198 | if ((c & 0xe0) == 0xe0) { |
| 7199 | |
| 7200 | if ((cur[2] & 0xc0) != 0x80) |
| 7201 | goto encoding_error; |
| 7202 | if ((c & 0xf0) == 0xf0) { |
| 7203 | if (((c & 0xf8) != 0xf0) || |
| 7204 | ((cur[3] & 0xc0) != 0x80)) |
| 7205 | goto encoding_error; |
| 7206 | /* 4-byte code */ |
| 7207 | *len = 4; |
| 7208 | val = (cur[0] & 0x7) << 18; |
| 7209 | val |= (cur[1] & 0x3f) << 12; |
| 7210 | val |= (cur[2] & 0x3f) << 6; |
| 7211 | val |= cur[3] & 0x3f; |
| 7212 | } else { |
| 7213 | /* 3-byte code */ |
| 7214 | *len = 3; |
| 7215 | val = (cur[0] & 0xf) << 12; |
| 7216 | val |= (cur[1] & 0x3f) << 6; |
| 7217 | val |= cur[2] & 0x3f; |
| 7218 | } |
| 7219 | } else { |
| 7220 | /* 2-byte code */ |
| 7221 | *len = 2; |
| 7222 | val = (cur[0] & 0x1f) << 6; |
| 7223 | val |= cur[1] & 0x3f; |
| 7224 | } |
| 7225 | if (!IS_CHAR(val)) { |
| 7226 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7227 | } |
| 7228 | return(val); |
| 7229 | } else { |
| 7230 | /* 1-byte code */ |
| 7231 | *len = 1; |
| 7232 | return((int) *cur); |
| 7233 | } |
| 7234 | encoding_error: |
| 7235 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7236 | * If we detect an UTF8 error that probably means that the |
| 7237 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7238 | * declaration header. Report the error and switch the encoding |
| 7239 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7240 | * encoding !) |
| 7241 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7242 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7243 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7247 | * xmlXPathParseNCName: |
| 7248 | * @ctxt: the XPath Parser context |
| 7249 | * |
| 7250 | * parse an XML namespace non qualified name. |
| 7251 | * |
| 7252 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7253 | * |
| 7254 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7255 | * CombiningChar | Extender |
| 7256 | * |
| 7257 | * Returns the namespace name or NULL |
| 7258 | */ |
| 7259 | |
| 7260 | xmlChar * |
| 7261 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7262 | const xmlChar *in; |
| 7263 | xmlChar *ret; |
| 7264 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7266 | /* |
| 7267 | * Accelerator for simple ASCII names |
| 7268 | */ |
| 7269 | in = ctxt->cur; |
| 7270 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7271 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7272 | (*in == '_')) { |
| 7273 | in++; |
| 7274 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7275 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7276 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7277 | (*in == '_') || (*in == '.') || |
| 7278 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7279 | in++; |
| 7280 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7281 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7282 | (*in == '@') || (*in == '*')) { |
| 7283 | count = in - ctxt->cur; |
| 7284 | if (count == 0) |
| 7285 | return(NULL); |
| 7286 | ret = xmlStrndup(ctxt->cur, count); |
| 7287 | ctxt->cur = in; |
| 7288 | return(ret); |
| 7289 | } |
| 7290 | } |
| 7291 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7294 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7295 | /** |
| 7296 | * xmlXPathParseQName: |
| 7297 | * @ctxt: the XPath Parser context |
| 7298 | * @prefix: a xmlChar ** |
| 7299 | * |
| 7300 | * parse an XML qualified name |
| 7301 | * |
| 7302 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7303 | * |
| 7304 | * [NS 6] Prefix ::= NCName |
| 7305 | * |
| 7306 | * [NS 7] LocalPart ::= NCName |
| 7307 | * |
| 7308 | * Returns the function returns the local part, and prefix is updated |
| 7309 | * to get the Prefix if any. |
| 7310 | */ |
| 7311 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7312 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7313 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7314 | xmlChar *ret = NULL; |
| 7315 | |
| 7316 | *prefix = NULL; |
| 7317 | ret = xmlXPathParseNCName(ctxt); |
| 7318 | if (CUR == ':') { |
| 7319 | *prefix = ret; |
| 7320 | NEXT; |
| 7321 | ret = xmlXPathParseNCName(ctxt); |
| 7322 | } |
| 7323 | return(ret); |
| 7324 | } |
| 7325 | |
| 7326 | /** |
| 7327 | * xmlXPathParseName: |
| 7328 | * @ctxt: the XPath Parser context |
| 7329 | * |
| 7330 | * parse an XML name |
| 7331 | * |
| 7332 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7333 | * CombiningChar | Extender |
| 7334 | * |
| 7335 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7336 | * |
| 7337 | * Returns the namespace name or NULL |
| 7338 | */ |
| 7339 | |
| 7340 | xmlChar * |
| 7341 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7342 | const xmlChar *in; |
| 7343 | xmlChar *ret; |
| 7344 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7345 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7346 | /* |
| 7347 | * Accelerator for simple ASCII names |
| 7348 | */ |
| 7349 | in = ctxt->cur; |
| 7350 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7351 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7352 | (*in == '_') || (*in == ':')) { |
| 7353 | in++; |
| 7354 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7355 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7356 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7357 | (*in == '_') || (*in == '-') || |
| 7358 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7359 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7360 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7361 | count = in - ctxt->cur; |
| 7362 | ret = xmlStrndup(ctxt->cur, count); |
| 7363 | ctxt->cur = in; |
| 7364 | return(ret); |
| 7365 | } |
| 7366 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7367 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7368 | } |
| 7369 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7370 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7371 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7372 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7373 | int len = 0, l; |
| 7374 | int c; |
| 7375 | |
| 7376 | /* |
| 7377 | * Handler for more complex cases |
| 7378 | */ |
| 7379 | c = CUR_CHAR(l); |
| 7380 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7381 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7382 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7383 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7384 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7385 | return(NULL); |
| 7386 | } |
| 7387 | |
| 7388 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7389 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7390 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7391 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7392 | (IS_COMBINING(c)) || |
| 7393 | (IS_EXTENDER(c)))) { |
| 7394 | COPY_BUF(l,buf,len,c); |
| 7395 | NEXTL(l); |
| 7396 | c = CUR_CHAR(l); |
| 7397 | if (len >= XML_MAX_NAMELEN) { |
| 7398 | /* |
| 7399 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7400 | * for the processing speed. |
| 7401 | */ |
| 7402 | xmlChar *buffer; |
| 7403 | int max = len * 2; |
| 7404 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7405 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7406 | if (buffer == NULL) { |
| 7407 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7408 | } |
| 7409 | memcpy(buffer, buf, len); |
| 7410 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7411 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7412 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7413 | (IS_COMBINING(c)) || |
| 7414 | (IS_EXTENDER(c))) { |
| 7415 | if (len + 10 > max) { |
| 7416 | max *= 2; |
| 7417 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7418 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7419 | if (buffer == NULL) { |
| 7420 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7421 | } |
| 7422 | } |
| 7423 | COPY_BUF(l,buffer,len,c); |
| 7424 | NEXTL(l); |
| 7425 | c = CUR_CHAR(l); |
| 7426 | } |
| 7427 | buffer[len] = 0; |
| 7428 | return(buffer); |
| 7429 | } |
| 7430 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7431 | if (len == 0) |
| 7432 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7433 | return(xmlStrndup(buf, len)); |
| 7434 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7435 | |
| 7436 | #define MAX_FRAC 20 |
| 7437 | |
| 7438 | static double my_pow10[MAX_FRAC] = { |
| 7439 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7440 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7441 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7442 | 100000000000000.0, |
| 7443 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
| 7444 | 1000000000000000000.0, 10000000000000000000.0 |
| 7445 | }; |
| 7446 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7447 | /** |
| 7448 | * xmlXPathStringEvalNumber: |
| 7449 | * @str: A string to scan |
| 7450 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7451 | * [30a] Float ::= Number ('e' Digits?)? |
| 7452 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7453 | * [30] Number ::= Digits ('.' Digits?)? |
| 7454 | * | '.' Digits |
| 7455 | * [31] Digits ::= [0-9]+ |
| 7456 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7457 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7458 | * In complement of the Number expression, this function also handles |
| 7459 | * negative values : '-' Number. |
| 7460 | * |
| 7461 | * Returns the double value. |
| 7462 | */ |
| 7463 | double |
| 7464 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7465 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7466 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7467 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7468 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7469 | int exponent = 0; |
| 7470 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7471 | #ifdef __GNUC__ |
| 7472 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7473 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7474 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7475 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7476 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7477 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7478 | return(xmlXPathNAN); |
| 7479 | } |
| 7480 | if (*cur == '-') { |
| 7481 | isneg = 1; |
| 7482 | cur++; |
| 7483 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7484 | |
| 7485 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7486 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7487 | * tmp/temp is a workaround against a gcc compiler bug |
| 7488 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7489 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7490 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7491 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7492 | ret = ret * 10; |
| 7493 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7494 | ok = 1; |
| 7495 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7496 | temp = (double) tmp; |
| 7497 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7498 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7499 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7500 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7501 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7502 | ret = ret * 10 + (*cur - '0'); |
| 7503 | ok = 1; |
| 7504 | cur++; |
| 7505 | } |
| 7506 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7507 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7508 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7509 | int v, frac = 0; |
| 7510 | double fraction = 0; |
| 7511 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7512 | cur++; |
| 7513 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7514 | return(xmlXPathNAN); |
| 7515 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7516 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7517 | v = (*cur - '0'); |
| 7518 | fraction = fraction * 10 + v; |
| 7519 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7520 | cur++; |
| 7521 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7522 | fraction /= my_pow10[frac]; |
| 7523 | ret = ret + fraction; |
| 7524 | while ((*cur >= '0') && (*cur <= '9')) |
| 7525 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7526 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7527 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7528 | cur++; |
| 7529 | if (*cur == '-') { |
| 7530 | is_exponent_negative = 1; |
| 7531 | cur++; |
| 7532 | } |
| 7533 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7534 | exponent = exponent * 10 + (*cur - '0'); |
| 7535 | cur++; |
| 7536 | } |
| 7537 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7538 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7539 | if (*cur != 0) return(xmlXPathNAN); |
| 7540 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7541 | if (is_exponent_negative) exponent = -exponent; |
| 7542 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7543 | return(ret); |
| 7544 | } |
| 7545 | |
| 7546 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7547 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7548 | * @ctxt: the XPath Parser context |
| 7549 | * |
| 7550 | * [30] Number ::= Digits ('.' Digits?)? |
| 7551 | * | '.' Digits |
| 7552 | * [31] Digits ::= [0-9]+ |
| 7553 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7554 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7555 | * |
| 7556 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7557 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7558 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7559 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7560 | double ret = 0.0; |
| 7561 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7562 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7563 | int exponent = 0; |
| 7564 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7565 | #ifdef __GNUC__ |
| 7566 | unsigned long tmp = 0; |
| 7567 | double temp; |
| 7568 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7569 | |
| 7570 | CHECK_ERROR; |
| 7571 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7572 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7573 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7574 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7575 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7576 | * tmp/temp is a workaround against a gcc compiler bug |
| 7577 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7578 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7579 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7580 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7581 | ret = ret * 10; |
| 7582 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7583 | ok = 1; |
| 7584 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7585 | temp = (double) tmp; |
| 7586 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7587 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7588 | #else |
| 7589 | ret = 0; |
| 7590 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7591 | ret = ret * 10 + (CUR - '0'); |
| 7592 | ok = 1; |
| 7593 | NEXT; |
| 7594 | } |
| 7595 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7596 | if (CUR == '.') { |
| 7597 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7598 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7599 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7600 | } |
| 7601 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7602 | mult /= 10; |
| 7603 | ret = ret + (CUR - '0') * mult; |
| 7604 | NEXT; |
| 7605 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7606 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7607 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7608 | NEXT; |
| 7609 | if (CUR == '-') { |
| 7610 | is_exponent_negative = 1; |
| 7611 | NEXT; |
| 7612 | } |
| 7613 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7614 | exponent = exponent * 10 + (CUR - '0'); |
| 7615 | NEXT; |
| 7616 | } |
| 7617 | if (is_exponent_negative) |
| 7618 | exponent = -exponent; |
| 7619 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7620 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7621 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7622 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
| 7625 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7626 | * xmlXPathParseLiteral: |
| 7627 | * @ctxt: the XPath Parser context |
| 7628 | * |
| 7629 | * Parse a Literal |
| 7630 | * |
| 7631 | * [29] Literal ::= '"' [^"]* '"' |
| 7632 | * | "'" [^']* "'" |
| 7633 | * |
| 7634 | * Returns the value found or NULL in case of error |
| 7635 | */ |
| 7636 | static xmlChar * |
| 7637 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7638 | const xmlChar *q; |
| 7639 | xmlChar *ret = NULL; |
| 7640 | |
| 7641 | if (CUR == '"') { |
| 7642 | NEXT; |
| 7643 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7644 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7645 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7646 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7647 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7648 | } else { |
| 7649 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7650 | NEXT; |
| 7651 | } |
| 7652 | } else if (CUR == '\'') { |
| 7653 | NEXT; |
| 7654 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7655 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7656 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7657 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7658 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7659 | } else { |
| 7660 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7661 | NEXT; |
| 7662 | } |
| 7663 | } else { |
| 7664 | XP_ERROR0(XPATH_START_LITERAL_ERROR); |
| 7665 | } |
| 7666 | return(ret); |
| 7667 | } |
| 7668 | |
| 7669 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7670 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7671 | * @ctxt: the XPath Parser context |
| 7672 | * |
| 7673 | * Parse a Literal and push it on the stack. |
| 7674 | * |
| 7675 | * [29] Literal ::= '"' [^"]* '"' |
| 7676 | * | "'" [^']* "'" |
| 7677 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7678 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7679 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7680 | static void |
| 7681 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7682 | const xmlChar *q; |
| 7683 | xmlChar *ret = NULL; |
| 7684 | |
| 7685 | if (CUR == '"') { |
| 7686 | NEXT; |
| 7687 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7688 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7689 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7690 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7691 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7692 | } else { |
| 7693 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7694 | NEXT; |
| 7695 | } |
| 7696 | } else if (CUR == '\'') { |
| 7697 | NEXT; |
| 7698 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7699 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7700 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7701 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7702 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7703 | } else { |
| 7704 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7705 | NEXT; |
| 7706 | } |
| 7707 | } else { |
| 7708 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7709 | } |
| 7710 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7711 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7712 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7713 | xmlFree(ret); |
| 7714 | } |
| 7715 | |
| 7716 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7717 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7718 | * @ctxt: the XPath Parser context |
| 7719 | * |
| 7720 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7721 | * |
| 7722 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7723 | * 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] | 7724 | * of any of the types that are possible for the value of an expression, |
| 7725 | * and may also be of additional types not specified here. |
| 7726 | * |
| 7727 | * Early evaluation is possible since: |
| 7728 | * The variable bindings [...] used to evaluate a subexpression are |
| 7729 | * always the same as those used to evaluate the containing expression. |
| 7730 | * |
| 7731 | * [36] VariableReference ::= '$' QName |
| 7732 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7733 | static void |
| 7734 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7735 | xmlChar *name; |
| 7736 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7737 | |
| 7738 | SKIP_BLANKS; |
| 7739 | if (CUR != '$') { |
| 7740 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7741 | } |
| 7742 | NEXT; |
| 7743 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7744 | if (name == NULL) { |
| 7745 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7746 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7747 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7748 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7749 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7750 | SKIP_BLANKS; |
| 7751 | } |
| 7752 | |
| 7753 | /** |
| 7754 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7755 | * @name: a name string |
| 7756 | * |
| 7757 | * Is the name given a NodeType one. |
| 7758 | * |
| 7759 | * [38] NodeType ::= 'comment' |
| 7760 | * | 'text' |
| 7761 | * | 'processing-instruction' |
| 7762 | * | 'node' |
| 7763 | * |
| 7764 | * Returns 1 if true 0 otherwise |
| 7765 | */ |
| 7766 | int |
| 7767 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7768 | if (name == NULL) |
| 7769 | return(0); |
| 7770 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7771 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7772 | return(1); |
| 7773 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7774 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7775 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7776 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7777 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7778 | return(1); |
| 7779 | return(0); |
| 7780 | } |
| 7781 | |
| 7782 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7783 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7784 | * @ctxt: the XPath Parser context |
| 7785 | * |
| 7786 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7787 | * [17] Argument ::= Expr |
| 7788 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7789 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7790 | * pushed on the stack |
| 7791 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7792 | static void |
| 7793 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7794 | xmlChar *name; |
| 7795 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7796 | int nbargs = 0; |
| 7797 | |
| 7798 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7799 | if (name == NULL) { |
| 7800 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7801 | } |
| 7802 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7803 | #ifdef DEBUG_EXPR |
| 7804 | if (prefix == NULL) |
| 7805 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7806 | name); |
| 7807 | else |
| 7808 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7809 | prefix, name); |
| 7810 | #endif |
| 7811 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7812 | if (CUR != '(') { |
| 7813 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7814 | } |
| 7815 | NEXT; |
| 7816 | SKIP_BLANKS; |
| 7817 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7818 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 7819 | if (CUR != ')') { |
| 7820 | while (CUR != 0) { |
| 7821 | int op1 = ctxt->comp->last; |
| 7822 | ctxt->comp->last = -1; |
| 7823 | xmlXPathCompileExpr(ctxt); |
| 7824 | CHECK_ERROR; |
| 7825 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 7826 | nbargs++; |
| 7827 | if (CUR == ')') break; |
| 7828 | if (CUR != ',') { |
| 7829 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7830 | } |
| 7831 | NEXT; |
| 7832 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7833 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7834 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7835 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 7836 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7837 | NEXT; |
| 7838 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
| 7841 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7842 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7843 | * @ctxt: the XPath Parser context |
| 7844 | * |
| 7845 | * [15] PrimaryExpr ::= VariableReference |
| 7846 | * | '(' Expr ')' |
| 7847 | * | Literal |
| 7848 | * | Number |
| 7849 | * | FunctionCall |
| 7850 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7851 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7852 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7853 | static void |
| 7854 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7855 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7856 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7857 | else if (CUR == '(') { |
| 7858 | NEXT; |
| 7859 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7860 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 7861 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7862 | if (CUR != ')') { |
| 7863 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7864 | } |
| 7865 | NEXT; |
| 7866 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7867 | } else if (IS_DIGIT_CH(CUR) || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7868 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7869 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7870 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7871 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7872 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7873 | } |
| 7874 | SKIP_BLANKS; |
| 7875 | } |
| 7876 | |
| 7877 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7878 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7879 | * @ctxt: the XPath Parser context |
| 7880 | * |
| 7881 | * [20] FilterExpr ::= PrimaryExpr |
| 7882 | * | FilterExpr Predicate |
| 7883 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7884 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7885 | * Square brackets are used to filter expressions in the same way that |
| 7886 | * they are used in location paths. It is an error if the expression to |
| 7887 | * be filtered does not evaluate to a node-set. The context node list |
| 7888 | * used for evaluating the expression in square brackets is the node-set |
| 7889 | * to be filtered listed in document order. |
| 7890 | */ |
| 7891 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7892 | static void |
| 7893 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 7894 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7895 | CHECK_ERROR; |
| 7896 | SKIP_BLANKS; |
| 7897 | |
| 7898 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7899 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7900 | SKIP_BLANKS; |
| 7901 | } |
| 7902 | |
| 7903 | |
| 7904 | } |
| 7905 | |
| 7906 | /** |
| 7907 | * xmlXPathScanName: |
| 7908 | * @ctxt: the XPath Parser context |
| 7909 | * |
| 7910 | * Trickery: parse an XML name but without consuming the input flow |
| 7911 | * Needed to avoid insanity in the parser state. |
| 7912 | * |
| 7913 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7914 | * CombiningChar | Extender |
| 7915 | * |
| 7916 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7917 | * |
| 7918 | * [6] Names ::= Name (S Name)* |
| 7919 | * |
| 7920 | * Returns the Name parsed or NULL |
| 7921 | */ |
| 7922 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7923 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7924 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
| 7925 | xmlChar buf[XML_MAX_NAMELEN]; |
| 7926 | int len = 0; |
| 7927 | |
| 7928 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7929 | if (!IS_LETTER_CH(CUR) && (CUR != '_') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7930 | (CUR != ':')) { |
| 7931 | return(NULL); |
| 7932 | } |
| 7933 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7934 | while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7935 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 7936 | (NXT(len) == '_') || (NXT(len) == ':') || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7937 | (IS_COMBINING_CH(NXT(len))) || |
| 7938 | (IS_EXTENDER_CH(NXT(len)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7939 | buf[len] = NXT(len); |
| 7940 | len++; |
| 7941 | if (len >= XML_MAX_NAMELEN) { |
| 7942 | xmlGenericError(xmlGenericErrorContext, |
| 7943 | "xmlScanName: reached XML_MAX_NAMELEN limit\n"); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7944 | while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7945 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 7946 | (NXT(len) == '_') || (NXT(len) == ':') || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7947 | (IS_COMBINING_CH(NXT(len))) || |
| 7948 | (IS_EXTENDER_CH(NXT(len)))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7949 | len++; |
| 7950 | break; |
| 7951 | } |
| 7952 | } |
| 7953 | return(xmlStrndup(buf, len)); |
| 7954 | } |
| 7955 | |
| 7956 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7957 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7958 | * @ctxt: the XPath Parser context |
| 7959 | * |
| 7960 | * [19] PathExpr ::= LocationPath |
| 7961 | * | FilterExpr |
| 7962 | * | FilterExpr '/' RelativeLocationPath |
| 7963 | * | FilterExpr '//' RelativeLocationPath |
| 7964 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7965 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7966 | * The / operator and // operators combine an arbitrary expression |
| 7967 | * and a relative location path. It is an error if the expression |
| 7968 | * does not evaluate to a node-set. |
| 7969 | * The / operator does composition in the same way as when / is |
| 7970 | * used in a location path. As in location paths, // is short for |
| 7971 | * /descendant-or-self::node()/. |
| 7972 | */ |
| 7973 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7974 | static void |
| 7975 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7976 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 7977 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 7978 | |
| 7979 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7980 | if ((CUR == '$') || (CUR == '(') || (IS_DIGIT_CH(CUR)) || |
| 7981 | (CUR == '\'') || (CUR == '"') || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7982 | lc = 0; |
| 7983 | } else if (CUR == '*') { |
| 7984 | /* relative or absolute location path */ |
| 7985 | lc = 1; |
| 7986 | } else if (CUR == '/') { |
| 7987 | /* relative or absolute location path */ |
| 7988 | lc = 1; |
| 7989 | } else if (CUR == '@') { |
| 7990 | /* relative abbreviated attribute location path */ |
| 7991 | lc = 1; |
| 7992 | } else if (CUR == '.') { |
| 7993 | /* relative abbreviated attribute location path */ |
| 7994 | lc = 1; |
| 7995 | } else { |
| 7996 | /* |
| 7997 | * Problem is finding if we have a name here whether it's: |
| 7998 | * - a nodetype |
| 7999 | * - a function call in which case it's followed by '(' |
| 8000 | * - an axis in which case it's followed by ':' |
| 8001 | * - a element name |
| 8002 | * We do an a priori analysis here rather than having to |
| 8003 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8004 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8005 | * read/write/debug. |
| 8006 | */ |
| 8007 | SKIP_BLANKS; |
| 8008 | name = xmlXPathScanName(ctxt); |
| 8009 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8010 | #ifdef DEBUG_STEP |
| 8011 | xmlGenericError(xmlGenericErrorContext, |
| 8012 | "PathExpr: Axis\n"); |
| 8013 | #endif |
| 8014 | lc = 1; |
| 8015 | xmlFree(name); |
| 8016 | } else if (name != NULL) { |
| 8017 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8018 | |
| 8019 | |
| 8020 | while (NXT(len) != 0) { |
| 8021 | if (NXT(len) == '/') { |
| 8022 | /* element name */ |
| 8023 | #ifdef DEBUG_STEP |
| 8024 | xmlGenericError(xmlGenericErrorContext, |
| 8025 | "PathExpr: AbbrRelLocation\n"); |
| 8026 | #endif |
| 8027 | lc = 1; |
| 8028 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8029 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8030 | /* ignore blanks */ |
| 8031 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8032 | } else if (NXT(len) == ':') { |
| 8033 | #ifdef DEBUG_STEP |
| 8034 | xmlGenericError(xmlGenericErrorContext, |
| 8035 | "PathExpr: AbbrRelLocation\n"); |
| 8036 | #endif |
| 8037 | lc = 1; |
| 8038 | break; |
| 8039 | } else if ((NXT(len) == '(')) { |
| 8040 | /* Note Type or Function */ |
| 8041 | if (xmlXPathIsNodeType(name)) { |
| 8042 | #ifdef DEBUG_STEP |
| 8043 | xmlGenericError(xmlGenericErrorContext, |
| 8044 | "PathExpr: Type search\n"); |
| 8045 | #endif |
| 8046 | lc = 1; |
| 8047 | } else { |
| 8048 | #ifdef DEBUG_STEP |
| 8049 | xmlGenericError(xmlGenericErrorContext, |
| 8050 | "PathExpr: function call\n"); |
| 8051 | #endif |
| 8052 | lc = 0; |
| 8053 | } |
| 8054 | break; |
| 8055 | } else if ((NXT(len) == '[')) { |
| 8056 | /* element name */ |
| 8057 | #ifdef DEBUG_STEP |
| 8058 | xmlGenericError(xmlGenericErrorContext, |
| 8059 | "PathExpr: AbbrRelLocation\n"); |
| 8060 | #endif |
| 8061 | lc = 1; |
| 8062 | break; |
| 8063 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8064 | (NXT(len) == '=')) { |
| 8065 | lc = 1; |
| 8066 | break; |
| 8067 | } else { |
| 8068 | lc = 1; |
| 8069 | break; |
| 8070 | } |
| 8071 | len++; |
| 8072 | } |
| 8073 | if (NXT(len) == 0) { |
| 8074 | #ifdef DEBUG_STEP |
| 8075 | xmlGenericError(xmlGenericErrorContext, |
| 8076 | "PathExpr: AbbrRelLocation\n"); |
| 8077 | #endif |
| 8078 | /* element name */ |
| 8079 | lc = 1; |
| 8080 | } |
| 8081 | xmlFree(name); |
| 8082 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8083 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8084 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8085 | } |
| 8086 | } |
| 8087 | |
| 8088 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8089 | if (CUR == '/') { |
| 8090 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8091 | } else { |
| 8092 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8093 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8094 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8095 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8096 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8097 | CHECK_ERROR; |
| 8098 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8099 | SKIP(2); |
| 8100 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8101 | |
| 8102 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8103 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8104 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8105 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8106 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8107 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8108 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8109 | } |
| 8110 | } |
| 8111 | SKIP_BLANKS; |
| 8112 | } |
| 8113 | |
| 8114 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8115 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8116 | * @ctxt: the XPath Parser context |
| 8117 | * |
| 8118 | * [18] UnionExpr ::= PathExpr |
| 8119 | * | UnionExpr '|' PathExpr |
| 8120 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8121 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8122 | */ |
| 8123 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8124 | static void |
| 8125 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8126 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8127 | CHECK_ERROR; |
| 8128 | SKIP_BLANKS; |
| 8129 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8130 | int op1 = ctxt->comp->last; |
| 8131 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8132 | |
| 8133 | NEXT; |
| 8134 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8135 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8136 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8137 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8138 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8139 | SKIP_BLANKS; |
| 8140 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8141 | } |
| 8142 | |
| 8143 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8144 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8145 | * @ctxt: the XPath Parser context |
| 8146 | * |
| 8147 | * [27] UnaryExpr ::= UnionExpr |
| 8148 | * | '-' UnaryExpr |
| 8149 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8150 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8151 | */ |
| 8152 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8153 | static void |
| 8154 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8155 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8156 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8157 | |
| 8158 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8159 | while (CUR == '-') { |
| 8160 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8161 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8162 | NEXT; |
| 8163 | SKIP_BLANKS; |
| 8164 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8165 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8166 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8167 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8168 | if (found) { |
| 8169 | if (minus) |
| 8170 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8171 | else |
| 8172 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8173 | } |
| 8174 | } |
| 8175 | |
| 8176 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8177 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8178 | * @ctxt: the XPath Parser context |
| 8179 | * |
| 8180 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8181 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8182 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8183 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8184 | * [34] MultiplyOperator ::= '*' |
| 8185 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8186 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8187 | */ |
| 8188 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8189 | static void |
| 8190 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8191 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8192 | CHECK_ERROR; |
| 8193 | SKIP_BLANKS; |
| 8194 | while ((CUR == '*') || |
| 8195 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8196 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8197 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8198 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8199 | |
| 8200 | if (CUR == '*') { |
| 8201 | op = 0; |
| 8202 | NEXT; |
| 8203 | } else if (CUR == 'd') { |
| 8204 | op = 1; |
| 8205 | SKIP(3); |
| 8206 | } else if (CUR == 'm') { |
| 8207 | op = 2; |
| 8208 | SKIP(3); |
| 8209 | } |
| 8210 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8211 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8212 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8213 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8214 | SKIP_BLANKS; |
| 8215 | } |
| 8216 | } |
| 8217 | |
| 8218 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8219 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | * @ctxt: the XPath Parser context |
| 8221 | * |
| 8222 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8223 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8224 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8225 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8226 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8227 | */ |
| 8228 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8229 | static void |
| 8230 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8231 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8232 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8233 | CHECK_ERROR; |
| 8234 | SKIP_BLANKS; |
| 8235 | while ((CUR == '+') || (CUR == '-')) { |
| 8236 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8237 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8238 | |
| 8239 | if (CUR == '+') plus = 1; |
| 8240 | else plus = 0; |
| 8241 | NEXT; |
| 8242 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8243 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8244 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8245 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8246 | SKIP_BLANKS; |
| 8247 | } |
| 8248 | } |
| 8249 | |
| 8250 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8251 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8252 | * @ctxt: the XPath Parser context |
| 8253 | * |
| 8254 | * [24] RelationalExpr ::= AdditiveExpr |
| 8255 | * | RelationalExpr '<' AdditiveExpr |
| 8256 | * | RelationalExpr '>' AdditiveExpr |
| 8257 | * | RelationalExpr '<=' AdditiveExpr |
| 8258 | * | RelationalExpr '>=' AdditiveExpr |
| 8259 | * |
| 8260 | * A <= B > C is allowed ? Answer from James, yes with |
| 8261 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8262 | * which is basically what got implemented. |
| 8263 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8264 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8265 | * on the stack |
| 8266 | */ |
| 8267 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8268 | static void |
| 8269 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8270 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8271 | CHECK_ERROR; |
| 8272 | SKIP_BLANKS; |
| 8273 | while ((CUR == '<') || |
| 8274 | (CUR == '>') || |
| 8275 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8276 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8277 | int inf, strict; |
| 8278 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8279 | |
| 8280 | if (CUR == '<') inf = 1; |
| 8281 | else inf = 0; |
| 8282 | if (NXT(1) == '=') strict = 0; |
| 8283 | else strict = 1; |
| 8284 | NEXT; |
| 8285 | if (!strict) NEXT; |
| 8286 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8287 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8288 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8289 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8290 | SKIP_BLANKS; |
| 8291 | } |
| 8292 | } |
| 8293 | |
| 8294 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8295 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8296 | * @ctxt: the XPath Parser context |
| 8297 | * |
| 8298 | * [23] EqualityExpr ::= RelationalExpr |
| 8299 | * | EqualityExpr '=' RelationalExpr |
| 8300 | * | EqualityExpr '!=' RelationalExpr |
| 8301 | * |
| 8302 | * A != B != C is allowed ? Answer from James, yes with |
| 8303 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8304 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8305 | * which is basically what got implemented. |
| 8306 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8307 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8308 | * |
| 8309 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8310 | static void |
| 8311 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8312 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8313 | CHECK_ERROR; |
| 8314 | SKIP_BLANKS; |
| 8315 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8316 | int eq; |
| 8317 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8318 | |
| 8319 | if (CUR == '=') eq = 1; |
| 8320 | else eq = 0; |
| 8321 | NEXT; |
| 8322 | if (!eq) NEXT; |
| 8323 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8324 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8325 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8326 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8327 | SKIP_BLANKS; |
| 8328 | } |
| 8329 | } |
| 8330 | |
| 8331 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8332 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | * @ctxt: the XPath Parser context |
| 8334 | * |
| 8335 | * [22] AndExpr ::= EqualityExpr |
| 8336 | * | AndExpr 'and' EqualityExpr |
| 8337 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8338 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8339 | * |
| 8340 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8341 | static void |
| 8342 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8343 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8344 | CHECK_ERROR; |
| 8345 | SKIP_BLANKS; |
| 8346 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8347 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8348 | SKIP(3); |
| 8349 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8350 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8351 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8352 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8353 | SKIP_BLANKS; |
| 8354 | } |
| 8355 | } |
| 8356 | |
| 8357 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8358 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | * @ctxt: the XPath Parser context |
| 8360 | * |
| 8361 | * [14] Expr ::= OrExpr |
| 8362 | * [21] OrExpr ::= AndExpr |
| 8363 | * | OrExpr 'or' AndExpr |
| 8364 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8365 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8366 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8367 | static void |
| 8368 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 8369 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8370 | CHECK_ERROR; |
| 8371 | SKIP_BLANKS; |
| 8372 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8373 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8374 | SKIP(2); |
| 8375 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8376 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8377 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8378 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8379 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8380 | SKIP_BLANKS; |
| 8381 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8382 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 8383 | /* more ops could be optimized too */ |
| 8384 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8385 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8389 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8390 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8391 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8392 | * |
| 8393 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8394 | * [9] PredicateExpr ::= Expr |
| 8395 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8396 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8397 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8398 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8399 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8400 | int op1 = ctxt->comp->last; |
| 8401 | |
| 8402 | SKIP_BLANKS; |
| 8403 | if (CUR != '[') { |
| 8404 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8405 | } |
| 8406 | NEXT; |
| 8407 | SKIP_BLANKS; |
| 8408 | |
| 8409 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8410 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8411 | CHECK_ERROR; |
| 8412 | |
| 8413 | if (CUR != ']') { |
| 8414 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8415 | } |
| 8416 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8417 | if (filter) |
| 8418 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8419 | else |
| 8420 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8421 | |
| 8422 | NEXT; |
| 8423 | SKIP_BLANKS; |
| 8424 | } |
| 8425 | |
| 8426 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8427 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8428 | * @ctxt: the XPath Parser context |
| 8429 | * @test: pointer to a xmlXPathTestVal |
| 8430 | * @type: pointer to a xmlXPathTypeVal |
| 8431 | * @prefix: placeholder for a possible name prefix |
| 8432 | * |
| 8433 | * [7] NodeTest ::= NameTest |
| 8434 | * | NodeType '(' ')' |
| 8435 | * | 'processing-instruction' '(' Literal ')' |
| 8436 | * |
| 8437 | * [37] NameTest ::= '*' |
| 8438 | * | NCName ':' '*' |
| 8439 | * | QName |
| 8440 | * [38] NodeType ::= 'comment' |
| 8441 | * | 'text' |
| 8442 | * | 'processing-instruction' |
| 8443 | * | 'node' |
| 8444 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8445 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8446 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8447 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8448 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8449 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8450 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8451 | int blanks; |
| 8452 | |
| 8453 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8454 | STRANGE; |
| 8455 | return(NULL); |
| 8456 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8457 | *type = (xmlXPathTypeVal) 0; |
| 8458 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8459 | *prefix = NULL; |
| 8460 | SKIP_BLANKS; |
| 8461 | |
| 8462 | if ((name == NULL) && (CUR == '*')) { |
| 8463 | /* |
| 8464 | * All elements |
| 8465 | */ |
| 8466 | NEXT; |
| 8467 | *test = NODE_TEST_ALL; |
| 8468 | return(NULL); |
| 8469 | } |
| 8470 | |
| 8471 | if (name == NULL) |
| 8472 | name = xmlXPathParseNCName(ctxt); |
| 8473 | if (name == NULL) { |
| 8474 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8475 | } |
| 8476 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8477 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8478 | SKIP_BLANKS; |
| 8479 | if (CUR == '(') { |
| 8480 | NEXT; |
| 8481 | /* |
| 8482 | * NodeType or PI search |
| 8483 | */ |
| 8484 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8485 | *type = NODE_TYPE_COMMENT; |
| 8486 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8487 | *type = NODE_TYPE_NODE; |
| 8488 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8489 | *type = NODE_TYPE_PI; |
| 8490 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8491 | *type = NODE_TYPE_TEXT; |
| 8492 | else { |
| 8493 | if (name != NULL) |
| 8494 | xmlFree(name); |
| 8495 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8496 | } |
| 8497 | |
| 8498 | *test = NODE_TEST_TYPE; |
| 8499 | |
| 8500 | SKIP_BLANKS; |
| 8501 | if (*type == NODE_TYPE_PI) { |
| 8502 | /* |
| 8503 | * Specific case: search a PI by name. |
| 8504 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8505 | if (name != NULL) |
| 8506 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8507 | name = NULL; |
| 8508 | if (CUR != ')') { |
| 8509 | name = xmlXPathParseLiteral(ctxt); |
| 8510 | CHECK_ERROR 0; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8511 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8512 | SKIP_BLANKS; |
| 8513 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8514 | } |
| 8515 | if (CUR != ')') { |
| 8516 | if (name != NULL) |
| 8517 | xmlFree(name); |
| 8518 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
| 8519 | } |
| 8520 | NEXT; |
| 8521 | return(name); |
| 8522 | } |
| 8523 | *test = NODE_TEST_NAME; |
| 8524 | if ((!blanks) && (CUR == ':')) { |
| 8525 | NEXT; |
| 8526 | |
| 8527 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8528 | * Since currently the parser context don't have a |
| 8529 | * namespace list associated: |
| 8530 | * The namespace name for this prefix can be computed |
| 8531 | * only at evaluation time. The compilation is done |
| 8532 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8533 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8534 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8535 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8536 | if (name != NULL) |
| 8537 | xmlFree(name); |
| 8538 | if (*prefix == NULL) { |
| 8539 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8540 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8541 | #else |
| 8542 | *prefix = name; |
| 8543 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8544 | |
| 8545 | if (CUR == '*') { |
| 8546 | /* |
| 8547 | * All elements |
| 8548 | */ |
| 8549 | NEXT; |
| 8550 | *test = NODE_TEST_ALL; |
| 8551 | return(NULL); |
| 8552 | } |
| 8553 | |
| 8554 | name = xmlXPathParseNCName(ctxt); |
| 8555 | if (name == NULL) { |
| 8556 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8557 | } |
| 8558 | } |
| 8559 | return(name); |
| 8560 | } |
| 8561 | |
| 8562 | /** |
| 8563 | * xmlXPathIsAxisName: |
| 8564 | * @name: a preparsed name token |
| 8565 | * |
| 8566 | * [6] AxisName ::= 'ancestor' |
| 8567 | * | 'ancestor-or-self' |
| 8568 | * | 'attribute' |
| 8569 | * | 'child' |
| 8570 | * | 'descendant' |
| 8571 | * | 'descendant-or-self' |
| 8572 | * | 'following' |
| 8573 | * | 'following-sibling' |
| 8574 | * | 'namespace' |
| 8575 | * | 'parent' |
| 8576 | * | 'preceding' |
| 8577 | * | 'preceding-sibling' |
| 8578 | * | 'self' |
| 8579 | * |
| 8580 | * Returns the axis or 0 |
| 8581 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8582 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8583 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8584 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8585 | switch (name[0]) { |
| 8586 | case 'a': |
| 8587 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8588 | ret = AXIS_ANCESTOR; |
| 8589 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8590 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8591 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8592 | ret = AXIS_ATTRIBUTE; |
| 8593 | break; |
| 8594 | case 'c': |
| 8595 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8596 | ret = AXIS_CHILD; |
| 8597 | break; |
| 8598 | case 'd': |
| 8599 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8600 | ret = AXIS_DESCENDANT; |
| 8601 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8602 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8603 | break; |
| 8604 | case 'f': |
| 8605 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8606 | ret = AXIS_FOLLOWING; |
| 8607 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8608 | ret = AXIS_FOLLOWING_SIBLING; |
| 8609 | break; |
| 8610 | case 'n': |
| 8611 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8612 | ret = AXIS_NAMESPACE; |
| 8613 | break; |
| 8614 | case 'p': |
| 8615 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8616 | ret = AXIS_PARENT; |
| 8617 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8618 | ret = AXIS_PRECEDING; |
| 8619 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8620 | ret = AXIS_PRECEDING_SIBLING; |
| 8621 | break; |
| 8622 | case 's': |
| 8623 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8624 | ret = AXIS_SELF; |
| 8625 | break; |
| 8626 | } |
| 8627 | return(ret); |
| 8628 | } |
| 8629 | |
| 8630 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8631 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8632 | * @ctxt: the XPath Parser context |
| 8633 | * |
| 8634 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8635 | * | AbbreviatedStep |
| 8636 | * |
| 8637 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8638 | * |
| 8639 | * [5] AxisSpecifier ::= AxisName '::' |
| 8640 | * | AbbreviatedAxisSpecifier |
| 8641 | * |
| 8642 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8643 | * |
| 8644 | * Modified for XPtr range support as: |
| 8645 | * |
| 8646 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8647 | * | AbbreviatedStep |
| 8648 | * | 'range-to' '(' Expr ')' Predicate* |
| 8649 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8650 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8651 | * A location step of . is short for self::node(). This is |
| 8652 | * particularly useful in conjunction with //. For example, the |
| 8653 | * location path .//para is short for |
| 8654 | * self::node()/descendant-or-self::node()/child::para |
| 8655 | * and so will select all para descendant elements of the context |
| 8656 | * node. |
| 8657 | * Similarly, a location step of .. is short for parent::node(). |
| 8658 | * For example, ../title is short for parent::node()/child::title |
| 8659 | * and so will select the title children of the parent of the context |
| 8660 | * node. |
| 8661 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8662 | static void |
| 8663 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8664 | #ifdef LIBXML_XPTR_ENABLED |
| 8665 | int rangeto = 0; |
| 8666 | int op2 = -1; |
| 8667 | #endif |
| 8668 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8669 | SKIP_BLANKS; |
| 8670 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8671 | SKIP(2); |
| 8672 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8673 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8674 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8675 | } else if (CUR == '.') { |
| 8676 | NEXT; |
| 8677 | SKIP_BLANKS; |
| 8678 | } else { |
| 8679 | xmlChar *name = NULL; |
| 8680 | const xmlChar *prefix = NULL; |
| 8681 | xmlXPathTestVal test; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8682 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8683 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8684 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8685 | |
| 8686 | /* |
| 8687 | * The modification needed for XPointer change to the production |
| 8688 | */ |
| 8689 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8690 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8691 | name = xmlXPathParseNCName(ctxt); |
| 8692 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8693 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8694 | xmlFree(name); |
| 8695 | SKIP_BLANKS; |
| 8696 | if (CUR != '(') { |
| 8697 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8698 | } |
| 8699 | NEXT; |
| 8700 | SKIP_BLANKS; |
| 8701 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8702 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8703 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8704 | CHECK_ERROR; |
| 8705 | |
| 8706 | SKIP_BLANKS; |
| 8707 | if (CUR != ')') { |
| 8708 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8709 | } |
| 8710 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8711 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8712 | goto eval_predicates; |
| 8713 | } |
| 8714 | } |
| 8715 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8716 | if (CUR == '*') { |
| 8717 | axis = AXIS_CHILD; |
| 8718 | } else { |
| 8719 | if (name == NULL) |
| 8720 | name = xmlXPathParseNCName(ctxt); |
| 8721 | if (name != NULL) { |
| 8722 | axis = xmlXPathIsAxisName(name); |
| 8723 | if (axis != 0) { |
| 8724 | SKIP_BLANKS; |
| 8725 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8726 | SKIP(2); |
| 8727 | xmlFree(name); |
| 8728 | name = NULL; |
| 8729 | } else { |
| 8730 | /* an element name can conflict with an axis one :-\ */ |
| 8731 | axis = AXIS_CHILD; |
| 8732 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8733 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8734 | axis = AXIS_CHILD; |
| 8735 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8736 | } else if (CUR == '@') { |
| 8737 | NEXT; |
| 8738 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8739 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8740 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8741 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
| 8744 | CHECK_ERROR; |
| 8745 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8746 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8747 | if (test == 0) |
| 8748 | return; |
| 8749 | |
| 8750 | #ifdef DEBUG_STEP |
| 8751 | xmlGenericError(xmlGenericErrorContext, |
| 8752 | "Basis : computing new set\n"); |
| 8753 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8754 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8755 | #ifdef DEBUG_STEP |
| 8756 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8757 | if (ctxt->value == NULL) |
| 8758 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8759 | else if (ctxt->value->nodesetval == NULL) |
| 8760 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8761 | else |
| 8762 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8763 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8764 | |
| 8765 | eval_predicates: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8766 | op1 = ctxt->comp->last; |
| 8767 | ctxt->comp->last = -1; |
| 8768 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8769 | SKIP_BLANKS; |
| 8770 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8771 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8772 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8773 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8774 | #ifdef LIBXML_XPTR_ENABLED |
| 8775 | if (rangeto) { |
| 8776 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8777 | } else |
| 8778 | #endif |
| 8779 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8780 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8781 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8782 | } |
| 8783 | #ifdef DEBUG_STEP |
| 8784 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8785 | if (ctxt->value == NULL) |
| 8786 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8787 | else if (ctxt->value->nodesetval == NULL) |
| 8788 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8789 | else |
| 8790 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8791 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8792 | #endif |
| 8793 | } |
| 8794 | |
| 8795 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8796 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8797 | * @ctxt: the XPath Parser context |
| 8798 | * |
| 8799 | * [3] RelativeLocationPath ::= Step |
| 8800 | * | RelativeLocationPath '/' Step |
| 8801 | * | AbbreviatedRelativeLocationPath |
| 8802 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8803 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8804 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8805 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8806 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8807 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8808 | (xmlXPathParserContextPtr ctxt) { |
| 8809 | SKIP_BLANKS; |
| 8810 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8811 | SKIP(2); |
| 8812 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8813 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8814 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8815 | } else if (CUR == '/') { |
| 8816 | NEXT; |
| 8817 | SKIP_BLANKS; |
| 8818 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8819 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8820 | SKIP_BLANKS; |
| 8821 | while (CUR == '/') { |
| 8822 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8823 | SKIP(2); |
| 8824 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8825 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8826 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8827 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8828 | } else if (CUR == '/') { |
| 8829 | NEXT; |
| 8830 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8831 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8832 | } |
| 8833 | SKIP_BLANKS; |
| 8834 | } |
| 8835 | } |
| 8836 | |
| 8837 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8838 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8839 | * @ctxt: the XPath Parser context |
| 8840 | * |
| 8841 | * [1] LocationPath ::= RelativeLocationPath |
| 8842 | * | AbsoluteLocationPath |
| 8843 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 8844 | * | AbbreviatedAbsoluteLocationPath |
| 8845 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 8846 | * '//' RelativeLocationPath |
| 8847 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8848 | * Compile a location path |
| 8849 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8850 | * // is short for /descendant-or-self::node()/. For example, |
| 8851 | * //para is short for /descendant-or-self::node()/child::para and |
| 8852 | * so will select any para element in the document (even a para element |
| 8853 | * that is a document element will be selected by //para since the |
| 8854 | * document element node is a child of the root node); div//para is |
| 8855 | * short for div/descendant-or-self::node()/child::para and so will |
| 8856 | * select all para descendants of div children. |
| 8857 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8858 | static void |
| 8859 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8860 | SKIP_BLANKS; |
| 8861 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8862 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8863 | } else { |
| 8864 | while (CUR == '/') { |
| 8865 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8866 | SKIP(2); |
| 8867 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8868 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8869 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8870 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8871 | } else if (CUR == '/') { |
| 8872 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 8873 | SKIP_BLANKS; |
| 8874 | if ((CUR != 0 ) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8875 | ((IS_LETTER_CH(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 8876 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8877 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8878 | } |
| 8879 | } |
| 8880 | } |
| 8881 | } |
| 8882 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8883 | /************************************************************************ |
| 8884 | * * |
| 8885 | * XPath precompiled expression evaluation * |
| 8886 | * * |
| 8887 | ************************************************************************/ |
| 8888 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8889 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8890 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 8891 | |
| 8892 | /** |
| 8893 | * xmlXPathNodeCollectAndTest: |
| 8894 | * @ctxt: the XPath Parser context |
| 8895 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8896 | * @first: pointer to the first element in document order |
| 8897 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8898 | * |
| 8899 | * This is the function implementing a step: based on the current list |
| 8900 | * 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] | 8901 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8902 | * |
| 8903 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8904 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8905 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8906 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8907 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8908 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8909 | xmlXPathStepOpPtr op, |
| 8910 | xmlNodePtr * first, xmlNodePtr * last) |
| 8911 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8912 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 8913 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 8914 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8915 | const xmlChar *prefix = op->value4; |
| 8916 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8917 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8918 | |
| 8919 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8920 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8921 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8922 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8923 | xmlNodeSetPtr ret, list; |
| 8924 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8925 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8926 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8927 | xmlNodePtr cur = NULL; |
| 8928 | xmlXPathObjectPtr obj; |
| 8929 | xmlNodeSetPtr nodelist; |
| 8930 | xmlNodePtr tmp; |
| 8931 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8932 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8933 | obj = valuePop(ctxt); |
| 8934 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8935 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8936 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8937 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 8938 | if (URI == NULL) |
| 8939 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 8940 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8941 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8942 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8943 | #endif |
| 8944 | switch (axis) { |
| 8945 | case AXIS_ANCESTOR: |
| 8946 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8947 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8948 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8949 | first = NULL; |
| 8950 | next = xmlXPathNextAncestor; |
| 8951 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8952 | case AXIS_ANCESTOR_OR_SELF: |
| 8953 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8954 | xmlGenericError(xmlGenericErrorContext, |
| 8955 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8956 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8957 | first = NULL; |
| 8958 | next = xmlXPathNextAncestorOrSelf; |
| 8959 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8960 | case AXIS_ATTRIBUTE: |
| 8961 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8962 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8963 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8964 | first = NULL; |
| 8965 | last = NULL; |
| 8966 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8967 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8968 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8969 | case AXIS_CHILD: |
| 8970 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8971 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8972 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8973 | last = NULL; |
| 8974 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 8975 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8976 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8977 | case AXIS_DESCENDANT: |
| 8978 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8979 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8980 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8981 | last = NULL; |
| 8982 | next = xmlXPathNextDescendant; |
| 8983 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8984 | case AXIS_DESCENDANT_OR_SELF: |
| 8985 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8986 | xmlGenericError(xmlGenericErrorContext, |
| 8987 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8988 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8989 | last = NULL; |
| 8990 | next = xmlXPathNextDescendantOrSelf; |
| 8991 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8992 | case AXIS_FOLLOWING: |
| 8993 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8994 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8995 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8996 | last = NULL; |
| 8997 | next = xmlXPathNextFollowing; |
| 8998 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8999 | case AXIS_FOLLOWING_SIBLING: |
| 9000 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9001 | xmlGenericError(xmlGenericErrorContext, |
| 9002 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9003 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9004 | last = NULL; |
| 9005 | next = xmlXPathNextFollowingSibling; |
| 9006 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9007 | case AXIS_NAMESPACE: |
| 9008 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9009 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9010 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9011 | first = NULL; |
| 9012 | last = NULL; |
| 9013 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9014 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9015 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9016 | case AXIS_PARENT: |
| 9017 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9018 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9019 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9020 | first = NULL; |
| 9021 | next = xmlXPathNextParent; |
| 9022 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9023 | case AXIS_PRECEDING: |
| 9024 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9025 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
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 = xmlXPathNextPrecedingInternal; |
| 9029 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9030 | case AXIS_PRECEDING_SIBLING: |
| 9031 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9032 | xmlGenericError(xmlGenericErrorContext, |
| 9033 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9034 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9035 | first = NULL; |
| 9036 | next = xmlXPathNextPrecedingSibling; |
| 9037 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9038 | case AXIS_SELF: |
| 9039 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9040 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9041 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9042 | first = NULL; |
| 9043 | last = NULL; |
| 9044 | next = xmlXPathNextSelf; |
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 | } |
| 9048 | if (next == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9049 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9050 | |
| 9051 | nodelist = obj->nodesetval; |
| 9052 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9053 | xmlXPathFreeObject(obj); |
| 9054 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9055 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9056 | } |
| 9057 | addNode = xmlXPathNodeSetAddUnique; |
| 9058 | ret = NULL; |
| 9059 | #ifdef DEBUG_STEP |
| 9060 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9061 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9062 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9063 | case NODE_TEST_NONE: |
| 9064 | xmlGenericError(xmlGenericErrorContext, |
| 9065 | " searching for none !!!\n"); |
| 9066 | break; |
| 9067 | case NODE_TEST_TYPE: |
| 9068 | xmlGenericError(xmlGenericErrorContext, |
| 9069 | " searching for type %d\n", type); |
| 9070 | break; |
| 9071 | case NODE_TEST_PI: |
| 9072 | xmlGenericError(xmlGenericErrorContext, |
| 9073 | " searching for PI !!!\n"); |
| 9074 | break; |
| 9075 | case NODE_TEST_ALL: |
| 9076 | xmlGenericError(xmlGenericErrorContext, |
| 9077 | " searching for *\n"); |
| 9078 | break; |
| 9079 | case NODE_TEST_NS: |
| 9080 | xmlGenericError(xmlGenericErrorContext, |
| 9081 | " searching for namespace %s\n", |
| 9082 | prefix); |
| 9083 | break; |
| 9084 | case NODE_TEST_NAME: |
| 9085 | xmlGenericError(xmlGenericErrorContext, |
| 9086 | " searching for name %s\n", name); |
| 9087 | if (prefix != NULL) |
| 9088 | xmlGenericError(xmlGenericErrorContext, |
| 9089 | " with namespace %s\n", prefix); |
| 9090 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9091 | } |
| 9092 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9093 | #endif |
| 9094 | /* |
| 9095 | * 2.3 Node Tests |
| 9096 | * - For the attribute axis, the principal node type is attribute. |
| 9097 | * - For the namespace axis, the principal node type is namespace. |
| 9098 | * - For other axes, the principal node type is element. |
| 9099 | * |
| 9100 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9101 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9102 | * select all element children of the context node |
| 9103 | */ |
| 9104 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9105 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9106 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9107 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9108 | cur = NULL; |
| 9109 | list = xmlXPathNodeSetCreate(NULL); |
| 9110 | do { |
| 9111 | cur = next(ctxt, cur); |
| 9112 | if (cur == NULL) |
| 9113 | break; |
| 9114 | if ((first != NULL) && (*first == cur)) |
| 9115 | break; |
| 9116 | if (((t % 256) == 0) && |
| 9117 | (first != NULL) && (*first != NULL) && |
| 9118 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9119 | break; |
| 9120 | if ((last != NULL) && (*last == cur)) |
| 9121 | break; |
| 9122 | if (((t % 256) == 0) && |
| 9123 | (last != NULL) && (*last != NULL) && |
| 9124 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9125 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9126 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9127 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9128 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9129 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9130 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9131 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9132 | ctxt->context->node = tmp; |
| 9133 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9134 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9135 | if ((cur->type == type) || |
| 9136 | ((type == NODE_TYPE_NODE) && |
| 9137 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9138 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9139 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9140 | (cur->type == XML_NAMESPACE_DECL) || |
| 9141 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9142 | (cur->type == XML_PI_NODE) || |
| 9143 | (cur->type == XML_COMMENT_NODE) || |
| 9144 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9145 | (cur->type == XML_TEXT_NODE))) || |
| 9146 | ((type == NODE_TYPE_TEXT) && |
| 9147 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9148 | #ifdef DEBUG_STEP |
| 9149 | n++; |
| 9150 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9151 | addNode(list, cur); |
| 9152 | } |
| 9153 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9154 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9155 | if (cur->type == XML_PI_NODE) { |
| 9156 | if ((name != NULL) && |
| 9157 | (!xmlStrEqual(name, cur->name))) |
| 9158 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9159 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9160 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9161 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9162 | addNode(list, cur); |
| 9163 | } |
| 9164 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9165 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9166 | if (axis == AXIS_ATTRIBUTE) { |
| 9167 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9168 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9169 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9170 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9171 | addNode(list, cur); |
| 9172 | } |
| 9173 | } else if (axis == AXIS_NAMESPACE) { |
| 9174 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9175 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9176 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9177 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9178 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9179 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9180 | } |
| 9181 | } else { |
| 9182 | if (cur->type == XML_ELEMENT_NODE) { |
| 9183 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9184 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9185 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9186 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9187 | addNode(list, cur); |
| 9188 | } else if ((cur->ns != NULL) && |
| 9189 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9190 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9191 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9192 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9193 | addNode(list, cur); |
| 9194 | } |
| 9195 | } |
| 9196 | } |
| 9197 | break; |
| 9198 | case NODE_TEST_NS:{ |
| 9199 | TODO; |
| 9200 | break; |
| 9201 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9202 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9203 | switch (cur->type) { |
| 9204 | case XML_ELEMENT_NODE: |
| 9205 | if (xmlStrEqual(name, cur->name)) { |
| 9206 | if (prefix == NULL) { |
| 9207 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9208 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9209 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9210 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9211 | addNode(list, cur); |
| 9212 | } |
| 9213 | } else { |
| 9214 | if ((cur->ns != NULL) && |
| 9215 | (xmlStrEqual(URI, |
| 9216 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9217 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9218 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9219 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9220 | addNode(list, cur); |
| 9221 | } |
| 9222 | } |
| 9223 | } |
| 9224 | break; |
| 9225 | case XML_ATTRIBUTE_NODE:{ |
| 9226 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9227 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9228 | if (xmlStrEqual(name, attr->name)) { |
| 9229 | if (prefix == NULL) { |
| 9230 | if ((attr->ns == NULL) || |
| 9231 | (attr->ns->prefix == NULL)) { |
| 9232 | #ifdef DEBUG_STEP |
| 9233 | n++; |
| 9234 | #endif |
| 9235 | addNode(list, |
| 9236 | (xmlNodePtr) attr); |
| 9237 | } |
| 9238 | } else { |
| 9239 | if ((attr->ns != NULL) && |
| 9240 | (xmlStrEqual(URI, |
| 9241 | attr->ns-> |
| 9242 | href))) { |
| 9243 | #ifdef DEBUG_STEP |
| 9244 | n++; |
| 9245 | #endif |
| 9246 | addNode(list, |
| 9247 | (xmlNodePtr) attr); |
| 9248 | } |
| 9249 | } |
| 9250 | } |
| 9251 | break; |
| 9252 | } |
| 9253 | case XML_NAMESPACE_DECL: |
| 9254 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9255 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9256 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9257 | if ((ns->prefix != NULL) && (name != NULL) |
| 9258 | && (xmlStrEqual(ns->prefix, name))) { |
| 9259 | #ifdef DEBUG_STEP |
| 9260 | n++; |
| 9261 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9262 | xmlXPathNodeSetAddNs(list, |
| 9263 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9264 | } |
| 9265 | } |
| 9266 | break; |
| 9267 | default: |
| 9268 | break; |
| 9269 | } |
| 9270 | break; |
| 9271 | break; |
| 9272 | } |
| 9273 | } while (cur != NULL); |
| 9274 | |
| 9275 | /* |
| 9276 | * If there is some predicate filtering do it now |
| 9277 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9278 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9279 | xmlXPathObjectPtr obj2; |
| 9280 | |
| 9281 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9282 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9283 | CHECK_TYPE0(XPATH_NODESET); |
| 9284 | obj2 = valuePop(ctxt); |
| 9285 | list = obj2->nodesetval; |
| 9286 | obj2->nodesetval = NULL; |
| 9287 | xmlXPathFreeObject(obj2); |
| 9288 | } |
| 9289 | if (ret == NULL) { |
| 9290 | ret = list; |
| 9291 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9292 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9293 | xmlXPathFreeNodeSet(list); |
| 9294 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9295 | } |
| 9296 | ctxt->context->node = tmp; |
| 9297 | #ifdef DEBUG_STEP |
| 9298 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9299 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9300 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9301 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9302 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9303 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9304 | ctxt->value->boolval = 1; |
| 9305 | ctxt->value->user = obj->user; |
| 9306 | obj->user = NULL; |
| 9307 | obj->boolval = 0; |
| 9308 | } |
| 9309 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9310 | return(t); |
| 9311 | } |
| 9312 | |
| 9313 | /** |
| 9314 | * xmlXPathNodeCollectAndTestNth: |
| 9315 | * @ctxt: the XPath Parser context |
| 9316 | * @op: the XPath precompiled step operation |
| 9317 | * @indx: the index to collect |
| 9318 | * @first: pointer to the first element in document order |
| 9319 | * @last: pointer to the last element in document order |
| 9320 | * |
| 9321 | * This is the function implementing a step: based on the current list |
| 9322 | * 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] | 9323 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9324 | * |
| 9325 | * Pushes the new NodeSet resulting from the search. |
| 9326 | * Returns the number of node traversed |
| 9327 | */ |
| 9328 | static int |
| 9329 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9330 | xmlXPathStepOpPtr op, int indx, |
| 9331 | xmlNodePtr * first, xmlNodePtr * last) |
| 9332 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9333 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9334 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9335 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9336 | const xmlChar *prefix = op->value4; |
| 9337 | const xmlChar *name = op->value5; |
| 9338 | const xmlChar *URI = NULL; |
| 9339 | int n = 0, t = 0; |
| 9340 | |
| 9341 | int i; |
| 9342 | xmlNodeSetPtr list; |
| 9343 | xmlXPathTraversalFunction next = NULL; |
| 9344 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9345 | xmlNodePtr cur = NULL; |
| 9346 | xmlXPathObjectPtr obj; |
| 9347 | xmlNodeSetPtr nodelist; |
| 9348 | xmlNodePtr tmp; |
| 9349 | |
| 9350 | CHECK_TYPE0(XPATH_NODESET); |
| 9351 | obj = valuePop(ctxt); |
| 9352 | addNode = xmlXPathNodeSetAdd; |
| 9353 | if (prefix != NULL) { |
| 9354 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 9355 | if (URI == NULL) |
| 9356 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 9357 | } |
| 9358 | #ifdef DEBUG_STEP_NTH |
| 9359 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9360 | if (first != NULL) { |
| 9361 | if (*first != NULL) |
| 9362 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9363 | (*first)->name); |
| 9364 | else |
| 9365 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9366 | } |
| 9367 | if (last != NULL) { |
| 9368 | if (*last != NULL) |
| 9369 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9370 | (*last)->name); |
| 9371 | else |
| 9372 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9373 | } |
| 9374 | #endif |
| 9375 | switch (axis) { |
| 9376 | case AXIS_ANCESTOR: |
| 9377 | #ifdef DEBUG_STEP_NTH |
| 9378 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9379 | #endif |
| 9380 | first = NULL; |
| 9381 | next = xmlXPathNextAncestor; |
| 9382 | break; |
| 9383 | case AXIS_ANCESTOR_OR_SELF: |
| 9384 | #ifdef DEBUG_STEP_NTH |
| 9385 | xmlGenericError(xmlGenericErrorContext, |
| 9386 | "axis 'ancestors-or-self' "); |
| 9387 | #endif |
| 9388 | first = NULL; |
| 9389 | next = xmlXPathNextAncestorOrSelf; |
| 9390 | break; |
| 9391 | case AXIS_ATTRIBUTE: |
| 9392 | #ifdef DEBUG_STEP_NTH |
| 9393 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 9394 | #endif |
| 9395 | first = NULL; |
| 9396 | last = NULL; |
| 9397 | next = xmlXPathNextAttribute; |
| 9398 | break; |
| 9399 | case AXIS_CHILD: |
| 9400 | #ifdef DEBUG_STEP_NTH |
| 9401 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9402 | #endif |
| 9403 | last = NULL; |
| 9404 | next = xmlXPathNextChild; |
| 9405 | break; |
| 9406 | case AXIS_DESCENDANT: |
| 9407 | #ifdef DEBUG_STEP_NTH |
| 9408 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9409 | #endif |
| 9410 | last = NULL; |
| 9411 | next = xmlXPathNextDescendant; |
| 9412 | break; |
| 9413 | case AXIS_DESCENDANT_OR_SELF: |
| 9414 | #ifdef DEBUG_STEP_NTH |
| 9415 | xmlGenericError(xmlGenericErrorContext, |
| 9416 | "axis 'descendant-or-self' "); |
| 9417 | #endif |
| 9418 | last = NULL; |
| 9419 | next = xmlXPathNextDescendantOrSelf; |
| 9420 | break; |
| 9421 | case AXIS_FOLLOWING: |
| 9422 | #ifdef DEBUG_STEP_NTH |
| 9423 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9424 | #endif |
| 9425 | last = NULL; |
| 9426 | next = xmlXPathNextFollowing; |
| 9427 | break; |
| 9428 | case AXIS_FOLLOWING_SIBLING: |
| 9429 | #ifdef DEBUG_STEP_NTH |
| 9430 | xmlGenericError(xmlGenericErrorContext, |
| 9431 | "axis 'following-siblings' "); |
| 9432 | #endif |
| 9433 | last = NULL; |
| 9434 | next = xmlXPathNextFollowingSibling; |
| 9435 | break; |
| 9436 | case AXIS_NAMESPACE: |
| 9437 | #ifdef DEBUG_STEP_NTH |
| 9438 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9439 | #endif |
| 9440 | last = NULL; |
| 9441 | first = NULL; |
| 9442 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9443 | break; |
| 9444 | case AXIS_PARENT: |
| 9445 | #ifdef DEBUG_STEP_NTH |
| 9446 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9447 | #endif |
| 9448 | first = NULL; |
| 9449 | next = xmlXPathNextParent; |
| 9450 | break; |
| 9451 | case AXIS_PRECEDING: |
| 9452 | #ifdef DEBUG_STEP_NTH |
| 9453 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9454 | #endif |
| 9455 | first = NULL; |
| 9456 | next = xmlXPathNextPrecedingInternal; |
| 9457 | break; |
| 9458 | case AXIS_PRECEDING_SIBLING: |
| 9459 | #ifdef DEBUG_STEP_NTH |
| 9460 | xmlGenericError(xmlGenericErrorContext, |
| 9461 | "axis 'preceding-sibling' "); |
| 9462 | #endif |
| 9463 | first = NULL; |
| 9464 | next = xmlXPathNextPrecedingSibling; |
| 9465 | break; |
| 9466 | case AXIS_SELF: |
| 9467 | #ifdef DEBUG_STEP_NTH |
| 9468 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9469 | #endif |
| 9470 | first = NULL; |
| 9471 | last = NULL; |
| 9472 | next = xmlXPathNextSelf; |
| 9473 | break; |
| 9474 | } |
| 9475 | if (next == NULL) |
| 9476 | return(0); |
| 9477 | |
| 9478 | nodelist = obj->nodesetval; |
| 9479 | if (nodelist == NULL) { |
| 9480 | xmlXPathFreeObject(obj); |
| 9481 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9482 | return(0); |
| 9483 | } |
| 9484 | addNode = xmlXPathNodeSetAddUnique; |
| 9485 | #ifdef DEBUG_STEP_NTH |
| 9486 | xmlGenericError(xmlGenericErrorContext, |
| 9487 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9488 | switch (test) { |
| 9489 | case NODE_TEST_NONE: |
| 9490 | xmlGenericError(xmlGenericErrorContext, |
| 9491 | " searching for none !!!\n"); |
| 9492 | break; |
| 9493 | case NODE_TEST_TYPE: |
| 9494 | xmlGenericError(xmlGenericErrorContext, |
| 9495 | " searching for type %d\n", type); |
| 9496 | break; |
| 9497 | case NODE_TEST_PI: |
| 9498 | xmlGenericError(xmlGenericErrorContext, |
| 9499 | " searching for PI !!!\n"); |
| 9500 | break; |
| 9501 | case NODE_TEST_ALL: |
| 9502 | xmlGenericError(xmlGenericErrorContext, |
| 9503 | " searching for *\n"); |
| 9504 | break; |
| 9505 | case NODE_TEST_NS: |
| 9506 | xmlGenericError(xmlGenericErrorContext, |
| 9507 | " searching for namespace %s\n", |
| 9508 | prefix); |
| 9509 | break; |
| 9510 | case NODE_TEST_NAME: |
| 9511 | xmlGenericError(xmlGenericErrorContext, |
| 9512 | " searching for name %s\n", name); |
| 9513 | if (prefix != NULL) |
| 9514 | xmlGenericError(xmlGenericErrorContext, |
| 9515 | " with namespace %s\n", prefix); |
| 9516 | break; |
| 9517 | } |
| 9518 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9519 | #endif |
| 9520 | /* |
| 9521 | * 2.3 Node Tests |
| 9522 | * - For the attribute axis, the principal node type is attribute. |
| 9523 | * - For the namespace axis, the principal node type is namespace. |
| 9524 | * - For other axes, the principal node type is element. |
| 9525 | * |
| 9526 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9527 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9528 | * select all element children of the context node |
| 9529 | */ |
| 9530 | tmp = ctxt->context->node; |
| 9531 | list = xmlXPathNodeSetCreate(NULL); |
| 9532 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9533 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9534 | |
| 9535 | cur = NULL; |
| 9536 | n = 0; |
| 9537 | do { |
| 9538 | cur = next(ctxt, cur); |
| 9539 | if (cur == NULL) |
| 9540 | break; |
| 9541 | if ((first != NULL) && (*first == cur)) |
| 9542 | break; |
| 9543 | if (((t % 256) == 0) && |
| 9544 | (first != NULL) && (*first != NULL) && |
| 9545 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9546 | break; |
| 9547 | if ((last != NULL) && (*last == cur)) |
| 9548 | break; |
| 9549 | if (((t % 256) == 0) && |
| 9550 | (last != NULL) && (*last != NULL) && |
| 9551 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9552 | break; |
| 9553 | t++; |
| 9554 | switch (test) { |
| 9555 | case NODE_TEST_NONE: |
| 9556 | ctxt->context->node = tmp; |
| 9557 | STRANGE return(0); |
| 9558 | case NODE_TEST_TYPE: |
| 9559 | if ((cur->type == type) || |
| 9560 | ((type == NODE_TYPE_NODE) && |
| 9561 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9562 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9563 | (cur->type == XML_ELEMENT_NODE) || |
| 9564 | (cur->type == XML_PI_NODE) || |
| 9565 | (cur->type == XML_COMMENT_NODE) || |
| 9566 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 9567 | (cur->type == XML_TEXT_NODE))) || |
| 9568 | ((type == NODE_TYPE_TEXT) && |
| 9569 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9570 | n++; |
| 9571 | if (n == indx) |
| 9572 | addNode(list, cur); |
| 9573 | } |
| 9574 | break; |
| 9575 | case NODE_TEST_PI: |
| 9576 | if (cur->type == XML_PI_NODE) { |
| 9577 | if ((name != NULL) && |
| 9578 | (!xmlStrEqual(name, cur->name))) |
| 9579 | break; |
| 9580 | n++; |
| 9581 | if (n == indx) |
| 9582 | addNode(list, cur); |
| 9583 | } |
| 9584 | break; |
| 9585 | case NODE_TEST_ALL: |
| 9586 | if (axis == AXIS_ATTRIBUTE) { |
| 9587 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9588 | n++; |
| 9589 | if (n == indx) |
| 9590 | addNode(list, cur); |
| 9591 | } |
| 9592 | } else if (axis == AXIS_NAMESPACE) { |
| 9593 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9594 | n++; |
| 9595 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9596 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9597 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9598 | } |
| 9599 | } else { |
| 9600 | if (cur->type == XML_ELEMENT_NODE) { |
| 9601 | if (prefix == NULL) { |
| 9602 | n++; |
| 9603 | if (n == indx) |
| 9604 | addNode(list, cur); |
| 9605 | } else if ((cur->ns != NULL) && |
| 9606 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9607 | n++; |
| 9608 | if (n == indx) |
| 9609 | addNode(list, cur); |
| 9610 | } |
| 9611 | } |
| 9612 | } |
| 9613 | break; |
| 9614 | case NODE_TEST_NS:{ |
| 9615 | TODO; |
| 9616 | break; |
| 9617 | } |
| 9618 | case NODE_TEST_NAME: |
| 9619 | switch (cur->type) { |
| 9620 | case XML_ELEMENT_NODE: |
| 9621 | if (xmlStrEqual(name, cur->name)) { |
| 9622 | if (prefix == NULL) { |
| 9623 | if (cur->ns == NULL) { |
| 9624 | n++; |
| 9625 | if (n == indx) |
| 9626 | addNode(list, cur); |
| 9627 | } |
| 9628 | } else { |
| 9629 | if ((cur->ns != NULL) && |
| 9630 | (xmlStrEqual(URI, |
| 9631 | cur->ns->href))) { |
| 9632 | n++; |
| 9633 | if (n == indx) |
| 9634 | addNode(list, cur); |
| 9635 | } |
| 9636 | } |
| 9637 | } |
| 9638 | break; |
| 9639 | case XML_ATTRIBUTE_NODE:{ |
| 9640 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9641 | |
| 9642 | if (xmlStrEqual(name, attr->name)) { |
| 9643 | if (prefix == NULL) { |
| 9644 | if ((attr->ns == NULL) || |
| 9645 | (attr->ns->prefix == NULL)) { |
| 9646 | n++; |
| 9647 | if (n == indx) |
| 9648 | addNode(list, cur); |
| 9649 | } |
| 9650 | } else { |
| 9651 | if ((attr->ns != NULL) && |
| 9652 | (xmlStrEqual(URI, |
| 9653 | attr->ns-> |
| 9654 | href))) { |
| 9655 | n++; |
| 9656 | if (n == indx) |
| 9657 | addNode(list, cur); |
| 9658 | } |
| 9659 | } |
| 9660 | } |
| 9661 | break; |
| 9662 | } |
| 9663 | case XML_NAMESPACE_DECL: |
| 9664 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9665 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9666 | |
| 9667 | if ((ns->prefix != NULL) && (name != NULL) |
| 9668 | && (xmlStrEqual(ns->prefix, name))) { |
| 9669 | n++; |
| 9670 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9671 | xmlXPathNodeSetAddNs(list, |
| 9672 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9673 | } |
| 9674 | } |
| 9675 | break; |
| 9676 | default: |
| 9677 | break; |
| 9678 | } |
| 9679 | break; |
| 9680 | break; |
| 9681 | } |
| 9682 | } while (n < indx); |
| 9683 | } |
| 9684 | ctxt->context->node = tmp; |
| 9685 | #ifdef DEBUG_STEP_NTH |
| 9686 | xmlGenericError(xmlGenericErrorContext, |
| 9687 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9688 | t, list->nodeNr); |
| 9689 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9690 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9691 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9692 | ctxt->value->boolval = 1; |
| 9693 | ctxt->value->user = obj->user; |
| 9694 | obj->user = NULL; |
| 9695 | obj->boolval = 0; |
| 9696 | } |
| 9697 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9698 | return(t); |
| 9699 | } |
| 9700 | |
| 9701 | /** |
| 9702 | * xmlXPathCompOpEvalFirst: |
| 9703 | * @ctxt: the XPath parser context with the compiled expression |
| 9704 | * @op: an XPath compiled operation |
| 9705 | * @first: the first elem found so far |
| 9706 | * |
| 9707 | * Evaluate the Precompiled XPath operation searching only the first |
| 9708 | * element in document order |
| 9709 | * |
| 9710 | * Returns the number of examined objects. |
| 9711 | */ |
| 9712 | static int |
| 9713 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9714 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9715 | { |
| 9716 | int total = 0, cur; |
| 9717 | xmlXPathCompExprPtr comp; |
| 9718 | xmlXPathObjectPtr arg1, arg2; |
| 9719 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9720 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9721 | comp = ctxt->comp; |
| 9722 | switch (op->op) { |
| 9723 | case XPATH_OP_END: |
| 9724 | return (0); |
| 9725 | case XPATH_OP_UNION: |
| 9726 | total = |
| 9727 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9728 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9729 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9730 | if ((ctxt->value != NULL) |
| 9731 | && (ctxt->value->type == XPATH_NODESET) |
| 9732 | && (ctxt->value->nodesetval != NULL) |
| 9733 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9734 | /* |
| 9735 | * limit tree traversing to first node in the result |
| 9736 | */ |
| 9737 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9738 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9739 | } |
| 9740 | cur = |
| 9741 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9742 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9743 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9744 | CHECK_TYPE0(XPATH_NODESET); |
| 9745 | arg2 = valuePop(ctxt); |
| 9746 | |
| 9747 | CHECK_TYPE0(XPATH_NODESET); |
| 9748 | arg1 = valuePop(ctxt); |
| 9749 | |
| 9750 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9751 | arg2->nodesetval); |
| 9752 | valuePush(ctxt, arg1); |
| 9753 | xmlXPathFreeObject(arg2); |
| 9754 | /* optimizer */ |
| 9755 | if (total > cur) |
| 9756 | xmlXPathCompSwap(op); |
| 9757 | return (total + cur); |
| 9758 | case XPATH_OP_ROOT: |
| 9759 | xmlXPathRoot(ctxt); |
| 9760 | return (0); |
| 9761 | case XPATH_OP_NODE: |
| 9762 | if (op->ch1 != -1) |
| 9763 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9764 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9765 | if (op->ch2 != -1) |
| 9766 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9767 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9768 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9769 | return (total); |
| 9770 | case XPATH_OP_RESET: |
| 9771 | if (op->ch1 != -1) |
| 9772 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9773 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9774 | if (op->ch2 != -1) |
| 9775 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9776 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9777 | ctxt->context->node = NULL; |
| 9778 | return (total); |
| 9779 | case XPATH_OP_COLLECT:{ |
| 9780 | if (op->ch1 == -1) |
| 9781 | return (total); |
| 9782 | |
| 9783 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9784 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9785 | |
| 9786 | /* |
| 9787 | * Optimization for [n] selection where n is a number |
| 9788 | */ |
| 9789 | if ((op->ch2 != -1) && |
| 9790 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9791 | (comp->steps[op->ch2].ch1 == -1) && |
| 9792 | (comp->steps[op->ch2].ch2 != -1) && |
| 9793 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9794 | XPATH_OP_VALUE)) { |
| 9795 | xmlXPathObjectPtr val; |
| 9796 | |
| 9797 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9798 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9799 | int indx = (int) val->floatval; |
| 9800 | |
| 9801 | if (val->floatval == (float) indx) { |
| 9802 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 9803 | first, NULL); |
| 9804 | return (total); |
| 9805 | } |
| 9806 | } |
| 9807 | } |
| 9808 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 9809 | return (total); |
| 9810 | } |
| 9811 | case XPATH_OP_VALUE: |
| 9812 | valuePush(ctxt, |
| 9813 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9814 | return (0); |
| 9815 | case XPATH_OP_SORT: |
| 9816 | if (op->ch1 != -1) |
| 9817 | total += |
| 9818 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9819 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9820 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9821 | if ((ctxt->value != NULL) |
| 9822 | && (ctxt->value->type == XPATH_NODESET) |
| 9823 | && (ctxt->value->nodesetval != NULL)) |
| 9824 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9825 | return (total); |
| 9826 | default: |
| 9827 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9828 | } |
| 9829 | } |
| 9830 | |
| 9831 | /** |
| 9832 | * xmlXPathCompOpEvalLast: |
| 9833 | * @ctxt: the XPath parser context with the compiled expression |
| 9834 | * @op: an XPath compiled operation |
| 9835 | * @last: the last elem found so far |
| 9836 | * |
| 9837 | * Evaluate the Precompiled XPath operation searching only the last |
| 9838 | * element in document order |
| 9839 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9840 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9841 | */ |
| 9842 | static int |
| 9843 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 9844 | xmlNodePtr * last) |
| 9845 | { |
| 9846 | int total = 0, cur; |
| 9847 | xmlXPathCompExprPtr comp; |
| 9848 | xmlXPathObjectPtr arg1, arg2; |
| 9849 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9850 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9851 | comp = ctxt->comp; |
| 9852 | switch (op->op) { |
| 9853 | case XPATH_OP_END: |
| 9854 | return (0); |
| 9855 | case XPATH_OP_UNION: |
| 9856 | total = |
| 9857 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9858 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9859 | if ((ctxt->value != NULL) |
| 9860 | && (ctxt->value->type == XPATH_NODESET) |
| 9861 | && (ctxt->value->nodesetval != NULL) |
| 9862 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9863 | /* |
| 9864 | * limit tree traversing to first node in the result |
| 9865 | */ |
| 9866 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9867 | *last = |
| 9868 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 9869 | nodesetval->nodeNr - |
| 9870 | 1]; |
| 9871 | } |
| 9872 | cur = |
| 9873 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9874 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9875 | if ((ctxt->value != NULL) |
| 9876 | && (ctxt->value->type == XPATH_NODESET) |
| 9877 | && (ctxt->value->nodesetval != NULL) |
| 9878 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9879 | } |
| 9880 | CHECK_TYPE0(XPATH_NODESET); |
| 9881 | arg2 = valuePop(ctxt); |
| 9882 | |
| 9883 | CHECK_TYPE0(XPATH_NODESET); |
| 9884 | arg1 = valuePop(ctxt); |
| 9885 | |
| 9886 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9887 | arg2->nodesetval); |
| 9888 | valuePush(ctxt, arg1); |
| 9889 | xmlXPathFreeObject(arg2); |
| 9890 | /* optimizer */ |
| 9891 | if (total > cur) |
| 9892 | xmlXPathCompSwap(op); |
| 9893 | return (total + cur); |
| 9894 | case XPATH_OP_ROOT: |
| 9895 | xmlXPathRoot(ctxt); |
| 9896 | return (0); |
| 9897 | case XPATH_OP_NODE: |
| 9898 | if (op->ch1 != -1) |
| 9899 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9900 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9901 | if (op->ch2 != -1) |
| 9902 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9903 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9904 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9905 | return (total); |
| 9906 | case XPATH_OP_RESET: |
| 9907 | if (op->ch1 != -1) |
| 9908 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9909 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9910 | if (op->ch2 != -1) |
| 9911 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9912 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9913 | ctxt->context->node = NULL; |
| 9914 | return (total); |
| 9915 | case XPATH_OP_COLLECT:{ |
| 9916 | if (op->ch1 == -1) |
| 9917 | return (0); |
| 9918 | |
| 9919 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9920 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9921 | |
| 9922 | /* |
| 9923 | * Optimization for [n] selection where n is a number |
| 9924 | */ |
| 9925 | if ((op->ch2 != -1) && |
| 9926 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9927 | (comp->steps[op->ch2].ch1 == -1) && |
| 9928 | (comp->steps[op->ch2].ch2 != -1) && |
| 9929 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9930 | XPATH_OP_VALUE)) { |
| 9931 | xmlXPathObjectPtr val; |
| 9932 | |
| 9933 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9934 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9935 | int indx = (int) val->floatval; |
| 9936 | |
| 9937 | if (val->floatval == (float) indx) { |
| 9938 | total += |
| 9939 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 9940 | indx, NULL, |
| 9941 | last); |
| 9942 | return (total); |
| 9943 | } |
| 9944 | } |
| 9945 | } |
| 9946 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 9947 | return (total); |
| 9948 | } |
| 9949 | case XPATH_OP_VALUE: |
| 9950 | valuePush(ctxt, |
| 9951 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9952 | return (0); |
| 9953 | case XPATH_OP_SORT: |
| 9954 | if (op->ch1 != -1) |
| 9955 | total += |
| 9956 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 9957 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9958 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9959 | if ((ctxt->value != NULL) |
| 9960 | && (ctxt->value->type == XPATH_NODESET) |
| 9961 | && (ctxt->value->nodesetval != NULL)) |
| 9962 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9963 | return (total); |
| 9964 | default: |
| 9965 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9966 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9969 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9970 | * xmlXPathCompOpEval: |
| 9971 | * @ctxt: the XPath parser context with the compiled expression |
| 9972 | * @op: an XPath compiled operation |
| 9973 | * |
| 9974 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9975 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9976 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9977 | static int |
| 9978 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 9979 | { |
| 9980 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9981 | int equal, ret; |
| 9982 | xmlXPathCompExprPtr comp; |
| 9983 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9984 | xmlNodePtr bak; |
| 9985 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 9986 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 9987 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9988 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9989 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9990 | comp = ctxt->comp; |
| 9991 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9992 | case XPATH_OP_END: |
| 9993 | return (0); |
| 9994 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 9995 | bakd = ctxt->context->doc; |
| 9996 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 9997 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 9998 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9999 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10000 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10001 | xmlXPathBooleanFunction(ctxt, 1); |
| 10002 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10003 | return (total); |
| 10004 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10005 | ctxt->context->doc = bakd; |
| 10006 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10007 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10008 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10009 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10010 | if (ctxt->error) { |
| 10011 | xmlXPathFreeObject(arg2); |
| 10012 | return(0); |
| 10013 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10014 | xmlXPathBooleanFunction(ctxt, 1); |
| 10015 | arg1 = valuePop(ctxt); |
| 10016 | arg1->boolval &= arg2->boolval; |
| 10017 | valuePush(ctxt, arg1); |
| 10018 | xmlXPathFreeObject(arg2); |
| 10019 | return (total); |
| 10020 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10021 | bakd = ctxt->context->doc; |
| 10022 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10023 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10024 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10025 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10026 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10027 | xmlXPathBooleanFunction(ctxt, 1); |
| 10028 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10029 | return (total); |
| 10030 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10031 | ctxt->context->doc = bakd; |
| 10032 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10033 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10034 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10035 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10036 | if (ctxt->error) { |
| 10037 | xmlXPathFreeObject(arg2); |
| 10038 | return(0); |
| 10039 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10040 | xmlXPathBooleanFunction(ctxt, 1); |
| 10041 | arg1 = valuePop(ctxt); |
| 10042 | arg1->boolval |= arg2->boolval; |
| 10043 | valuePush(ctxt, arg1); |
| 10044 | xmlXPathFreeObject(arg2); |
| 10045 | return (total); |
| 10046 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10047 | bakd = ctxt->context->doc; |
| 10048 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10049 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10050 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10051 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10052 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10053 | ctxt->context->doc = bakd; |
| 10054 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10055 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10056 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10057 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10058 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10059 | if (op->value) |
| 10060 | equal = xmlXPathEqualValues(ctxt); |
| 10061 | else |
| 10062 | equal = xmlXPathNotEqualValues(ctxt); |
| 10063 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10064 | return (total); |
| 10065 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10066 | bakd = ctxt->context->doc; |
| 10067 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10068 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10069 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10070 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10071 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10072 | ctxt->context->doc = bakd; |
| 10073 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10074 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10075 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10076 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10077 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10078 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 10079 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 10080 | return (total); |
| 10081 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10082 | bakd = ctxt->context->doc; |
| 10083 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10084 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10085 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10086 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10087 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10088 | if (op->ch2 != -1) { |
| 10089 | ctxt->context->doc = bakd; |
| 10090 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10091 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10092 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10093 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10094 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10095 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10096 | if (op->value == 0) |
| 10097 | xmlXPathSubValues(ctxt); |
| 10098 | else if (op->value == 1) |
| 10099 | xmlXPathAddValues(ctxt); |
| 10100 | else if (op->value == 2) |
| 10101 | xmlXPathValueFlipSign(ctxt); |
| 10102 | else if (op->value == 3) { |
| 10103 | CAST_TO_NUMBER; |
| 10104 | CHECK_TYPE0(XPATH_NUMBER); |
| 10105 | } |
| 10106 | return (total); |
| 10107 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10108 | bakd = ctxt->context->doc; |
| 10109 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10110 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10111 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10112 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10113 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10114 | ctxt->context->doc = bakd; |
| 10115 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10116 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10117 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10118 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10119 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10120 | if (op->value == 0) |
| 10121 | xmlXPathMultValues(ctxt); |
| 10122 | else if (op->value == 1) |
| 10123 | xmlXPathDivValues(ctxt); |
| 10124 | else if (op->value == 2) |
| 10125 | xmlXPathModValues(ctxt); |
| 10126 | return (total); |
| 10127 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10128 | bakd = ctxt->context->doc; |
| 10129 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10130 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10131 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10132 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10133 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10134 | ctxt->context->doc = bakd; |
| 10135 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10136 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10137 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10138 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10139 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10140 | CHECK_TYPE0(XPATH_NODESET); |
| 10141 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10142 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10143 | CHECK_TYPE0(XPATH_NODESET); |
| 10144 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10145 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10146 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10147 | arg2->nodesetval); |
| 10148 | valuePush(ctxt, arg1); |
| 10149 | xmlXPathFreeObject(arg2); |
| 10150 | return (total); |
| 10151 | case XPATH_OP_ROOT: |
| 10152 | xmlXPathRoot(ctxt); |
| 10153 | return (total); |
| 10154 | case XPATH_OP_NODE: |
| 10155 | if (op->ch1 != -1) |
| 10156 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10157 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10158 | if (op->ch2 != -1) |
| 10159 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10160 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10161 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10162 | return (total); |
| 10163 | case XPATH_OP_RESET: |
| 10164 | if (op->ch1 != -1) |
| 10165 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10166 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10167 | if (op->ch2 != -1) |
| 10168 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10169 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10170 | ctxt->context->node = NULL; |
| 10171 | return (total); |
| 10172 | case XPATH_OP_COLLECT:{ |
| 10173 | if (op->ch1 == -1) |
| 10174 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10175 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10176 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10177 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10178 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10179 | /* |
| 10180 | * Optimization for [n] selection where n is a number |
| 10181 | */ |
| 10182 | if ((op->ch2 != -1) && |
| 10183 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10184 | (comp->steps[op->ch2].ch1 == -1) && |
| 10185 | (comp->steps[op->ch2].ch2 != -1) && |
| 10186 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10187 | XPATH_OP_VALUE)) { |
| 10188 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 10189 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10190 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10191 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10192 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10193 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10194 | if (val->floatval == (float) indx) { |
| 10195 | total += |
| 10196 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10197 | indx, NULL, |
| 10198 | NULL); |
| 10199 | return (total); |
| 10200 | } |
| 10201 | } |
| 10202 | } |
| 10203 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 10204 | return (total); |
| 10205 | } |
| 10206 | case XPATH_OP_VALUE: |
| 10207 | valuePush(ctxt, |
| 10208 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10209 | return (total); |
| 10210 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10211 | xmlXPathObjectPtr val; |
| 10212 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10213 | if (op->ch1 != -1) |
| 10214 | total += |
| 10215 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10216 | if (op->value5 == NULL) { |
| 10217 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 10218 | if (val == NULL) { |
| 10219 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10220 | return(0); |
| 10221 | } |
| 10222 | valuePush(ctxt, val); |
| 10223 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10224 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10225 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10226 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10227 | if (URI == NULL) { |
| 10228 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10229 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10230 | op->value4, op->value5); |
| 10231 | return (total); |
| 10232 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10233 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 10234 | op->value4, URI); |
| 10235 | if (val == NULL) { |
| 10236 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10237 | return(0); |
| 10238 | } |
| 10239 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10240 | } |
| 10241 | return (total); |
| 10242 | } |
| 10243 | case XPATH_OP_FUNCTION:{ |
| 10244 | xmlXPathFunction func; |
| 10245 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10246 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10247 | |
| 10248 | if (op->ch1 != -1) |
| 10249 | total += |
| 10250 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10251 | if (ctxt->valueNr < op->value) { |
| 10252 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10253 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10254 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10255 | return (total); |
| 10256 | } |
| 10257 | for (i = 0; i < op->value; i++) |
| 10258 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 10259 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10260 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10261 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10262 | return (total); |
| 10263 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10264 | if (op->cache != NULL) |
| 10265 | func = (xmlXPathFunction) op->cache; |
| 10266 | else { |
| 10267 | const xmlChar *URI = NULL; |
| 10268 | |
| 10269 | if (op->value5 == NULL) |
| 10270 | func = |
| 10271 | xmlXPathFunctionLookup(ctxt->context, |
| 10272 | op->value4); |
| 10273 | else { |
| 10274 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10275 | if (URI == NULL) { |
| 10276 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10277 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10278 | op->value4, op->value5); |
| 10279 | return (total); |
| 10280 | } |
| 10281 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 10282 | op->value4, URI); |
| 10283 | } |
| 10284 | if (func == NULL) { |
| 10285 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10286 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10287 | op->value4); |
| 10288 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10289 | } |
| 10290 | op->cache = (void *) func; |
| 10291 | op->cacheURI = (void *) URI; |
| 10292 | } |
| 10293 | oldFunc = ctxt->context->function; |
| 10294 | oldFuncURI = ctxt->context->functionURI; |
| 10295 | ctxt->context->function = op->value4; |
| 10296 | ctxt->context->functionURI = op->cacheURI; |
| 10297 | func(ctxt, op->value); |
| 10298 | ctxt->context->function = oldFunc; |
| 10299 | ctxt->context->functionURI = oldFuncURI; |
| 10300 | return (total); |
| 10301 | } |
| 10302 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10303 | bakd = ctxt->context->doc; |
| 10304 | bak = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10305 | if (op->ch1 != -1) |
| 10306 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10307 | ctxt->context->doc = bakd; |
| 10308 | ctxt->context->node = bak; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10309 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10310 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10311 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10312 | ctxt->context->doc = bakd; |
| 10313 | ctxt->context->node = bak; |
| 10314 | CHECK_ERROR0; |
| 10315 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10316 | return (total); |
| 10317 | case XPATH_OP_PREDICATE: |
| 10318 | case XPATH_OP_FILTER:{ |
| 10319 | xmlXPathObjectPtr res; |
| 10320 | xmlXPathObjectPtr obj, tmp; |
| 10321 | xmlNodeSetPtr newset = NULL; |
| 10322 | xmlNodeSetPtr oldset; |
| 10323 | xmlNodePtr oldnode; |
| 10324 | int i; |
| 10325 | |
| 10326 | /* |
| 10327 | * Optimization for ()[1] selection i.e. the first elem |
| 10328 | */ |
| 10329 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10330 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10331 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 10332 | xmlXPathObjectPtr val; |
| 10333 | |
| 10334 | val = comp->steps[op->ch2].value4; |
| 10335 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 10336 | (val->floatval == 1.0)) { |
| 10337 | xmlNodePtr first = NULL; |
| 10338 | |
| 10339 | total += |
| 10340 | xmlXPathCompOpEvalFirst(ctxt, |
| 10341 | &comp->steps[op->ch1], |
| 10342 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10343 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10344 | /* |
| 10345 | * The nodeset should be in document order, |
| 10346 | * Keep only the first value |
| 10347 | */ |
| 10348 | if ((ctxt->value != NULL) && |
| 10349 | (ctxt->value->type == XPATH_NODESET) && |
| 10350 | (ctxt->value->nodesetval != NULL) && |
| 10351 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 10352 | ctxt->value->nodesetval->nodeNr = 1; |
| 10353 | return (total); |
| 10354 | } |
| 10355 | } |
| 10356 | /* |
| 10357 | * Optimization for ()[last()] selection i.e. the last elem |
| 10358 | */ |
| 10359 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10360 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10361 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10362 | int f = comp->steps[op->ch2].ch1; |
| 10363 | |
| 10364 | if ((f != -1) && |
| 10365 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10366 | (comp->steps[f].value5 == NULL) && |
| 10367 | (comp->steps[f].value == 0) && |
| 10368 | (comp->steps[f].value4 != NULL) && |
| 10369 | (xmlStrEqual |
| 10370 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10371 | xmlNodePtr last = NULL; |
| 10372 | |
| 10373 | total += |
| 10374 | xmlXPathCompOpEvalLast(ctxt, |
| 10375 | &comp->steps[op->ch1], |
| 10376 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10377 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10378 | /* |
| 10379 | * The nodeset should be in document order, |
| 10380 | * Keep only the last value |
| 10381 | */ |
| 10382 | if ((ctxt->value != NULL) && |
| 10383 | (ctxt->value->type == XPATH_NODESET) && |
| 10384 | (ctxt->value->nodesetval != NULL) && |
| 10385 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10386 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10387 | ctxt->value->nodesetval->nodeTab[0] = |
| 10388 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10389 | value-> |
| 10390 | nodesetval-> |
| 10391 | nodeNr - |
| 10392 | 1]; |
| 10393 | ctxt->value->nodesetval->nodeNr = 1; |
| 10394 | } |
| 10395 | return (total); |
| 10396 | } |
| 10397 | } |
| 10398 | |
| 10399 | if (op->ch1 != -1) |
| 10400 | total += |
| 10401 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10402 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10403 | if (op->ch2 == -1) |
| 10404 | return (total); |
| 10405 | if (ctxt->value == NULL) |
| 10406 | return (total); |
| 10407 | |
| 10408 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10409 | |
| 10410 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10411 | /* |
| 10412 | * Hum are we filtering the result of an XPointer expression |
| 10413 | */ |
| 10414 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10415 | xmlLocationSetPtr newlocset = NULL; |
| 10416 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10417 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10418 | /* |
| 10419 | * Extract the old locset, and then evaluate the result of the |
| 10420 | * expression for all the element in the locset. use it to grow |
| 10421 | * up a new locset. |
| 10422 | */ |
| 10423 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10424 | obj = valuePop(ctxt); |
| 10425 | oldlocset = obj->user; |
| 10426 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10427 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10428 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10429 | ctxt->context->contextSize = 0; |
| 10430 | ctxt->context->proximityPosition = 0; |
| 10431 | if (op->ch2 != -1) |
| 10432 | total += |
| 10433 | xmlXPathCompOpEval(ctxt, |
| 10434 | &comp->steps[op->ch2]); |
| 10435 | res = valuePop(ctxt); |
| 10436 | if (res != NULL) |
| 10437 | xmlXPathFreeObject(res); |
| 10438 | valuePush(ctxt, obj); |
| 10439 | CHECK_ERROR0; |
| 10440 | return (total); |
| 10441 | } |
| 10442 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10443 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10444 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10445 | /* |
| 10446 | * Run the evaluation with a node list made of a |
| 10447 | * single item in the nodelocset. |
| 10448 | */ |
| 10449 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10450 | ctxt->context->contextSize = oldlocset->locNr; |
| 10451 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10452 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10453 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10454 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10455 | if (op->ch2 != -1) |
| 10456 | total += |
| 10457 | xmlXPathCompOpEval(ctxt, |
| 10458 | &comp->steps[op->ch2]); |
| 10459 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10460 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10461 | /* |
| 10462 | * The result of the evaluation need to be tested to |
| 10463 | * decided whether the filter succeeded or not |
| 10464 | */ |
| 10465 | res = valuePop(ctxt); |
| 10466 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10467 | xmlXPtrLocationSetAdd(newlocset, |
| 10468 | xmlXPathObjectCopy |
| 10469 | (oldlocset->locTab[i])); |
| 10470 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10471 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10472 | /* |
| 10473 | * Cleanup |
| 10474 | */ |
| 10475 | if (res != NULL) |
| 10476 | xmlXPathFreeObject(res); |
| 10477 | if (ctxt->value == tmp) { |
| 10478 | res = valuePop(ctxt); |
| 10479 | xmlXPathFreeObject(res); |
| 10480 | } |
| 10481 | |
| 10482 | ctxt->context->node = NULL; |
| 10483 | } |
| 10484 | |
| 10485 | /* |
| 10486 | * The result is used as the new evaluation locset. |
| 10487 | */ |
| 10488 | xmlXPathFreeObject(obj); |
| 10489 | ctxt->context->node = NULL; |
| 10490 | ctxt->context->contextSize = -1; |
| 10491 | ctxt->context->proximityPosition = -1; |
| 10492 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10493 | ctxt->context->node = oldnode; |
| 10494 | return (total); |
| 10495 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10496 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10497 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10498 | /* |
| 10499 | * Extract the old set, and then evaluate the result of the |
| 10500 | * expression for all the element in the set. use it to grow |
| 10501 | * up a new set. |
| 10502 | */ |
| 10503 | CHECK_TYPE0(XPATH_NODESET); |
| 10504 | obj = valuePop(ctxt); |
| 10505 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10506 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10507 | oldnode = ctxt->context->node; |
| 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 ((oldset == NULL) || (oldset->nodeNr == 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]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10517 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10518 | res = valuePop(ctxt); |
| 10519 | if (res != NULL) |
| 10520 | xmlXPathFreeObject(res); |
| 10521 | valuePush(ctxt, obj); |
| 10522 | ctxt->context->node = oldnode; |
| 10523 | CHECK_ERROR0; |
| 10524 | } else { |
| 10525 | /* |
| 10526 | * Initialize the new set. |
| 10527 | */ |
| 10528 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10529 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10530 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10531 | /* |
| 10532 | * Run the evaluation with a node list made of |
| 10533 | * a single item in the nodeset. |
| 10534 | */ |
| 10535 | ctxt->context->node = oldset->nodeTab[i]; |
| 10536 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10537 | valuePush(ctxt, tmp); |
| 10538 | ctxt->context->contextSize = oldset->nodeNr; |
| 10539 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10540 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10541 | if (op->ch2 != -1) |
| 10542 | total += |
| 10543 | xmlXPathCompOpEval(ctxt, |
| 10544 | &comp->steps[op->ch2]); |
| 10545 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10546 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10547 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10548 | * The result of the evaluation needs to be tested to |
| 10549 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10550 | */ |
| 10551 | res = valuePop(ctxt); |
| 10552 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10553 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10554 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10555 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10556 | /* |
| 10557 | * Cleanup |
| 10558 | */ |
| 10559 | if (res != NULL) |
| 10560 | xmlXPathFreeObject(res); |
| 10561 | if (ctxt->value == tmp) { |
| 10562 | res = valuePop(ctxt); |
| 10563 | xmlXPathFreeObject(res); |
| 10564 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10565 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10566 | ctxt->context->node = NULL; |
| 10567 | } |
| 10568 | |
| 10569 | /* |
| 10570 | * The result is used as the new evaluation set. |
| 10571 | */ |
| 10572 | xmlXPathFreeObject(obj); |
| 10573 | ctxt->context->node = NULL; |
| 10574 | ctxt->context->contextSize = -1; |
| 10575 | ctxt->context->proximityPosition = -1; |
| 10576 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10577 | } |
| 10578 | ctxt->context->node = oldnode; |
| 10579 | return (total); |
| 10580 | } |
| 10581 | case XPATH_OP_SORT: |
| 10582 | if (op->ch1 != -1) |
| 10583 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10584 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10585 | if ((ctxt->value != NULL) && |
| 10586 | (ctxt->value->type == XPATH_NODESET) && |
| 10587 | (ctxt->value->nodesetval != NULL)) |
| 10588 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10589 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10590 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10591 | case XPATH_OP_RANGETO:{ |
| 10592 | xmlXPathObjectPtr range; |
| 10593 | xmlXPathObjectPtr res, obj; |
| 10594 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10595 | xmlLocationSetPtr newlocset = NULL; |
| 10596 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10597 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10598 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10599 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10600 | if (op->ch1 != -1) |
| 10601 | total += |
| 10602 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10603 | if (op->ch2 == -1) |
| 10604 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10605 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10606 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10607 | /* |
| 10608 | * Extract the old locset, and then evaluate the result of the |
| 10609 | * expression for all the element in the locset. use it to grow |
| 10610 | * up a new locset. |
| 10611 | */ |
| 10612 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10613 | obj = valuePop(ctxt); |
| 10614 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10615 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10616 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10617 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10618 | ctxt->context->contextSize = 0; |
| 10619 | ctxt->context->proximityPosition = 0; |
| 10620 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 10621 | res = valuePop(ctxt); |
| 10622 | if (res != NULL) |
| 10623 | xmlXPathFreeObject(res); |
| 10624 | valuePush(ctxt, obj); |
| 10625 | CHECK_ERROR0; |
| 10626 | return (total); |
| 10627 | } |
| 10628 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10629 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10630 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10631 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10632 | * Run the evaluation with a node list made of a |
| 10633 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10634 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10635 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10636 | ctxt->context->contextSize = oldlocset->locNr; |
| 10637 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10638 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10639 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10640 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10641 | if (op->ch2 != -1) |
| 10642 | total += |
| 10643 | xmlXPathCompOpEval(ctxt, |
| 10644 | &comp->steps[op->ch2]); |
| 10645 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10646 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10647 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10648 | if (res->type == XPATH_LOCATIONSET) { |
| 10649 | xmlLocationSetPtr rloc = |
| 10650 | (xmlLocationSetPtr)res->user; |
| 10651 | for (j=0; j<rloc->locNr; j++) { |
| 10652 | range = xmlXPtrNewRange( |
| 10653 | oldlocset->locTab[i]->user, |
| 10654 | oldlocset->locTab[i]->index, |
| 10655 | rloc->locTab[j]->user2, |
| 10656 | rloc->locTab[j]->index2); |
| 10657 | if (range != NULL) { |
| 10658 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10659 | } |
| 10660 | } |
| 10661 | } else { |
| 10662 | range = xmlXPtrNewRangeNodeObject( |
| 10663 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 10664 | if (range != NULL) { |
| 10665 | xmlXPtrLocationSetAdd(newlocset,range); |
| 10666 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10667 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10668 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10669 | /* |
| 10670 | * Cleanup |
| 10671 | */ |
| 10672 | if (res != NULL) |
| 10673 | xmlXPathFreeObject(res); |
| 10674 | if (ctxt->value == tmp) { |
| 10675 | res = valuePop(ctxt); |
| 10676 | xmlXPathFreeObject(res); |
| 10677 | } |
| 10678 | |
| 10679 | ctxt->context->node = NULL; |
| 10680 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10681 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10682 | CHECK_TYPE0(XPATH_NODESET); |
| 10683 | obj = valuePop(ctxt); |
| 10684 | oldset = obj->nodesetval; |
| 10685 | ctxt->context->node = NULL; |
| 10686 | |
| 10687 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10688 | |
| 10689 | if (oldset != NULL) { |
| 10690 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10691 | /* |
| 10692 | * Run the evaluation with a node list made of a single item |
| 10693 | * in the nodeset. |
| 10694 | */ |
| 10695 | ctxt->context->node = oldset->nodeTab[i]; |
| 10696 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10697 | valuePush(ctxt, tmp); |
| 10698 | |
| 10699 | if (op->ch2 != -1) |
| 10700 | total += |
| 10701 | xmlXPathCompOpEval(ctxt, |
| 10702 | &comp->steps[op->ch2]); |
| 10703 | CHECK_ERROR0; |
| 10704 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10705 | res = valuePop(ctxt); |
| 10706 | range = |
| 10707 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10708 | res); |
| 10709 | if (range != NULL) { |
| 10710 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10711 | } |
| 10712 | |
| 10713 | /* |
| 10714 | * Cleanup |
| 10715 | */ |
| 10716 | if (res != NULL) |
| 10717 | xmlXPathFreeObject(res); |
| 10718 | if (ctxt->value == tmp) { |
| 10719 | res = valuePop(ctxt); |
| 10720 | xmlXPathFreeObject(res); |
| 10721 | } |
| 10722 | |
| 10723 | ctxt->context->node = NULL; |
| 10724 | } |
| 10725 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10726 | } |
| 10727 | |
| 10728 | /* |
| 10729 | * The result is used as the new evaluation set. |
| 10730 | */ |
| 10731 | xmlXPathFreeObject(obj); |
| 10732 | ctxt->context->node = NULL; |
| 10733 | ctxt->context->contextSize = -1; |
| 10734 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10735 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10736 | return (total); |
| 10737 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10738 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10739 | } |
| 10740 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10741 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10742 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
| 10745 | /** |
| 10746 | * xmlXPathRunEval: |
| 10747 | * @ctxt: the XPath parser context with the compiled expression |
| 10748 | * |
| 10749 | * Evaluate the Precompiled XPath expression in the given context. |
| 10750 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10751 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10752 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 10753 | xmlXPathCompExprPtr comp; |
| 10754 | |
| 10755 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 10756 | return; |
| 10757 | |
| 10758 | if (ctxt->valueTab == NULL) { |
| 10759 | /* Allocate the value stack */ |
| 10760 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 10761 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 10762 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 10763 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10764 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10765 | } |
| 10766 | ctxt->valueNr = 0; |
| 10767 | ctxt->valueMax = 10; |
| 10768 | ctxt->value = NULL; |
| 10769 | } |
| 10770 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 10771 | if(comp->last < 0) { |
| 10772 | xmlGenericError(xmlGenericErrorContext, |
| 10773 | "xmlXPathRunEval: last is less than zero\n"); |
| 10774 | return; |
| 10775 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10776 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 10777 | } |
| 10778 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10779 | /************************************************************************ |
| 10780 | * * |
| 10781 | * Public interfaces * |
| 10782 | * * |
| 10783 | ************************************************************************/ |
| 10784 | |
| 10785 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10786 | * xmlXPathEvalPredicate: |
| 10787 | * @ctxt: the XPath context |
| 10788 | * @res: the Predicate Expression evaluation result |
| 10789 | * |
| 10790 | * Evaluate a predicate result for the current node. |
| 10791 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10792 | * the result to a boolean. If the result is a number, the result will |
| 10793 | * be converted to true if the number is equal to the position of the |
| 10794 | * context node in the context node list (as returned by the position |
| 10795 | * function) and will be converted to false otherwise; if the result |
| 10796 | * is not a number, then the result will be converted as if by a call |
| 10797 | * to the boolean function. |
| 10798 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10799 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10800 | */ |
| 10801 | int |
| 10802 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
| 10803 | if (res == NULL) return(0); |
| 10804 | switch (res->type) { |
| 10805 | case XPATH_BOOLEAN: |
| 10806 | return(res->boolval); |
| 10807 | case XPATH_NUMBER: |
| 10808 | return(res->floatval == ctxt->proximityPosition); |
| 10809 | case XPATH_NODESET: |
| 10810 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10811 | if (res->nodesetval == NULL) |
| 10812 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10813 | return(res->nodesetval->nodeNr != 0); |
| 10814 | case XPATH_STRING: |
| 10815 | return((res->stringval != NULL) && |
| 10816 | (xmlStrlen(res->stringval) != 0)); |
| 10817 | default: |
| 10818 | STRANGE |
| 10819 | } |
| 10820 | return(0); |
| 10821 | } |
| 10822 | |
| 10823 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10824 | * xmlXPathEvaluatePredicateResult: |
| 10825 | * @ctxt: the XPath Parser context |
| 10826 | * @res: the Predicate Expression evaluation result |
| 10827 | * |
| 10828 | * Evaluate a predicate result for the current node. |
| 10829 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10830 | * the result to a boolean. If the result is a number, the result will |
| 10831 | * be converted to true if the number is equal to the position of the |
| 10832 | * context node in the context node list (as returned by the position |
| 10833 | * function) and will be converted to false otherwise; if the result |
| 10834 | * is not a number, then the result will be converted as if by a call |
| 10835 | * to the boolean function. |
| 10836 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10837 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10838 | */ |
| 10839 | int |
| 10840 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 10841 | xmlXPathObjectPtr res) { |
| 10842 | if (res == NULL) return(0); |
| 10843 | switch (res->type) { |
| 10844 | case XPATH_BOOLEAN: |
| 10845 | return(res->boolval); |
| 10846 | case XPATH_NUMBER: |
| 10847 | return(res->floatval == ctxt->context->proximityPosition); |
| 10848 | case XPATH_NODESET: |
| 10849 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 10850 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10851 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10852 | return(res->nodesetval->nodeNr != 0); |
| 10853 | case XPATH_STRING: |
| 10854 | return((res->stringval != NULL) && |
| 10855 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10856 | #ifdef LIBXML_XPTR_ENABLED |
| 10857 | case XPATH_LOCATIONSET:{ |
| 10858 | xmlLocationSetPtr ptr = res->user; |
| 10859 | if (ptr == NULL) |
| 10860 | return(0); |
| 10861 | return (ptr->locNr != 0); |
| 10862 | } |
| 10863 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10864 | default: |
| 10865 | STRANGE |
| 10866 | } |
| 10867 | return(0); |
| 10868 | } |
| 10869 | |
| 10870 | /** |
| 10871 | * xmlXPathCompile: |
| 10872 | * @str: the XPath expression |
| 10873 | * |
| 10874 | * Compile an XPath expression |
| 10875 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 10876 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10877 | * the caller has to free the object. |
| 10878 | */ |
| 10879 | xmlXPathCompExprPtr |
| 10880 | xmlXPathCompile(const xmlChar *str) { |
| 10881 | xmlXPathParserContextPtr ctxt; |
| 10882 | xmlXPathCompExprPtr comp; |
| 10883 | |
| 10884 | xmlXPathInit(); |
| 10885 | |
| 10886 | ctxt = xmlXPathNewParserContext(str, NULL); |
| 10887 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | ae9733a | 2003-10-28 19:02:21 +0000 | [diff] [blame] | 10888 | |
| 10889 | if( ctxt->error != XPATH_EXPRESSION_OK ) |
| 10890 | { |
| 10891 | xmlXPathFreeParserContext(ctxt); |
| 10892 | return (0); |
| 10893 | } |
| 10894 | |
Daniel Veillard | 40af649 | 2001-04-22 08:50:55 +0000 | [diff] [blame] | 10895 | if (*ctxt->cur != 0) { |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10896 | /* |
| 10897 | * aleksey: in some cases this line prints *second* error message |
| 10898 | * (see bug #78858) and probably this should be fixed. |
| 10899 | * However, we are not sure that all error messages are printed |
| 10900 | * out in other places. It's not critical so we leave it as-is for now |
| 10901 | */ |
Daniel Veillard | 40af649 | 2001-04-22 08:50:55 +0000 | [diff] [blame] | 10902 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 10903 | comp = NULL; |
| 10904 | } else { |
| 10905 | comp = ctxt->comp; |
| 10906 | ctxt->comp = NULL; |
| 10907 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10908 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10909 | if (comp != NULL) { |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 10910 | comp->expr = xmlStrdup(str); |
| 10911 | #ifdef DEBUG_EVAL_COUNTS |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10912 | comp->string = xmlStrdup(str); |
| 10913 | comp->nb = 0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10914 | #endif |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 10915 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10916 | return(comp); |
| 10917 | } |
| 10918 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10919 | /** |
| 10920 | * xmlXPathCompiledEval: |
| 10921 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10922 | * @ctx: the XPath context |
| 10923 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10924 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10925 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10926 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10927 | * the caller has to free the object. |
| 10928 | */ |
| 10929 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10930 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10931 | xmlXPathParserContextPtr ctxt; |
| 10932 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 10933 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10934 | #ifndef LIBXML_THREAD_ENABLED |
| 10935 | static int reentance = 0; |
| 10936 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10937 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10938 | if ((comp == NULL) || (ctx == NULL)) |
| 10939 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10940 | xmlXPathInit(); |
| 10941 | |
| 10942 | CHECK_CONTEXT(ctx) |
| 10943 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10944 | #ifndef LIBXML_THREAD_ENABLED |
| 10945 | reentance++; |
| 10946 | if (reentance > 1) |
| 10947 | xmlXPathDisableOptimizer = 1; |
| 10948 | #endif |
| 10949 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10950 | #ifdef DEBUG_EVAL_COUNTS |
| 10951 | comp->nb++; |
| 10952 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 10953 | fprintf(stderr, "100 x %s\n", comp->string); |
| 10954 | comp->nb = 0; |
| 10955 | } |
| 10956 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10957 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 10958 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10959 | |
| 10960 | if (ctxt->value == NULL) { |
| 10961 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10962 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10963 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10964 | } else { |
| 10965 | res = valuePop(ctxt); |
| 10966 | } |
| 10967 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10968 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10969 | do { |
| 10970 | tmp = valuePop(ctxt); |
| 10971 | if (tmp != NULL) { |
| 10972 | if (tmp != init) |
| 10973 | stack++; |
| 10974 | xmlXPathFreeObject(tmp); |
| 10975 | } |
| 10976 | } while (tmp != NULL); |
| 10977 | if ((stack != 0) && (res != NULL)) { |
| 10978 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10979 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10980 | stack); |
| 10981 | } |
| 10982 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10983 | xmlXPathFreeObject(res); |
| 10984 | res = NULL; |
| 10985 | } |
| 10986 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10987 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10988 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10989 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 10990 | #ifndef LIBXML_THREAD_ENABLED |
| 10991 | reentance--; |
| 10992 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10993 | return(res); |
| 10994 | } |
| 10995 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10996 | /** |
| 10997 | * xmlXPathEvalExpr: |
| 10998 | * @ctxt: the XPath Parser context |
| 10999 | * |
| 11000 | * Parse and evaluate an XPath expression in the given context, |
| 11001 | * then push the result on the context stack |
| 11002 | */ |
| 11003 | void |
| 11004 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
| 11005 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 11006 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11007 | xmlXPathRunEval(ctxt); |
| 11008 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11009 | |
| 11010 | /** |
| 11011 | * xmlXPathEval: |
| 11012 | * @str: the XPath expression |
| 11013 | * @ctx: the XPath context |
| 11014 | * |
| 11015 | * Evaluate the XPath Location Path in the given context. |
| 11016 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11017 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11018 | * the caller has to free the object. |
| 11019 | */ |
| 11020 | xmlXPathObjectPtr |
| 11021 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 11022 | xmlXPathParserContextPtr ctxt; |
| 11023 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11024 | int stack = 0; |
| 11025 | |
| 11026 | xmlXPathInit(); |
| 11027 | |
| 11028 | CHECK_CONTEXT(ctx) |
| 11029 | |
| 11030 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 11031 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11032 | |
| 11033 | if (ctxt->value == NULL) { |
| 11034 | xmlGenericError(xmlGenericErrorContext, |
| 11035 | "xmlXPathEval: evaluation failed\n"); |
| 11036 | res = NULL; |
| 11037 | } else if (*ctxt->cur != 0) { |
| 11038 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11039 | res = NULL; |
| 11040 | } else { |
| 11041 | res = valuePop(ctxt); |
| 11042 | } |
| 11043 | |
| 11044 | do { |
| 11045 | tmp = valuePop(ctxt); |
| 11046 | if (tmp != NULL) { |
| 11047 | if (tmp != init) |
| 11048 | stack++; |
| 11049 | xmlXPathFreeObject(tmp); |
| 11050 | } |
| 11051 | } while (tmp != NULL); |
| 11052 | if ((stack != 0) && (res != NULL)) { |
| 11053 | xmlGenericError(xmlGenericErrorContext, |
| 11054 | "xmlXPathEval: %d object left on the stack\n", |
| 11055 | stack); |
| 11056 | } |
| 11057 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11058 | xmlXPathFreeObject(res); |
| 11059 | res = NULL; |
| 11060 | } |
| 11061 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11062 | xmlXPathFreeParserContext(ctxt); |
| 11063 | return(res); |
| 11064 | } |
| 11065 | |
| 11066 | /** |
| 11067 | * xmlXPathEvalExpression: |
| 11068 | * @str: the XPath expression |
| 11069 | * @ctxt: the XPath context |
| 11070 | * |
| 11071 | * Evaluate the XPath expression in the given context. |
| 11072 | * |
| 11073 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 11074 | * the caller has to free the object. |
| 11075 | */ |
| 11076 | xmlXPathObjectPtr |
| 11077 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 11078 | xmlXPathParserContextPtr pctxt; |
| 11079 | xmlXPathObjectPtr res, tmp; |
| 11080 | int stack = 0; |
| 11081 | |
| 11082 | xmlXPathInit(); |
| 11083 | |
| 11084 | CHECK_CONTEXT(ctxt) |
| 11085 | |
| 11086 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11087 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11088 | |
| 11089 | if (*pctxt->cur != 0) { |
| 11090 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11091 | res = NULL; |
| 11092 | } else { |
| 11093 | res = valuePop(pctxt); |
| 11094 | } |
| 11095 | do { |
| 11096 | tmp = valuePop(pctxt); |
| 11097 | if (tmp != NULL) { |
| 11098 | xmlXPathFreeObject(tmp); |
| 11099 | stack++; |
| 11100 | } |
| 11101 | } while (tmp != NULL); |
| 11102 | if ((stack != 0) && (res != NULL)) { |
| 11103 | xmlGenericError(xmlGenericErrorContext, |
| 11104 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 11105 | stack); |
| 11106 | } |
| 11107 | xmlXPathFreeParserContext(pctxt); |
| 11108 | return(res); |
| 11109 | } |
| 11110 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11111 | /************************************************************************ |
| 11112 | * * |
| 11113 | * Extra functions not pertaining to the XPath spec * |
| 11114 | * * |
| 11115 | ************************************************************************/ |
| 11116 | /** |
| 11117 | * xmlXPathEscapeUriFunction: |
| 11118 | * @ctxt: the XPath Parser context |
| 11119 | * @nargs: the number of arguments |
| 11120 | * |
| 11121 | * Implement the escape-uri() XPath function |
| 11122 | * string escape-uri(string $str, bool $escape-reserved) |
| 11123 | * |
| 11124 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 11125 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 11126 | * or part of a URI. The effect of the function is to replace any special |
| 11127 | * character in the string by an escape sequence of the form %xx%yy..., |
| 11128 | * where xxyy... is the hexadecimal representation of the octets used to |
| 11129 | * represent the character in UTF-8. |
| 11130 | * |
| 11131 | * The set of characters that are escaped depends on the setting of the |
| 11132 | * boolean argument $escape-reserved. |
| 11133 | * |
| 11134 | * If $escape-reserved is true, all characters are escaped other than lower |
| 11135 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 11136 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 11137 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 11138 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 11139 | * A-F). |
| 11140 | * |
| 11141 | * If $escape-reserved is false, the behavior differs in that characters |
| 11142 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 11143 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 11144 | * |
| 11145 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 11146 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 11147 | * compared using string comparison functions, this function must always use |
| 11148 | * the upper-case letters A-F. |
| 11149 | * |
| 11150 | * Generally, $escape-reserved should be set to true when escaping a string |
| 11151 | * that is to form a single part of a URI, and to false when escaping an |
| 11152 | * entire URI or URI reference. |
| 11153 | * |
| 11154 | * In the case of non-ascii characters, the string is encoded according to |
| 11155 | * utf-8 and then converted according to RFC 2396. |
| 11156 | * |
| 11157 | * Examples |
| 11158 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 11159 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 11160 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 11161 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 11162 | * |
| 11163 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 11164 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11165 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 11166 | xmlXPathObjectPtr str; |
| 11167 | int escape_reserved; |
| 11168 | xmlBufferPtr target; |
| 11169 | xmlChar *cptr; |
| 11170 | xmlChar escape[4]; |
| 11171 | |
| 11172 | CHECK_ARITY(2); |
| 11173 | |
| 11174 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 11175 | |
| 11176 | CAST_TO_STRING; |
| 11177 | str = valuePop(ctxt); |
| 11178 | |
| 11179 | target = xmlBufferCreate(); |
| 11180 | |
| 11181 | escape[0] = '%'; |
| 11182 | escape[3] = 0; |
| 11183 | |
| 11184 | if (target) { |
| 11185 | for (cptr = str->stringval; *cptr; cptr++) { |
| 11186 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 11187 | (*cptr >= 'a' && *cptr <= 'z') || |
| 11188 | (*cptr >= '0' && *cptr <= '9') || |
| 11189 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 11190 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 11191 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 11192 | (*cptr == '%' && |
| 11193 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 11194 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 11195 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 11196 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 11197 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 11198 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 11199 | (!escape_reserved && |
| 11200 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 11201 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 11202 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 11203 | *cptr == ','))) { |
| 11204 | xmlBufferAdd(target, cptr, 1); |
| 11205 | } else { |
| 11206 | if ((*cptr >> 4) < 10) |
| 11207 | escape[1] = '0' + (*cptr >> 4); |
| 11208 | else |
| 11209 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 11210 | if ((*cptr & 0xF) < 10) |
| 11211 | escape[2] = '0' + (*cptr & 0xF); |
| 11212 | else |
| 11213 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 11214 | |
| 11215 | xmlBufferAdd(target, &escape[0], 3); |
| 11216 | } |
| 11217 | } |
| 11218 | } |
| 11219 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 11220 | xmlBufferFree(target); |
| 11221 | xmlXPathFreeObject(str); |
| 11222 | } |
| 11223 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11224 | /** |
| 11225 | * xmlXPathRegisterAllFunctions: |
| 11226 | * @ctxt: the XPath context |
| 11227 | * |
| 11228 | * Registers all default XPath functions in this context |
| 11229 | */ |
| 11230 | void |
| 11231 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 11232 | { |
| 11233 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 11234 | xmlXPathBooleanFunction); |
| 11235 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 11236 | xmlXPathCeilingFunction); |
| 11237 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 11238 | xmlXPathCountFunction); |
| 11239 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 11240 | xmlXPathConcatFunction); |
| 11241 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 11242 | xmlXPathContainsFunction); |
| 11243 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 11244 | xmlXPathIdFunction); |
| 11245 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 11246 | xmlXPathFalseFunction); |
| 11247 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 11248 | xmlXPathFloorFunction); |
| 11249 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 11250 | xmlXPathLastFunction); |
| 11251 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 11252 | xmlXPathLangFunction); |
| 11253 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 11254 | xmlXPathLocalNameFunction); |
| 11255 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 11256 | xmlXPathNotFunction); |
| 11257 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 11258 | xmlXPathNameFunction); |
| 11259 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 11260 | xmlXPathNamespaceURIFunction); |
| 11261 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 11262 | xmlXPathNormalizeFunction); |
| 11263 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 11264 | xmlXPathNumberFunction); |
| 11265 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 11266 | xmlXPathPositionFunction); |
| 11267 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 11268 | xmlXPathRoundFunction); |
| 11269 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 11270 | xmlXPathStringFunction); |
| 11271 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 11272 | xmlXPathStringLengthFunction); |
| 11273 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 11274 | xmlXPathStartsWithFunction); |
| 11275 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 11276 | xmlXPathSubstringFunction); |
| 11277 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 11278 | xmlXPathSubstringBeforeFunction); |
| 11279 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 11280 | xmlXPathSubstringAfterFunction); |
| 11281 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 11282 | xmlXPathSumFunction); |
| 11283 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 11284 | xmlXPathTrueFunction); |
| 11285 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 11286 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11287 | |
| 11288 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 11289 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 11290 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11291 | } |
| 11292 | |
| 11293 | #endif /* LIBXML_XPATH_ENABLED */ |