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> |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 54 | #ifdef LIBXML_PATTERN_ENABLED |
| 55 | #include <libxml/pattern.h> |
| 56 | #endif |
| 57 | |
| 58 | #ifdef LIBXML_PATTERN_ENABLED |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 59 | #define XPATH_STREAMING |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 60 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 62 | #define TODO \ |
| 63 | xmlGenericError(xmlGenericErrorContext, \ |
| 64 | "Unimplemented block at %s:%d\n", \ |
| 65 | __FILE__, __LINE__); |
| 66 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 67 | /* |
| 68 | * TODO: |
| 69 | * There are a few spots where some tests are done which depend upon ascii |
| 70 | * data. These should be enhanced for full UTF8 support (see particularly |
| 71 | * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) |
| 72 | */ |
| 73 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 74 | #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 75 | /************************************************************************ |
| 76 | * * |
| 77 | * Floating point stuff * |
| 78 | * * |
| 79 | ************************************************************************/ |
| 80 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 81 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 82 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 83 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 84 | #include "trionan.c" |
| 85 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 86 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 87 | * The lack of portability of this section of the libc is annoying ! |
| 88 | */ |
| 89 | double xmlXPathNAN = 0; |
| 90 | double xmlXPathPINF = 1; |
| 91 | double xmlXPathNINF = -1; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 92 | static double xmlXPathNZERO = 0; /* not exported from headers */ |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 93 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 94 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 95 | /** |
| 96 | * xmlXPathInit: |
| 97 | * |
| 98 | * Initialize the XPath environment |
| 99 | */ |
| 100 | void |
| 101 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 102 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 103 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 104 | xmlXPathPINF = trio_pinf(); |
| 105 | xmlXPathNINF = trio_ninf(); |
| 106 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 107 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 108 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 109 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 112 | /** |
| 113 | * xmlXPathIsNaN: |
| 114 | * @val: a double value |
| 115 | * |
| 116 | * Provides a portable isnan() function to detect whether a double |
| 117 | * is a NotaNumber. Based on trio code |
| 118 | * http://sourceforge.net/projects/ctrio/ |
| 119 | * |
| 120 | * Returns 1 if the value is a NaN, 0 otherwise |
| 121 | */ |
| 122 | int |
| 123 | xmlXPathIsNaN(double val) { |
| 124 | return(trio_isnan(val)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * xmlXPathIsInf: |
| 129 | * @val: a double value |
| 130 | * |
| 131 | * Provides a portable isinf() function to detect whether a double |
| 132 | * is a +Infinite or -Infinite. Based on trio code |
| 133 | * http://sourceforge.net/projects/ctrio/ |
| 134 | * |
| 135 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 136 | */ |
| 137 | int |
| 138 | xmlXPathIsInf(double val) { |
| 139 | return(trio_isinf(val)); |
| 140 | } |
| 141 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 142 | #endif /* SCHEMAS or XPATH */ |
| 143 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 144 | /** |
| 145 | * xmlXPathGetSign: |
| 146 | * @val: a double value |
| 147 | * |
| 148 | * Provides a portable function to detect the sign of a double |
| 149 | * Modified from trio code |
| 150 | * http://sourceforge.net/projects/ctrio/ |
| 151 | * |
| 152 | * Returns 1 if the value is Negative, 0 if positive |
| 153 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 154 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 155 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 156 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 160 | /* |
| 161 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 162 | * the test should just be name[0] = ' ' |
| 163 | */ |
| 164 | /* #define DEBUG */ |
| 165 | /* #define DEBUG_STEP */ |
| 166 | /* #define DEBUG_STEP_NTH */ |
| 167 | /* #define DEBUG_EXPR */ |
| 168 | /* #define DEBUG_EVAL_COUNTS */ |
| 169 | |
| 170 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 171 | NULL, |
| 172 | XML_NAMESPACE_DECL, |
| 173 | XML_XML_NAMESPACE, |
| 174 | BAD_CAST "xml", |
| 175 | NULL |
| 176 | }; |
| 177 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 178 | #ifndef LIBXML_THREAD_ENABLED |
| 179 | /* |
| 180 | * Optimizer is disabled only when threaded apps are detected while |
| 181 | * the library ain't compiled for thread safety. |
| 182 | */ |
| 183 | static int xmlXPathDisableOptimizer = 0; |
| 184 | #endif |
| 185 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 186 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 187 | * * |
| 188 | * Error handling routines * |
| 189 | * * |
| 190 | ************************************************************************/ |
| 191 | |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 192 | /** |
| 193 | * XP_ERRORNULL: |
| 194 | * @X: the error code |
| 195 | * |
| 196 | * Macro to raise an XPath error and return NULL. |
| 197 | */ |
| 198 | #define XP_ERRORNULL(X) \ |
| 199 | { xmlXPathErr(ctxt, X); return(NULL); } |
| 200 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 201 | /* |
| 202 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 203 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 204 | static const char *xmlXPathErrorMessages[] = { |
| 205 | "Ok\n", |
| 206 | "Number encoding\n", |
| 207 | "Unfinished literal\n", |
| 208 | "Start of literal\n", |
| 209 | "Expected $ for variable reference\n", |
| 210 | "Undefined variable\n", |
| 211 | "Invalid predicate\n", |
| 212 | "Invalid expression\n", |
| 213 | "Missing closing curly brace\n", |
| 214 | "Unregistered function\n", |
| 215 | "Invalid operand\n", |
| 216 | "Invalid type\n", |
| 217 | "Invalid number of arguments\n", |
| 218 | "Invalid context size\n", |
| 219 | "Invalid context position\n", |
| 220 | "Memory allocation error\n", |
| 221 | "Syntax error\n", |
| 222 | "Resource error\n", |
| 223 | "Sub resource error\n", |
| 224 | "Undefined namespace prefix\n", |
| 225 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 226 | "Char out of XML range\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 227 | "Invalid or incomplete context\n", |
| 228 | "?? Unknown error ??\n" /* Must be last in the list! */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 229 | }; |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 230 | #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \ |
| 231 | sizeof(xmlXPathErrorMessages[0])) - 1) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 232 | /** |
| 233 | * xmlXPathErrMemory: |
| 234 | * @ctxt: an XPath context |
| 235 | * @extra: extra informations |
| 236 | * |
| 237 | * Handle a redefinition of attribute error |
| 238 | */ |
| 239 | static void |
| 240 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 241 | { |
| 242 | if (ctxt != NULL) { |
| 243 | if (extra) { |
| 244 | xmlChar buf[200]; |
| 245 | |
| 246 | xmlStrPrintf(buf, 200, |
| 247 | BAD_CAST "Memory allocation failed : %s\n", |
| 248 | extra); |
| 249 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 250 | } else { |
| 251 | ctxt->lastError.message = (char *) |
| 252 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 253 | } |
| 254 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 255 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 256 | if (ctxt->error != NULL) |
| 257 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 258 | } else { |
| 259 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 260 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 261 | NULL, NULL, XML_FROM_XPATH, |
| 262 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 263 | extra, NULL, NULL, 0, 0, |
| 264 | "Memory allocation failed : %s\n", extra); |
| 265 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 266 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 267 | NULL, NULL, XML_FROM_XPATH, |
| 268 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 269 | NULL, NULL, NULL, 0, 0, |
| 270 | "Memory allocation failed\n"); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 275 | * xmlXPathPErrMemory: |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 276 | * @ctxt: an XPath parser context |
| 277 | * @extra: extra informations |
| 278 | * |
| 279 | * Handle a redefinition of attribute error |
| 280 | */ |
| 281 | static void |
| 282 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 283 | { |
| 284 | ctxt->error = XPATH_MEMORY_ERROR; |
| 285 | if (ctxt == NULL) |
| 286 | xmlXPathErrMemory(NULL, extra); |
| 287 | else |
| 288 | xmlXPathErrMemory(ctxt->context, extra); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * xmlXPathErr: |
| 293 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 294 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 295 | * |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 296 | * Handle an XPath error |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 297 | */ |
| 298 | void |
| 299 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 300 | { |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 301 | if ((error < 0) || (error > MAXERRNO)) |
| 302 | error = MAXERRNO; |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 303 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 304 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 305 | NULL, NULL, XML_FROM_XPATH, |
| 306 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 307 | XML_ERR_ERROR, NULL, 0, |
| 308 | NULL, NULL, NULL, 0, 0, |
| 309 | xmlXPathErrorMessages[error]); |
| 310 | return; |
| 311 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 312 | ctxt->error = error; |
| 313 | if (ctxt->context == NULL) { |
| 314 | __xmlRaiseError(NULL, NULL, NULL, |
| 315 | NULL, NULL, XML_FROM_XPATH, |
| 316 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 317 | XML_ERR_ERROR, NULL, 0, |
| 318 | (const char *) ctxt->base, NULL, NULL, |
| 319 | ctxt->cur - ctxt->base, 0, |
| 320 | xmlXPathErrorMessages[error]); |
| 321 | return; |
| 322 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 323 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 324 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 325 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 326 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 327 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 328 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 329 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 330 | if (ctxt->context->error != NULL) { |
| 331 | ctxt->context->error(ctxt->context->userData, |
| 332 | &ctxt->context->lastError); |
| 333 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 334 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 335 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 336 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 337 | XML_ERR_ERROR, NULL, 0, |
| 338 | (const char *) ctxt->base, NULL, NULL, |
| 339 | ctxt->cur - ctxt->base, 0, |
| 340 | xmlXPathErrorMessages[error]); |
| 341 | } |
| 342 | |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * xmlXPatherror: |
| 347 | * @ctxt: the XPath Parser context |
| 348 | * @file: the file name |
| 349 | * @line: the line number |
| 350 | * @no: the error number |
| 351 | * |
| 352 | * Formats an error message. |
| 353 | */ |
| 354 | void |
| 355 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 356 | int line ATTRIBUTE_UNUSED, int no) { |
| 357 | xmlXPathErr(ctxt, no); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 362 | * * |
| 363 | * Parser Types * |
| 364 | * * |
| 365 | ************************************************************************/ |
| 366 | |
| 367 | /* |
| 368 | * Types are private: |
| 369 | */ |
| 370 | |
| 371 | typedef enum { |
| 372 | XPATH_OP_END=0, |
| 373 | XPATH_OP_AND, |
| 374 | XPATH_OP_OR, |
| 375 | XPATH_OP_EQUAL, |
| 376 | XPATH_OP_CMP, |
| 377 | XPATH_OP_PLUS, |
| 378 | XPATH_OP_MULT, |
| 379 | XPATH_OP_UNION, |
| 380 | XPATH_OP_ROOT, |
| 381 | XPATH_OP_NODE, |
| 382 | XPATH_OP_RESET, |
| 383 | XPATH_OP_COLLECT, |
| 384 | XPATH_OP_VALUE, |
| 385 | XPATH_OP_VARIABLE, |
| 386 | XPATH_OP_FUNCTION, |
| 387 | XPATH_OP_ARG, |
| 388 | XPATH_OP_PREDICATE, |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 389 | XPATH_OP_FILTER, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 390 | XPATH_OP_SORT |
| 391 | #ifdef LIBXML_XPTR_ENABLED |
| 392 | ,XPATH_OP_RANGETO |
| 393 | #endif |
| 394 | } xmlXPathOp; |
| 395 | |
| 396 | typedef enum { |
| 397 | AXIS_ANCESTOR = 1, |
| 398 | AXIS_ANCESTOR_OR_SELF, |
| 399 | AXIS_ATTRIBUTE, |
| 400 | AXIS_CHILD, |
| 401 | AXIS_DESCENDANT, |
| 402 | AXIS_DESCENDANT_OR_SELF, |
| 403 | AXIS_FOLLOWING, |
| 404 | AXIS_FOLLOWING_SIBLING, |
| 405 | AXIS_NAMESPACE, |
| 406 | AXIS_PARENT, |
| 407 | AXIS_PRECEDING, |
| 408 | AXIS_PRECEDING_SIBLING, |
| 409 | AXIS_SELF |
| 410 | } xmlXPathAxisVal; |
| 411 | |
| 412 | typedef enum { |
| 413 | NODE_TEST_NONE = 0, |
| 414 | NODE_TEST_TYPE = 1, |
| 415 | NODE_TEST_PI = 2, |
| 416 | NODE_TEST_ALL = 3, |
| 417 | NODE_TEST_NS = 4, |
| 418 | NODE_TEST_NAME = 5 |
| 419 | } xmlXPathTestVal; |
| 420 | |
| 421 | typedef enum { |
| 422 | NODE_TYPE_NODE = 0, |
| 423 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 424 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 425 | NODE_TYPE_PI = XML_PI_NODE |
| 426 | } xmlXPathTypeVal; |
| 427 | |
| 428 | |
| 429 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 430 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 431 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 432 | xmlXPathOp op; /* The identifier of the operation */ |
| 433 | int ch1; /* First child */ |
| 434 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 435 | int value; |
| 436 | int value2; |
| 437 | int value3; |
| 438 | void *value4; |
| 439 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 440 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 441 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 445 | int nbStep; /* Number of steps in this expression */ |
| 446 | int maxStep; /* Maximum number of steps allocated */ |
| 447 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 448 | int last; /* index of last step in expression */ |
| 449 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 450 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 451 | #ifdef DEBUG_EVAL_COUNTS |
| 452 | int nb; |
| 453 | xmlChar *string; |
| 454 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 455 | #ifdef XPATH_STREAMING |
| 456 | xmlPatternPtr stream; |
| 457 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | /************************************************************************ |
| 461 | * * |
| 462 | * Parser Type functions * |
| 463 | * * |
| 464 | ************************************************************************/ |
| 465 | |
| 466 | /** |
| 467 | * xmlXPathNewCompExpr: |
| 468 | * |
| 469 | * Create a new Xpath component |
| 470 | * |
| 471 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 472 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 473 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 474 | xmlXPathNewCompExpr(void) { |
| 475 | xmlXPathCompExprPtr cur; |
| 476 | |
| 477 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 478 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 479 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 480 | return(NULL); |
| 481 | } |
| 482 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 483 | cur->maxStep = 10; |
| 484 | cur->nbStep = 0; |
| 485 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 486 | sizeof(xmlXPathStepOp)); |
| 487 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 488 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 489 | xmlFree(cur); |
| 490 | return(NULL); |
| 491 | } |
| 492 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 493 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 494 | #ifdef DEBUG_EVAL_COUNTS |
| 495 | cur->nb = 0; |
| 496 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 497 | return(cur); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * xmlXPathFreeCompExpr: |
| 502 | * @comp: an XPATH comp |
| 503 | * |
| 504 | * Free up the memory allocated by @comp |
| 505 | */ |
| 506 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 507 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 508 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 509 | xmlXPathStepOpPtr op; |
| 510 | int i; |
| 511 | |
| 512 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 513 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 514 | if (comp->dict == NULL) { |
| 515 | for (i = 0; i < comp->nbStep; i++) { |
| 516 | op = &comp->steps[i]; |
| 517 | if (op->value4 != NULL) { |
| 518 | if (op->op == XPATH_OP_VALUE) |
| 519 | xmlXPathFreeObject(op->value4); |
| 520 | else |
| 521 | xmlFree(op->value4); |
| 522 | } |
| 523 | if (op->value5 != NULL) |
| 524 | xmlFree(op->value5); |
| 525 | } |
| 526 | } else { |
| 527 | for (i = 0; i < comp->nbStep; i++) { |
| 528 | op = &comp->steps[i]; |
| 529 | if (op->value4 != NULL) { |
| 530 | if (op->op == XPATH_OP_VALUE) |
| 531 | xmlXPathFreeObject(op->value4); |
| 532 | } |
| 533 | } |
| 534 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 535 | } |
| 536 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 537 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 538 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 539 | #ifdef DEBUG_EVAL_COUNTS |
| 540 | if (comp->string != NULL) { |
| 541 | xmlFree(comp->string); |
| 542 | } |
| 543 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 544 | #ifdef XPATH_STREAMING |
| 545 | if (comp->stream != NULL) { |
| 546 | xmlFreePatternList(comp->stream); |
| 547 | } |
| 548 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 549 | if (comp->expr != NULL) { |
| 550 | xmlFree(comp->expr); |
| 551 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 552 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 553 | xmlFree(comp); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * xmlXPathCompExprAdd: |
| 558 | * @comp: the compiled expression |
| 559 | * @ch1: first child index |
| 560 | * @ch2: second child index |
| 561 | * @op: an op |
| 562 | * @value: the first int value |
| 563 | * @value2: the second int value |
| 564 | * @value3: the third int value |
| 565 | * @value4: the first string value |
| 566 | * @value5: the second string value |
| 567 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 568 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 569 | * |
| 570 | * Returns -1 in case of failure, the index otherwise |
| 571 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 572 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 573 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 574 | xmlXPathOp op, int value, |
| 575 | int value2, int value3, void *value4, void *value5) { |
| 576 | if (comp->nbStep >= comp->maxStep) { |
| 577 | xmlXPathStepOp *real; |
| 578 | |
| 579 | comp->maxStep *= 2; |
| 580 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 581 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 582 | if (real == NULL) { |
| 583 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 584 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 585 | return(-1); |
| 586 | } |
| 587 | comp->steps = real; |
| 588 | } |
| 589 | comp->last = comp->nbStep; |
| 590 | comp->steps[comp->nbStep].ch1 = ch1; |
| 591 | comp->steps[comp->nbStep].ch2 = ch2; |
| 592 | comp->steps[comp->nbStep].op = op; |
| 593 | comp->steps[comp->nbStep].value = value; |
| 594 | comp->steps[comp->nbStep].value2 = value2; |
| 595 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 596 | if ((comp->dict != NULL) && |
| 597 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 598 | (op == XPATH_OP_COLLECT))) { |
| 599 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 600 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 601 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 602 | xmlFree(value4); |
| 603 | } else |
| 604 | comp->steps[comp->nbStep].value4 = NULL; |
| 605 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 606 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 607 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 608 | xmlFree(value5); |
| 609 | } else |
| 610 | comp->steps[comp->nbStep].value5 = NULL; |
| 611 | } else { |
| 612 | comp->steps[comp->nbStep].value4 = value4; |
| 613 | comp->steps[comp->nbStep].value5 = value5; |
| 614 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 615 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 616 | return(comp->nbStep++); |
| 617 | } |
| 618 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 619 | /** |
| 620 | * xmlXPathCompSwap: |
| 621 | * @comp: the compiled expression |
| 622 | * @op: operation index |
| 623 | * |
| 624 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 625 | */ |
| 626 | static void |
| 627 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 628 | int tmp; |
| 629 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 630 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 631 | /* |
| 632 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 633 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 634 | * application |
| 635 | */ |
| 636 | if (xmlXPathDisableOptimizer) |
| 637 | return; |
| 638 | #endif |
| 639 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 640 | tmp = op->ch1; |
| 641 | op->ch1 = op->ch2; |
| 642 | op->ch2 = tmp; |
| 643 | } |
| 644 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 645 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 646 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 647 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 648 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 649 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 650 | (op), (val), (val2), (val3), (val4), (val5)) |
| 651 | |
| 652 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 653 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 654 | |
| 655 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 656 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 657 | |
| 658 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 659 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 660 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 661 | |
| 662 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 663 | * * |
| 664 | * Debugging related functions * |
| 665 | * * |
| 666 | ************************************************************************/ |
| 667 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 668 | #define STRANGE \ |
| 669 | xmlGenericError(xmlGenericErrorContext, \ |
| 670 | "Internal error at %s:%d\n", \ |
| 671 | __FILE__, __LINE__); |
| 672 | |
| 673 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 674 | static void |
| 675 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | int i; |
| 677 | char shift[100]; |
| 678 | |
| 679 | for (i = 0;((i < depth) && (i < 25));i++) |
| 680 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 681 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 682 | if (cur == NULL) { |
| 683 | fprintf(output, shift); |
| 684 | fprintf(output, "Node is NULL !\n"); |
| 685 | return; |
| 686 | |
| 687 | } |
| 688 | |
| 689 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 690 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 691 | fprintf(output, shift); |
| 692 | fprintf(output, " /\n"); |
| 693 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 694 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 695 | else |
| 696 | xmlDebugDumpOneNode(output, cur, depth); |
| 697 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 698 | static void |
| 699 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 700 | xmlNodePtr tmp; |
| 701 | int i; |
| 702 | char shift[100]; |
| 703 | |
| 704 | for (i = 0;((i < depth) && (i < 25));i++) |
| 705 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 706 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 707 | if (cur == NULL) { |
| 708 | fprintf(output, shift); |
| 709 | fprintf(output, "Node is NULL !\n"); |
| 710 | return; |
| 711 | |
| 712 | } |
| 713 | |
| 714 | while (cur != NULL) { |
| 715 | tmp = cur; |
| 716 | cur = cur->next; |
| 717 | xmlDebugDumpOneNode(output, tmp, depth); |
| 718 | } |
| 719 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 720 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 721 | static void |
| 722 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 723 | int i; |
| 724 | char shift[100]; |
| 725 | |
| 726 | for (i = 0;((i < depth) && (i < 25));i++) |
| 727 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 728 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 729 | |
| 730 | if (cur == NULL) { |
| 731 | fprintf(output, shift); |
| 732 | fprintf(output, "NodeSet is NULL !\n"); |
| 733 | return; |
| 734 | |
| 735 | } |
| 736 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 737 | if (cur != NULL) { |
| 738 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 739 | for (i = 0;i < cur->nodeNr;i++) { |
| 740 | fprintf(output, shift); |
| 741 | fprintf(output, "%d", i + 1); |
| 742 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 743 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 747 | static void |
| 748 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 749 | int i; |
| 750 | char shift[100]; |
| 751 | |
| 752 | for (i = 0;((i < depth) && (i < 25));i++) |
| 753 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 754 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 755 | |
| 756 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 757 | fprintf(output, shift); |
| 758 | fprintf(output, "Value Tree is NULL !\n"); |
| 759 | return; |
| 760 | |
| 761 | } |
| 762 | |
| 763 | fprintf(output, shift); |
| 764 | fprintf(output, "%d", i + 1); |
| 765 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 766 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 767 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 768 | static void |
| 769 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 770 | int i; |
| 771 | char shift[100]; |
| 772 | |
| 773 | for (i = 0;((i < depth) && (i < 25));i++) |
| 774 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 775 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 776 | |
| 777 | if (cur == NULL) { |
| 778 | fprintf(output, shift); |
| 779 | fprintf(output, "LocationSet is NULL !\n"); |
| 780 | return; |
| 781 | |
| 782 | } |
| 783 | |
| 784 | for (i = 0;i < cur->locNr;i++) { |
| 785 | fprintf(output, shift); |
| 786 | fprintf(output, "%d : ", i + 1); |
| 787 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 788 | } |
| 789 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 790 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 791 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 792 | /** |
| 793 | * xmlXPathDebugDumpObject: |
| 794 | * @output: the FILE * to dump the output |
| 795 | * @cur: the object to inspect |
| 796 | * @depth: indentation level |
| 797 | * |
| 798 | * Dump the content of the object for debugging purposes |
| 799 | */ |
| 800 | void |
| 801 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 802 | int i; |
| 803 | char shift[100]; |
| 804 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 805 | if (output == NULL) return; |
| 806 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 807 | for (i = 0;((i < depth) && (i < 25));i++) |
| 808 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 809 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 810 | |
| 811 | fprintf(output, shift); |
| 812 | |
| 813 | if (cur == NULL) { |
| 814 | fprintf(output, "Object is empty (NULL)\n"); |
| 815 | return; |
| 816 | } |
| 817 | switch(cur->type) { |
| 818 | case XPATH_UNDEFINED: |
| 819 | fprintf(output, "Object is uninitialized\n"); |
| 820 | break; |
| 821 | case XPATH_NODESET: |
| 822 | fprintf(output, "Object is a Node Set :\n"); |
| 823 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 824 | break; |
| 825 | case XPATH_XSLT_TREE: |
| 826 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 827 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 828 | break; |
| 829 | case XPATH_BOOLEAN: |
| 830 | fprintf(output, "Object is a Boolean : "); |
| 831 | if (cur->boolval) fprintf(output, "true\n"); |
| 832 | else fprintf(output, "false\n"); |
| 833 | break; |
| 834 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 835 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 836 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 837 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 838 | break; |
| 839 | case -1: |
| 840 | fprintf(output, "Object is a number : -Infinity\n"); |
| 841 | break; |
| 842 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 843 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 844 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 845 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 846 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 847 | } else { |
| 848 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 849 | } |
| 850 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 851 | break; |
| 852 | case XPATH_STRING: |
| 853 | fprintf(output, "Object is a string : "); |
| 854 | xmlDebugDumpString(output, cur->stringval); |
| 855 | fprintf(output, "\n"); |
| 856 | break; |
| 857 | case XPATH_POINT: |
| 858 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 859 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 860 | fprintf(output, "\n"); |
| 861 | break; |
| 862 | case XPATH_RANGE: |
| 863 | if ((cur->user2 == NULL) || |
| 864 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 865 | fprintf(output, "Object is a collapsed range :\n"); |
| 866 | fprintf(output, shift); |
| 867 | if (cur->index >= 0) |
| 868 | fprintf(output, "index %d in ", cur->index); |
| 869 | fprintf(output, "node\n"); |
| 870 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 871 | depth + 1); |
| 872 | } else { |
| 873 | fprintf(output, "Object is a range :\n"); |
| 874 | fprintf(output, shift); |
| 875 | fprintf(output, "From "); |
| 876 | if (cur->index >= 0) |
| 877 | fprintf(output, "index %d in ", cur->index); |
| 878 | fprintf(output, "node\n"); |
| 879 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 880 | depth + 1); |
| 881 | fprintf(output, shift); |
| 882 | fprintf(output, "To "); |
| 883 | if (cur->index2 >= 0) |
| 884 | fprintf(output, "index %d in ", cur->index2); |
| 885 | fprintf(output, "node\n"); |
| 886 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 887 | depth + 1); |
| 888 | fprintf(output, "\n"); |
| 889 | } |
| 890 | break; |
| 891 | case XPATH_LOCATIONSET: |
| 892 | #if defined(LIBXML_XPTR_ENABLED) |
| 893 | fprintf(output, "Object is a Location Set:\n"); |
| 894 | xmlXPathDebugDumpLocationSet(output, |
| 895 | (xmlLocationSetPtr) cur->user, depth); |
| 896 | #endif |
| 897 | break; |
| 898 | case XPATH_USERS: |
| 899 | fprintf(output, "Object is user defined\n"); |
| 900 | break; |
| 901 | } |
| 902 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 903 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 904 | static void |
| 905 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 906 | xmlXPathStepOpPtr op, int depth) { |
| 907 | int i; |
| 908 | char shift[100]; |
| 909 | |
| 910 | for (i = 0;((i < depth) && (i < 25));i++) |
| 911 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 912 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 913 | |
| 914 | fprintf(output, shift); |
| 915 | if (op == NULL) { |
| 916 | fprintf(output, "Step is NULL\n"); |
| 917 | return; |
| 918 | } |
| 919 | switch (op->op) { |
| 920 | case XPATH_OP_END: |
| 921 | fprintf(output, "END"); break; |
| 922 | case XPATH_OP_AND: |
| 923 | fprintf(output, "AND"); break; |
| 924 | case XPATH_OP_OR: |
| 925 | fprintf(output, "OR"); break; |
| 926 | case XPATH_OP_EQUAL: |
| 927 | if (op->value) |
| 928 | fprintf(output, "EQUAL ="); |
| 929 | else |
| 930 | fprintf(output, "EQUAL !="); |
| 931 | break; |
| 932 | case XPATH_OP_CMP: |
| 933 | if (op->value) |
| 934 | fprintf(output, "CMP <"); |
| 935 | else |
| 936 | fprintf(output, "CMP >"); |
| 937 | if (!op->value2) |
| 938 | fprintf(output, "="); |
| 939 | break; |
| 940 | case XPATH_OP_PLUS: |
| 941 | if (op->value == 0) |
| 942 | fprintf(output, "PLUS -"); |
| 943 | else if (op->value == 1) |
| 944 | fprintf(output, "PLUS +"); |
| 945 | else if (op->value == 2) |
| 946 | fprintf(output, "PLUS unary -"); |
| 947 | else if (op->value == 3) |
| 948 | fprintf(output, "PLUS unary - -"); |
| 949 | break; |
| 950 | case XPATH_OP_MULT: |
| 951 | if (op->value == 0) |
| 952 | fprintf(output, "MULT *"); |
| 953 | else if (op->value == 1) |
| 954 | fprintf(output, "MULT div"); |
| 955 | else |
| 956 | fprintf(output, "MULT mod"); |
| 957 | break; |
| 958 | case XPATH_OP_UNION: |
| 959 | fprintf(output, "UNION"); break; |
| 960 | case XPATH_OP_ROOT: |
| 961 | fprintf(output, "ROOT"); break; |
| 962 | case XPATH_OP_NODE: |
| 963 | fprintf(output, "NODE"); break; |
| 964 | case XPATH_OP_RESET: |
| 965 | fprintf(output, "RESET"); break; |
| 966 | case XPATH_OP_SORT: |
| 967 | fprintf(output, "SORT"); break; |
| 968 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 969 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 970 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 971 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 972 | const xmlChar *prefix = op->value4; |
| 973 | const xmlChar *name = op->value5; |
| 974 | |
| 975 | fprintf(output, "COLLECT "); |
| 976 | switch (axis) { |
| 977 | case AXIS_ANCESTOR: |
| 978 | fprintf(output, " 'ancestors' "); break; |
| 979 | case AXIS_ANCESTOR_OR_SELF: |
| 980 | fprintf(output, " 'ancestors-or-self' "); break; |
| 981 | case AXIS_ATTRIBUTE: |
| 982 | fprintf(output, " 'attributes' "); break; |
| 983 | case AXIS_CHILD: |
| 984 | fprintf(output, " 'child' "); break; |
| 985 | case AXIS_DESCENDANT: |
| 986 | fprintf(output, " 'descendant' "); break; |
| 987 | case AXIS_DESCENDANT_OR_SELF: |
| 988 | fprintf(output, " 'descendant-or-self' "); break; |
| 989 | case AXIS_FOLLOWING: |
| 990 | fprintf(output, " 'following' "); break; |
| 991 | case AXIS_FOLLOWING_SIBLING: |
| 992 | fprintf(output, " 'following-siblings' "); break; |
| 993 | case AXIS_NAMESPACE: |
| 994 | fprintf(output, " 'namespace' "); break; |
| 995 | case AXIS_PARENT: |
| 996 | fprintf(output, " 'parent' "); break; |
| 997 | case AXIS_PRECEDING: |
| 998 | fprintf(output, " 'preceding' "); break; |
| 999 | case AXIS_PRECEDING_SIBLING: |
| 1000 | fprintf(output, " 'preceding-sibling' "); break; |
| 1001 | case AXIS_SELF: |
| 1002 | fprintf(output, " 'self' "); break; |
| 1003 | } |
| 1004 | switch (test) { |
| 1005 | case NODE_TEST_NONE: |
| 1006 | fprintf(output, "'none' "); break; |
| 1007 | case NODE_TEST_TYPE: |
| 1008 | fprintf(output, "'type' "); break; |
| 1009 | case NODE_TEST_PI: |
| 1010 | fprintf(output, "'PI' "); break; |
| 1011 | case NODE_TEST_ALL: |
| 1012 | fprintf(output, "'all' "); break; |
| 1013 | case NODE_TEST_NS: |
| 1014 | fprintf(output, "'namespace' "); break; |
| 1015 | case NODE_TEST_NAME: |
| 1016 | fprintf(output, "'name' "); break; |
| 1017 | } |
| 1018 | switch (type) { |
| 1019 | case NODE_TYPE_NODE: |
| 1020 | fprintf(output, "'node' "); break; |
| 1021 | case NODE_TYPE_COMMENT: |
| 1022 | fprintf(output, "'comment' "); break; |
| 1023 | case NODE_TYPE_TEXT: |
| 1024 | fprintf(output, "'text' "); break; |
| 1025 | case NODE_TYPE_PI: |
| 1026 | fprintf(output, "'PI' "); break; |
| 1027 | } |
| 1028 | if (prefix != NULL) |
| 1029 | fprintf(output, "%s:", prefix); |
| 1030 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1031 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | |
| 1034 | } |
| 1035 | case XPATH_OP_VALUE: { |
| 1036 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1037 | |
| 1038 | fprintf(output, "ELEM "); |
| 1039 | xmlXPathDebugDumpObject(output, object, 0); |
| 1040 | goto finish; |
| 1041 | } |
| 1042 | case XPATH_OP_VARIABLE: { |
| 1043 | const xmlChar *prefix = op->value5; |
| 1044 | const xmlChar *name = op->value4; |
| 1045 | |
| 1046 | if (prefix != NULL) |
| 1047 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1048 | else |
| 1049 | fprintf(output, "VARIABLE %s", name); |
| 1050 | break; |
| 1051 | } |
| 1052 | case XPATH_OP_FUNCTION: { |
| 1053 | int nbargs = op->value; |
| 1054 | const xmlChar *prefix = op->value5; |
| 1055 | const xmlChar *name = op->value4; |
| 1056 | |
| 1057 | if (prefix != NULL) |
| 1058 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1059 | prefix, name, nbargs); |
| 1060 | else |
| 1061 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1062 | break; |
| 1063 | } |
| 1064 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1065 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1066 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1067 | #ifdef LIBXML_XPTR_ENABLED |
| 1068 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1069 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1070 | default: |
| 1071 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1072 | } |
| 1073 | fprintf(output, "\n"); |
| 1074 | finish: |
| 1075 | if (op->ch1 >= 0) |
| 1076 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1077 | if (op->ch2 >= 0) |
| 1078 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1079 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1080 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1081 | /** |
| 1082 | * xmlXPathDebugDumpCompExpr: |
| 1083 | * @output: the FILE * for the output |
| 1084 | * @comp: the precompiled XPath expression |
| 1085 | * @depth: the indentation level. |
| 1086 | * |
| 1087 | * Dumps the tree of the compiled XPath expression. |
| 1088 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1089 | void |
| 1090 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1091 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1092 | int i; |
| 1093 | char shift[100]; |
| 1094 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1095 | if ((output == NULL) || (comp == NULL)) return; |
| 1096 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1097 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1098 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1099 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1100 | |
| 1101 | fprintf(output, shift); |
| 1102 | |
| 1103 | if (comp == NULL) { |
| 1104 | fprintf(output, "Compiled Expression is NULL\n"); |
| 1105 | return; |
| 1106 | } |
| 1107 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1108 | comp->nbStep); |
| 1109 | i = comp->last; |
| 1110 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1111 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1112 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1113 | |
| 1114 | /************************************************************************ |
| 1115 | * * |
| 1116 | * Parser stacks related functions and macros * |
| 1117 | * * |
| 1118 | ************************************************************************/ |
| 1119 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1120 | /** |
| 1121 | * valuePop: |
| 1122 | * @ctxt: an XPath evaluation context |
| 1123 | * |
| 1124 | * Pops the top XPath object from the value stack |
| 1125 | * |
| 1126 | * Returns the XPath object just removed |
| 1127 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1128 | xmlXPathObjectPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1129 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1130 | { |
| 1131 | xmlXPathObjectPtr ret; |
| 1132 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1133 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1134 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1135 | ctxt->valueNr--; |
| 1136 | if (ctxt->valueNr > 0) |
| 1137 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1138 | else |
| 1139 | ctxt->value = NULL; |
| 1140 | ret = ctxt->valueTab[ctxt->valueNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1141 | ctxt->valueTab[ctxt->valueNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1142 | return (ret); |
| 1143 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1144 | /** |
| 1145 | * valuePush: |
| 1146 | * @ctxt: an XPath evaluation context |
| 1147 | * @value: the XPath object |
| 1148 | * |
| 1149 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1150 | * |
| 1151 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1152 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1153 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1154 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1155 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1156 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1157 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1158 | xmlXPathObjectPtr *tmp; |
| 1159 | |
| 1160 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1161 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1162 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1163 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1164 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1165 | return (0); |
| 1166 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1167 | ctxt->valueMax *= 2; |
| 1168 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1169 | } |
| 1170 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1171 | ctxt->value = value; |
| 1172 | return (ctxt->valueNr++); |
| 1173 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1174 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1175 | /** |
| 1176 | * xmlXPathPopBoolean: |
| 1177 | * @ctxt: an XPath parser context |
| 1178 | * |
| 1179 | * Pops a boolean from the stack, handling conversion if needed. |
| 1180 | * Check error with #xmlXPathCheckError. |
| 1181 | * |
| 1182 | * Returns the boolean |
| 1183 | */ |
| 1184 | int |
| 1185 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1186 | xmlXPathObjectPtr obj; |
| 1187 | int ret; |
| 1188 | |
| 1189 | obj = valuePop(ctxt); |
| 1190 | if (obj == NULL) { |
| 1191 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1192 | return(0); |
| 1193 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1194 | if (obj->type != XPATH_BOOLEAN) |
| 1195 | ret = xmlXPathCastToBoolean(obj); |
| 1196 | else |
| 1197 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1198 | xmlXPathFreeObject(obj); |
| 1199 | return(ret); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * xmlXPathPopNumber: |
| 1204 | * @ctxt: an XPath parser context |
| 1205 | * |
| 1206 | * Pops a number from the stack, handling conversion if needed. |
| 1207 | * Check error with #xmlXPathCheckError. |
| 1208 | * |
| 1209 | * Returns the number |
| 1210 | */ |
| 1211 | double |
| 1212 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1213 | xmlXPathObjectPtr obj; |
| 1214 | double ret; |
| 1215 | |
| 1216 | obj = valuePop(ctxt); |
| 1217 | if (obj == NULL) { |
| 1218 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1219 | return(0); |
| 1220 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1221 | if (obj->type != XPATH_NUMBER) |
| 1222 | ret = xmlXPathCastToNumber(obj); |
| 1223 | else |
| 1224 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1225 | xmlXPathFreeObject(obj); |
| 1226 | return(ret); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * xmlXPathPopString: |
| 1231 | * @ctxt: an XPath parser context |
| 1232 | * |
| 1233 | * Pops a string from the stack, handling conversion if needed. |
| 1234 | * Check error with #xmlXPathCheckError. |
| 1235 | * |
| 1236 | * Returns the string |
| 1237 | */ |
| 1238 | xmlChar * |
| 1239 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1240 | xmlXPathObjectPtr obj; |
| 1241 | xmlChar * ret; |
| 1242 | |
| 1243 | obj = valuePop(ctxt); |
| 1244 | if (obj == NULL) { |
| 1245 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1246 | return(NULL); |
| 1247 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1248 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1249 | /* TODO: needs refactoring somewhere else */ |
| 1250 | if (obj->stringval == ret) |
| 1251 | obj->stringval = NULL; |
| 1252 | xmlXPathFreeObject(obj); |
| 1253 | return(ret); |
| 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * xmlXPathPopNodeSet: |
| 1258 | * @ctxt: an XPath parser context |
| 1259 | * |
| 1260 | * Pops a node-set from the stack, handling conversion if needed. |
| 1261 | * Check error with #xmlXPathCheckError. |
| 1262 | * |
| 1263 | * Returns the node-set |
| 1264 | */ |
| 1265 | xmlNodeSetPtr |
| 1266 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1267 | xmlXPathObjectPtr obj; |
| 1268 | xmlNodeSetPtr ret; |
| 1269 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 1270 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1271 | if (ctxt->value == NULL) { |
| 1272 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1273 | return(NULL); |
| 1274 | } |
| 1275 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1276 | xmlXPathSetTypeError(ctxt); |
| 1277 | return(NULL); |
| 1278 | } |
| 1279 | obj = valuePop(ctxt); |
| 1280 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1281 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1282 | /* to fix memory leak of not clearing obj->user */ |
| 1283 | if (obj->boolval && obj->user != NULL) |
| 1284 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1285 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1286 | xmlXPathFreeNodeSetList(obj); |
| 1287 | return(ret); |
| 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * xmlXPathPopExternal: |
| 1292 | * @ctxt: an XPath parser context |
| 1293 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1294 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1295 | * Check error with #xmlXPathCheckError. |
| 1296 | * |
| 1297 | * Returns the object |
| 1298 | */ |
| 1299 | void * |
| 1300 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1301 | xmlXPathObjectPtr obj; |
| 1302 | void * ret; |
| 1303 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1304 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1305 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1306 | return(NULL); |
| 1307 | } |
| 1308 | if (ctxt->value->type != XPATH_USERS) { |
| 1309 | xmlXPathSetTypeError(ctxt); |
| 1310 | return(NULL); |
| 1311 | } |
| 1312 | obj = valuePop(ctxt); |
| 1313 | ret = obj->user; |
| 1314 | xmlXPathFreeObject(obj); |
| 1315 | return(ret); |
| 1316 | } |
| 1317 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1318 | /* |
| 1319 | * Macros for accessing the content. Those should be used only by the parser, |
| 1320 | * and not exported. |
| 1321 | * |
| 1322 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1323 | * |
| 1324 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1325 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1326 | * in ISO-Latin or UTF-8. |
| 1327 | * This should be used internally by the parser |
| 1328 | * only to compare to ASCII values otherwise it would break when |
| 1329 | * running with UTF-8 encoding. |
| 1330 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1331 | * to compare on ASCII based substring. |
| 1332 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1333 | * strings within the parser. |
| 1334 | * CURRENT Returns the current char value, with the full decoding of |
| 1335 | * UTF-8 if we are using this mode. It returns an int. |
| 1336 | * NEXT Skip to the next character, this does the proper decoding |
| 1337 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1338 | * It returns the pointer to the current xmlChar. |
| 1339 | */ |
| 1340 | |
| 1341 | #define CUR (*ctxt->cur) |
| 1342 | #define SKIP(val) ctxt->cur += (val) |
| 1343 | #define NXT(val) ctxt->cur[(val)] |
| 1344 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1345 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1346 | |
| 1347 | #define COPY_BUF(l,b,i,v) \ |
| 1348 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1349 | else i += xmlCopyChar(l,&b[i],v) |
| 1350 | |
| 1351 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | |
| 1353 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1354 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | |
| 1356 | #define CURRENT (*ctxt->cur) |
| 1357 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1358 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1359 | |
| 1360 | #ifndef DBL_DIG |
| 1361 | #define DBL_DIG 16 |
| 1362 | #endif |
| 1363 | #ifndef DBL_EPSILON |
| 1364 | #define DBL_EPSILON 1E-9 |
| 1365 | #endif |
| 1366 | |
| 1367 | #define UPPER_DOUBLE 1E9 |
| 1368 | #define LOWER_DOUBLE 1E-5 |
| 1369 | |
| 1370 | #define INTEGER_DIGITS DBL_DIG |
| 1371 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1372 | #define EXPONENT_DIGITS (3 + 2) |
| 1373 | |
| 1374 | /** |
| 1375 | * xmlXPathFormatNumber: |
| 1376 | * @number: number to format |
| 1377 | * @buffer: output buffer |
| 1378 | * @buffersize: size of output buffer |
| 1379 | * |
| 1380 | * Convert the number into a string representation. |
| 1381 | */ |
| 1382 | static void |
| 1383 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1384 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1385 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1386 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1387 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1388 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | case -1: |
| 1391 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1392 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1395 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1396 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1397 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1398 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1399 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1400 | } else if (number == ((int) number)) { |
| 1401 | char work[30]; |
| 1402 | char *ptr, *cur; |
| 1403 | int res, value = (int) number; |
| 1404 | |
| 1405 | ptr = &buffer[0]; |
| 1406 | if (value < 0) { |
| 1407 | *ptr++ = '-'; |
| 1408 | value = -value; |
| 1409 | } |
| 1410 | if (value == 0) { |
| 1411 | *ptr++ = '0'; |
| 1412 | } else { |
| 1413 | cur = &work[0]; |
| 1414 | while (value != 0) { |
| 1415 | res = value % 10; |
| 1416 | value = value / 10; |
| 1417 | *cur++ = '0' + res; |
| 1418 | } |
| 1419 | cur--; |
| 1420 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1421 | *ptr++ = *cur--; |
| 1422 | } |
| 1423 | } |
| 1424 | if (ptr - buffer < buffersize) { |
| 1425 | *ptr = 0; |
| 1426 | } else if (buffersize > 0) { |
| 1427 | ptr--; |
| 1428 | *ptr = 0; |
| 1429 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1430 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1431 | /* 3 is sign, decimal point, and terminating zero */ |
| 1432 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1433 | int integer_place, fraction_place; |
| 1434 | char *ptr; |
| 1435 | char *after_fraction; |
| 1436 | double absolute_value; |
| 1437 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1438 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1439 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1440 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1441 | /* |
| 1442 | * First choose format - scientific or regular floating point. |
| 1443 | * In either case, result is in work, and after_fraction points |
| 1444 | * just past the fractional part. |
| 1445 | */ |
| 1446 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1447 | (absolute_value < LOWER_DOUBLE)) && |
| 1448 | (absolute_value != 0.0) ) { |
| 1449 | /* Use scientific notation */ |
| 1450 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1451 | fraction_place = DBL_DIG - 1; |
| 1452 | snprintf(work, sizeof(work),"%*.*e", |
| 1453 | integer_place, fraction_place, number); |
| 1454 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1455 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1456 | else { |
| 1457 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1458 | if (absolute_value > 0.0) |
| 1459 | integer_place = 1 + (int)log10(absolute_value); |
| 1460 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1461 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1462 | fraction_place = (integer_place > 0) |
| 1463 | ? DBL_DIG - integer_place |
| 1464 | : DBL_DIG; |
| 1465 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1466 | fraction_place, number); |
| 1467 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1470 | /* Remove fractional trailing zeroes */ |
| 1471 | ptr = after_fraction; |
| 1472 | while (*(--ptr) == '0') |
| 1473 | ; |
| 1474 | if (*ptr != '.') |
| 1475 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1476 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1477 | |
| 1478 | /* Finally copy result back to caller */ |
| 1479 | size = strlen(work) + 1; |
| 1480 | if (size > buffersize) { |
| 1481 | work[buffersize - 1] = 0; |
| 1482 | size = buffersize; |
| 1483 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1484 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1485 | } |
| 1486 | break; |
| 1487 | } |
| 1488 | } |
| 1489 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1490 | |
| 1491 | /************************************************************************ |
| 1492 | * * |
| 1493 | * Routines to handle NodeSets * |
| 1494 | * * |
| 1495 | ************************************************************************/ |
| 1496 | |
| 1497 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1498 | * xmlXPathOrderDocElems: |
| 1499 | * @doc: an input document |
| 1500 | * |
| 1501 | * Call this routine to speed up XPath computation on static documents. |
| 1502 | * This stamps all the element nodes with the document order |
| 1503 | * 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] | 1504 | * field, the value stored is actually - the node number (starting at -1) |
| 1505 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1506 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1507 | * 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] | 1508 | * of error. |
| 1509 | */ |
| 1510 | long |
| 1511 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1512 | long count = 0; |
| 1513 | xmlNodePtr cur; |
| 1514 | |
| 1515 | if (doc == NULL) |
| 1516 | return(-1); |
| 1517 | cur = doc->children; |
| 1518 | while (cur != NULL) { |
| 1519 | if (cur->type == XML_ELEMENT_NODE) { |
| 1520 | cur->content = (void *) (-(++count)); |
| 1521 | if (cur->children != NULL) { |
| 1522 | cur = cur->children; |
| 1523 | continue; |
| 1524 | } |
| 1525 | } |
| 1526 | if (cur->next != NULL) { |
| 1527 | cur = cur->next; |
| 1528 | continue; |
| 1529 | } |
| 1530 | do { |
| 1531 | cur = cur->parent; |
| 1532 | if (cur == NULL) |
| 1533 | break; |
| 1534 | if (cur == (xmlNodePtr) doc) { |
| 1535 | cur = NULL; |
| 1536 | break; |
| 1537 | } |
| 1538 | if (cur->next != NULL) { |
| 1539 | cur = cur->next; |
| 1540 | break; |
| 1541 | } |
| 1542 | } while (cur != NULL); |
| 1543 | } |
| 1544 | return(count); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1548 | * xmlXPathCmpNodes: |
| 1549 | * @node1: the first node |
| 1550 | * @node2: the second node |
| 1551 | * |
| 1552 | * Compare two nodes w.r.t document order |
| 1553 | * |
| 1554 | * 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] | 1555 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1556 | */ |
| 1557 | int |
| 1558 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1559 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1560 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1561 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1562 | xmlNodePtr cur, root; |
| 1563 | |
| 1564 | if ((node1 == NULL) || (node2 == NULL)) |
| 1565 | return(-2); |
| 1566 | /* |
| 1567 | * a couple of optimizations which will avoid computations in most cases |
| 1568 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1569 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1570 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1571 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1572 | node1 = node1->parent; |
| 1573 | } |
| 1574 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1575 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1576 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1577 | node2 = node2->parent; |
| 1578 | } |
| 1579 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1580 | if (attr1 == attr2) { |
| 1581 | /* not required, but we keep attributes in order */ |
| 1582 | if (attr1 != 0) { |
| 1583 | cur = attrNode2->prev; |
| 1584 | while (cur != NULL) { |
| 1585 | if (cur == attrNode1) |
| 1586 | return (1); |
| 1587 | cur = cur->prev; |
| 1588 | } |
| 1589 | return (-1); |
| 1590 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1591 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1592 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1593 | if (attr2 == 1) |
| 1594 | return(1); |
| 1595 | return(-1); |
| 1596 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1597 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1598 | (node2->type == XML_NAMESPACE_DECL)) |
| 1599 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1600 | if (node1 == node2->prev) |
| 1601 | return(1); |
| 1602 | if (node1 == node2->next) |
| 1603 | return(-1); |
| 1604 | |
| 1605 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1606 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1607 | */ |
| 1608 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1609 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1610 | (0 > (long) node1->content) && |
| 1611 | (0 > (long) node2->content) && |
| 1612 | (node1->doc == node2->doc)) { |
| 1613 | long l1, l2; |
| 1614 | |
| 1615 | l1 = -((long) node1->content); |
| 1616 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1617 | if (l1 < l2) |
| 1618 | return(1); |
| 1619 | if (l1 > l2) |
| 1620 | return(-1); |
| 1621 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1622 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1623 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1624 | * compute depth to root |
| 1625 | */ |
| 1626 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1627 | if (cur == node1) |
| 1628 | return(1); |
| 1629 | depth2++; |
| 1630 | } |
| 1631 | root = cur; |
| 1632 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1633 | if (cur == node2) |
| 1634 | return(-1); |
| 1635 | depth1++; |
| 1636 | } |
| 1637 | /* |
| 1638 | * Distinct document (or distinct entities :-( ) case. |
| 1639 | */ |
| 1640 | if (root != cur) { |
| 1641 | return(-2); |
| 1642 | } |
| 1643 | /* |
| 1644 | * get the nearest common ancestor. |
| 1645 | */ |
| 1646 | while (depth1 > depth2) { |
| 1647 | depth1--; |
| 1648 | node1 = node1->parent; |
| 1649 | } |
| 1650 | while (depth2 > depth1) { |
| 1651 | depth2--; |
| 1652 | node2 = node2->parent; |
| 1653 | } |
| 1654 | while (node1->parent != node2->parent) { |
| 1655 | node1 = node1->parent; |
| 1656 | node2 = node2->parent; |
| 1657 | /* should not happen but just in case ... */ |
| 1658 | if ((node1 == NULL) || (node2 == NULL)) |
| 1659 | return(-2); |
| 1660 | } |
| 1661 | /* |
| 1662 | * Find who's first. |
| 1663 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1664 | if (node1 == node2->prev) |
| 1665 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1666 | if (node1 == node2->next) |
| 1667 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1668 | /* |
| 1669 | * Speedup using document order if availble. |
| 1670 | */ |
| 1671 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1672 | (node2->type == XML_ELEMENT_NODE) && |
| 1673 | (0 > (long) node1->content) && |
| 1674 | (0 > (long) node2->content) && |
| 1675 | (node1->doc == node2->doc)) { |
| 1676 | long l1, l2; |
| 1677 | |
| 1678 | l1 = -((long) node1->content); |
| 1679 | l2 = -((long) node2->content); |
| 1680 | if (l1 < l2) |
| 1681 | return(1); |
| 1682 | if (l1 > l2) |
| 1683 | return(-1); |
| 1684 | } |
| 1685 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1686 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1687 | if (cur == node2) |
| 1688 | return(1); |
| 1689 | return(-1); /* assume there is no sibling list corruption */ |
| 1690 | } |
| 1691 | |
| 1692 | /** |
| 1693 | * xmlXPathNodeSetSort: |
| 1694 | * @set: the node set |
| 1695 | * |
| 1696 | * Sort the node set in document order |
| 1697 | */ |
| 1698 | void |
| 1699 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1700 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1701 | xmlNodePtr tmp; |
| 1702 | |
| 1703 | if (set == NULL) |
| 1704 | return; |
| 1705 | |
| 1706 | /* Use Shell's sort to sort the node-set */ |
| 1707 | len = set->nodeNr; |
| 1708 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1709 | for (i = incr; i < len; i++) { |
| 1710 | j = i - incr; |
| 1711 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1712 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1713 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1714 | tmp = set->nodeTab[j]; |
| 1715 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1716 | set->nodeTab[j + incr] = tmp; |
| 1717 | j -= incr; |
| 1718 | } else |
| 1719 | break; |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | #define XML_NODESET_DEFAULT 10 |
| 1726 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1727 | * xmlXPathNodeSetDupNs: |
| 1728 | * @node: the parent node of the namespace XPath node |
| 1729 | * @ns: the libxml namespace declaration node. |
| 1730 | * |
| 1731 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1732 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1733 | * parent node in the XPath semantic. |
| 1734 | * |
| 1735 | * Returns the newly created object. |
| 1736 | */ |
| 1737 | static xmlNodePtr |
| 1738 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1739 | xmlNsPtr cur; |
| 1740 | |
| 1741 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1742 | return(NULL); |
| 1743 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1744 | return((xmlNodePtr) ns); |
| 1745 | |
| 1746 | /* |
| 1747 | * Allocate a new Namespace and fill the fields. |
| 1748 | */ |
| 1749 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1750 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1751 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1752 | return(NULL); |
| 1753 | } |
| 1754 | memset(cur, 0, sizeof(xmlNs)); |
| 1755 | cur->type = XML_NAMESPACE_DECL; |
| 1756 | if (ns->href != NULL) |
| 1757 | cur->href = xmlStrdup(ns->href); |
| 1758 | if (ns->prefix != NULL) |
| 1759 | cur->prefix = xmlStrdup(ns->prefix); |
| 1760 | cur->next = (xmlNsPtr) node; |
| 1761 | return((xmlNodePtr) cur); |
| 1762 | } |
| 1763 | |
| 1764 | /** |
| 1765 | * xmlXPathNodeSetFreeNs: |
| 1766 | * @ns: the XPath namespace node found in a nodeset. |
| 1767 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1768 | * 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] | 1769 | * 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] | 1770 | * 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] | 1771 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1772 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1773 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1774 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1775 | return; |
| 1776 | |
| 1777 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1778 | if (ns->href != NULL) |
| 1779 | xmlFree((xmlChar *)ns->href); |
| 1780 | if (ns->prefix != NULL) |
| 1781 | xmlFree((xmlChar *)ns->prefix); |
| 1782 | xmlFree(ns); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1787 | * xmlXPathNodeSetCreate: |
| 1788 | * @val: an initial xmlNodePtr, or NULL |
| 1789 | * |
| 1790 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1791 | * |
| 1792 | * Returns the newly created object. |
| 1793 | */ |
| 1794 | xmlNodeSetPtr |
| 1795 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1796 | xmlNodeSetPtr ret; |
| 1797 | |
| 1798 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1799 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1800 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1801 | return(NULL); |
| 1802 | } |
| 1803 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1804 | if (val != NULL) { |
| 1805 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1806 | sizeof(xmlNodePtr)); |
| 1807 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1808 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 1809 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1810 | return(NULL); |
| 1811 | } |
| 1812 | memset(ret->nodeTab, 0 , |
| 1813 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1814 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1815 | if (val->type == XML_NAMESPACE_DECL) { |
| 1816 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1817 | |
| 1818 | ret->nodeTab[ret->nodeNr++] = |
| 1819 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1820 | } else |
| 1821 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1822 | } |
| 1823 | return(ret); |
| 1824 | } |
| 1825 | |
| 1826 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1827 | * xmlXPathNodeSetContains: |
| 1828 | * @cur: the node-set |
| 1829 | * @val: the node |
| 1830 | * |
| 1831 | * checks whether @cur contains @val |
| 1832 | * |
| 1833 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1834 | */ |
| 1835 | int |
| 1836 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1837 | int i; |
| 1838 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1839 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1840 | if (val->type == XML_NAMESPACE_DECL) { |
| 1841 | for (i = 0; i < cur->nodeNr; i++) { |
| 1842 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1843 | xmlNsPtr ns1, ns2; |
| 1844 | |
| 1845 | ns1 = (xmlNsPtr) val; |
| 1846 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1847 | if (ns1 == ns2) |
| 1848 | return(1); |
| 1849 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1850 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1851 | return(1); |
| 1852 | } |
| 1853 | } |
| 1854 | } else { |
| 1855 | for (i = 0; i < cur->nodeNr; i++) { |
| 1856 | if (cur->nodeTab[i] == val) |
| 1857 | return(1); |
| 1858 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1859 | } |
| 1860 | return(0); |
| 1861 | } |
| 1862 | |
| 1863 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1864 | * xmlXPathNodeSetAddNs: |
| 1865 | * @cur: the initial node set |
| 1866 | * @node: the hosting node |
| 1867 | * @ns: a the namespace node |
| 1868 | * |
| 1869 | * add a new namespace node to an existing NodeSet |
| 1870 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1871 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1872 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1873 | int i; |
| 1874 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1875 | |
| 1876 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 1877 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1878 | (node->type != XML_ELEMENT_NODE)) |
| 1879 | return; |
| 1880 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1881 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1882 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1883 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1884 | */ |
| 1885 | for (i = 0;i < cur->nodeNr;i++) { |
| 1886 | if ((cur->nodeTab[i] != NULL) && |
| 1887 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1888 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1889 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * grow the nodeTab if needed |
| 1895 | */ |
| 1896 | if (cur->nodeMax == 0) { |
| 1897 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1898 | sizeof(xmlNodePtr)); |
| 1899 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1900 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1901 | return; |
| 1902 | } |
| 1903 | memset(cur->nodeTab, 0 , |
| 1904 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1905 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1906 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1907 | xmlNodePtr *temp; |
| 1908 | |
| 1909 | cur->nodeMax *= 2; |
| 1910 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1911 | sizeof(xmlNodePtr)); |
| 1912 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1913 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1914 | return; |
| 1915 | } |
| 1916 | cur->nodeTab = temp; |
| 1917 | } |
| 1918 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1919 | } |
| 1920 | |
| 1921 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | * xmlXPathNodeSetAdd: |
| 1923 | * @cur: the initial node set |
| 1924 | * @val: a new xmlNodePtr |
| 1925 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1926 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | */ |
| 1928 | void |
| 1929 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1930 | int i; |
| 1931 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1932 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1933 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1934 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1935 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1936 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1937 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1938 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1939 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1941 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1942 | */ |
| 1943 | for (i = 0;i < cur->nodeNr;i++) |
| 1944 | if (cur->nodeTab[i] == val) return; |
| 1945 | |
| 1946 | /* |
| 1947 | * grow the nodeTab if needed |
| 1948 | */ |
| 1949 | if (cur->nodeMax == 0) { |
| 1950 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1951 | sizeof(xmlNodePtr)); |
| 1952 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1953 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1954 | return; |
| 1955 | } |
| 1956 | memset(cur->nodeTab, 0 , |
| 1957 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1958 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1959 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1960 | xmlNodePtr *temp; |
| 1961 | |
| 1962 | cur->nodeMax *= 2; |
| 1963 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1964 | sizeof(xmlNodePtr)); |
| 1965 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1966 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1967 | return; |
| 1968 | } |
| 1969 | cur->nodeTab = temp; |
| 1970 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1971 | if (val->type == XML_NAMESPACE_DECL) { |
| 1972 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1973 | |
| 1974 | cur->nodeTab[cur->nodeNr++] = |
| 1975 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1976 | } else |
| 1977 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * xmlXPathNodeSetAddUnique: |
| 1982 | * @cur: the initial node set |
| 1983 | * @val: a new xmlNodePtr |
| 1984 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1985 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | * when we are sure the node is not already in the set. |
| 1987 | */ |
| 1988 | void |
| 1989 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1990 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1991 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1992 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1993 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1994 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1995 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1996 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1997 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * grow the nodeTab if needed |
| 2000 | */ |
| 2001 | if (cur->nodeMax == 0) { |
| 2002 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2003 | sizeof(xmlNodePtr)); |
| 2004 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2005 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2006 | return; |
| 2007 | } |
| 2008 | memset(cur->nodeTab, 0 , |
| 2009 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2010 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 2011 | } else if (cur->nodeNr == cur->nodeMax) { |
| 2012 | xmlNodePtr *temp; |
| 2013 | |
| 2014 | cur->nodeMax *= 2; |
| 2015 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 2016 | sizeof(xmlNodePtr)); |
| 2017 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2018 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | cur->nodeTab = temp; |
| 2022 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2023 | if (val->type == XML_NAMESPACE_DECL) { |
| 2024 | xmlNsPtr ns = (xmlNsPtr) val; |
| 2025 | |
| 2026 | cur->nodeTab[cur->nodeNr++] = |
| 2027 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2028 | } else |
| 2029 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * xmlXPathNodeSetMerge: |
| 2034 | * @val1: the first NodeSet or NULL |
| 2035 | * @val2: the second NodeSet |
| 2036 | * |
| 2037 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2038 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2039 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2040 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2041 | */ |
| 2042 | xmlNodeSetPtr |
| 2043 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2044 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (val2 == NULL) return(val1); |
| 2047 | if (val1 == NULL) { |
| 2048 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2049 | } |
| 2050 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2051 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2052 | initNr = val1->nodeNr; |
| 2053 | |
| 2054 | for (i = 0;i < val2->nodeNr;i++) { |
| 2055 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2056 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2057 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2058 | skip = 0; |
| 2059 | for (j = 0; j < initNr; j++) { |
| 2060 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 2061 | skip = 1; |
| 2062 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2063 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 2064 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 2065 | xmlNsPtr ns1, ns2; |
| 2066 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 2067 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 2068 | if ((ns1->next == ns2->next) && |
| 2069 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 2070 | skip = 1; |
| 2071 | break; |
| 2072 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | if (skip) |
| 2076 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2077 | |
| 2078 | /* |
| 2079 | * grow the nodeTab if needed |
| 2080 | */ |
| 2081 | if (val1->nodeMax == 0) { |
| 2082 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2083 | sizeof(xmlNodePtr)); |
| 2084 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2085 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | return(NULL); |
| 2087 | } |
| 2088 | memset(val1->nodeTab, 0 , |
| 2089 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2090 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2091 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2092 | xmlNodePtr *temp; |
| 2093 | |
| 2094 | val1->nodeMax *= 2; |
| 2095 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2096 | sizeof(xmlNodePtr)); |
| 2097 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2098 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2099 | return(NULL); |
| 2100 | } |
| 2101 | val1->nodeTab = temp; |
| 2102 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2103 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2104 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2105 | |
| 2106 | val1->nodeTab[val1->nodeNr++] = |
| 2107 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2108 | } else |
| 2109 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | return(val1); |
| 2113 | } |
| 2114 | |
| 2115 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2116 | * xmlXPathNodeSetMergeUnique: |
| 2117 | * @val1: the first NodeSet or NULL |
| 2118 | * @val2: the second NodeSet |
| 2119 | * |
| 2120 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2121 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2122 | * |
| 2123 | * Returns @val1 once extended or NULL in case of error. |
| 2124 | */ |
| 2125 | static xmlNodeSetPtr |
| 2126 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2127 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2128 | |
| 2129 | if (val2 == NULL) return(val1); |
| 2130 | if (val1 == NULL) { |
| 2131 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2132 | } |
| 2133 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2134 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2135 | |
| 2136 | for (i = 0;i < val2->nodeNr;i++) { |
| 2137 | /* |
| 2138 | * grow the nodeTab if needed |
| 2139 | */ |
| 2140 | if (val1->nodeMax == 0) { |
| 2141 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2142 | sizeof(xmlNodePtr)); |
| 2143 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2144 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2145 | return(NULL); |
| 2146 | } |
| 2147 | memset(val1->nodeTab, 0 , |
| 2148 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2149 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2150 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2151 | xmlNodePtr *temp; |
| 2152 | |
| 2153 | val1->nodeMax *= 2; |
| 2154 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2155 | sizeof(xmlNodePtr)); |
| 2156 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2157 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2158 | return(NULL); |
| 2159 | } |
| 2160 | val1->nodeTab = temp; |
| 2161 | } |
| 2162 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2163 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2164 | |
| 2165 | val1->nodeTab[val1->nodeNr++] = |
| 2166 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2167 | } else |
| 2168 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2169 | } |
| 2170 | |
| 2171 | return(val1); |
| 2172 | } |
| 2173 | |
| 2174 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2175 | * xmlXPathNodeSetDel: |
| 2176 | * @cur: the initial node set |
| 2177 | * @val: an xmlNodePtr |
| 2178 | * |
| 2179 | * Removes an xmlNodePtr from an existing NodeSet |
| 2180 | */ |
| 2181 | void |
| 2182 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2183 | int i; |
| 2184 | |
| 2185 | if (cur == NULL) return; |
| 2186 | if (val == NULL) return; |
| 2187 | |
| 2188 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2189 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2190 | */ |
| 2191 | for (i = 0;i < cur->nodeNr;i++) |
| 2192 | if (cur->nodeTab[i] == val) break; |
| 2193 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2194 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2195 | #ifdef DEBUG |
| 2196 | xmlGenericError(xmlGenericErrorContext, |
| 2197 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2198 | val->name); |
| 2199 | #endif |
| 2200 | return; |
| 2201 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2202 | if ((cur->nodeTab[i] != NULL) && |
| 2203 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2204 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2205 | cur->nodeNr--; |
| 2206 | for (;i < cur->nodeNr;i++) |
| 2207 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2208 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2209 | } |
| 2210 | |
| 2211 | /** |
| 2212 | * xmlXPathNodeSetRemove: |
| 2213 | * @cur: the initial node set |
| 2214 | * @val: the index to remove |
| 2215 | * |
| 2216 | * Removes an entry from an existing NodeSet list. |
| 2217 | */ |
| 2218 | void |
| 2219 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2220 | if (cur == NULL) return; |
| 2221 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2222 | if ((cur->nodeTab[val] != NULL) && |
| 2223 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2224 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2225 | cur->nodeNr--; |
| 2226 | for (;val < cur->nodeNr;val++) |
| 2227 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2228 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * xmlXPathFreeNodeSet: |
| 2233 | * @obj: the xmlNodeSetPtr to free |
| 2234 | * |
| 2235 | * Free the NodeSet compound (not the actual nodes !). |
| 2236 | */ |
| 2237 | void |
| 2238 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2239 | if (obj == NULL) return; |
| 2240 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2241 | int i; |
| 2242 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2243 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2244 | for (i = 0;i < obj->nodeNr;i++) |
| 2245 | if ((obj->nodeTab[i] != NULL) && |
| 2246 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2247 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2248 | xmlFree(obj->nodeTab); |
| 2249 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2250 | xmlFree(obj); |
| 2251 | } |
| 2252 | |
| 2253 | /** |
| 2254 | * xmlXPathFreeValueTree: |
| 2255 | * @obj: the xmlNodeSetPtr to free |
| 2256 | * |
| 2257 | * Free the NodeSet compound and the actual tree, this is different |
| 2258 | * from xmlXPathFreeNodeSet() |
| 2259 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2260 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2261 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2262 | int i; |
| 2263 | |
| 2264 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2265 | |
| 2266 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2267 | for (i = 0;i < obj->nodeNr;i++) { |
| 2268 | if (obj->nodeTab[i] != NULL) { |
| 2269 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2270 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2271 | } else { |
| 2272 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2273 | } |
| 2274 | } |
| 2275 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | xmlFree(obj->nodeTab); |
| 2277 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2278 | xmlFree(obj); |
| 2279 | } |
| 2280 | |
| 2281 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2282 | /** |
| 2283 | * xmlGenericErrorContextNodeSet: |
| 2284 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2285 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2286 | * |
| 2287 | * Quick display of a NodeSet |
| 2288 | */ |
| 2289 | void |
| 2290 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2291 | int i; |
| 2292 | |
| 2293 | if (output == NULL) output = xmlGenericErrorContext; |
| 2294 | if (obj == NULL) { |
| 2295 | fprintf(output, "NodeSet == NULL !\n"); |
| 2296 | return; |
| 2297 | } |
| 2298 | if (obj->nodeNr == 0) { |
| 2299 | fprintf(output, "NodeSet is empty\n"); |
| 2300 | return; |
| 2301 | } |
| 2302 | if (obj->nodeTab == NULL) { |
| 2303 | fprintf(output, " nodeTab == NULL !\n"); |
| 2304 | return; |
| 2305 | } |
| 2306 | for (i = 0; i < obj->nodeNr; i++) { |
| 2307 | if (obj->nodeTab[i] == NULL) { |
| 2308 | fprintf(output, " NULL !\n"); |
| 2309 | return; |
| 2310 | } |
| 2311 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2312 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2313 | fprintf(output, " /"); |
| 2314 | else if (obj->nodeTab[i]->name == NULL) |
| 2315 | fprintf(output, " noname!"); |
| 2316 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2317 | } |
| 2318 | fprintf(output, "\n"); |
| 2319 | } |
| 2320 | #endif |
| 2321 | |
| 2322 | /** |
| 2323 | * xmlXPathNewNodeSet: |
| 2324 | * @val: the NodePtr value |
| 2325 | * |
| 2326 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2327 | * it with the single Node @val |
| 2328 | * |
| 2329 | * Returns the newly created object. |
| 2330 | */ |
| 2331 | xmlXPathObjectPtr |
| 2332 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2333 | xmlXPathObjectPtr ret; |
| 2334 | |
| 2335 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2336 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2337 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2338 | return(NULL); |
| 2339 | } |
| 2340 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2341 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2342 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2344 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2345 | return(ret); |
| 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * xmlXPathNewValueTree: |
| 2350 | * @val: the NodePtr value |
| 2351 | * |
| 2352 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2353 | * it with the tree root @val |
| 2354 | * |
| 2355 | * Returns the newly created object. |
| 2356 | */ |
| 2357 | xmlXPathObjectPtr |
| 2358 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2359 | xmlXPathObjectPtr ret; |
| 2360 | |
| 2361 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2362 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2363 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | return(NULL); |
| 2365 | } |
| 2366 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2367 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2368 | ret->boolval = 1; |
| 2369 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2370 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2371 | return(ret); |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * xmlXPathNewNodeSetList: |
| 2376 | * @val: an existing NodeSet |
| 2377 | * |
| 2378 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2379 | * it with the Nodeset @val |
| 2380 | * |
| 2381 | * Returns the newly created object. |
| 2382 | */ |
| 2383 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2384 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2385 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2386 | xmlXPathObjectPtr ret; |
| 2387 | int i; |
| 2388 | |
| 2389 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2390 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2392 | ret = xmlXPathNewNodeSet(NULL); |
| 2393 | else { |
| 2394 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2395 | for (i = 1; i < val->nodeNr; ++i) |
| 2396 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2397 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2398 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2399 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlXPathWrapNodeSet: |
| 2404 | * @val: the NodePtr value |
| 2405 | * |
| 2406 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2407 | * |
| 2408 | * Returns the newly created object. |
| 2409 | */ |
| 2410 | xmlXPathObjectPtr |
| 2411 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2412 | xmlXPathObjectPtr ret; |
| 2413 | |
| 2414 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2415 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2416 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2417 | return(NULL); |
| 2418 | } |
| 2419 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2420 | ret->type = XPATH_NODESET; |
| 2421 | ret->nodesetval = val; |
| 2422 | return(ret); |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * xmlXPathFreeNodeSetList: |
| 2427 | * @obj: an existing NodeSetList object |
| 2428 | * |
| 2429 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2430 | * the list contrary to xmlXPathFreeObject(). |
| 2431 | */ |
| 2432 | void |
| 2433 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2434 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2435 | xmlFree(obj); |
| 2436 | } |
| 2437 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2438 | /** |
| 2439 | * xmlXPathDifference: |
| 2440 | * @nodes1: a node-set |
| 2441 | * @nodes2: a node-set |
| 2442 | * |
| 2443 | * Implements the EXSLT - Sets difference() function: |
| 2444 | * node-set set:difference (node-set, node-set) |
| 2445 | * |
| 2446 | * Returns the difference between the two node sets, or nodes1 if |
| 2447 | * nodes2 is empty |
| 2448 | */ |
| 2449 | xmlNodeSetPtr |
| 2450 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2451 | xmlNodeSetPtr ret; |
| 2452 | int i, l1; |
| 2453 | xmlNodePtr cur; |
| 2454 | |
| 2455 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2456 | return(nodes1); |
| 2457 | |
| 2458 | ret = xmlXPathNodeSetCreate(NULL); |
| 2459 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2460 | return(ret); |
| 2461 | |
| 2462 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2463 | |
| 2464 | for (i = 0; i < l1; i++) { |
| 2465 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2466 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2467 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2468 | } |
| 2469 | return(ret); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * xmlXPathIntersection: |
| 2474 | * @nodes1: a node-set |
| 2475 | * @nodes2: a node-set |
| 2476 | * |
| 2477 | * Implements the EXSLT - Sets intersection() function: |
| 2478 | * node-set set:intersection (node-set, node-set) |
| 2479 | * |
| 2480 | * Returns a node set comprising the nodes that are within both the |
| 2481 | * node sets passed as arguments |
| 2482 | */ |
| 2483 | xmlNodeSetPtr |
| 2484 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2485 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2486 | int i, l1; |
| 2487 | xmlNodePtr cur; |
| 2488 | |
| 2489 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2490 | return(ret); |
| 2491 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2492 | return(ret); |
| 2493 | |
| 2494 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2495 | |
| 2496 | for (i = 0; i < l1; i++) { |
| 2497 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2498 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2499 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2500 | } |
| 2501 | return(ret); |
| 2502 | } |
| 2503 | |
| 2504 | /** |
| 2505 | * xmlXPathDistinctSorted: |
| 2506 | * @nodes: a node-set, sorted by document order |
| 2507 | * |
| 2508 | * Implements the EXSLT - Sets distinct() function: |
| 2509 | * node-set set:distinct (node-set) |
| 2510 | * |
| 2511 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2512 | * it is empty |
| 2513 | */ |
| 2514 | xmlNodeSetPtr |
| 2515 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2516 | xmlNodeSetPtr ret; |
| 2517 | xmlHashTablePtr hash; |
| 2518 | int i, l; |
| 2519 | xmlChar * strval; |
| 2520 | xmlNodePtr cur; |
| 2521 | |
| 2522 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2523 | return(nodes); |
| 2524 | |
| 2525 | ret = xmlXPathNodeSetCreate(NULL); |
| 2526 | l = xmlXPathNodeSetGetLength(nodes); |
| 2527 | hash = xmlHashCreate (l); |
| 2528 | for (i = 0; i < l; i++) { |
| 2529 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2530 | strval = xmlXPathCastNodeToString(cur); |
| 2531 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2532 | xmlHashAddEntry(hash, strval, strval); |
| 2533 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2534 | } else { |
| 2535 | xmlFree(strval); |
| 2536 | } |
| 2537 | } |
| 2538 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2539 | return(ret); |
| 2540 | } |
| 2541 | |
| 2542 | /** |
| 2543 | * xmlXPathDistinct: |
| 2544 | * @nodes: a node-set |
| 2545 | * |
| 2546 | * Implements the EXSLT - Sets distinct() function: |
| 2547 | * node-set set:distinct (node-set) |
| 2548 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2549 | * is called with the sorted node-set |
| 2550 | * |
| 2551 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2552 | * it is empty |
| 2553 | */ |
| 2554 | xmlNodeSetPtr |
| 2555 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2556 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2557 | return(nodes); |
| 2558 | |
| 2559 | xmlXPathNodeSetSort(nodes); |
| 2560 | return(xmlXPathDistinctSorted(nodes)); |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * xmlXPathHasSameNodes: |
| 2565 | * @nodes1: a node-set |
| 2566 | * @nodes2: a node-set |
| 2567 | * |
| 2568 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2569 | * boolean set:has-same-node(node-set, node-set) |
| 2570 | * |
| 2571 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2572 | * otherwise |
| 2573 | */ |
| 2574 | int |
| 2575 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2576 | int i, l; |
| 2577 | xmlNodePtr cur; |
| 2578 | |
| 2579 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2580 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2581 | return(0); |
| 2582 | |
| 2583 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2584 | for (i = 0; i < l; i++) { |
| 2585 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2586 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2587 | return(1); |
| 2588 | } |
| 2589 | return(0); |
| 2590 | } |
| 2591 | |
| 2592 | /** |
| 2593 | * xmlXPathNodeLeadingSorted: |
| 2594 | * @nodes: a node-set, sorted by document order |
| 2595 | * @node: a node |
| 2596 | * |
| 2597 | * Implements the EXSLT - Sets leading() function: |
| 2598 | * node-set set:leading (node-set, node-set) |
| 2599 | * |
| 2600 | * Returns the nodes in @nodes that precede @node in document order, |
| 2601 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2602 | * doesn't contain @node |
| 2603 | */ |
| 2604 | xmlNodeSetPtr |
| 2605 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2606 | int i, l; |
| 2607 | xmlNodePtr cur; |
| 2608 | xmlNodeSetPtr ret; |
| 2609 | |
| 2610 | if (node == NULL) |
| 2611 | return(nodes); |
| 2612 | |
| 2613 | ret = xmlXPathNodeSetCreate(NULL); |
| 2614 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2615 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2616 | return(ret); |
| 2617 | |
| 2618 | l = xmlXPathNodeSetGetLength(nodes); |
| 2619 | for (i = 0; i < l; i++) { |
| 2620 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2621 | if (cur == node) |
| 2622 | break; |
| 2623 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2624 | } |
| 2625 | return(ret); |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * xmlXPathNodeLeading: |
| 2630 | * @nodes: a node-set |
| 2631 | * @node: a node |
| 2632 | * |
| 2633 | * Implements the EXSLT - Sets leading() function: |
| 2634 | * node-set set:leading (node-set, node-set) |
| 2635 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2636 | * is called. |
| 2637 | * |
| 2638 | * Returns the nodes in @nodes that precede @node in document order, |
| 2639 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2640 | * doesn't contain @node |
| 2641 | */ |
| 2642 | xmlNodeSetPtr |
| 2643 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2644 | xmlXPathNodeSetSort(nodes); |
| 2645 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2646 | } |
| 2647 | |
| 2648 | /** |
| 2649 | * xmlXPathLeadingSorted: |
| 2650 | * @nodes1: a node-set, sorted by document order |
| 2651 | * @nodes2: a node-set, sorted by document order |
| 2652 | * |
| 2653 | * Implements the EXSLT - Sets leading() function: |
| 2654 | * node-set set:leading (node-set, node-set) |
| 2655 | * |
| 2656 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2657 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2658 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2659 | */ |
| 2660 | xmlNodeSetPtr |
| 2661 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2662 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2663 | return(nodes1); |
| 2664 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2665 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2666 | } |
| 2667 | |
| 2668 | /** |
| 2669 | * xmlXPathLeading: |
| 2670 | * @nodes1: a node-set |
| 2671 | * @nodes2: a node-set |
| 2672 | * |
| 2673 | * Implements the EXSLT - Sets leading() function: |
| 2674 | * node-set set:leading (node-set, node-set) |
| 2675 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2676 | * #exslSetsLeadingSorted is called. |
| 2677 | * |
| 2678 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2679 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2680 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2681 | */ |
| 2682 | xmlNodeSetPtr |
| 2683 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2684 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2685 | return(nodes1); |
| 2686 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2687 | return(xmlXPathNodeSetCreate(NULL)); |
| 2688 | xmlXPathNodeSetSort(nodes1); |
| 2689 | xmlXPathNodeSetSort(nodes2); |
| 2690 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2691 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2692 | } |
| 2693 | |
| 2694 | /** |
| 2695 | * xmlXPathNodeTrailingSorted: |
| 2696 | * @nodes: a node-set, sorted by document order |
| 2697 | * @node: a node |
| 2698 | * |
| 2699 | * Implements the EXSLT - Sets trailing() function: |
| 2700 | * node-set set:trailing (node-set, node-set) |
| 2701 | * |
| 2702 | * Returns the nodes in @nodes that follow @node in document order, |
| 2703 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2704 | * doesn't contain @node |
| 2705 | */ |
| 2706 | xmlNodeSetPtr |
| 2707 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2708 | int i, l; |
| 2709 | xmlNodePtr cur; |
| 2710 | xmlNodeSetPtr ret; |
| 2711 | |
| 2712 | if (node == NULL) |
| 2713 | return(nodes); |
| 2714 | |
| 2715 | ret = xmlXPathNodeSetCreate(NULL); |
| 2716 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2717 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2718 | return(ret); |
| 2719 | |
| 2720 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2721 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2722 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2723 | if (cur == node) |
| 2724 | break; |
| 2725 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2726 | } |
| 2727 | return(ret); |
| 2728 | } |
| 2729 | |
| 2730 | /** |
| 2731 | * xmlXPathNodeTrailing: |
| 2732 | * @nodes: a node-set |
| 2733 | * @node: a node |
| 2734 | * |
| 2735 | * Implements the EXSLT - Sets trailing() function: |
| 2736 | * node-set set:trailing (node-set, node-set) |
| 2737 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2738 | * is called. |
| 2739 | * |
| 2740 | * Returns the nodes in @nodes that follow @node in document order, |
| 2741 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2742 | * doesn't contain @node |
| 2743 | */ |
| 2744 | xmlNodeSetPtr |
| 2745 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2746 | xmlXPathNodeSetSort(nodes); |
| 2747 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2748 | } |
| 2749 | |
| 2750 | /** |
| 2751 | * xmlXPathTrailingSorted: |
| 2752 | * @nodes1: a node-set, sorted by document order |
| 2753 | * @nodes2: a node-set, sorted by document order |
| 2754 | * |
| 2755 | * Implements the EXSLT - Sets trailing() function: |
| 2756 | * node-set set:trailing (node-set, node-set) |
| 2757 | * |
| 2758 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2759 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2760 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2761 | */ |
| 2762 | xmlNodeSetPtr |
| 2763 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2764 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2765 | return(nodes1); |
| 2766 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2767 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2768 | } |
| 2769 | |
| 2770 | /** |
| 2771 | * xmlXPathTrailing: |
| 2772 | * @nodes1: a node-set |
| 2773 | * @nodes2: a node-set |
| 2774 | * |
| 2775 | * Implements the EXSLT - Sets trailing() function: |
| 2776 | * node-set set:trailing (node-set, node-set) |
| 2777 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2778 | * #xmlXPathTrailingSorted is called. |
| 2779 | * |
| 2780 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2781 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2782 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2783 | */ |
| 2784 | xmlNodeSetPtr |
| 2785 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2786 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2787 | return(nodes1); |
| 2788 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2789 | return(xmlXPathNodeSetCreate(NULL)); |
| 2790 | xmlXPathNodeSetSort(nodes1); |
| 2791 | xmlXPathNodeSetSort(nodes2); |
| 2792 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2793 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2794 | } |
| 2795 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2796 | /************************************************************************ |
| 2797 | * * |
| 2798 | * Routines to handle extra functions * |
| 2799 | * * |
| 2800 | ************************************************************************/ |
| 2801 | |
| 2802 | /** |
| 2803 | * xmlXPathRegisterFunc: |
| 2804 | * @ctxt: the XPath context |
| 2805 | * @name: the function name |
| 2806 | * @f: the function implementation or NULL |
| 2807 | * |
| 2808 | * Register a new function. If @f is NULL it unregisters the function |
| 2809 | * |
| 2810 | * Returns 0 in case of success, -1 in case of error |
| 2811 | */ |
| 2812 | int |
| 2813 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2814 | xmlXPathFunction f) { |
| 2815 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2816 | } |
| 2817 | |
| 2818 | /** |
| 2819 | * xmlXPathRegisterFuncNS: |
| 2820 | * @ctxt: the XPath context |
| 2821 | * @name: the function name |
| 2822 | * @ns_uri: the function namespace URI |
| 2823 | * @f: the function implementation or NULL |
| 2824 | * |
| 2825 | * Register a new function. If @f is NULL it unregisters the function |
| 2826 | * |
| 2827 | * Returns 0 in case of success, -1 in case of error |
| 2828 | */ |
| 2829 | int |
| 2830 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2831 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2832 | if (ctxt == NULL) |
| 2833 | return(-1); |
| 2834 | if (name == NULL) |
| 2835 | return(-1); |
| 2836 | |
| 2837 | if (ctxt->funcHash == NULL) |
| 2838 | ctxt->funcHash = xmlHashCreate(0); |
| 2839 | if (ctxt->funcHash == NULL) |
| 2840 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2841 | if (f == NULL) |
| 2842 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2843 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2847 | * xmlXPathRegisterFuncLookup: |
| 2848 | * @ctxt: the XPath context |
| 2849 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2850 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2851 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2852 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2853 | */ |
| 2854 | void |
| 2855 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2856 | xmlXPathFuncLookupFunc f, |
| 2857 | void *funcCtxt) { |
| 2858 | if (ctxt == NULL) |
| 2859 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2860 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2861 | ctxt->funcLookupData = funcCtxt; |
| 2862 | } |
| 2863 | |
| 2864 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2865 | * xmlXPathFunctionLookup: |
| 2866 | * @ctxt: the XPath context |
| 2867 | * @name: the function name |
| 2868 | * |
| 2869 | * Search in the Function array of the context for the given |
| 2870 | * function. |
| 2871 | * |
| 2872 | * Returns the xmlXPathFunction or NULL if not found |
| 2873 | */ |
| 2874 | xmlXPathFunction |
| 2875 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2876 | if (ctxt == NULL) |
| 2877 | return (NULL); |
| 2878 | |
| 2879 | if (ctxt->funcLookupFunc != NULL) { |
| 2880 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2881 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2882 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2883 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2884 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2885 | if (ret != NULL) |
| 2886 | return(ret); |
| 2887 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2888 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2889 | } |
| 2890 | |
| 2891 | /** |
| 2892 | * xmlXPathFunctionLookupNS: |
| 2893 | * @ctxt: the XPath context |
| 2894 | * @name: the function name |
| 2895 | * @ns_uri: the function namespace URI |
| 2896 | * |
| 2897 | * Search in the Function array of the context for the given |
| 2898 | * function. |
| 2899 | * |
| 2900 | * Returns the xmlXPathFunction or NULL if not found |
| 2901 | */ |
| 2902 | xmlXPathFunction |
| 2903 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2904 | const xmlChar *ns_uri) { |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2905 | xmlXPathFunction ret; |
| 2906 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2907 | if (ctxt == NULL) |
| 2908 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2909 | if (name == NULL) |
| 2910 | return(NULL); |
| 2911 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2912 | if (ctxt->funcLookupFunc != NULL) { |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2913 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2914 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2915 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2916 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2917 | if (ret != NULL) |
| 2918 | return(ret); |
| 2919 | } |
| 2920 | |
| 2921 | if (ctxt->funcHash == NULL) |
| 2922 | return(NULL); |
| 2923 | |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2924 | XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri); |
| 2925 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | /** |
| 2929 | * xmlXPathRegisteredFuncsCleanup: |
| 2930 | * @ctxt: the XPath context |
| 2931 | * |
| 2932 | * Cleanup the XPath context data associated to registered functions |
| 2933 | */ |
| 2934 | void |
| 2935 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2936 | if (ctxt == NULL) |
| 2937 | return; |
| 2938 | |
| 2939 | xmlHashFree(ctxt->funcHash, NULL); |
| 2940 | ctxt->funcHash = NULL; |
| 2941 | } |
| 2942 | |
| 2943 | /************************************************************************ |
| 2944 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2945 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2946 | * * |
| 2947 | ************************************************************************/ |
| 2948 | |
| 2949 | /** |
| 2950 | * xmlXPathRegisterVariable: |
| 2951 | * @ctxt: the XPath context |
| 2952 | * @name: the variable name |
| 2953 | * @value: the variable value or NULL |
| 2954 | * |
| 2955 | * Register a new variable value. If @value is NULL it unregisters |
| 2956 | * the variable |
| 2957 | * |
| 2958 | * Returns 0 in case of success, -1 in case of error |
| 2959 | */ |
| 2960 | int |
| 2961 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2962 | xmlXPathObjectPtr value) { |
| 2963 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2964 | } |
| 2965 | |
| 2966 | /** |
| 2967 | * xmlXPathRegisterVariableNS: |
| 2968 | * @ctxt: the XPath context |
| 2969 | * @name: the variable name |
| 2970 | * @ns_uri: the variable namespace URI |
| 2971 | * @value: the variable value or NULL |
| 2972 | * |
| 2973 | * Register a new variable value. If @value is NULL it unregisters |
| 2974 | * the variable |
| 2975 | * |
| 2976 | * Returns 0 in case of success, -1 in case of error |
| 2977 | */ |
| 2978 | int |
| 2979 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2980 | const xmlChar *ns_uri, |
| 2981 | xmlXPathObjectPtr value) { |
| 2982 | if (ctxt == NULL) |
| 2983 | return(-1); |
| 2984 | if (name == NULL) |
| 2985 | return(-1); |
| 2986 | |
| 2987 | if (ctxt->varHash == NULL) |
| 2988 | ctxt->varHash = xmlHashCreate(0); |
| 2989 | if (ctxt->varHash == NULL) |
| 2990 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2991 | if (value == NULL) |
| 2992 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 2993 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2994 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2995 | (void *) value, |
| 2996 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2997 | } |
| 2998 | |
| 2999 | /** |
| 3000 | * xmlXPathRegisterVariableLookup: |
| 3001 | * @ctxt: the XPath context |
| 3002 | * @f: the lookup function |
| 3003 | * @data: the lookup data |
| 3004 | * |
| 3005 | * register an external mechanism to do variable lookup |
| 3006 | */ |
| 3007 | void |
| 3008 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 3009 | xmlXPathVariableLookupFunc f, void *data) { |
| 3010 | if (ctxt == NULL) |
| 3011 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3012 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3013 | ctxt->varLookupData = data; |
| 3014 | } |
| 3015 | |
| 3016 | /** |
| 3017 | * xmlXPathVariableLookup: |
| 3018 | * @ctxt: the XPath context |
| 3019 | * @name: the variable name |
| 3020 | * |
| 3021 | * Search in the Variable array of the context for the given |
| 3022 | * variable value. |
| 3023 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3024 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3025 | */ |
| 3026 | xmlXPathObjectPtr |
| 3027 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 3028 | if (ctxt == NULL) |
| 3029 | return(NULL); |
| 3030 | |
| 3031 | if (ctxt->varLookupFunc != NULL) { |
| 3032 | xmlXPathObjectPtr ret; |
| 3033 | |
| 3034 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3035 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 3036 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | } |
| 3038 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 3039 | } |
| 3040 | |
| 3041 | /** |
| 3042 | * xmlXPathVariableLookupNS: |
| 3043 | * @ctxt: the XPath context |
| 3044 | * @name: the variable name |
| 3045 | * @ns_uri: the variable namespace URI |
| 3046 | * |
| 3047 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3048 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3049 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3050 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | */ |
| 3052 | xmlXPathObjectPtr |
| 3053 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3054 | const xmlChar *ns_uri) { |
| 3055 | if (ctxt == NULL) |
| 3056 | return(NULL); |
| 3057 | |
| 3058 | if (ctxt->varLookupFunc != NULL) { |
| 3059 | xmlXPathObjectPtr ret; |
| 3060 | |
| 3061 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3062 | (ctxt->varLookupData, name, ns_uri); |
| 3063 | if (ret != NULL) return(ret); |
| 3064 | } |
| 3065 | |
| 3066 | if (ctxt->varHash == NULL) |
| 3067 | return(NULL); |
| 3068 | if (name == NULL) |
| 3069 | return(NULL); |
| 3070 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 3071 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 3072 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | /** |
| 3076 | * xmlXPathRegisteredVariablesCleanup: |
| 3077 | * @ctxt: the XPath context |
| 3078 | * |
| 3079 | * Cleanup the XPath context data associated to registered variables |
| 3080 | */ |
| 3081 | void |
| 3082 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 3083 | if (ctxt == NULL) |
| 3084 | return; |
| 3085 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3086 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3087 | ctxt->varHash = NULL; |
| 3088 | } |
| 3089 | |
| 3090 | /** |
| 3091 | * xmlXPathRegisterNs: |
| 3092 | * @ctxt: the XPath context |
| 3093 | * @prefix: the namespace prefix |
| 3094 | * @ns_uri: the namespace name |
| 3095 | * |
| 3096 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 3097 | * the namespace |
| 3098 | * |
| 3099 | * Returns 0 in case of success, -1 in case of error |
| 3100 | */ |
| 3101 | int |
| 3102 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3103 | const xmlChar *ns_uri) { |
| 3104 | if (ctxt == NULL) |
| 3105 | return(-1); |
| 3106 | if (prefix == NULL) |
| 3107 | return(-1); |
| 3108 | |
| 3109 | if (ctxt->nsHash == NULL) |
| 3110 | ctxt->nsHash = xmlHashCreate(10); |
| 3111 | if (ctxt->nsHash == NULL) |
| 3112 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3113 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3114 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3115 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3116 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | (xmlHashDeallocator)xmlFree)); |
| 3118 | } |
| 3119 | |
| 3120 | /** |
| 3121 | * xmlXPathNsLookup: |
| 3122 | * @ctxt: the XPath context |
| 3123 | * @prefix: the namespace prefix value |
| 3124 | * |
| 3125 | * Search in the namespace declaration array of the context for the given |
| 3126 | * namespace name associated to the given prefix |
| 3127 | * |
| 3128 | * Returns the value or NULL if not found |
| 3129 | */ |
| 3130 | const xmlChar * |
| 3131 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3132 | if (ctxt == NULL) |
| 3133 | return(NULL); |
| 3134 | if (prefix == NULL) |
| 3135 | return(NULL); |
| 3136 | |
| 3137 | #ifdef XML_XML_NAMESPACE |
| 3138 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3139 | return(XML_XML_NAMESPACE); |
| 3140 | #endif |
| 3141 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3142 | if (ctxt->namespaces != NULL) { |
| 3143 | int i; |
| 3144 | |
| 3145 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3146 | if ((ctxt->namespaces[i] != NULL) && |
| 3147 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3148 | return(ctxt->namespaces[i]->href); |
| 3149 | } |
| 3150 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3151 | |
| 3152 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3153 | } |
| 3154 | |
| 3155 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3156 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3157 | * @ctxt: the XPath context |
| 3158 | * |
| 3159 | * Cleanup the XPath context data associated to registered variables |
| 3160 | */ |
| 3161 | void |
| 3162 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3163 | if (ctxt == NULL) |
| 3164 | return; |
| 3165 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3166 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3167 | ctxt->nsHash = NULL; |
| 3168 | } |
| 3169 | |
| 3170 | /************************************************************************ |
| 3171 | * * |
| 3172 | * Routines to handle Values * |
| 3173 | * * |
| 3174 | ************************************************************************/ |
| 3175 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3176 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3177 | |
| 3178 | /** |
| 3179 | * xmlXPathNewFloat: |
| 3180 | * @val: the double value |
| 3181 | * |
| 3182 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3183 | * |
| 3184 | * Returns the newly created object. |
| 3185 | */ |
| 3186 | xmlXPathObjectPtr |
| 3187 | xmlXPathNewFloat(double val) { |
| 3188 | xmlXPathObjectPtr ret; |
| 3189 | |
| 3190 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3191 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3192 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3193 | return(NULL); |
| 3194 | } |
| 3195 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3196 | ret->type = XPATH_NUMBER; |
| 3197 | ret->floatval = val; |
| 3198 | return(ret); |
| 3199 | } |
| 3200 | |
| 3201 | /** |
| 3202 | * xmlXPathNewBoolean: |
| 3203 | * @val: the boolean value |
| 3204 | * |
| 3205 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3206 | * |
| 3207 | * Returns the newly created object. |
| 3208 | */ |
| 3209 | xmlXPathObjectPtr |
| 3210 | xmlXPathNewBoolean(int val) { |
| 3211 | xmlXPathObjectPtr ret; |
| 3212 | |
| 3213 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3214 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3215 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3216 | return(NULL); |
| 3217 | } |
| 3218 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3219 | ret->type = XPATH_BOOLEAN; |
| 3220 | ret->boolval = (val != 0); |
| 3221 | return(ret); |
| 3222 | } |
| 3223 | |
| 3224 | /** |
| 3225 | * xmlXPathNewString: |
| 3226 | * @val: the xmlChar * value |
| 3227 | * |
| 3228 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3229 | * |
| 3230 | * Returns the newly created object. |
| 3231 | */ |
| 3232 | xmlXPathObjectPtr |
| 3233 | xmlXPathNewString(const xmlChar *val) { |
| 3234 | xmlXPathObjectPtr ret; |
| 3235 | |
| 3236 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3237 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3238 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3239 | return(NULL); |
| 3240 | } |
| 3241 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3242 | ret->type = XPATH_STRING; |
| 3243 | if (val != NULL) |
| 3244 | ret->stringval = xmlStrdup(val); |
| 3245 | else |
| 3246 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3247 | return(ret); |
| 3248 | } |
| 3249 | |
| 3250 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3251 | * xmlXPathWrapString: |
| 3252 | * @val: the xmlChar * value |
| 3253 | * |
| 3254 | * Wraps the @val string into an XPath object. |
| 3255 | * |
| 3256 | * Returns the newly created object. |
| 3257 | */ |
| 3258 | xmlXPathObjectPtr |
| 3259 | xmlXPathWrapString (xmlChar *val) { |
| 3260 | xmlXPathObjectPtr ret; |
| 3261 | |
| 3262 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3263 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3264 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3265 | return(NULL); |
| 3266 | } |
| 3267 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3268 | ret->type = XPATH_STRING; |
| 3269 | ret->stringval = val; |
| 3270 | return(ret); |
| 3271 | } |
| 3272 | |
| 3273 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3274 | * xmlXPathNewCString: |
| 3275 | * @val: the char * value |
| 3276 | * |
| 3277 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3278 | * |
| 3279 | * Returns the newly created object. |
| 3280 | */ |
| 3281 | xmlXPathObjectPtr |
| 3282 | xmlXPathNewCString(const char *val) { |
| 3283 | xmlXPathObjectPtr ret; |
| 3284 | |
| 3285 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3286 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3287 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3288 | return(NULL); |
| 3289 | } |
| 3290 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3291 | ret->type = XPATH_STRING; |
| 3292 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3293 | return(ret); |
| 3294 | } |
| 3295 | |
| 3296 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3297 | * xmlXPathWrapCString: |
| 3298 | * @val: the char * value |
| 3299 | * |
| 3300 | * Wraps a string into an XPath object. |
| 3301 | * |
| 3302 | * Returns the newly created object. |
| 3303 | */ |
| 3304 | xmlXPathObjectPtr |
| 3305 | xmlXPathWrapCString (char * val) { |
| 3306 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3310 | * xmlXPathWrapExternal: |
| 3311 | * @val: the user data |
| 3312 | * |
| 3313 | * Wraps the @val data into an XPath object. |
| 3314 | * |
| 3315 | * Returns the newly created object. |
| 3316 | */ |
| 3317 | xmlXPathObjectPtr |
| 3318 | xmlXPathWrapExternal (void *val) { |
| 3319 | xmlXPathObjectPtr ret; |
| 3320 | |
| 3321 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3322 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3323 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3324 | return(NULL); |
| 3325 | } |
| 3326 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3327 | ret->type = XPATH_USERS; |
| 3328 | ret->user = val; |
| 3329 | return(ret); |
| 3330 | } |
| 3331 | |
| 3332 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3333 | * xmlXPathObjectCopy: |
| 3334 | * @val: the original object |
| 3335 | * |
| 3336 | * allocate a new copy of a given object |
| 3337 | * |
| 3338 | * Returns the newly created object. |
| 3339 | */ |
| 3340 | xmlXPathObjectPtr |
| 3341 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3342 | xmlXPathObjectPtr ret; |
| 3343 | |
| 3344 | if (val == NULL) |
| 3345 | return(NULL); |
| 3346 | |
| 3347 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3348 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3349 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3350 | return(NULL); |
| 3351 | } |
| 3352 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3353 | switch (val->type) { |
| 3354 | case XPATH_BOOLEAN: |
| 3355 | case XPATH_NUMBER: |
| 3356 | case XPATH_POINT: |
| 3357 | case XPATH_RANGE: |
| 3358 | break; |
| 3359 | case XPATH_STRING: |
| 3360 | ret->stringval = xmlStrdup(val->stringval); |
| 3361 | break; |
| 3362 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3363 | #if 0 |
| 3364 | /* |
| 3365 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 3366 | this previous handling is no longer correct, and can cause some serious |
| 3367 | problems (ref. bug 145547) |
| 3368 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3369 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3370 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3371 | xmlNodePtr cur, tmp; |
| 3372 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3373 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3374 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3375 | top = xmlNewDoc(NULL); |
| 3376 | top->name = (char *) |
| 3377 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3378 | ret->user = top; |
| 3379 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3380 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3381 | cur = val->nodesetval->nodeTab[0]->children; |
| 3382 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3383 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3384 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3385 | cur = cur->next; |
| 3386 | } |
| 3387 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3388 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3389 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3390 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3391 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3392 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3393 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3394 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3395 | case XPATH_NODESET: |
| 3396 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3397 | /* Do not deallocate the copied tree value */ |
| 3398 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3399 | break; |
| 3400 | case XPATH_LOCATIONSET: |
| 3401 | #ifdef LIBXML_XPTR_ENABLED |
| 3402 | { |
| 3403 | xmlLocationSetPtr loc = val->user; |
| 3404 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3405 | break; |
| 3406 | } |
| 3407 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3408 | case XPATH_USERS: |
| 3409 | ret->user = val->user; |
| 3410 | break; |
| 3411 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3412 | xmlGenericError(xmlGenericErrorContext, |
| 3413 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3414 | val->type); |
| 3415 | break; |
| 3416 | } |
| 3417 | return(ret); |
| 3418 | } |
| 3419 | |
| 3420 | /** |
| 3421 | * xmlXPathFreeObject: |
| 3422 | * @obj: the object to free |
| 3423 | * |
| 3424 | * Free up an xmlXPathObjectPtr object. |
| 3425 | */ |
| 3426 | void |
| 3427 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3428 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3429 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3430 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3431 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3432 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3433 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3434 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3435 | } else |
| 3436 | #endif |
| 3437 | if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3438 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3439 | } else { |
| 3440 | if (obj->nodesetval != NULL) |
| 3441 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3442 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3443 | #ifdef LIBXML_XPTR_ENABLED |
| 3444 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3445 | if (obj->user != NULL) |
| 3446 | xmlXPtrFreeLocationSet(obj->user); |
| 3447 | #endif |
| 3448 | } else if (obj->type == XPATH_STRING) { |
| 3449 | if (obj->stringval != NULL) |
| 3450 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3453 | xmlFree(obj); |
| 3454 | } |
| 3455 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3456 | |
| 3457 | /************************************************************************ |
| 3458 | * * |
| 3459 | * Type Casting Routines * |
| 3460 | * * |
| 3461 | ************************************************************************/ |
| 3462 | |
| 3463 | /** |
| 3464 | * xmlXPathCastBooleanToString: |
| 3465 | * @val: a boolean |
| 3466 | * |
| 3467 | * Converts a boolean to its string value. |
| 3468 | * |
| 3469 | * Returns a newly allocated string. |
| 3470 | */ |
| 3471 | xmlChar * |
| 3472 | xmlXPathCastBooleanToString (int val) { |
| 3473 | xmlChar *ret; |
| 3474 | if (val) |
| 3475 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3476 | else |
| 3477 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3478 | return(ret); |
| 3479 | } |
| 3480 | |
| 3481 | /** |
| 3482 | * xmlXPathCastNumberToString: |
| 3483 | * @val: a number |
| 3484 | * |
| 3485 | * Converts a number to its string value. |
| 3486 | * |
| 3487 | * Returns a newly allocated string. |
| 3488 | */ |
| 3489 | xmlChar * |
| 3490 | xmlXPathCastNumberToString (double val) { |
| 3491 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3492 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3493 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3494 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3495 | break; |
| 3496 | case -1: |
| 3497 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3498 | break; |
| 3499 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3500 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3501 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3502 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3503 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3504 | } else { |
| 3505 | /* could be improved */ |
| 3506 | char buf[100]; |
| 3507 | xmlXPathFormatNumber(val, buf, 100); |
| 3508 | ret = xmlStrdup((const xmlChar *) buf); |
| 3509 | } |
| 3510 | } |
| 3511 | return(ret); |
| 3512 | } |
| 3513 | |
| 3514 | /** |
| 3515 | * xmlXPathCastNodeToString: |
| 3516 | * @node: a node |
| 3517 | * |
| 3518 | * Converts a node to its string value. |
| 3519 | * |
| 3520 | * Returns a newly allocated string. |
| 3521 | */ |
| 3522 | xmlChar * |
| 3523 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3524 | return(xmlNodeGetContent(node)); |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * xmlXPathCastNodeSetToString: |
| 3529 | * @ns: a node-set |
| 3530 | * |
| 3531 | * Converts a node-set to its string value. |
| 3532 | * |
| 3533 | * Returns a newly allocated string. |
| 3534 | */ |
| 3535 | xmlChar * |
| 3536 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3537 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3538 | return(xmlStrdup((const xmlChar *) "")); |
| 3539 | |
| 3540 | xmlXPathNodeSetSort(ns); |
| 3541 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3542 | } |
| 3543 | |
| 3544 | /** |
| 3545 | * xmlXPathCastToString: |
| 3546 | * @val: an XPath object |
| 3547 | * |
| 3548 | * Converts an existing object to its string() equivalent |
| 3549 | * |
| 3550 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3551 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3552 | * string object). |
| 3553 | */ |
| 3554 | xmlChar * |
| 3555 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3556 | xmlChar *ret = NULL; |
| 3557 | |
| 3558 | if (val == NULL) |
| 3559 | return(xmlStrdup((const xmlChar *) "")); |
| 3560 | switch (val->type) { |
| 3561 | case XPATH_UNDEFINED: |
| 3562 | #ifdef DEBUG_EXPR |
| 3563 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3564 | #endif |
| 3565 | ret = xmlStrdup((const xmlChar *) ""); |
| 3566 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3567 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3568 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3569 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3570 | break; |
| 3571 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3572 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3573 | case XPATH_BOOLEAN: |
| 3574 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3575 | break; |
| 3576 | case XPATH_NUMBER: { |
| 3577 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3578 | break; |
| 3579 | } |
| 3580 | case XPATH_USERS: |
| 3581 | case XPATH_POINT: |
| 3582 | case XPATH_RANGE: |
| 3583 | case XPATH_LOCATIONSET: |
| 3584 | TODO |
| 3585 | ret = xmlStrdup((const xmlChar *) ""); |
| 3586 | break; |
| 3587 | } |
| 3588 | return(ret); |
| 3589 | } |
| 3590 | |
| 3591 | /** |
| 3592 | * xmlXPathConvertString: |
| 3593 | * @val: an XPath object |
| 3594 | * |
| 3595 | * Converts an existing object to its string() equivalent |
| 3596 | * |
| 3597 | * Returns the new object, the old one is freed (or the operation |
| 3598 | * is done directly on @val) |
| 3599 | */ |
| 3600 | xmlXPathObjectPtr |
| 3601 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3602 | xmlChar *res = NULL; |
| 3603 | |
| 3604 | if (val == NULL) |
| 3605 | return(xmlXPathNewCString("")); |
| 3606 | |
| 3607 | switch (val->type) { |
| 3608 | case XPATH_UNDEFINED: |
| 3609 | #ifdef DEBUG_EXPR |
| 3610 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3611 | #endif |
| 3612 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3613 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3614 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3615 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3616 | break; |
| 3617 | case XPATH_STRING: |
| 3618 | return(val); |
| 3619 | case XPATH_BOOLEAN: |
| 3620 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3621 | break; |
| 3622 | case XPATH_NUMBER: |
| 3623 | res = xmlXPathCastNumberToString(val->floatval); |
| 3624 | break; |
| 3625 | case XPATH_USERS: |
| 3626 | case XPATH_POINT: |
| 3627 | case XPATH_RANGE: |
| 3628 | case XPATH_LOCATIONSET: |
| 3629 | TODO; |
| 3630 | break; |
| 3631 | } |
| 3632 | xmlXPathFreeObject(val); |
| 3633 | if (res == NULL) |
| 3634 | return(xmlXPathNewCString("")); |
| 3635 | return(xmlXPathWrapString(res)); |
| 3636 | } |
| 3637 | |
| 3638 | /** |
| 3639 | * xmlXPathCastBooleanToNumber: |
| 3640 | * @val: a boolean |
| 3641 | * |
| 3642 | * Converts a boolean to its number value |
| 3643 | * |
| 3644 | * Returns the number value |
| 3645 | */ |
| 3646 | double |
| 3647 | xmlXPathCastBooleanToNumber(int val) { |
| 3648 | if (val) |
| 3649 | return(1.0); |
| 3650 | return(0.0); |
| 3651 | } |
| 3652 | |
| 3653 | /** |
| 3654 | * xmlXPathCastStringToNumber: |
| 3655 | * @val: a string |
| 3656 | * |
| 3657 | * Converts a string to its number value |
| 3658 | * |
| 3659 | * Returns the number value |
| 3660 | */ |
| 3661 | double |
| 3662 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3663 | return(xmlXPathStringEvalNumber(val)); |
| 3664 | } |
| 3665 | |
| 3666 | /** |
| 3667 | * xmlXPathCastNodeToNumber: |
| 3668 | * @node: a node |
| 3669 | * |
| 3670 | * Converts a node to its number value |
| 3671 | * |
| 3672 | * Returns the number value |
| 3673 | */ |
| 3674 | double |
| 3675 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3676 | xmlChar *strval; |
| 3677 | double ret; |
| 3678 | |
| 3679 | if (node == NULL) |
| 3680 | return(xmlXPathNAN); |
| 3681 | strval = xmlXPathCastNodeToString(node); |
| 3682 | if (strval == NULL) |
| 3683 | return(xmlXPathNAN); |
| 3684 | ret = xmlXPathCastStringToNumber(strval); |
| 3685 | xmlFree(strval); |
| 3686 | |
| 3687 | return(ret); |
| 3688 | } |
| 3689 | |
| 3690 | /** |
| 3691 | * xmlXPathCastNodeSetToNumber: |
| 3692 | * @ns: a node-set |
| 3693 | * |
| 3694 | * Converts a node-set to its number value |
| 3695 | * |
| 3696 | * Returns the number value |
| 3697 | */ |
| 3698 | double |
| 3699 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3700 | xmlChar *str; |
| 3701 | double ret; |
| 3702 | |
| 3703 | if (ns == NULL) |
| 3704 | return(xmlXPathNAN); |
| 3705 | str = xmlXPathCastNodeSetToString(ns); |
| 3706 | ret = xmlXPathCastStringToNumber(str); |
| 3707 | xmlFree(str); |
| 3708 | return(ret); |
| 3709 | } |
| 3710 | |
| 3711 | /** |
| 3712 | * xmlXPathCastToNumber: |
| 3713 | * @val: an XPath object |
| 3714 | * |
| 3715 | * Converts an XPath object to its number value |
| 3716 | * |
| 3717 | * Returns the number value |
| 3718 | */ |
| 3719 | double |
| 3720 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3721 | double ret = 0.0; |
| 3722 | |
| 3723 | if (val == NULL) |
| 3724 | return(xmlXPathNAN); |
| 3725 | switch (val->type) { |
| 3726 | case XPATH_UNDEFINED: |
| 3727 | #ifdef DEGUB_EXPR |
| 3728 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3729 | #endif |
| 3730 | ret = xmlXPathNAN; |
| 3731 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3732 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3733 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3734 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3735 | break; |
| 3736 | case XPATH_STRING: |
| 3737 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3738 | break; |
| 3739 | case XPATH_NUMBER: |
| 3740 | ret = val->floatval; |
| 3741 | break; |
| 3742 | case XPATH_BOOLEAN: |
| 3743 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3744 | break; |
| 3745 | case XPATH_USERS: |
| 3746 | case XPATH_POINT: |
| 3747 | case XPATH_RANGE: |
| 3748 | case XPATH_LOCATIONSET: |
| 3749 | TODO; |
| 3750 | ret = xmlXPathNAN; |
| 3751 | break; |
| 3752 | } |
| 3753 | return(ret); |
| 3754 | } |
| 3755 | |
| 3756 | /** |
| 3757 | * xmlXPathConvertNumber: |
| 3758 | * @val: an XPath object |
| 3759 | * |
| 3760 | * Converts an existing object to its number() equivalent |
| 3761 | * |
| 3762 | * Returns the new object, the old one is freed (or the operation |
| 3763 | * is done directly on @val) |
| 3764 | */ |
| 3765 | xmlXPathObjectPtr |
| 3766 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3767 | xmlXPathObjectPtr ret; |
| 3768 | |
| 3769 | if (val == NULL) |
| 3770 | return(xmlXPathNewFloat(0.0)); |
| 3771 | if (val->type == XPATH_NUMBER) |
| 3772 | return(val); |
| 3773 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3774 | xmlXPathFreeObject(val); |
| 3775 | return(ret); |
| 3776 | } |
| 3777 | |
| 3778 | /** |
| 3779 | * xmlXPathCastNumberToBoolean: |
| 3780 | * @val: a number |
| 3781 | * |
| 3782 | * Converts a number to its boolean value |
| 3783 | * |
| 3784 | * Returns the boolean value |
| 3785 | */ |
| 3786 | int |
| 3787 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3788 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3789 | return(0); |
| 3790 | return(1); |
| 3791 | } |
| 3792 | |
| 3793 | /** |
| 3794 | * xmlXPathCastStringToBoolean: |
| 3795 | * @val: a string |
| 3796 | * |
| 3797 | * Converts a string to its boolean value |
| 3798 | * |
| 3799 | * Returns the boolean value |
| 3800 | */ |
| 3801 | int |
| 3802 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3803 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3804 | return(0); |
| 3805 | return(1); |
| 3806 | } |
| 3807 | |
| 3808 | /** |
| 3809 | * xmlXPathCastNodeSetToBoolean: |
| 3810 | * @ns: a node-set |
| 3811 | * |
| 3812 | * Converts a node-set to its boolean value |
| 3813 | * |
| 3814 | * Returns the boolean value |
| 3815 | */ |
| 3816 | int |
| 3817 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3818 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3819 | return(0); |
| 3820 | return(1); |
| 3821 | } |
| 3822 | |
| 3823 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3824 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3825 | * @val: an XPath object |
| 3826 | * |
| 3827 | * Converts an XPath object to its boolean value |
| 3828 | * |
| 3829 | * Returns the boolean value |
| 3830 | */ |
| 3831 | int |
| 3832 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3833 | int ret = 0; |
| 3834 | |
| 3835 | if (val == NULL) |
| 3836 | return(0); |
| 3837 | switch (val->type) { |
| 3838 | case XPATH_UNDEFINED: |
| 3839 | #ifdef DEBUG_EXPR |
| 3840 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3841 | #endif |
| 3842 | ret = 0; |
| 3843 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3844 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3845 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3846 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3847 | break; |
| 3848 | case XPATH_STRING: |
| 3849 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3850 | break; |
| 3851 | case XPATH_NUMBER: |
| 3852 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3853 | break; |
| 3854 | case XPATH_BOOLEAN: |
| 3855 | ret = val->boolval; |
| 3856 | break; |
| 3857 | case XPATH_USERS: |
| 3858 | case XPATH_POINT: |
| 3859 | case XPATH_RANGE: |
| 3860 | case XPATH_LOCATIONSET: |
| 3861 | TODO; |
| 3862 | ret = 0; |
| 3863 | break; |
| 3864 | } |
| 3865 | return(ret); |
| 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | /** |
| 3870 | * xmlXPathConvertBoolean: |
| 3871 | * @val: an XPath object |
| 3872 | * |
| 3873 | * Converts an existing object to its boolean() equivalent |
| 3874 | * |
| 3875 | * Returns the new object, the old one is freed (or the operation |
| 3876 | * is done directly on @val) |
| 3877 | */ |
| 3878 | xmlXPathObjectPtr |
| 3879 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3880 | xmlXPathObjectPtr ret; |
| 3881 | |
| 3882 | if (val == NULL) |
| 3883 | return(xmlXPathNewBoolean(0)); |
| 3884 | if (val->type == XPATH_BOOLEAN) |
| 3885 | return(val); |
| 3886 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3887 | xmlXPathFreeObject(val); |
| 3888 | return(ret); |
| 3889 | } |
| 3890 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | /************************************************************************ |
| 3892 | * * |
| 3893 | * Routines to handle XPath contexts * |
| 3894 | * * |
| 3895 | ************************************************************************/ |
| 3896 | |
| 3897 | /** |
| 3898 | * xmlXPathNewContext: |
| 3899 | * @doc: the XML document |
| 3900 | * |
| 3901 | * Create a new xmlXPathContext |
| 3902 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3903 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3904 | */ |
| 3905 | xmlXPathContextPtr |
| 3906 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3907 | xmlXPathContextPtr ret; |
| 3908 | |
| 3909 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3910 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3911 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3912 | return(NULL); |
| 3913 | } |
| 3914 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3915 | ret->doc = doc; |
| 3916 | ret->node = NULL; |
| 3917 | |
| 3918 | ret->varHash = NULL; |
| 3919 | |
| 3920 | ret->nb_types = 0; |
| 3921 | ret->max_types = 0; |
| 3922 | ret->types = NULL; |
| 3923 | |
| 3924 | ret->funcHash = xmlHashCreate(0); |
| 3925 | |
| 3926 | ret->nb_axis = 0; |
| 3927 | ret->max_axis = 0; |
| 3928 | ret->axis = NULL; |
| 3929 | |
| 3930 | ret->nsHash = NULL; |
| 3931 | ret->user = NULL; |
| 3932 | |
| 3933 | ret->contextSize = -1; |
| 3934 | ret->proximityPosition = -1; |
| 3935 | |
| 3936 | xmlXPathRegisterAllFunctions(ret); |
| 3937 | |
| 3938 | return(ret); |
| 3939 | } |
| 3940 | |
| 3941 | /** |
| 3942 | * xmlXPathFreeContext: |
| 3943 | * @ctxt: the context to free |
| 3944 | * |
| 3945 | * Free up an xmlXPathContext |
| 3946 | */ |
| 3947 | void |
| 3948 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 3949 | if (ctxt == NULL) return; |
| 3950 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3951 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3952 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3953 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 3954 | xmlResetError(&ctxt->lastError); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3955 | xmlFree(ctxt); |
| 3956 | } |
| 3957 | |
| 3958 | /************************************************************************ |
| 3959 | * * |
| 3960 | * Routines to handle XPath parser contexts * |
| 3961 | * * |
| 3962 | ************************************************************************/ |
| 3963 | |
| 3964 | #define CHECK_CTXT(ctxt) \ |
| 3965 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 3966 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 3967 | NULL, NULL, XML_FROM_XPATH, \ |
| 3968 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 3969 | __FILE__, __LINE__, \ |
| 3970 | NULL, NULL, NULL, 0, 0, \ |
| 3971 | "NULL context pointer\n"); \ |
| 3972 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3973 | } \ |
| 3974 | |
| 3975 | |
| 3976 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3977 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
| 3978 | (ctxt->doc->children == NULL)) { \ |
| 3979 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 3980 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3981 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3982 | |
| 3983 | |
| 3984 | /** |
| 3985 | * xmlXPathNewParserContext: |
| 3986 | * @str: the XPath expression |
| 3987 | * @ctxt: the XPath context |
| 3988 | * |
| 3989 | * Create a new xmlXPathParserContext |
| 3990 | * |
| 3991 | * Returns the xmlXPathParserContext just allocated. |
| 3992 | */ |
| 3993 | xmlXPathParserContextPtr |
| 3994 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3995 | xmlXPathParserContextPtr ret; |
| 3996 | |
| 3997 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3998 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3999 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4000 | return(NULL); |
| 4001 | } |
| 4002 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4003 | ret->cur = ret->base = str; |
| 4004 | ret->context = ctxt; |
| 4005 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4006 | ret->comp = xmlXPathNewCompExpr(); |
| 4007 | if (ret->comp == NULL) { |
| 4008 | xmlFree(ret->valueTab); |
| 4009 | xmlFree(ret); |
| 4010 | return(NULL); |
| 4011 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 4012 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 4013 | ret->comp->dict = ctxt->dict; |
| 4014 | xmlDictReference(ret->comp->dict); |
| 4015 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4016 | |
| 4017 | return(ret); |
| 4018 | } |
| 4019 | |
| 4020 | /** |
| 4021 | * xmlXPathCompParserContext: |
| 4022 | * @comp: the XPath compiled expression |
| 4023 | * @ctxt: the XPath context |
| 4024 | * |
| 4025 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 4026 | * |
| 4027 | * Returns the xmlXPathParserContext just allocated. |
| 4028 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4029 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4030 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 4031 | xmlXPathParserContextPtr ret; |
| 4032 | |
| 4033 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 4034 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4035 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4036 | return(NULL); |
| 4037 | } |
| 4038 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4039 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4040 | /* Allocate the value stack */ |
| 4041 | ret->valueTab = (xmlXPathObjectPtr *) |
| 4042 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4043 | if (ret->valueTab == NULL) { |
| 4044 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4045 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4046 | return(NULL); |
| 4047 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4048 | ret->valueNr = 0; |
| 4049 | ret->valueMax = 10; |
| 4050 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4051 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 4052 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4053 | ret->comp = comp; |
| 4054 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4055 | return(ret); |
| 4056 | } |
| 4057 | |
| 4058 | /** |
| 4059 | * xmlXPathFreeParserContext: |
| 4060 | * @ctxt: the context to free |
| 4061 | * |
| 4062 | * Free up an xmlXPathParserContext |
| 4063 | */ |
| 4064 | void |
| 4065 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 4066 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4067 | xmlFree(ctxt->valueTab); |
| 4068 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4069 | if (ctxt->comp != NULL) { |
| 4070 | #ifdef XPATH_STREAMING |
| 4071 | if (ctxt->comp->stream != NULL) { |
| 4072 | xmlFreePatternList(ctxt->comp->stream); |
| 4073 | ctxt->comp->stream = NULL; |
| 4074 | } |
| 4075 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4076 | xmlXPathFreeCompExpr(ctxt->comp); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4077 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4078 | xmlFree(ctxt); |
| 4079 | } |
| 4080 | |
| 4081 | /************************************************************************ |
| 4082 | * * |
| 4083 | * The implicit core function library * |
| 4084 | * * |
| 4085 | ************************************************************************/ |
| 4086 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4088 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4089 | * @node: a node pointer |
| 4090 | * |
| 4091 | * Function computing the beginning of the string value of the node, |
| 4092 | * used to speed up comparisons |
| 4093 | * |
| 4094 | * Returns an int usable as a hash |
| 4095 | */ |
| 4096 | static unsigned int |
| 4097 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 4098 | int len = 2; |
| 4099 | const xmlChar * string = NULL; |
| 4100 | xmlNodePtr tmp = NULL; |
| 4101 | unsigned int ret = 0; |
| 4102 | |
| 4103 | if (node == NULL) |
| 4104 | return(0); |
| 4105 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4106 | if (node->type == XML_DOCUMENT_NODE) { |
| 4107 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 4108 | if (tmp == NULL) |
| 4109 | node = node->children; |
| 4110 | else |
| 4111 | node = tmp; |
| 4112 | |
| 4113 | if (node == NULL) |
| 4114 | return(0); |
| 4115 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4116 | |
| 4117 | switch (node->type) { |
| 4118 | case XML_COMMENT_NODE: |
| 4119 | case XML_PI_NODE: |
| 4120 | case XML_CDATA_SECTION_NODE: |
| 4121 | case XML_TEXT_NODE: |
| 4122 | string = node->content; |
| 4123 | if (string == NULL) |
| 4124 | return(0); |
| 4125 | if (string[0] == 0) |
| 4126 | return(0); |
| 4127 | return(((unsigned int) string[0]) + |
| 4128 | (((unsigned int) string[1]) << 8)); |
| 4129 | case XML_NAMESPACE_DECL: |
| 4130 | string = ((xmlNsPtr)node)->href; |
| 4131 | if (string == NULL) |
| 4132 | return(0); |
| 4133 | if (string[0] == 0) |
| 4134 | return(0); |
| 4135 | return(((unsigned int) string[0]) + |
| 4136 | (((unsigned int) string[1]) << 8)); |
| 4137 | case XML_ATTRIBUTE_NODE: |
| 4138 | tmp = ((xmlAttrPtr) node)->children; |
| 4139 | break; |
| 4140 | case XML_ELEMENT_NODE: |
| 4141 | tmp = node->children; |
| 4142 | break; |
| 4143 | default: |
| 4144 | return(0); |
| 4145 | } |
| 4146 | while (tmp != NULL) { |
| 4147 | switch (tmp->type) { |
| 4148 | case XML_COMMENT_NODE: |
| 4149 | case XML_PI_NODE: |
| 4150 | case XML_CDATA_SECTION_NODE: |
| 4151 | case XML_TEXT_NODE: |
| 4152 | string = tmp->content; |
| 4153 | break; |
| 4154 | case XML_NAMESPACE_DECL: |
| 4155 | string = ((xmlNsPtr)tmp)->href; |
| 4156 | break; |
| 4157 | default: |
| 4158 | break; |
| 4159 | } |
| 4160 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4161 | if (len == 1) { |
| 4162 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4163 | } |
| 4164 | if (string[1] == 0) { |
| 4165 | len = 1; |
| 4166 | ret = (unsigned int) string[0]; |
| 4167 | } else { |
| 4168 | return(((unsigned int) string[0]) + |
| 4169 | (((unsigned int) string[1]) << 8)); |
| 4170 | } |
| 4171 | } |
| 4172 | /* |
| 4173 | * Skip to next node |
| 4174 | */ |
| 4175 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4176 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4177 | tmp = tmp->children; |
| 4178 | continue; |
| 4179 | } |
| 4180 | } |
| 4181 | if (tmp == node) |
| 4182 | break; |
| 4183 | |
| 4184 | if (tmp->next != NULL) { |
| 4185 | tmp = tmp->next; |
| 4186 | continue; |
| 4187 | } |
| 4188 | |
| 4189 | do { |
| 4190 | tmp = tmp->parent; |
| 4191 | if (tmp == NULL) |
| 4192 | break; |
| 4193 | if (tmp == node) { |
| 4194 | tmp = NULL; |
| 4195 | break; |
| 4196 | } |
| 4197 | if (tmp->next != NULL) { |
| 4198 | tmp = tmp->next; |
| 4199 | break; |
| 4200 | } |
| 4201 | } while (tmp != NULL); |
| 4202 | } |
| 4203 | return(ret); |
| 4204 | } |
| 4205 | |
| 4206 | /** |
| 4207 | * xmlXPathStringHash: |
| 4208 | * @string: a string |
| 4209 | * |
| 4210 | * Function computing the beginning of the string value of the node, |
| 4211 | * used to speed up comparisons |
| 4212 | * |
| 4213 | * Returns an int usable as a hash |
| 4214 | */ |
| 4215 | static unsigned int |
| 4216 | xmlXPathStringHash(const xmlChar * string) { |
| 4217 | if (string == NULL) |
| 4218 | return((unsigned int) 0); |
| 4219 | if (string[0] == 0) |
| 4220 | return(0); |
| 4221 | return(((unsigned int) string[0]) + |
| 4222 | (((unsigned int) string[1]) << 8)); |
| 4223 | } |
| 4224 | |
| 4225 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4226 | * xmlXPathCompareNodeSetFloat: |
| 4227 | * @ctxt: the XPath Parser context |
| 4228 | * @inf: less than (1) or greater than (0) |
| 4229 | * @strict: is the comparison strict |
| 4230 | * @arg: the node set |
| 4231 | * @f: the value |
| 4232 | * |
| 4233 | * Implement the compare operation between a nodeset and a number |
| 4234 | * @ns < @val (1, 1, ... |
| 4235 | * @ns <= @val (1, 0, ... |
| 4236 | * @ns > @val (0, 1, ... |
| 4237 | * @ns >= @val (0, 0, ... |
| 4238 | * |
| 4239 | * If one object to be compared is a node-set and the other is a number, |
| 4240 | * then the comparison will be true if and only if there is a node in the |
| 4241 | * node-set such that the result of performing the comparison on the number |
| 4242 | * to be compared and on the result of converting the string-value of that |
| 4243 | * node to a number using the number function is true. |
| 4244 | * |
| 4245 | * Returns 0 or 1 depending on the results of the test. |
| 4246 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4247 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4248 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4249 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4250 | int i, ret = 0; |
| 4251 | xmlNodeSetPtr ns; |
| 4252 | xmlChar *str2; |
| 4253 | |
| 4254 | if ((f == NULL) || (arg == NULL) || |
| 4255 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4256 | xmlXPathFreeObject(arg); |
| 4257 | xmlXPathFreeObject(f); |
| 4258 | return(0); |
| 4259 | } |
| 4260 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4261 | if (ns != NULL) { |
| 4262 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4263 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4264 | if (str2 != NULL) { |
| 4265 | valuePush(ctxt, |
| 4266 | xmlXPathNewString(str2)); |
| 4267 | xmlFree(str2); |
| 4268 | xmlXPathNumberFunction(ctxt, 1); |
| 4269 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4270 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4271 | if (ret) |
| 4272 | break; |
| 4273 | } |
| 4274 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | } |
| 4276 | xmlXPathFreeObject(arg); |
| 4277 | xmlXPathFreeObject(f); |
| 4278 | return(ret); |
| 4279 | } |
| 4280 | |
| 4281 | /** |
| 4282 | * xmlXPathCompareNodeSetString: |
| 4283 | * @ctxt: the XPath Parser context |
| 4284 | * @inf: less than (1) or greater than (0) |
| 4285 | * @strict: is the comparison strict |
| 4286 | * @arg: the node set |
| 4287 | * @s: the value |
| 4288 | * |
| 4289 | * Implement the compare operation between a nodeset and a string |
| 4290 | * @ns < @val (1, 1, ... |
| 4291 | * @ns <= @val (1, 0, ... |
| 4292 | * @ns > @val (0, 1, ... |
| 4293 | * @ns >= @val (0, 0, ... |
| 4294 | * |
| 4295 | * If one object to be compared is a node-set and the other is a string, |
| 4296 | * then the comparison will be true if and only if there is a node in |
| 4297 | * the node-set such that the result of performing the comparison on the |
| 4298 | * string-value of the node and the other string is true. |
| 4299 | * |
| 4300 | * Returns 0 or 1 depending on the results of the test. |
| 4301 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4302 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4303 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4304 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4305 | int i, ret = 0; |
| 4306 | xmlNodeSetPtr ns; |
| 4307 | xmlChar *str2; |
| 4308 | |
| 4309 | if ((s == NULL) || (arg == NULL) || |
| 4310 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4311 | xmlXPathFreeObject(arg); |
| 4312 | xmlXPathFreeObject(s); |
| 4313 | return(0); |
| 4314 | } |
| 4315 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4316 | if (ns != NULL) { |
| 4317 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4318 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4319 | if (str2 != NULL) { |
| 4320 | valuePush(ctxt, |
| 4321 | xmlXPathNewString(str2)); |
| 4322 | xmlFree(str2); |
| 4323 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4324 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4325 | if (ret) |
| 4326 | break; |
| 4327 | } |
| 4328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4329 | } |
| 4330 | xmlXPathFreeObject(arg); |
| 4331 | xmlXPathFreeObject(s); |
| 4332 | return(ret); |
| 4333 | } |
| 4334 | |
| 4335 | /** |
| 4336 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4337 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4338 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4339 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4340 | * @arg2: the second node set object |
| 4341 | * |
| 4342 | * Implement the compare operation on nodesets: |
| 4343 | * |
| 4344 | * If both objects to be compared are node-sets, then the comparison |
| 4345 | * will be true if and only if there is a node in the first node-set |
| 4346 | * and a node in the second node-set such that the result of performing |
| 4347 | * the comparison on the string-values of the two nodes is true. |
| 4348 | * .... |
| 4349 | * When neither object to be compared is a node-set and the operator |
| 4350 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4351 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4352 | * .... |
| 4353 | * The number function converts its argument to a number as follows: |
| 4354 | * - a string that consists of optional whitespace followed by an |
| 4355 | * optional minus sign followed by a Number followed by whitespace |
| 4356 | * is converted to the IEEE 754 number that is nearest (according |
| 4357 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4358 | * represented by the string; any other string is converted to NaN |
| 4359 | * |
| 4360 | * Conclusion all nodes need to be converted first to their string value |
| 4361 | * and then the comparison must be done when possible |
| 4362 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4363 | static int |
| 4364 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4365 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4366 | int i, j, init = 0; |
| 4367 | double val1; |
| 4368 | double *values2; |
| 4369 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | xmlNodeSetPtr ns1; |
| 4371 | xmlNodeSetPtr ns2; |
| 4372 | |
| 4373 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4374 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4375 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4377 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4378 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4379 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4380 | xmlXPathFreeObject(arg1); |
| 4381 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4382 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4383 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4384 | |
| 4385 | ns1 = arg1->nodesetval; |
| 4386 | ns2 = arg2->nodesetval; |
| 4387 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4388 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4389 | xmlXPathFreeObject(arg1); |
| 4390 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4391 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4392 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4393 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4394 | xmlXPathFreeObject(arg1); |
| 4395 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4396 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4397 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4398 | |
| 4399 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4400 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4401 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4402 | xmlXPathFreeObject(arg1); |
| 4403 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4404 | return(0); |
| 4405 | } |
| 4406 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4407 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4408 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4409 | continue; |
| 4410 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4411 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4412 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4413 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4414 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4415 | continue; |
| 4416 | if (inf && strict) |
| 4417 | ret = (val1 < values2[j]); |
| 4418 | else if (inf && !strict) |
| 4419 | ret = (val1 <= values2[j]); |
| 4420 | else if (!inf && strict) |
| 4421 | ret = (val1 > values2[j]); |
| 4422 | else if (!inf && !strict) |
| 4423 | ret = (val1 >= values2[j]); |
| 4424 | if (ret) |
| 4425 | break; |
| 4426 | } |
| 4427 | if (ret) |
| 4428 | break; |
| 4429 | init = 1; |
| 4430 | } |
| 4431 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4432 | xmlXPathFreeObject(arg1); |
| 4433 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4434 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | /** |
| 4438 | * xmlXPathCompareNodeSetValue: |
| 4439 | * @ctxt: the XPath Parser context |
| 4440 | * @inf: less than (1) or greater than (0) |
| 4441 | * @strict: is the comparison strict |
| 4442 | * @arg: the node set |
| 4443 | * @val: the value |
| 4444 | * |
| 4445 | * Implement the compare operation between a nodeset and a value |
| 4446 | * @ns < @val (1, 1, ... |
| 4447 | * @ns <= @val (1, 0, ... |
| 4448 | * @ns > @val (0, 1, ... |
| 4449 | * @ns >= @val (0, 0, ... |
| 4450 | * |
| 4451 | * If one object to be compared is a node-set and the other is a boolean, |
| 4452 | * then the comparison will be true if and only if the result of performing |
| 4453 | * the comparison on the boolean and on the result of converting |
| 4454 | * the node-set to a boolean using the boolean function is true. |
| 4455 | * |
| 4456 | * Returns 0 or 1 depending on the results of the test. |
| 4457 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4458 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4459 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4460 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4461 | if ((val == NULL) || (arg == NULL) || |
| 4462 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4463 | return(0); |
| 4464 | |
| 4465 | switch(val->type) { |
| 4466 | case XPATH_NUMBER: |
| 4467 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4468 | case XPATH_NODESET: |
| 4469 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4470 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4471 | case XPATH_STRING: |
| 4472 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4473 | case XPATH_BOOLEAN: |
| 4474 | valuePush(ctxt, arg); |
| 4475 | xmlXPathBooleanFunction(ctxt, 1); |
| 4476 | valuePush(ctxt, val); |
| 4477 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4478 | default: |
| 4479 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4480 | } |
| 4481 | return(0); |
| 4482 | } |
| 4483 | |
| 4484 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4485 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4486 | * @arg: the nodeset object argument |
| 4487 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4488 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4489 | * |
| 4490 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4491 | * If one object to be compared is a node-set and the other is a string, |
| 4492 | * then the comparison will be true if and only if there is a node in |
| 4493 | * the node-set such that the result of performing the comparison on the |
| 4494 | * string-value of the node and the other string is true. |
| 4495 | * |
| 4496 | * Returns 0 or 1 depending on the results of the test. |
| 4497 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4498 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4499 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4500 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4501 | int i; |
| 4502 | xmlNodeSetPtr ns; |
| 4503 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4504 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4505 | |
| 4506 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4507 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4508 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4509 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4510 | /* |
| 4511 | * A NULL nodeset compared with a string is always false |
| 4512 | * (since there is no node equal, and no node not equal) |
| 4513 | */ |
| 4514 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4515 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4516 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4517 | for (i = 0; i < ns->nodeNr; i++) { |
| 4518 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4519 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4520 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4521 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4522 | if (neq) |
| 4523 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4524 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4525 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4526 | if (neq) |
| 4527 | continue; |
| 4528 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4529 | } else if (neq) { |
| 4530 | if (str2 != NULL) |
| 4531 | xmlFree(str2); |
| 4532 | return (1); |
| 4533 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4534 | if (str2 != NULL) |
| 4535 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4536 | } else if (neq) |
| 4537 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4539 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4543 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4544 | * @arg: the nodeset object argument |
| 4545 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4546 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4547 | * |
| 4548 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4549 | * If one object to be compared is a node-set and the other is a number, |
| 4550 | * then the comparison will be true if and only if there is a node in |
| 4551 | * the node-set such that the result of performing the comparison on the |
| 4552 | * number to be compared and on the result of converting the string-value |
| 4553 | * of that node to a number using the number function is true. |
| 4554 | * |
| 4555 | * Returns 0 or 1 depending on the results of the test. |
| 4556 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4557 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4558 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4559 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4560 | int i, ret=0; |
| 4561 | xmlNodeSetPtr ns; |
| 4562 | xmlChar *str2; |
| 4563 | xmlXPathObjectPtr val; |
| 4564 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4565 | |
| 4566 | if ((arg == NULL) || |
| 4567 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4568 | return(0); |
| 4569 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4570 | ns = arg->nodesetval; |
| 4571 | if (ns != NULL) { |
| 4572 | for (i=0;i<ns->nodeNr;i++) { |
| 4573 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4574 | if (str2 != NULL) { |
| 4575 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4576 | xmlFree(str2); |
| 4577 | xmlXPathNumberFunction(ctxt, 1); |
| 4578 | val = valuePop(ctxt); |
| 4579 | v = val->floatval; |
| 4580 | xmlXPathFreeObject(val); |
| 4581 | if (!xmlXPathIsNaN(v)) { |
| 4582 | if ((!neq) && (v==f)) { |
| 4583 | ret = 1; |
| 4584 | break; |
| 4585 | } else if ((neq) && (v!=f)) { |
| 4586 | ret = 1; |
| 4587 | break; |
| 4588 | } |
William M. Brack | 32f0f71 | 2005-07-14 07:00:33 +0000 | [diff] [blame] | 4589 | } else { /* NaN is unequal to any value */ |
| 4590 | if (neq) |
| 4591 | ret = 1; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4592 | } |
| 4593 | } |
| 4594 | } |
| 4595 | } |
| 4596 | |
| 4597 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | |
| 4601 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4602 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4603 | * @arg1: first nodeset object argument |
| 4604 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4605 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4606 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4607 | * Implement the equal / not equal operation on XPath nodesets: |
| 4608 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4609 | * If both objects to be compared are node-sets, then the comparison |
| 4610 | * will be true if and only if there is a node in the first node-set and |
| 4611 | * a node in the second node-set such that the result of performing the |
| 4612 | * comparison on the string-values of the two nodes is true. |
| 4613 | * |
| 4614 | * (needless to say, this is a costly operation) |
| 4615 | * |
| 4616 | * Returns 0 or 1 depending on the results of the test. |
| 4617 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4618 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4619 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4620 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4621 | unsigned int *hashs1; |
| 4622 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4623 | xmlChar **values1; |
| 4624 | xmlChar **values2; |
| 4625 | int ret = 0; |
| 4626 | xmlNodeSetPtr ns1; |
| 4627 | xmlNodeSetPtr ns2; |
| 4628 | |
| 4629 | if ((arg1 == NULL) || |
| 4630 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4631 | return(0); |
| 4632 | if ((arg2 == NULL) || |
| 4633 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4634 | return(0); |
| 4635 | |
| 4636 | ns1 = arg1->nodesetval; |
| 4637 | ns2 = arg2->nodesetval; |
| 4638 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4639 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4641 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4642 | return(0); |
| 4643 | |
| 4644 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4645 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4646 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4647 | if (neq == 0) |
| 4648 | for (i = 0;i < ns1->nodeNr;i++) |
| 4649 | for (j = 0;j < ns2->nodeNr;j++) |
| 4650 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4651 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4652 | |
| 4653 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4654 | if (values1 == NULL) { |
| 4655 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4656 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4657 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4658 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4659 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4660 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4661 | xmlFree(values1); |
| 4662 | return(0); |
| 4663 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4664 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4665 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4666 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4667 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4668 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4669 | xmlFree(values1); |
| 4670 | return(0); |
| 4671 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4672 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4673 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4674 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4675 | xmlFree(hashs1); |
| 4676 | xmlFree(values1); |
| 4677 | xmlFree(values2); |
| 4678 | return(0); |
| 4679 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4680 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4681 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4682 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4683 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4684 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4685 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4686 | if (hashs1[i] != hashs2[j]) { |
| 4687 | if (neq) { |
| 4688 | ret = 1; |
| 4689 | break; |
| 4690 | } |
| 4691 | } |
| 4692 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4693 | if (values1[i] == NULL) |
| 4694 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4695 | if (values2[j] == NULL) |
| 4696 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4697 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4698 | if (ret) |
| 4699 | break; |
| 4700 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | } |
| 4702 | if (ret) |
| 4703 | break; |
| 4704 | } |
| 4705 | for (i = 0;i < ns1->nodeNr;i++) |
| 4706 | if (values1[i] != NULL) |
| 4707 | xmlFree(values1[i]); |
| 4708 | for (j = 0;j < ns2->nodeNr;j++) |
| 4709 | if (values2[j] != NULL) |
| 4710 | xmlFree(values2[j]); |
| 4711 | xmlFree(values1); |
| 4712 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4713 | xmlFree(hashs1); |
| 4714 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4715 | return(ret); |
| 4716 | } |
| 4717 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4718 | static int |
| 4719 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 4720 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4721 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4722 | /* |
| 4723 | *At this point we are assured neither arg1 nor arg2 |
| 4724 | *is a nodeset, so we can just pick the appropriate routine. |
| 4725 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4726 | switch (arg1->type) { |
| 4727 | case XPATH_UNDEFINED: |
| 4728 | #ifdef DEBUG_EXPR |
| 4729 | xmlGenericError(xmlGenericErrorContext, |
| 4730 | "Equal: undefined\n"); |
| 4731 | #endif |
| 4732 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4733 | case XPATH_BOOLEAN: |
| 4734 | switch (arg2->type) { |
| 4735 | case XPATH_UNDEFINED: |
| 4736 | #ifdef DEBUG_EXPR |
| 4737 | xmlGenericError(xmlGenericErrorContext, |
| 4738 | "Equal: undefined\n"); |
| 4739 | #endif |
| 4740 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4741 | case XPATH_BOOLEAN: |
| 4742 | #ifdef DEBUG_EXPR |
| 4743 | xmlGenericError(xmlGenericErrorContext, |
| 4744 | "Equal: %d boolean %d \n", |
| 4745 | arg1->boolval, arg2->boolval); |
| 4746 | #endif |
| 4747 | ret = (arg1->boolval == arg2->boolval); |
| 4748 | break; |
| 4749 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4750 | ret = (arg1->boolval == |
| 4751 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4752 | break; |
| 4753 | case XPATH_STRING: |
| 4754 | if ((arg2->stringval == NULL) || |
| 4755 | (arg2->stringval[0] == 0)) ret = 0; |
| 4756 | else |
| 4757 | ret = 1; |
| 4758 | ret = (arg1->boolval == ret); |
| 4759 | break; |
| 4760 | case XPATH_USERS: |
| 4761 | case XPATH_POINT: |
| 4762 | case XPATH_RANGE: |
| 4763 | case XPATH_LOCATIONSET: |
| 4764 | TODO |
| 4765 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4766 | case XPATH_NODESET: |
| 4767 | case XPATH_XSLT_TREE: |
| 4768 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4769 | } |
| 4770 | break; |
| 4771 | case XPATH_NUMBER: |
| 4772 | switch (arg2->type) { |
| 4773 | case XPATH_UNDEFINED: |
| 4774 | #ifdef DEBUG_EXPR |
| 4775 | xmlGenericError(xmlGenericErrorContext, |
| 4776 | "Equal: undefined\n"); |
| 4777 | #endif |
| 4778 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4780 | ret = (arg2->boolval== |
| 4781 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4782 | break; |
| 4783 | case XPATH_STRING: |
| 4784 | valuePush(ctxt, arg2); |
| 4785 | xmlXPathNumberFunction(ctxt, 1); |
| 4786 | arg2 = valuePop(ctxt); |
| 4787 | /* no break on purpose */ |
| 4788 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4789 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4790 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4791 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4792 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4793 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4794 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4795 | ret = 1; |
| 4796 | else |
| 4797 | ret = 0; |
| 4798 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4799 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4800 | ret = 1; |
| 4801 | else |
| 4802 | ret = 0; |
| 4803 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4804 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4805 | ret = 1; |
| 4806 | else |
| 4807 | ret = 0; |
| 4808 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4809 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4810 | ret = 1; |
| 4811 | else |
| 4812 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4813 | } else { |
| 4814 | ret = (arg1->floatval == arg2->floatval); |
| 4815 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4816 | break; |
| 4817 | case XPATH_USERS: |
| 4818 | case XPATH_POINT: |
| 4819 | case XPATH_RANGE: |
| 4820 | case XPATH_LOCATIONSET: |
| 4821 | TODO |
| 4822 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4823 | case XPATH_NODESET: |
| 4824 | case XPATH_XSLT_TREE: |
| 4825 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4826 | } |
| 4827 | break; |
| 4828 | case XPATH_STRING: |
| 4829 | switch (arg2->type) { |
| 4830 | case XPATH_UNDEFINED: |
| 4831 | #ifdef DEBUG_EXPR |
| 4832 | xmlGenericError(xmlGenericErrorContext, |
| 4833 | "Equal: undefined\n"); |
| 4834 | #endif |
| 4835 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4836 | case XPATH_BOOLEAN: |
| 4837 | if ((arg1->stringval == NULL) || |
| 4838 | (arg1->stringval[0] == 0)) ret = 0; |
| 4839 | else |
| 4840 | ret = 1; |
| 4841 | ret = (arg2->boolval == ret); |
| 4842 | break; |
| 4843 | case XPATH_STRING: |
| 4844 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4845 | break; |
| 4846 | case XPATH_NUMBER: |
| 4847 | valuePush(ctxt, arg1); |
| 4848 | xmlXPathNumberFunction(ctxt, 1); |
| 4849 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4850 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4851 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4852 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4853 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4854 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4855 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4856 | ret = 1; |
| 4857 | else |
| 4858 | ret = 0; |
| 4859 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4860 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4861 | ret = 1; |
| 4862 | else |
| 4863 | ret = 0; |
| 4864 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4865 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4866 | ret = 1; |
| 4867 | else |
| 4868 | ret = 0; |
| 4869 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4870 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4871 | ret = 1; |
| 4872 | else |
| 4873 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4874 | } else { |
| 4875 | ret = (arg1->floatval == arg2->floatval); |
| 4876 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | case XPATH_USERS: |
| 4879 | case XPATH_POINT: |
| 4880 | case XPATH_RANGE: |
| 4881 | case XPATH_LOCATIONSET: |
| 4882 | TODO |
| 4883 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4884 | case XPATH_NODESET: |
| 4885 | case XPATH_XSLT_TREE: |
| 4886 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4887 | } |
| 4888 | break; |
| 4889 | case XPATH_USERS: |
| 4890 | case XPATH_POINT: |
| 4891 | case XPATH_RANGE: |
| 4892 | case XPATH_LOCATIONSET: |
| 4893 | TODO |
| 4894 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4895 | case XPATH_NODESET: |
| 4896 | case XPATH_XSLT_TREE: |
| 4897 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4898 | } |
| 4899 | xmlXPathFreeObject(arg1); |
| 4900 | xmlXPathFreeObject(arg2); |
| 4901 | return(ret); |
| 4902 | } |
| 4903 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4904 | /** |
| 4905 | * xmlXPathEqualValues: |
| 4906 | * @ctxt: the XPath Parser context |
| 4907 | * |
| 4908 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4909 | * |
| 4910 | * Returns 0 or 1 depending on the results of the test. |
| 4911 | */ |
| 4912 | int |
| 4913 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4914 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4915 | int ret = 0; |
| 4916 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 4917 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4918 | arg2 = valuePop(ctxt); |
| 4919 | arg1 = valuePop(ctxt); |
| 4920 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4921 | if (arg1 != NULL) |
| 4922 | xmlXPathFreeObject(arg1); |
| 4923 | else |
| 4924 | xmlXPathFreeObject(arg2); |
| 4925 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4926 | } |
| 4927 | |
| 4928 | if (arg1 == arg2) { |
| 4929 | #ifdef DEBUG_EXPR |
| 4930 | xmlGenericError(xmlGenericErrorContext, |
| 4931 | "Equal: by pointer\n"); |
| 4932 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 4933 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4934 | return(1); |
| 4935 | } |
| 4936 | |
| 4937 | /* |
| 4938 | *If either argument is a nodeset, it's a 'special case' |
| 4939 | */ |
| 4940 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4941 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4942 | /* |
| 4943 | *Hack it to assure arg1 is the nodeset |
| 4944 | */ |
| 4945 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4946 | argtmp = arg2; |
| 4947 | arg2 = arg1; |
| 4948 | arg1 = argtmp; |
| 4949 | } |
| 4950 | switch (arg2->type) { |
| 4951 | case XPATH_UNDEFINED: |
| 4952 | #ifdef DEBUG_EXPR |
| 4953 | xmlGenericError(xmlGenericErrorContext, |
| 4954 | "Equal: undefined\n"); |
| 4955 | #endif |
| 4956 | break; |
| 4957 | case XPATH_NODESET: |
| 4958 | case XPATH_XSLT_TREE: |
| 4959 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 4960 | break; |
| 4961 | case XPATH_BOOLEAN: |
| 4962 | if ((arg1->nodesetval == NULL) || |
| 4963 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4964 | else |
| 4965 | ret = 1; |
| 4966 | ret = (ret == arg2->boolval); |
| 4967 | break; |
| 4968 | case XPATH_NUMBER: |
| 4969 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 4970 | break; |
| 4971 | case XPATH_STRING: |
| 4972 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 4973 | break; |
| 4974 | case XPATH_USERS: |
| 4975 | case XPATH_POINT: |
| 4976 | case XPATH_RANGE: |
| 4977 | case XPATH_LOCATIONSET: |
| 4978 | TODO |
| 4979 | break; |
| 4980 | } |
| 4981 | xmlXPathFreeObject(arg1); |
| 4982 | xmlXPathFreeObject(arg2); |
| 4983 | return(ret); |
| 4984 | } |
| 4985 | |
| 4986 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4987 | } |
| 4988 | |
| 4989 | /** |
| 4990 | * xmlXPathNotEqualValues: |
| 4991 | * @ctxt: the XPath Parser context |
| 4992 | * |
| 4993 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4994 | * |
| 4995 | * Returns 0 or 1 depending on the results of the test. |
| 4996 | */ |
| 4997 | int |
| 4998 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4999 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 5000 | int ret = 0; |
| 5001 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5002 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5003 | arg2 = valuePop(ctxt); |
| 5004 | arg1 = valuePop(ctxt); |
| 5005 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5006 | if (arg1 != NULL) |
| 5007 | xmlXPathFreeObject(arg1); |
| 5008 | else |
| 5009 | xmlXPathFreeObject(arg2); |
| 5010 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5011 | } |
| 5012 | |
| 5013 | if (arg1 == arg2) { |
| 5014 | #ifdef DEBUG_EXPR |
| 5015 | xmlGenericError(xmlGenericErrorContext, |
| 5016 | "NotEqual: by pointer\n"); |
| 5017 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 5018 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5019 | return(0); |
| 5020 | } |
| 5021 | |
| 5022 | /* |
| 5023 | *If either argument is a nodeset, it's a 'special case' |
| 5024 | */ |
| 5025 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5026 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5027 | /* |
| 5028 | *Hack it to assure arg1 is the nodeset |
| 5029 | */ |
| 5030 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 5031 | argtmp = arg2; |
| 5032 | arg2 = arg1; |
| 5033 | arg1 = argtmp; |
| 5034 | } |
| 5035 | switch (arg2->type) { |
| 5036 | case XPATH_UNDEFINED: |
| 5037 | #ifdef DEBUG_EXPR |
| 5038 | xmlGenericError(xmlGenericErrorContext, |
| 5039 | "NotEqual: undefined\n"); |
| 5040 | #endif |
| 5041 | break; |
| 5042 | case XPATH_NODESET: |
| 5043 | case XPATH_XSLT_TREE: |
| 5044 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 5045 | break; |
| 5046 | case XPATH_BOOLEAN: |
| 5047 | if ((arg1->nodesetval == NULL) || |
| 5048 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 5049 | else |
| 5050 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5051 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5052 | break; |
| 5053 | case XPATH_NUMBER: |
| 5054 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 5055 | break; |
| 5056 | case XPATH_STRING: |
| 5057 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 5058 | break; |
| 5059 | case XPATH_USERS: |
| 5060 | case XPATH_POINT: |
| 5061 | case XPATH_RANGE: |
| 5062 | case XPATH_LOCATIONSET: |
| 5063 | TODO |
| 5064 | break; |
| 5065 | } |
| 5066 | xmlXPathFreeObject(arg1); |
| 5067 | xmlXPathFreeObject(arg2); |
| 5068 | return(ret); |
| 5069 | } |
| 5070 | |
| 5071 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5072 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5073 | |
| 5074 | /** |
| 5075 | * xmlXPathCompareValues: |
| 5076 | * @ctxt: the XPath Parser context |
| 5077 | * @inf: less than (1) or greater than (0) |
| 5078 | * @strict: is the comparison strict |
| 5079 | * |
| 5080 | * Implement the compare operation on XPath objects: |
| 5081 | * @arg1 < @arg2 (1, 1, ... |
| 5082 | * @arg1 <= @arg2 (1, 0, ... |
| 5083 | * @arg1 > @arg2 (0, 1, ... |
| 5084 | * @arg1 >= @arg2 (0, 0, ... |
| 5085 | * |
| 5086 | * When neither object to be compared is a node-set and the operator is |
| 5087 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 5088 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 5089 | * comparison will be true if and only if the first number is less than the |
| 5090 | * second number. The <= comparison will be true if and only if the first |
| 5091 | * number is less than or equal to the second number. The > comparison |
| 5092 | * will be true if and only if the first number is greater than the second |
| 5093 | * number. The >= comparison will be true if and only if the first number |
| 5094 | * is greater than or equal to the second number. |
| 5095 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5096 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5097 | */ |
| 5098 | int |
| 5099 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5100 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5101 | xmlXPathObjectPtr arg1, arg2; |
| 5102 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5103 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5104 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5106 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5107 | if (arg1 != NULL) |
| 5108 | xmlXPathFreeObject(arg1); |
| 5109 | else |
| 5110 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5111 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5112 | } |
| 5113 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5114 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5115 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 5116 | /* |
| 5117 | * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments |
| 5118 | * are not freed from within this routine; they will be freed from the |
| 5119 | * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue |
| 5120 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5121 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 5122 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5123 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5124 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5125 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5126 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 5127 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5129 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 5130 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5131 | } |
| 5132 | } |
| 5133 | return(ret); |
| 5134 | } |
| 5135 | |
| 5136 | if (arg1->type != XPATH_NUMBER) { |
| 5137 | valuePush(ctxt, arg1); |
| 5138 | xmlXPathNumberFunction(ctxt, 1); |
| 5139 | arg1 = valuePop(ctxt); |
| 5140 | } |
| 5141 | if (arg1->type != XPATH_NUMBER) { |
| 5142 | xmlXPathFreeObject(arg1); |
| 5143 | xmlXPathFreeObject(arg2); |
| 5144 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5145 | } |
| 5146 | if (arg2->type != XPATH_NUMBER) { |
| 5147 | valuePush(ctxt, arg2); |
| 5148 | xmlXPathNumberFunction(ctxt, 1); |
| 5149 | arg2 = valuePop(ctxt); |
| 5150 | } |
| 5151 | if (arg2->type != XPATH_NUMBER) { |
| 5152 | xmlXPathFreeObject(arg1); |
| 5153 | xmlXPathFreeObject(arg2); |
| 5154 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5155 | } |
| 5156 | /* |
| 5157 | * Add tests for infinity and nan |
| 5158 | * => feedback on 3.4 for Inf and NaN |
| 5159 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5160 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5161 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5162 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5163 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5164 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5165 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5166 | if (inf && strict) { |
| 5167 | if ((arg1i == -1 && arg2i != -1) || |
| 5168 | (arg2i == 1 && arg1i != 1)) { |
| 5169 | ret = 1; |
| 5170 | } else if (arg1i == 0 && arg2i == 0) { |
| 5171 | ret = (arg1->floatval < arg2->floatval); |
| 5172 | } else { |
| 5173 | ret = 0; |
| 5174 | } |
| 5175 | } |
| 5176 | else if (inf && !strict) { |
| 5177 | if (arg1i == -1 || arg2i == 1) { |
| 5178 | ret = 1; |
| 5179 | } else if (arg1i == 0 && arg2i == 0) { |
| 5180 | ret = (arg1->floatval <= arg2->floatval); |
| 5181 | } else { |
| 5182 | ret = 0; |
| 5183 | } |
| 5184 | } |
| 5185 | else if (!inf && strict) { |
| 5186 | if ((arg1i == 1 && arg2i != 1) || |
| 5187 | (arg2i == -1 && arg1i != -1)) { |
| 5188 | ret = 1; |
| 5189 | } else if (arg1i == 0 && arg2i == 0) { |
| 5190 | ret = (arg1->floatval > arg2->floatval); |
| 5191 | } else { |
| 5192 | ret = 0; |
| 5193 | } |
| 5194 | } |
| 5195 | else if (!inf && !strict) { |
| 5196 | if (arg1i == 1 || arg2i == -1) { |
| 5197 | ret = 1; |
| 5198 | } else if (arg1i == 0 && arg2i == 0) { |
| 5199 | ret = (arg1->floatval >= arg2->floatval); |
| 5200 | } else { |
| 5201 | ret = 0; |
| 5202 | } |
| 5203 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5204 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5205 | xmlXPathFreeObject(arg1); |
| 5206 | xmlXPathFreeObject(arg2); |
| 5207 | return(ret); |
| 5208 | } |
| 5209 | |
| 5210 | /** |
| 5211 | * xmlXPathValueFlipSign: |
| 5212 | * @ctxt: the XPath Parser context |
| 5213 | * |
| 5214 | * Implement the unary - operation on an XPath object |
| 5215 | * The numeric operators convert their operands to numbers as if |
| 5216 | * by calling the number function. |
| 5217 | */ |
| 5218 | void |
| 5219 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5220 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5221 | CAST_TO_NUMBER; |
| 5222 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5223 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5224 | ctxt->value->floatval=xmlXPathNAN; |
| 5225 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5226 | ctxt->value->floatval=xmlXPathNINF; |
| 5227 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5228 | ctxt->value->floatval=xmlXPathPINF; |
| 5229 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5230 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5231 | ctxt->value->floatval = xmlXPathNZERO; |
| 5232 | else |
| 5233 | ctxt->value->floatval = 0; |
| 5234 | } |
| 5235 | else |
| 5236 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /** |
| 5240 | * xmlXPathAddValues: |
| 5241 | * @ctxt: the XPath Parser context |
| 5242 | * |
| 5243 | * Implement the add operation on XPath objects: |
| 5244 | * The numeric operators convert their operands to numbers as if |
| 5245 | * by calling the number function. |
| 5246 | */ |
| 5247 | void |
| 5248 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5249 | xmlXPathObjectPtr arg; |
| 5250 | double val; |
| 5251 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5252 | arg = valuePop(ctxt); |
| 5253 | if (arg == NULL) |
| 5254 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5255 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5256 | xmlXPathFreeObject(arg); |
| 5257 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5258 | CAST_TO_NUMBER; |
| 5259 | CHECK_TYPE(XPATH_NUMBER); |
| 5260 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
| 5263 | /** |
| 5264 | * xmlXPathSubValues: |
| 5265 | * @ctxt: the XPath Parser context |
| 5266 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5267 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5268 | * The numeric operators convert their operands to numbers as if |
| 5269 | * by calling the number function. |
| 5270 | */ |
| 5271 | void |
| 5272 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5273 | xmlXPathObjectPtr arg; |
| 5274 | double val; |
| 5275 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5276 | arg = valuePop(ctxt); |
| 5277 | if (arg == NULL) |
| 5278 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5279 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5280 | xmlXPathFreeObject(arg); |
| 5281 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5282 | CAST_TO_NUMBER; |
| 5283 | CHECK_TYPE(XPATH_NUMBER); |
| 5284 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | /** |
| 5288 | * xmlXPathMultValues: |
| 5289 | * @ctxt: the XPath Parser context |
| 5290 | * |
| 5291 | * Implement the multiply operation on XPath objects: |
| 5292 | * The numeric operators convert their operands to numbers as if |
| 5293 | * by calling the number function. |
| 5294 | */ |
| 5295 | void |
| 5296 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5297 | xmlXPathObjectPtr arg; |
| 5298 | double val; |
| 5299 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5300 | arg = valuePop(ctxt); |
| 5301 | if (arg == NULL) |
| 5302 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5303 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5304 | xmlXPathFreeObject(arg); |
| 5305 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5306 | CAST_TO_NUMBER; |
| 5307 | CHECK_TYPE(XPATH_NUMBER); |
| 5308 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /** |
| 5312 | * xmlXPathDivValues: |
| 5313 | * @ctxt: the XPath Parser context |
| 5314 | * |
| 5315 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5316 | * The numeric operators convert their operands to numbers as if |
| 5317 | * by calling the number function. |
| 5318 | */ |
| 5319 | void |
| 5320 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5321 | xmlXPathObjectPtr arg; |
| 5322 | double val; |
| 5323 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5324 | arg = valuePop(ctxt); |
| 5325 | if (arg == NULL) |
| 5326 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5327 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5328 | xmlXPathFreeObject(arg); |
| 5329 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5330 | CAST_TO_NUMBER; |
| 5331 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5332 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5333 | ctxt->value->floatval = xmlXPathNAN; |
| 5334 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5335 | if (ctxt->value->floatval == 0) |
| 5336 | ctxt->value->floatval = xmlXPathNAN; |
| 5337 | else if (ctxt->value->floatval > 0) |
| 5338 | ctxt->value->floatval = xmlXPathNINF; |
| 5339 | else if (ctxt->value->floatval < 0) |
| 5340 | ctxt->value->floatval = xmlXPathPINF; |
| 5341 | } |
| 5342 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5343 | if (ctxt->value->floatval == 0) |
| 5344 | ctxt->value->floatval = xmlXPathNAN; |
| 5345 | else if (ctxt->value->floatval > 0) |
| 5346 | ctxt->value->floatval = xmlXPathPINF; |
| 5347 | else if (ctxt->value->floatval < 0) |
| 5348 | ctxt->value->floatval = xmlXPathNINF; |
| 5349 | } else |
| 5350 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | /** |
| 5354 | * xmlXPathModValues: |
| 5355 | * @ctxt: the XPath Parser context |
| 5356 | * |
| 5357 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5358 | * The numeric operators convert their operands to numbers as if |
| 5359 | * by calling the number function. |
| 5360 | */ |
| 5361 | void |
| 5362 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5363 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5364 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5365 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5366 | arg = valuePop(ctxt); |
| 5367 | if (arg == NULL) |
| 5368 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5369 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5370 | xmlXPathFreeObject(arg); |
| 5371 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5372 | CAST_TO_NUMBER; |
| 5373 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5374 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5375 | if (arg2 == 0) |
| 5376 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5377 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5378 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5379 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | /************************************************************************ |
| 5383 | * * |
| 5384 | * The traversal functions * |
| 5385 | * * |
| 5386 | ************************************************************************/ |
| 5387 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5388 | /* |
| 5389 | * A traversal function enumerates nodes along an axis. |
| 5390 | * Initially it must be called with NULL, and it indicates |
| 5391 | * termination on the axis by returning NULL. |
| 5392 | */ |
| 5393 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5394 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5395 | |
| 5396 | /** |
| 5397 | * xmlXPathNextSelf: |
| 5398 | * @ctxt: the XPath Parser context |
| 5399 | * @cur: the current node in the traversal |
| 5400 | * |
| 5401 | * Traversal function for the "self" direction |
| 5402 | * The self axis contains just the context node itself |
| 5403 | * |
| 5404 | * Returns the next element following that axis |
| 5405 | */ |
| 5406 | xmlNodePtr |
| 5407 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5408 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5409 | if (cur == NULL) |
| 5410 | return(ctxt->context->node); |
| 5411 | return(NULL); |
| 5412 | } |
| 5413 | |
| 5414 | /** |
| 5415 | * xmlXPathNextChild: |
| 5416 | * @ctxt: the XPath Parser context |
| 5417 | * @cur: the current node in the traversal |
| 5418 | * |
| 5419 | * Traversal function for the "child" direction |
| 5420 | * The child axis contains the children of the context node in document order. |
| 5421 | * |
| 5422 | * Returns the next element following that axis |
| 5423 | */ |
| 5424 | xmlNodePtr |
| 5425 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5426 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5427 | if (cur == NULL) { |
| 5428 | if (ctxt->context->node == NULL) return(NULL); |
| 5429 | switch (ctxt->context->node->type) { |
| 5430 | case XML_ELEMENT_NODE: |
| 5431 | case XML_TEXT_NODE: |
| 5432 | case XML_CDATA_SECTION_NODE: |
| 5433 | case XML_ENTITY_REF_NODE: |
| 5434 | case XML_ENTITY_NODE: |
| 5435 | case XML_PI_NODE: |
| 5436 | case XML_COMMENT_NODE: |
| 5437 | case XML_NOTATION_NODE: |
| 5438 | case XML_DTD_NODE: |
| 5439 | return(ctxt->context->node->children); |
| 5440 | case XML_DOCUMENT_NODE: |
| 5441 | case XML_DOCUMENT_TYPE_NODE: |
| 5442 | case XML_DOCUMENT_FRAG_NODE: |
| 5443 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5444 | #ifdef LIBXML_DOCB_ENABLED |
| 5445 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5446 | #endif |
| 5447 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5448 | case XML_ELEMENT_DECL: |
| 5449 | case XML_ATTRIBUTE_DECL: |
| 5450 | case XML_ENTITY_DECL: |
| 5451 | case XML_ATTRIBUTE_NODE: |
| 5452 | case XML_NAMESPACE_DECL: |
| 5453 | case XML_XINCLUDE_START: |
| 5454 | case XML_XINCLUDE_END: |
| 5455 | return(NULL); |
| 5456 | } |
| 5457 | return(NULL); |
| 5458 | } |
| 5459 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5460 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5461 | return(NULL); |
| 5462 | return(cur->next); |
| 5463 | } |
| 5464 | |
| 5465 | /** |
| 5466 | * xmlXPathNextDescendant: |
| 5467 | * @ctxt: the XPath Parser context |
| 5468 | * @cur: the current node in the traversal |
| 5469 | * |
| 5470 | * Traversal function for the "descendant" direction |
| 5471 | * the descendant axis contains the descendants of the context node in document |
| 5472 | * order; a descendant is a child or a child of a child and so on. |
| 5473 | * |
| 5474 | * Returns the next element following that axis |
| 5475 | */ |
| 5476 | xmlNodePtr |
| 5477 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5478 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5479 | if (cur == NULL) { |
| 5480 | if (ctxt->context->node == NULL) |
| 5481 | return(NULL); |
| 5482 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5483 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5484 | return(NULL); |
| 5485 | |
| 5486 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5487 | return(ctxt->context->doc->children); |
| 5488 | return(ctxt->context->node->children); |
| 5489 | } |
| 5490 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5491 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5492 | /* |
| 5493 | * Do not descend on entities declarations |
| 5494 | */ |
| 5495 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5496 | cur = cur->children; |
| 5497 | /* |
| 5498 | * Skip DTDs |
| 5499 | */ |
| 5500 | if (cur->type != XML_DTD_NODE) |
| 5501 | return(cur); |
| 5502 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | if (cur == ctxt->context->node) return(NULL); |
| 5506 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5507 | while (cur->next != NULL) { |
| 5508 | cur = cur->next; |
| 5509 | if ((cur->type != XML_ENTITY_DECL) && |
| 5510 | (cur->type != XML_DTD_NODE)) |
| 5511 | return(cur); |
| 5512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5513 | |
| 5514 | do { |
| 5515 | cur = cur->parent; |
| 5516 | if (cur == NULL) return(NULL); |
| 5517 | if (cur == ctxt->context->node) return(NULL); |
| 5518 | if (cur->next != NULL) { |
| 5519 | cur = cur->next; |
| 5520 | return(cur); |
| 5521 | } |
| 5522 | } while (cur != NULL); |
| 5523 | return(cur); |
| 5524 | } |
| 5525 | |
| 5526 | /** |
| 5527 | * xmlXPathNextDescendantOrSelf: |
| 5528 | * @ctxt: the XPath Parser context |
| 5529 | * @cur: the current node in the traversal |
| 5530 | * |
| 5531 | * Traversal function for the "descendant-or-self" direction |
| 5532 | * the descendant-or-self axis contains the context node and the descendants |
| 5533 | * of the context node in document order; thus the context node is the first |
| 5534 | * node on the axis, and the first child of the context node is the second node |
| 5535 | * on the axis |
| 5536 | * |
| 5537 | * Returns the next element following that axis |
| 5538 | */ |
| 5539 | xmlNodePtr |
| 5540 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5541 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5542 | if (cur == NULL) { |
| 5543 | if (ctxt->context->node == NULL) |
| 5544 | return(NULL); |
| 5545 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5546 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5547 | return(NULL); |
| 5548 | return(ctxt->context->node); |
| 5549 | } |
| 5550 | |
| 5551 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5552 | } |
| 5553 | |
| 5554 | /** |
| 5555 | * xmlXPathNextParent: |
| 5556 | * @ctxt: the XPath Parser context |
| 5557 | * @cur: the current node in the traversal |
| 5558 | * |
| 5559 | * Traversal function for the "parent" direction |
| 5560 | * The parent axis contains the parent of the context node, if there is one. |
| 5561 | * |
| 5562 | * Returns the next element following that axis |
| 5563 | */ |
| 5564 | xmlNodePtr |
| 5565 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5566 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5567 | /* |
| 5568 | * the parent of an attribute or namespace node is the element |
| 5569 | * to which the attribute or namespace node is attached |
| 5570 | * Namespace handling !!! |
| 5571 | */ |
| 5572 | if (cur == NULL) { |
| 5573 | if (ctxt->context->node == NULL) return(NULL); |
| 5574 | switch (ctxt->context->node->type) { |
| 5575 | case XML_ELEMENT_NODE: |
| 5576 | case XML_TEXT_NODE: |
| 5577 | case XML_CDATA_SECTION_NODE: |
| 5578 | case XML_ENTITY_REF_NODE: |
| 5579 | case XML_ENTITY_NODE: |
| 5580 | case XML_PI_NODE: |
| 5581 | case XML_COMMENT_NODE: |
| 5582 | case XML_NOTATION_NODE: |
| 5583 | case XML_DTD_NODE: |
| 5584 | case XML_ELEMENT_DECL: |
| 5585 | case XML_ATTRIBUTE_DECL: |
| 5586 | case XML_XINCLUDE_START: |
| 5587 | case XML_XINCLUDE_END: |
| 5588 | case XML_ENTITY_DECL: |
| 5589 | if (ctxt->context->node->parent == NULL) |
| 5590 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5591 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5592 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5593 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5594 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5595 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | return(ctxt->context->node->parent); |
| 5597 | case XML_ATTRIBUTE_NODE: { |
| 5598 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5599 | |
| 5600 | return(att->parent); |
| 5601 | } |
| 5602 | case XML_DOCUMENT_NODE: |
| 5603 | case XML_DOCUMENT_TYPE_NODE: |
| 5604 | case XML_DOCUMENT_FRAG_NODE: |
| 5605 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5606 | #ifdef LIBXML_DOCB_ENABLED |
| 5607 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5608 | #endif |
| 5609 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5610 | case XML_NAMESPACE_DECL: { |
| 5611 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5612 | |
| 5613 | if ((ns->next != NULL) && |
| 5614 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5615 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5616 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5617 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5618 | } |
| 5619 | } |
| 5620 | return(NULL); |
| 5621 | } |
| 5622 | |
| 5623 | /** |
| 5624 | * xmlXPathNextAncestor: |
| 5625 | * @ctxt: the XPath Parser context |
| 5626 | * @cur: the current node in the traversal |
| 5627 | * |
| 5628 | * Traversal function for the "ancestor" direction |
| 5629 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5630 | * of the context node consist of the parent of context node and the parent's |
| 5631 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5632 | * parent is the first node on the axis, and the parent's parent is the second |
| 5633 | * node on the axis |
| 5634 | * |
| 5635 | * Returns the next element following that axis |
| 5636 | */ |
| 5637 | xmlNodePtr |
| 5638 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5639 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5640 | /* |
| 5641 | * the parent of an attribute or namespace node is the element |
| 5642 | * to which the attribute or namespace node is attached |
| 5643 | * !!!!!!!!!!!!! |
| 5644 | */ |
| 5645 | if (cur == NULL) { |
| 5646 | if (ctxt->context->node == NULL) return(NULL); |
| 5647 | switch (ctxt->context->node->type) { |
| 5648 | case XML_ELEMENT_NODE: |
| 5649 | case XML_TEXT_NODE: |
| 5650 | case XML_CDATA_SECTION_NODE: |
| 5651 | case XML_ENTITY_REF_NODE: |
| 5652 | case XML_ENTITY_NODE: |
| 5653 | case XML_PI_NODE: |
| 5654 | case XML_COMMENT_NODE: |
| 5655 | case XML_DTD_NODE: |
| 5656 | case XML_ELEMENT_DECL: |
| 5657 | case XML_ATTRIBUTE_DECL: |
| 5658 | case XML_ENTITY_DECL: |
| 5659 | case XML_NOTATION_NODE: |
| 5660 | case XML_XINCLUDE_START: |
| 5661 | case XML_XINCLUDE_END: |
| 5662 | if (ctxt->context->node->parent == NULL) |
| 5663 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5664 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5665 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5666 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5667 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5668 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | return(ctxt->context->node->parent); |
| 5670 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5671 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5672 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5673 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5674 | } |
| 5675 | case XML_DOCUMENT_NODE: |
| 5676 | case XML_DOCUMENT_TYPE_NODE: |
| 5677 | case XML_DOCUMENT_FRAG_NODE: |
| 5678 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5679 | #ifdef LIBXML_DOCB_ENABLED |
| 5680 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5681 | #endif |
| 5682 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5683 | case XML_NAMESPACE_DECL: { |
| 5684 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5685 | |
| 5686 | if ((ns->next != NULL) && |
| 5687 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5688 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5689 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5690 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5691 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5692 | } |
| 5693 | return(NULL); |
| 5694 | } |
| 5695 | if (cur == ctxt->context->doc->children) |
| 5696 | return((xmlNodePtr) ctxt->context->doc); |
| 5697 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5698 | return(NULL); |
| 5699 | switch (cur->type) { |
| 5700 | case XML_ELEMENT_NODE: |
| 5701 | case XML_TEXT_NODE: |
| 5702 | case XML_CDATA_SECTION_NODE: |
| 5703 | case XML_ENTITY_REF_NODE: |
| 5704 | case XML_ENTITY_NODE: |
| 5705 | case XML_PI_NODE: |
| 5706 | case XML_COMMENT_NODE: |
| 5707 | case XML_NOTATION_NODE: |
| 5708 | case XML_DTD_NODE: |
| 5709 | case XML_ELEMENT_DECL: |
| 5710 | case XML_ATTRIBUTE_DECL: |
| 5711 | case XML_ENTITY_DECL: |
| 5712 | case XML_XINCLUDE_START: |
| 5713 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5714 | if (cur->parent == NULL) |
| 5715 | return(NULL); |
| 5716 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5717 | ((cur->parent->name[0] == ' ') || |
| 5718 | (xmlStrEqual(cur->parent->name, |
| 5719 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5720 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5721 | return(cur->parent); |
| 5722 | case XML_ATTRIBUTE_NODE: { |
| 5723 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5724 | |
| 5725 | return(att->parent); |
| 5726 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5727 | case XML_NAMESPACE_DECL: { |
| 5728 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5729 | |
| 5730 | if ((ns->next != NULL) && |
| 5731 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5732 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5733 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5734 | return(NULL); |
| 5735 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5736 | case XML_DOCUMENT_NODE: |
| 5737 | case XML_DOCUMENT_TYPE_NODE: |
| 5738 | case XML_DOCUMENT_FRAG_NODE: |
| 5739 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5740 | #ifdef LIBXML_DOCB_ENABLED |
| 5741 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5742 | #endif |
| 5743 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5744 | } |
| 5745 | return(NULL); |
| 5746 | } |
| 5747 | |
| 5748 | /** |
| 5749 | * xmlXPathNextAncestorOrSelf: |
| 5750 | * @ctxt: the XPath Parser context |
| 5751 | * @cur: the current node in the traversal |
| 5752 | * |
| 5753 | * Traversal function for the "ancestor-or-self" direction |
| 5754 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5755 | * the context node in reverse document order; thus the context node is |
| 5756 | * the first node on the axis, and the context node's parent the second; |
| 5757 | * parent here is defined the same as with the parent axis. |
| 5758 | * |
| 5759 | * Returns the next element following that axis |
| 5760 | */ |
| 5761 | xmlNodePtr |
| 5762 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5763 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5764 | if (cur == NULL) |
| 5765 | return(ctxt->context->node); |
| 5766 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5767 | } |
| 5768 | |
| 5769 | /** |
| 5770 | * xmlXPathNextFollowingSibling: |
| 5771 | * @ctxt: the XPath Parser context |
| 5772 | * @cur: the current node in the traversal |
| 5773 | * |
| 5774 | * Traversal function for the "following-sibling" direction |
| 5775 | * The following-sibling axis contains the following siblings of the context |
| 5776 | * node in document order. |
| 5777 | * |
| 5778 | * Returns the next element following that axis |
| 5779 | */ |
| 5780 | xmlNodePtr |
| 5781 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5782 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5784 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5785 | return(NULL); |
| 5786 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5787 | return(NULL); |
| 5788 | if (cur == NULL) |
| 5789 | return(ctxt->context->node->next); |
| 5790 | return(cur->next); |
| 5791 | } |
| 5792 | |
| 5793 | /** |
| 5794 | * xmlXPathNextPrecedingSibling: |
| 5795 | * @ctxt: the XPath Parser context |
| 5796 | * @cur: the current node in the traversal |
| 5797 | * |
| 5798 | * Traversal function for the "preceding-sibling" direction |
| 5799 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5800 | * node in reverse document order; the first preceding sibling is first on the |
| 5801 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5802 | * |
| 5803 | * Returns the next element following that axis |
| 5804 | */ |
| 5805 | xmlNodePtr |
| 5806 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5807 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5808 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5809 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5810 | return(NULL); |
| 5811 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5812 | return(NULL); |
| 5813 | if (cur == NULL) |
| 5814 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5815 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5816 | cur = cur->prev; |
| 5817 | if (cur == NULL) |
| 5818 | return(ctxt->context->node->prev); |
| 5819 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5820 | return(cur->prev); |
| 5821 | } |
| 5822 | |
| 5823 | /** |
| 5824 | * xmlXPathNextFollowing: |
| 5825 | * @ctxt: the XPath Parser context |
| 5826 | * @cur: the current node in the traversal |
| 5827 | * |
| 5828 | * Traversal function for the "following" direction |
| 5829 | * The following axis contains all nodes in the same document as the context |
| 5830 | * node that are after the context node in document order, excluding any |
| 5831 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5832 | * are ordered in document order |
| 5833 | * |
| 5834 | * Returns the next element following that axis |
| 5835 | */ |
| 5836 | xmlNodePtr |
| 5837 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5838 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5839 | if (cur != NULL && cur->children != NULL) |
| 5840 | return cur->children ; |
| 5841 | if (cur == NULL) cur = ctxt->context->node; |
| 5842 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5843 | if (cur->next != NULL) return(cur->next) ; |
| 5844 | do { |
| 5845 | cur = cur->parent; |
| 5846 | if (cur == NULL) return(NULL); |
| 5847 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5848 | if (cur->next != NULL) return(cur->next); |
| 5849 | } while (cur != NULL); |
| 5850 | return(cur); |
| 5851 | } |
| 5852 | |
| 5853 | /* |
| 5854 | * xmlXPathIsAncestor: |
| 5855 | * @ancestor: the ancestor node |
| 5856 | * @node: the current node |
| 5857 | * |
| 5858 | * Check that @ancestor is a @node's ancestor |
| 5859 | * |
| 5860 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5861 | */ |
| 5862 | static int |
| 5863 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5864 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5865 | /* nodes need to be in the same document */ |
| 5866 | if (ancestor->doc != node->doc) return(0); |
| 5867 | /* avoid searching if ancestor or node is the root node */ |
| 5868 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5869 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5870 | while (node->parent != NULL) { |
| 5871 | if (node->parent == ancestor) |
| 5872 | return(1); |
| 5873 | node = node->parent; |
| 5874 | } |
| 5875 | return(0); |
| 5876 | } |
| 5877 | |
| 5878 | /** |
| 5879 | * xmlXPathNextPreceding: |
| 5880 | * @ctxt: the XPath Parser context |
| 5881 | * @cur: the current node in the traversal |
| 5882 | * |
| 5883 | * Traversal function for the "preceding" direction |
| 5884 | * the preceding axis contains all nodes in the same document as the context |
| 5885 | * node that are before the context node in document order, excluding any |
| 5886 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5887 | * ordered in reverse document order |
| 5888 | * |
| 5889 | * Returns the next element following that axis |
| 5890 | */ |
| 5891 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5892 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5893 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5894 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5895 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5896 | cur = ctxt->context->node; |
| 5897 | if (cur == NULL) |
| 5898 | return (NULL); |
| 5899 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5900 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5901 | do { |
| 5902 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5903 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5904 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5908 | if (cur == NULL) |
| 5909 | return (NULL); |
| 5910 | if (cur == ctxt->context->doc->children) |
| 5911 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5912 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5913 | return (cur); |
| 5914 | } |
| 5915 | |
| 5916 | /** |
| 5917 | * xmlXPathNextPrecedingInternal: |
| 5918 | * @ctxt: the XPath Parser context |
| 5919 | * @cur: the current node in the traversal |
| 5920 | * |
| 5921 | * Traversal function for the "preceding" direction |
| 5922 | * the preceding axis contains all nodes in the same document as the context |
| 5923 | * node that are before the context node in document order, excluding any |
| 5924 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5925 | * ordered in reverse document order |
| 5926 | * This is a faster implementation but internal only since it requires a |
| 5927 | * state kept in the parser context: ctxt->ancestor. |
| 5928 | * |
| 5929 | * Returns the next element following that axis |
| 5930 | */ |
| 5931 | static xmlNodePtr |
| 5932 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5933 | xmlNodePtr cur) |
| 5934 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5935 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5936 | if (cur == NULL) { |
| 5937 | cur = ctxt->context->node; |
| 5938 | if (cur == NULL) |
| 5939 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 5940 | if (cur->type == XML_NAMESPACE_DECL) |
| 5941 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5942 | ctxt->ancestor = cur->parent; |
| 5943 | } |
| 5944 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5945 | cur = cur->prev; |
| 5946 | while (cur->prev == NULL) { |
| 5947 | cur = cur->parent; |
| 5948 | if (cur == NULL) |
| 5949 | return (NULL); |
| 5950 | if (cur == ctxt->context->doc->children) |
| 5951 | return (NULL); |
| 5952 | if (cur != ctxt->ancestor) |
| 5953 | return (cur); |
| 5954 | ctxt->ancestor = cur->parent; |
| 5955 | } |
| 5956 | cur = cur->prev; |
| 5957 | while (cur->last != NULL) |
| 5958 | cur = cur->last; |
| 5959 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | /** |
| 5963 | * xmlXPathNextNamespace: |
| 5964 | * @ctxt: the XPath Parser context |
| 5965 | * @cur: the current attribute in the traversal |
| 5966 | * |
| 5967 | * Traversal function for the "namespace" direction |
| 5968 | * the namespace axis contains the namespace nodes of the context node; |
| 5969 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5970 | * be empty unless the context node is an element |
| 5971 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5972 | * We keep the XML namespace node at the end of the list. |
| 5973 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5974 | * Returns the next element following that axis |
| 5975 | */ |
| 5976 | xmlNodePtr |
| 5977 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5978 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5979 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5980 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5981 | if (ctxt->context->tmpNsList != NULL) |
| 5982 | xmlFree(ctxt->context->tmpNsList); |
| 5983 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5984 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5985 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5986 | if (ctxt->context->tmpNsList != NULL) { |
| 5987 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 5988 | ctxt->context->tmpNsNr++; |
| 5989 | } |
| 5990 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5991 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5992 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5993 | if (ctxt->context->tmpNsNr > 0) { |
| 5994 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 5995 | } else { |
| 5996 | if (ctxt->context->tmpNsList != NULL) |
| 5997 | xmlFree(ctxt->context->tmpNsList); |
| 5998 | ctxt->context->tmpNsList = NULL; |
| 5999 | return(NULL); |
| 6000 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | /** |
| 6004 | * xmlXPathNextAttribute: |
| 6005 | * @ctxt: the XPath Parser context |
| 6006 | * @cur: the current attribute in the traversal |
| 6007 | * |
| 6008 | * Traversal function for the "attribute" direction |
| 6009 | * TODO: support DTD inherited default attributes |
| 6010 | * |
| 6011 | * Returns the next element following that axis |
| 6012 | */ |
| 6013 | xmlNodePtr |
| 6014 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6015 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6016 | if (ctxt->context->node == NULL) |
| 6017 | return(NULL); |
| 6018 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 6019 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6020 | if (cur == NULL) { |
| 6021 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 6022 | return(NULL); |
| 6023 | return((xmlNodePtr)ctxt->context->node->properties); |
| 6024 | } |
| 6025 | return((xmlNodePtr)cur->next); |
| 6026 | } |
| 6027 | |
| 6028 | /************************************************************************ |
| 6029 | * * |
| 6030 | * NodeTest Functions * |
| 6031 | * * |
| 6032 | ************************************************************************/ |
| 6033 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6034 | #define IS_FUNCTION 200 |
| 6035 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | |
| 6037 | /************************************************************************ |
| 6038 | * * |
| 6039 | * Implicit tree core function library * |
| 6040 | * * |
| 6041 | ************************************************************************/ |
| 6042 | |
| 6043 | /** |
| 6044 | * xmlXPathRoot: |
| 6045 | * @ctxt: the XPath Parser context |
| 6046 | * |
| 6047 | * Initialize the context to the root of the document |
| 6048 | */ |
| 6049 | void |
| 6050 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6051 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6052 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 6053 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6054 | } |
| 6055 | |
| 6056 | /************************************************************************ |
| 6057 | * * |
| 6058 | * The explicit core function library * |
| 6059 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 6060 | * * |
| 6061 | ************************************************************************/ |
| 6062 | |
| 6063 | |
| 6064 | /** |
| 6065 | * xmlXPathLastFunction: |
| 6066 | * @ctxt: the XPath Parser context |
| 6067 | * @nargs: the number of arguments |
| 6068 | * |
| 6069 | * Implement the last() XPath function |
| 6070 | * number last() |
| 6071 | * The last function returns the number of nodes in the context node list. |
| 6072 | */ |
| 6073 | void |
| 6074 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6075 | CHECK_ARITY(0); |
| 6076 | if (ctxt->context->contextSize >= 0) { |
| 6077 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 6078 | #ifdef DEBUG_EXPR |
| 6079 | xmlGenericError(xmlGenericErrorContext, |
| 6080 | "last() : %d\n", ctxt->context->contextSize); |
| 6081 | #endif |
| 6082 | } else { |
| 6083 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 6084 | } |
| 6085 | } |
| 6086 | |
| 6087 | /** |
| 6088 | * xmlXPathPositionFunction: |
| 6089 | * @ctxt: the XPath Parser context |
| 6090 | * @nargs: the number of arguments |
| 6091 | * |
| 6092 | * Implement the position() XPath function |
| 6093 | * number position() |
| 6094 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6095 | * 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] | 6096 | * will be equal to last(). |
| 6097 | */ |
| 6098 | void |
| 6099 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6100 | CHECK_ARITY(0); |
| 6101 | if (ctxt->context->proximityPosition >= 0) { |
| 6102 | valuePush(ctxt, |
| 6103 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 6104 | #ifdef DEBUG_EXPR |
| 6105 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 6106 | ctxt->context->proximityPosition); |
| 6107 | #endif |
| 6108 | } else { |
| 6109 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 6110 | } |
| 6111 | } |
| 6112 | |
| 6113 | /** |
| 6114 | * xmlXPathCountFunction: |
| 6115 | * @ctxt: the XPath Parser context |
| 6116 | * @nargs: the number of arguments |
| 6117 | * |
| 6118 | * Implement the count() XPath function |
| 6119 | * number count(node-set) |
| 6120 | */ |
| 6121 | void |
| 6122 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6123 | xmlXPathObjectPtr cur; |
| 6124 | |
| 6125 | CHECK_ARITY(1); |
| 6126 | if ((ctxt->value == NULL) || |
| 6127 | ((ctxt->value->type != XPATH_NODESET) && |
| 6128 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6129 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6130 | cur = valuePop(ctxt); |
| 6131 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6132 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 6133 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6134 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6135 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 6136 | } else { |
| 6137 | if ((cur->nodesetval->nodeNr != 1) || |
| 6138 | (cur->nodesetval->nodeTab == NULL)) { |
| 6139 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 6140 | } else { |
| 6141 | xmlNodePtr tmp; |
| 6142 | int i = 0; |
| 6143 | |
| 6144 | tmp = cur->nodesetval->nodeTab[0]; |
| 6145 | if (tmp != NULL) { |
| 6146 | tmp = tmp->children; |
| 6147 | while (tmp != NULL) { |
| 6148 | tmp = tmp->next; |
| 6149 | i++; |
| 6150 | } |
| 6151 | } |
| 6152 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6153 | } |
| 6154 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6155 | xmlXPathFreeObject(cur); |
| 6156 | } |
| 6157 | |
| 6158 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6159 | * xmlXPathGetElementsByIds: |
| 6160 | * @doc: the document |
| 6161 | * @ids: a whitespace separated list of IDs |
| 6162 | * |
| 6163 | * Selects elements by their unique ID. |
| 6164 | * |
| 6165 | * Returns a node-set of selected elements. |
| 6166 | */ |
| 6167 | static xmlNodeSetPtr |
| 6168 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6169 | xmlNodeSetPtr ret; |
| 6170 | const xmlChar *cur = ids; |
| 6171 | xmlChar *ID; |
| 6172 | xmlAttrPtr attr; |
| 6173 | xmlNodePtr elem = NULL; |
| 6174 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6175 | if (ids == NULL) return(NULL); |
| 6176 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6177 | ret = xmlXPathNodeSetCreate(NULL); |
| 6178 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6179 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6180 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6181 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6182 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6183 | |
| 6184 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6185 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 6186 | /* |
| 6187 | * We used to check the fact that the value passed |
| 6188 | * was an NCName, but this generated much troubles for |
| 6189 | * me and Aleksey Sanin, people blatantly violated that |
| 6190 | * constaint, like Visa3D spec. |
| 6191 | * if (xmlValidateNCName(ID, 1) == 0) |
| 6192 | */ |
| 6193 | attr = xmlGetID(doc, ID); |
| 6194 | if (attr != NULL) { |
| 6195 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6196 | elem = attr->parent; |
| 6197 | else if (attr->type == XML_ELEMENT_NODE) |
| 6198 | elem = (xmlNodePtr) attr; |
| 6199 | else |
| 6200 | elem = NULL; |
| 6201 | if (elem != NULL) |
| 6202 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6203 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6204 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6205 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6206 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6207 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6208 | ids = cur; |
| 6209 | } |
| 6210 | return(ret); |
| 6211 | } |
| 6212 | |
| 6213 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6214 | * xmlXPathIdFunction: |
| 6215 | * @ctxt: the XPath Parser context |
| 6216 | * @nargs: the number of arguments |
| 6217 | * |
| 6218 | * Implement the id() XPath function |
| 6219 | * node-set id(object) |
| 6220 | * The id function selects elements by their unique ID |
| 6221 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6222 | * then the result is the union of the result of applying id to the |
| 6223 | * string value of each of the nodes in the argument node-set. When the |
| 6224 | * argument to id is of any other type, the argument is converted to a |
| 6225 | * string as if by a call to the string function; the string is split |
| 6226 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6227 | * of characters matching the production S); the result is a node-set |
| 6228 | * containing the elements in the same document as the context node that |
| 6229 | * have a unique ID equal to any of the tokens in the list. |
| 6230 | */ |
| 6231 | void |
| 6232 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6233 | xmlChar *tokens; |
| 6234 | xmlNodeSetPtr ret; |
| 6235 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6236 | |
| 6237 | CHECK_ARITY(1); |
| 6238 | obj = valuePop(ctxt); |
| 6239 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6240 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6241 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | int i; |
| 6243 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6244 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6245 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6246 | if (obj->nodesetval != NULL) { |
| 6247 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6248 | tokens = |
| 6249 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6250 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6251 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6252 | xmlXPathFreeNodeSet(ns); |
| 6253 | if (tokens != NULL) |
| 6254 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6255 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
| 6258 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6259 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6260 | return; |
| 6261 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6262 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6264 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6265 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6266 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6267 | xmlXPathFreeObject(obj); |
| 6268 | return; |
| 6269 | } |
| 6270 | |
| 6271 | /** |
| 6272 | * xmlXPathLocalNameFunction: |
| 6273 | * @ctxt: the XPath Parser context |
| 6274 | * @nargs: the number of arguments |
| 6275 | * |
| 6276 | * Implement the local-name() XPath function |
| 6277 | * string local-name(node-set?) |
| 6278 | * The local-name function returns a string containing the local part |
| 6279 | * of the name of the node in the argument node-set that is first in |
| 6280 | * document order. If the node-set is empty or the first node has no |
| 6281 | * name, an empty string is returned. If the argument is omitted it |
| 6282 | * defaults to the context node. |
| 6283 | */ |
| 6284 | void |
| 6285 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6286 | xmlXPathObjectPtr cur; |
| 6287 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6288 | if (ctxt == NULL) return; |
| 6289 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6290 | if (nargs == 0) { |
| 6291 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6292 | nargs = 1; |
| 6293 | } |
| 6294 | |
| 6295 | CHECK_ARITY(1); |
| 6296 | if ((ctxt->value == NULL) || |
| 6297 | ((ctxt->value->type != XPATH_NODESET) && |
| 6298 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6299 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6300 | cur = valuePop(ctxt); |
| 6301 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6302 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6303 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6304 | } else { |
| 6305 | int i = 0; /* Should be first in document order !!!!! */ |
| 6306 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6307 | case XML_ELEMENT_NODE: |
| 6308 | case XML_ATTRIBUTE_NODE: |
| 6309 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6310 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6311 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6312 | else |
| 6313 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6314 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6315 | break; |
| 6316 | case XML_NAMESPACE_DECL: |
| 6317 | valuePush(ctxt, xmlXPathNewString( |
| 6318 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6319 | break; |
| 6320 | default: |
| 6321 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6322 | } |
| 6323 | } |
| 6324 | xmlXPathFreeObject(cur); |
| 6325 | } |
| 6326 | |
| 6327 | /** |
| 6328 | * xmlXPathNamespaceURIFunction: |
| 6329 | * @ctxt: the XPath Parser context |
| 6330 | * @nargs: the number of arguments |
| 6331 | * |
| 6332 | * Implement the namespace-uri() XPath function |
| 6333 | * string namespace-uri(node-set?) |
| 6334 | * The namespace-uri function returns a string containing the |
| 6335 | * namespace URI of the expanded name of the node in the argument |
| 6336 | * node-set that is first in document order. If the node-set is empty, |
| 6337 | * the first node has no name, or the expanded name has no namespace |
| 6338 | * URI, an empty string is returned. If the argument is omitted it |
| 6339 | * defaults to the context node. |
| 6340 | */ |
| 6341 | void |
| 6342 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6343 | xmlXPathObjectPtr cur; |
| 6344 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6345 | if (ctxt == NULL) return; |
| 6346 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6347 | if (nargs == 0) { |
| 6348 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6349 | nargs = 1; |
| 6350 | } |
| 6351 | CHECK_ARITY(1); |
| 6352 | if ((ctxt->value == NULL) || |
| 6353 | ((ctxt->value->type != XPATH_NODESET) && |
| 6354 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6355 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6356 | cur = valuePop(ctxt); |
| 6357 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6358 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6359 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6360 | } else { |
| 6361 | int i = 0; /* Should be first in document order !!!!! */ |
| 6362 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6363 | case XML_ELEMENT_NODE: |
| 6364 | case XML_ATTRIBUTE_NODE: |
| 6365 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6366 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6367 | else |
| 6368 | valuePush(ctxt, xmlXPathNewString( |
| 6369 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6370 | break; |
| 6371 | default: |
| 6372 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6373 | } |
| 6374 | } |
| 6375 | xmlXPathFreeObject(cur); |
| 6376 | } |
| 6377 | |
| 6378 | /** |
| 6379 | * xmlXPathNameFunction: |
| 6380 | * @ctxt: the XPath Parser context |
| 6381 | * @nargs: the number of arguments |
| 6382 | * |
| 6383 | * Implement the name() XPath function |
| 6384 | * string name(node-set?) |
| 6385 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6386 | * 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] | 6387 | * order. The QName must represent the name with respect to the namespace |
| 6388 | * declarations in effect on the node whose name is being represented. |
| 6389 | * Typically, this will be the form in which the name occurred in the XML |
| 6390 | * source. This need not be the case if there are namespace declarations |
| 6391 | * in effect on the node that associate multiple prefixes with the same |
| 6392 | * namespace. However, an implementation may include information about |
| 6393 | * the original prefix in its representation of nodes; in this case, an |
| 6394 | * implementation can ensure that the returned string is always the same |
| 6395 | * as the QName used in the XML source. If the argument it omitted it |
| 6396 | * defaults to the context node. |
| 6397 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6398 | * returned. |
| 6399 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6400 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6401 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6402 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | xmlXPathObjectPtr cur; |
| 6404 | |
| 6405 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6406 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6407 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6411 | if ((ctxt->value == NULL) || |
| 6412 | ((ctxt->value->type != XPATH_NODESET) && |
| 6413 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6414 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6415 | cur = valuePop(ctxt); |
| 6416 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6417 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6418 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6419 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6420 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6421 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6422 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6423 | case XML_ELEMENT_NODE: |
| 6424 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6425 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6426 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6427 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6428 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6429 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6430 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6431 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6432 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6433 | xmlChar *fullname; |
| 6434 | |
| 6435 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6436 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6437 | NULL, 0); |
| 6438 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6439 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6440 | if (fullname == NULL) { |
| 6441 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6442 | } |
| 6443 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6444 | } |
| 6445 | break; |
| 6446 | default: |
| 6447 | valuePush(ctxt, |
| 6448 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6449 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6450 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6451 | } |
| 6452 | xmlXPathFreeObject(cur); |
| 6453 | } |
| 6454 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6455 | |
| 6456 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6457 | * xmlXPathStringFunction: |
| 6458 | * @ctxt: the XPath Parser context |
| 6459 | * @nargs: the number of arguments |
| 6460 | * |
| 6461 | * Implement the string() XPath function |
| 6462 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6463 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6464 | * - A node-set is converted to a string by returning the value of |
| 6465 | * the node in the node-set that is first in document order. |
| 6466 | * If the node-set is empty, an empty string is returned. |
| 6467 | * - A number is converted to a string as follows |
| 6468 | * + NaN is converted to the string NaN |
| 6469 | * + positive zero is converted to the string 0 |
| 6470 | * + negative zero is converted to the string 0 |
| 6471 | * + positive infinity is converted to the string Infinity |
| 6472 | * + negative infinity is converted to the string -Infinity |
| 6473 | * + if the number is an integer, the number is represented in |
| 6474 | * decimal form as a Number with no decimal point and no leading |
| 6475 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6476 | * + otherwise, the number is represented in decimal form as a |
| 6477 | * Number including a decimal point with at least one digit |
| 6478 | * before the decimal point and at least one digit after the |
| 6479 | * decimal point, preceded by a minus sign (-) if the number |
| 6480 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6481 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6482 | * before the decimal point; beyond the one required digit |
| 6483 | * after the decimal point there must be as many, but only as |
| 6484 | * many, more digits as are needed to uniquely distinguish the |
| 6485 | * number from all other IEEE 754 numeric values. |
| 6486 | * - The boolean false value is converted to the string false. |
| 6487 | * The boolean true value is converted to the string true. |
| 6488 | * |
| 6489 | * If the argument is omitted, it defaults to a node-set with the |
| 6490 | * context node as its only member. |
| 6491 | */ |
| 6492 | void |
| 6493 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6494 | xmlXPathObjectPtr cur; |
| 6495 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6496 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6497 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6498 | valuePush(ctxt, |
| 6499 | xmlXPathWrapString( |
| 6500 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6501 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | CHECK_ARITY(1); |
| 6505 | cur = valuePop(ctxt); |
| 6506 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6507 | cur = xmlXPathConvertString(cur); |
| 6508 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
| 6511 | /** |
| 6512 | * xmlXPathStringLengthFunction: |
| 6513 | * @ctxt: the XPath Parser context |
| 6514 | * @nargs: the number of arguments |
| 6515 | * |
| 6516 | * Implement the string-length() XPath function |
| 6517 | * number string-length(string?) |
| 6518 | * The string-length returns the number of characters in the string |
| 6519 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6520 | * the context node converted to a string, in other words the value |
| 6521 | * of the context node. |
| 6522 | */ |
| 6523 | void |
| 6524 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6525 | xmlXPathObjectPtr cur; |
| 6526 | |
| 6527 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6528 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 6529 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6530 | if (ctxt->context->node == NULL) { |
| 6531 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6532 | } else { |
| 6533 | xmlChar *content; |
| 6534 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6535 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6536 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | xmlFree(content); |
| 6538 | } |
| 6539 | return; |
| 6540 | } |
| 6541 | CHECK_ARITY(1); |
| 6542 | CAST_TO_STRING; |
| 6543 | CHECK_TYPE(XPATH_STRING); |
| 6544 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6545 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6546 | xmlXPathFreeObject(cur); |
| 6547 | } |
| 6548 | |
| 6549 | /** |
| 6550 | * xmlXPathConcatFunction: |
| 6551 | * @ctxt: the XPath Parser context |
| 6552 | * @nargs: the number of arguments |
| 6553 | * |
| 6554 | * Implement the concat() XPath function |
| 6555 | * string concat(string, string, string*) |
| 6556 | * The concat function returns the concatenation of its arguments. |
| 6557 | */ |
| 6558 | void |
| 6559 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6560 | xmlXPathObjectPtr cur, newobj; |
| 6561 | xmlChar *tmp; |
| 6562 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6563 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6564 | if (nargs < 2) { |
| 6565 | CHECK_ARITY(2); |
| 6566 | } |
| 6567 | |
| 6568 | CAST_TO_STRING; |
| 6569 | cur = valuePop(ctxt); |
| 6570 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6571 | xmlXPathFreeObject(cur); |
| 6572 | return; |
| 6573 | } |
| 6574 | nargs--; |
| 6575 | |
| 6576 | while (nargs > 0) { |
| 6577 | CAST_TO_STRING; |
| 6578 | newobj = valuePop(ctxt); |
| 6579 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6580 | xmlXPathFreeObject(newobj); |
| 6581 | xmlXPathFreeObject(cur); |
| 6582 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6583 | } |
| 6584 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6585 | newobj->stringval = cur->stringval; |
| 6586 | cur->stringval = tmp; |
| 6587 | |
| 6588 | xmlXPathFreeObject(newobj); |
| 6589 | nargs--; |
| 6590 | } |
| 6591 | valuePush(ctxt, cur); |
| 6592 | } |
| 6593 | |
| 6594 | /** |
| 6595 | * xmlXPathContainsFunction: |
| 6596 | * @ctxt: the XPath Parser context |
| 6597 | * @nargs: the number of arguments |
| 6598 | * |
| 6599 | * Implement the contains() XPath function |
| 6600 | * boolean contains(string, string) |
| 6601 | * The contains function returns true if the first argument string |
| 6602 | * contains the second argument string, and otherwise returns false. |
| 6603 | */ |
| 6604 | void |
| 6605 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6606 | xmlXPathObjectPtr hay, needle; |
| 6607 | |
| 6608 | CHECK_ARITY(2); |
| 6609 | CAST_TO_STRING; |
| 6610 | CHECK_TYPE(XPATH_STRING); |
| 6611 | needle = valuePop(ctxt); |
| 6612 | CAST_TO_STRING; |
| 6613 | hay = valuePop(ctxt); |
| 6614 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6615 | xmlXPathFreeObject(hay); |
| 6616 | xmlXPathFreeObject(needle); |
| 6617 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6618 | } |
| 6619 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6620 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6621 | else |
| 6622 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6623 | xmlXPathFreeObject(hay); |
| 6624 | xmlXPathFreeObject(needle); |
| 6625 | } |
| 6626 | |
| 6627 | /** |
| 6628 | * xmlXPathStartsWithFunction: |
| 6629 | * @ctxt: the XPath Parser context |
| 6630 | * @nargs: the number of arguments |
| 6631 | * |
| 6632 | * Implement the starts-with() XPath function |
| 6633 | * boolean starts-with(string, string) |
| 6634 | * The starts-with function returns true if the first argument string |
| 6635 | * starts with the second argument string, and otherwise returns false. |
| 6636 | */ |
| 6637 | void |
| 6638 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6639 | xmlXPathObjectPtr hay, needle; |
| 6640 | int n; |
| 6641 | |
| 6642 | CHECK_ARITY(2); |
| 6643 | CAST_TO_STRING; |
| 6644 | CHECK_TYPE(XPATH_STRING); |
| 6645 | needle = valuePop(ctxt); |
| 6646 | CAST_TO_STRING; |
| 6647 | hay = valuePop(ctxt); |
| 6648 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6649 | xmlXPathFreeObject(hay); |
| 6650 | xmlXPathFreeObject(needle); |
| 6651 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6652 | } |
| 6653 | n = xmlStrlen(needle->stringval); |
| 6654 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6655 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6656 | else |
| 6657 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6658 | xmlXPathFreeObject(hay); |
| 6659 | xmlXPathFreeObject(needle); |
| 6660 | } |
| 6661 | |
| 6662 | /** |
| 6663 | * xmlXPathSubstringFunction: |
| 6664 | * @ctxt: the XPath Parser context |
| 6665 | * @nargs: the number of arguments |
| 6666 | * |
| 6667 | * Implement the substring() XPath function |
| 6668 | * string substring(string, number, number?) |
| 6669 | * The substring function returns the substring of the first argument |
| 6670 | * starting at the position specified in the second argument with |
| 6671 | * length specified in the third argument. For example, |
| 6672 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6673 | * specified, it returns the substring starting at the position specified |
| 6674 | * in the second argument and continuing to the end of the string. For |
| 6675 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6676 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6677 | * numeric position: the position of the first character is 1, the position |
| 6678 | * of the second character is 2 and so on. The returned substring contains |
| 6679 | * those characters for which the position of the character is greater than |
| 6680 | * or equal to the second argument and, if the third argument is specified, |
| 6681 | * less than the sum of the second and third arguments; the comparisons |
| 6682 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6683 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6684 | * - substring("12345", 0, 3) returns "12" |
| 6685 | * - substring("12345", 0 div 0, 3) returns "" |
| 6686 | * - substring("12345", 1, 0 div 0) returns "" |
| 6687 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6688 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6689 | */ |
| 6690 | void |
| 6691 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6692 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6693 | double le=0, in; |
| 6694 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6695 | xmlChar *ret; |
| 6696 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | if (nargs < 2) { |
| 6698 | CHECK_ARITY(2); |
| 6699 | } |
| 6700 | if (nargs > 3) { |
| 6701 | CHECK_ARITY(3); |
| 6702 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6703 | /* |
| 6704 | * take care of possible last (position) argument |
| 6705 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6706 | if (nargs == 3) { |
| 6707 | CAST_TO_NUMBER; |
| 6708 | CHECK_TYPE(XPATH_NUMBER); |
| 6709 | len = valuePop(ctxt); |
| 6710 | le = len->floatval; |
| 6711 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6712 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6713 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6714 | CAST_TO_NUMBER; |
| 6715 | CHECK_TYPE(XPATH_NUMBER); |
| 6716 | start = valuePop(ctxt); |
| 6717 | in = start->floatval; |
| 6718 | xmlXPathFreeObject(start); |
| 6719 | CAST_TO_STRING; |
| 6720 | CHECK_TYPE(XPATH_STRING); |
| 6721 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6722 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6723 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6724 | /* |
| 6725 | * If last pos not present, calculate last position |
| 6726 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6727 | if (nargs != 3) { |
| 6728 | le = (double)m; |
| 6729 | if (in < 1.0) |
| 6730 | in = 1.0; |
| 6731 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6732 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6733 | /* Need to check for the special cases where either |
| 6734 | * the index is NaN, the length is NaN, or both |
| 6735 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6736 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6737 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6738 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6739 | * To meet the requirements of the spec, the arguments |
| 6740 | * must be converted to integer format before |
| 6741 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6742 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6743 | * First we go to integer form, rounding up |
| 6744 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6745 | */ |
| 6746 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6747 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6748 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6749 | if (xmlXPathIsInf(le) == 1) { |
| 6750 | l = m; |
| 6751 | if (i < 1) |
| 6752 | i = 1; |
| 6753 | } |
| 6754 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 6755 | l = 0; |
| 6756 | else { |
| 6757 | l = (int) le; |
| 6758 | if (((double)l)+0.5 <= le) l++; |
| 6759 | } |
| 6760 | |
| 6761 | /* Now we normalize inidices */ |
| 6762 | i -= 1; |
| 6763 | l += i; |
| 6764 | if (i < 0) |
| 6765 | i = 0; |
| 6766 | if (l > m) |
| 6767 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6768 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6769 | /* number of chars to copy */ |
| 6770 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6771 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6772 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6773 | } |
| 6774 | else { |
| 6775 | ret = NULL; |
| 6776 | } |
| 6777 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6778 | if (ret == NULL) |
| 6779 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6780 | else { |
| 6781 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6782 | xmlFree(ret); |
| 6783 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6784 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6785 | xmlXPathFreeObject(str); |
| 6786 | } |
| 6787 | |
| 6788 | /** |
| 6789 | * xmlXPathSubstringBeforeFunction: |
| 6790 | * @ctxt: the XPath Parser context |
| 6791 | * @nargs: the number of arguments |
| 6792 | * |
| 6793 | * Implement the substring-before() XPath function |
| 6794 | * string substring-before(string, string) |
| 6795 | * The substring-before function returns the substring of the first |
| 6796 | * argument string that precedes the first occurrence of the second |
| 6797 | * argument string in the first argument string, or the empty string |
| 6798 | * if the first argument string does not contain the second argument |
| 6799 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6800 | */ |
| 6801 | void |
| 6802 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6803 | xmlXPathObjectPtr str; |
| 6804 | xmlXPathObjectPtr find; |
| 6805 | xmlBufferPtr target; |
| 6806 | const xmlChar *point; |
| 6807 | int offset; |
| 6808 | |
| 6809 | CHECK_ARITY(2); |
| 6810 | CAST_TO_STRING; |
| 6811 | find = valuePop(ctxt); |
| 6812 | CAST_TO_STRING; |
| 6813 | str = valuePop(ctxt); |
| 6814 | |
| 6815 | target = xmlBufferCreate(); |
| 6816 | if (target) { |
| 6817 | point = xmlStrstr(str->stringval, find->stringval); |
| 6818 | if (point) { |
| 6819 | offset = (int)(point - str->stringval); |
| 6820 | xmlBufferAdd(target, str->stringval, offset); |
| 6821 | } |
| 6822 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6823 | xmlBufferFree(target); |
| 6824 | } |
| 6825 | |
| 6826 | xmlXPathFreeObject(str); |
| 6827 | xmlXPathFreeObject(find); |
| 6828 | } |
| 6829 | |
| 6830 | /** |
| 6831 | * xmlXPathSubstringAfterFunction: |
| 6832 | * @ctxt: the XPath Parser context |
| 6833 | * @nargs: the number of arguments |
| 6834 | * |
| 6835 | * Implement the substring-after() XPath function |
| 6836 | * string substring-after(string, string) |
| 6837 | * The substring-after function returns the substring of the first |
| 6838 | * argument string that follows the first occurrence of the second |
| 6839 | * argument string in the first argument string, or the empty stringi |
| 6840 | * if the first argument string does not contain the second argument |
| 6841 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6842 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6843 | */ |
| 6844 | void |
| 6845 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6846 | xmlXPathObjectPtr str; |
| 6847 | xmlXPathObjectPtr find; |
| 6848 | xmlBufferPtr target; |
| 6849 | const xmlChar *point; |
| 6850 | int offset; |
| 6851 | |
| 6852 | CHECK_ARITY(2); |
| 6853 | CAST_TO_STRING; |
| 6854 | find = valuePop(ctxt); |
| 6855 | CAST_TO_STRING; |
| 6856 | str = valuePop(ctxt); |
| 6857 | |
| 6858 | target = xmlBufferCreate(); |
| 6859 | if (target) { |
| 6860 | point = xmlStrstr(str->stringval, find->stringval); |
| 6861 | if (point) { |
| 6862 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6863 | xmlBufferAdd(target, &str->stringval[offset], |
| 6864 | xmlStrlen(str->stringval) - offset); |
| 6865 | } |
| 6866 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6867 | xmlBufferFree(target); |
| 6868 | } |
| 6869 | |
| 6870 | xmlXPathFreeObject(str); |
| 6871 | xmlXPathFreeObject(find); |
| 6872 | } |
| 6873 | |
| 6874 | /** |
| 6875 | * xmlXPathNormalizeFunction: |
| 6876 | * @ctxt: the XPath Parser context |
| 6877 | * @nargs: the number of arguments |
| 6878 | * |
| 6879 | * Implement the normalize-space() XPath function |
| 6880 | * string normalize-space(string?) |
| 6881 | * The normalize-space function returns the argument string with white |
| 6882 | * space normalized by stripping leading and trailing whitespace |
| 6883 | * and replacing sequences of whitespace characters by a single |
| 6884 | * space. Whitespace characters are the same allowed by the S production |
| 6885 | * in XML. If the argument is omitted, it defaults to the context |
| 6886 | * node converted to a string, in other words the value of the context node. |
| 6887 | */ |
| 6888 | void |
| 6889 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6890 | xmlXPathObjectPtr obj = NULL; |
| 6891 | xmlChar *source = NULL; |
| 6892 | xmlBufferPtr target; |
| 6893 | xmlChar blank; |
| 6894 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6895 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6896 | if (nargs == 0) { |
| 6897 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6898 | valuePush(ctxt, |
| 6899 | xmlXPathWrapString( |
| 6900 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6901 | nargs = 1; |
| 6902 | } |
| 6903 | |
| 6904 | CHECK_ARITY(1); |
| 6905 | CAST_TO_STRING; |
| 6906 | CHECK_TYPE(XPATH_STRING); |
| 6907 | obj = valuePop(ctxt); |
| 6908 | source = obj->stringval; |
| 6909 | |
| 6910 | target = xmlBufferCreate(); |
| 6911 | if (target && source) { |
| 6912 | |
| 6913 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6914 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6915 | source++; |
| 6916 | |
| 6917 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6918 | blank = 0; |
| 6919 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6920 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6921 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6922 | } else { |
| 6923 | if (blank) { |
| 6924 | xmlBufferAdd(target, &blank, 1); |
| 6925 | blank = 0; |
| 6926 | } |
| 6927 | xmlBufferAdd(target, source, 1); |
| 6928 | } |
| 6929 | source++; |
| 6930 | } |
| 6931 | |
| 6932 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6933 | xmlBufferFree(target); |
| 6934 | } |
| 6935 | xmlXPathFreeObject(obj); |
| 6936 | } |
| 6937 | |
| 6938 | /** |
| 6939 | * xmlXPathTranslateFunction: |
| 6940 | * @ctxt: the XPath Parser context |
| 6941 | * @nargs: the number of arguments |
| 6942 | * |
| 6943 | * Implement the translate() XPath function |
| 6944 | * string translate(string, string, string) |
| 6945 | * The translate function returns the first argument string with |
| 6946 | * occurrences of characters in the second argument string replaced |
| 6947 | * by the character at the corresponding position in the third argument |
| 6948 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6949 | * BAr. If there is a character in the second argument string with no |
| 6950 | * character at a corresponding position in the third argument string |
| 6951 | * (because the second argument string is longer than the third argument |
| 6952 | * string), then occurrences of that character in the first argument |
| 6953 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6954 | * returns "AAA". If a character occurs more than once in second |
| 6955 | * argument string, then the first occurrence determines the replacement |
| 6956 | * character. If the third argument string is longer than the second |
| 6957 | * argument string, then excess characters are ignored. |
| 6958 | */ |
| 6959 | void |
| 6960 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6961 | xmlXPathObjectPtr str; |
| 6962 | xmlXPathObjectPtr from; |
| 6963 | xmlXPathObjectPtr to; |
| 6964 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6965 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6966 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 6967 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6968 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6969 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6970 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6971 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6972 | CAST_TO_STRING; |
| 6973 | to = valuePop(ctxt); |
| 6974 | CAST_TO_STRING; |
| 6975 | from = valuePop(ctxt); |
| 6976 | CAST_TO_STRING; |
| 6977 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6978 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6979 | target = xmlBufferCreate(); |
| 6980 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6981 | max = xmlUTF8Strlen(to->stringval); |
| 6982 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6983 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6984 | if (offset >= 0) { |
| 6985 | if (offset < max) { |
| 6986 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6987 | if (point) |
| 6988 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6989 | } |
| 6990 | } else |
| 6991 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6992 | |
| 6993 | /* Step to next character in input */ |
| 6994 | cptr++; |
| 6995 | if ( ch & 0x80 ) { |
| 6996 | /* if not simple ascii, verify proper format */ |
| 6997 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6998 | xmlGenericError(xmlGenericErrorContext, |
| 6999 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 7000 | break; |
| 7001 | } |
| 7002 | /* then skip over remaining bytes for this char */ |
| 7003 | while ( (ch <<= 1) & 0x80 ) |
| 7004 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 7005 | xmlGenericError(xmlGenericErrorContext, |
| 7006 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 7007 | break; |
| 7008 | } |
| 7009 | if (ch & 0x80) /* must have had error encountered */ |
| 7010 | break; |
| 7011 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7012 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7013 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7014 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7015 | xmlBufferFree(target); |
| 7016 | xmlXPathFreeObject(str); |
| 7017 | xmlXPathFreeObject(from); |
| 7018 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7019 | } |
| 7020 | |
| 7021 | /** |
| 7022 | * xmlXPathBooleanFunction: |
| 7023 | * @ctxt: the XPath Parser context |
| 7024 | * @nargs: the number of arguments |
| 7025 | * |
| 7026 | * Implement the boolean() XPath function |
| 7027 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7028 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7029 | * - a number is true if and only if it is neither positive or |
| 7030 | * negative zero nor NaN |
| 7031 | * - a node-set is true if and only if it is non-empty |
| 7032 | * - a string is true if and only if its length is non-zero |
| 7033 | */ |
| 7034 | void |
| 7035 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7036 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7037 | |
| 7038 | CHECK_ARITY(1); |
| 7039 | cur = valuePop(ctxt); |
| 7040 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7041 | cur = xmlXPathConvertBoolean(cur); |
| 7042 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | /** |
| 7046 | * xmlXPathNotFunction: |
| 7047 | * @ctxt: the XPath Parser context |
| 7048 | * @nargs: the number of arguments |
| 7049 | * |
| 7050 | * Implement the not() XPath function |
| 7051 | * boolean not(boolean) |
| 7052 | * The not function returns true if its argument is false, |
| 7053 | * and false otherwise. |
| 7054 | */ |
| 7055 | void |
| 7056 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7057 | CHECK_ARITY(1); |
| 7058 | CAST_TO_BOOLEAN; |
| 7059 | CHECK_TYPE(XPATH_BOOLEAN); |
| 7060 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 7061 | } |
| 7062 | |
| 7063 | /** |
| 7064 | * xmlXPathTrueFunction: |
| 7065 | * @ctxt: the XPath Parser context |
| 7066 | * @nargs: the number of arguments |
| 7067 | * |
| 7068 | * Implement the true() XPath function |
| 7069 | * boolean true() |
| 7070 | */ |
| 7071 | void |
| 7072 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7073 | CHECK_ARITY(0); |
| 7074 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7075 | } |
| 7076 | |
| 7077 | /** |
| 7078 | * xmlXPathFalseFunction: |
| 7079 | * @ctxt: the XPath Parser context |
| 7080 | * @nargs: the number of arguments |
| 7081 | * |
| 7082 | * Implement the false() XPath function |
| 7083 | * boolean false() |
| 7084 | */ |
| 7085 | void |
| 7086 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7087 | CHECK_ARITY(0); |
| 7088 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7089 | } |
| 7090 | |
| 7091 | /** |
| 7092 | * xmlXPathLangFunction: |
| 7093 | * @ctxt: the XPath Parser context |
| 7094 | * @nargs: the number of arguments |
| 7095 | * |
| 7096 | * Implement the lang() XPath function |
| 7097 | * boolean lang(string) |
| 7098 | * The lang function returns true or false depending on whether the |
| 7099 | * language of the context node as specified by xml:lang attributes |
| 7100 | * is the same as or is a sublanguage of the language specified by |
| 7101 | * the argument string. The language of the context node is determined |
| 7102 | * by the value of the xml:lang attribute on the context node, or, if |
| 7103 | * the context node has no xml:lang attribute, by the value of the |
| 7104 | * xml:lang attribute on the nearest ancestor of the context node that |
| 7105 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 7106 | * returns false. If there is such an attribute, then lang returns |
| 7107 | * true if the attribute value is equal to the argument ignoring case, |
| 7108 | * or if there is some suffix starting with - such that the attribute |
| 7109 | * value is equal to the argument ignoring that suffix of the attribute |
| 7110 | * value and ignoring case. |
| 7111 | */ |
| 7112 | void |
| 7113 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7114 | xmlXPathObjectPtr val = NULL; |
| 7115 | const xmlChar *theLang = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7116 | const xmlChar *lang; |
| 7117 | int ret = 0; |
| 7118 | int i; |
| 7119 | |
| 7120 | CHECK_ARITY(1); |
| 7121 | CAST_TO_STRING; |
| 7122 | CHECK_TYPE(XPATH_STRING); |
| 7123 | val = valuePop(ctxt); |
| 7124 | lang = val->stringval; |
| 7125 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 7126 | if ((theLang != NULL) && (lang != NULL)) { |
| 7127 | for (i = 0;lang[i] != 0;i++) |
| 7128 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 7129 | goto not_equal; |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7130 | if ((theLang[i] == 0) || (theLang[i] == '-')) |
| 7131 | ret = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7132 | } |
| 7133 | not_equal: |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7134 | if (theLang != NULL) |
| 7135 | xmlFree((void *)theLang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7136 | xmlXPathFreeObject(val); |
| 7137 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 7138 | } |
| 7139 | |
| 7140 | /** |
| 7141 | * xmlXPathNumberFunction: |
| 7142 | * @ctxt: the XPath Parser context |
| 7143 | * @nargs: the number of arguments |
| 7144 | * |
| 7145 | * Implement the number() XPath function |
| 7146 | * number number(object?) |
| 7147 | */ |
| 7148 | void |
| 7149 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7150 | xmlXPathObjectPtr cur; |
| 7151 | double res; |
| 7152 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7153 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7154 | if (nargs == 0) { |
| 7155 | if (ctxt->context->node == NULL) { |
| 7156 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 7157 | } else { |
| 7158 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 7159 | |
| 7160 | res = xmlXPathStringEvalNumber(content); |
| 7161 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 7162 | xmlFree(content); |
| 7163 | } |
| 7164 | return; |
| 7165 | } |
| 7166 | |
| 7167 | CHECK_ARITY(1); |
| 7168 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7169 | cur = xmlXPathConvertNumber(cur); |
| 7170 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7171 | } |
| 7172 | |
| 7173 | /** |
| 7174 | * xmlXPathSumFunction: |
| 7175 | * @ctxt: the XPath Parser context |
| 7176 | * @nargs: the number of arguments |
| 7177 | * |
| 7178 | * Implement the sum() XPath function |
| 7179 | * number sum(node-set) |
| 7180 | * The sum function returns the sum of the values of the nodes in |
| 7181 | * the argument node-set. |
| 7182 | */ |
| 7183 | void |
| 7184 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7185 | xmlXPathObjectPtr cur; |
| 7186 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7187 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7188 | |
| 7189 | CHECK_ARITY(1); |
| 7190 | if ((ctxt->value == NULL) || |
| 7191 | ((ctxt->value->type != XPATH_NODESET) && |
| 7192 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7193 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7194 | cur = valuePop(ctxt); |
| 7195 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7196 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7197 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7198 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7199 | } |
| 7200 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7201 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7202 | xmlXPathFreeObject(cur); |
| 7203 | } |
| 7204 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7205 | /* |
| 7206 | * To assure working code on multiple platforms, we want to only depend |
| 7207 | * upon the characteristic truncation of converting a floating point value |
| 7208 | * to an integer. Unfortunately, because of the different storage sizes |
| 7209 | * of our internal floating point value (double) and integer (int), we |
| 7210 | * can't directly convert (see bug 301162). This macro is a messy |
| 7211 | * 'workaround' |
| 7212 | */ |
| 7213 | #define XTRUNC(f, v) \ |
| 7214 | f = fmod((v), INT_MAX); \ |
| 7215 | f = (v) - (f) + (double)((int)(f)); |
| 7216 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7217 | /** |
| 7218 | * xmlXPathFloorFunction: |
| 7219 | * @ctxt: the XPath Parser context |
| 7220 | * @nargs: the number of arguments |
| 7221 | * |
| 7222 | * Implement the floor() XPath function |
| 7223 | * number floor(number) |
| 7224 | * The floor function returns the largest (closest to positive infinity) |
| 7225 | * number that is not greater than the argument and that is an integer. |
| 7226 | */ |
| 7227 | void |
| 7228 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7229 | double f; |
| 7230 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7231 | CHECK_ARITY(1); |
| 7232 | CAST_TO_NUMBER; |
| 7233 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7234 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7235 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7236 | if (f != ctxt->value->floatval) { |
| 7237 | if (ctxt->value->floatval > 0) |
| 7238 | ctxt->value->floatval = f; |
| 7239 | else |
| 7240 | ctxt->value->floatval = f - 1; |
| 7241 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | /** |
| 7245 | * xmlXPathCeilingFunction: |
| 7246 | * @ctxt: the XPath Parser context |
| 7247 | * @nargs: the number of arguments |
| 7248 | * |
| 7249 | * Implement the ceiling() XPath function |
| 7250 | * number ceiling(number) |
| 7251 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7252 | * number that is not less than the argument and that is an integer. |
| 7253 | */ |
| 7254 | void |
| 7255 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7256 | double f; |
| 7257 | |
| 7258 | CHECK_ARITY(1); |
| 7259 | CAST_TO_NUMBER; |
| 7260 | CHECK_TYPE(XPATH_NUMBER); |
| 7261 | |
| 7262 | #if 0 |
| 7263 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7264 | #else |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7265 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7266 | if (f != ctxt->value->floatval) { |
| 7267 | if (ctxt->value->floatval > 0) |
| 7268 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7269 | else { |
| 7270 | if (ctxt->value->floatval < 0 && f == 0) |
| 7271 | ctxt->value->floatval = xmlXPathNZERO; |
| 7272 | else |
| 7273 | ctxt->value->floatval = f; |
| 7274 | } |
| 7275 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7276 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7277 | #endif |
| 7278 | } |
| 7279 | |
| 7280 | /** |
| 7281 | * xmlXPathRoundFunction: |
| 7282 | * @ctxt: the XPath Parser context |
| 7283 | * @nargs: the number of arguments |
| 7284 | * |
| 7285 | * Implement the round() XPath function |
| 7286 | * number round(number) |
| 7287 | * The round function returns the number that is closest to the |
| 7288 | * argument and that is an integer. If there are two such numbers, |
| 7289 | * then the one that is even is returned. |
| 7290 | */ |
| 7291 | void |
| 7292 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7293 | double f; |
| 7294 | |
| 7295 | CHECK_ARITY(1); |
| 7296 | CAST_TO_NUMBER; |
| 7297 | CHECK_TYPE(XPATH_NUMBER); |
| 7298 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7299 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7300 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7301 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7302 | (ctxt->value->floatval == 0.0)) |
| 7303 | return; |
| 7304 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7305 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7306 | if (ctxt->value->floatval < 0) { |
| 7307 | if (ctxt->value->floatval < f - 0.5) |
| 7308 | ctxt->value->floatval = f - 1; |
| 7309 | else |
| 7310 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7311 | if (ctxt->value->floatval == 0) |
| 7312 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7313 | } else { |
| 7314 | if (ctxt->value->floatval < f + 0.5) |
| 7315 | ctxt->value->floatval = f; |
| 7316 | else |
| 7317 | ctxt->value->floatval = f + 1; |
| 7318 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
| 7321 | /************************************************************************ |
| 7322 | * * |
| 7323 | * The Parser * |
| 7324 | * * |
| 7325 | ************************************************************************/ |
| 7326 | |
| 7327 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7328 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7329 | * implementation. |
| 7330 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7331 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7332 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7333 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7334 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7335 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7336 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7337 | |
| 7338 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7339 | * xmlXPathCurrentChar: |
| 7340 | * @ctxt: the XPath parser context |
| 7341 | * @cur: pointer to the beginning of the char |
| 7342 | * @len: pointer to the length of the char read |
| 7343 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7344 | * 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] | 7345 | * bytes in the input buffer. |
| 7346 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7347 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7348 | */ |
| 7349 | |
| 7350 | static int |
| 7351 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7352 | unsigned char c; |
| 7353 | unsigned int val; |
| 7354 | const xmlChar *cur; |
| 7355 | |
| 7356 | if (ctxt == NULL) |
| 7357 | return(0); |
| 7358 | cur = ctxt->cur; |
| 7359 | |
| 7360 | /* |
| 7361 | * We are supposed to handle UTF8, check it's valid |
| 7362 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7363 | * |
| 7364 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7365 | * 0000 0000-0000 007F 0xxxxxxx |
| 7366 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7367 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7368 | * |
| 7369 | * Check for the 0x110000 limit too |
| 7370 | */ |
| 7371 | c = *cur; |
| 7372 | if (c & 0x80) { |
| 7373 | if ((cur[1] & 0xc0) != 0x80) |
| 7374 | goto encoding_error; |
| 7375 | if ((c & 0xe0) == 0xe0) { |
| 7376 | |
| 7377 | if ((cur[2] & 0xc0) != 0x80) |
| 7378 | goto encoding_error; |
| 7379 | if ((c & 0xf0) == 0xf0) { |
| 7380 | if (((c & 0xf8) != 0xf0) || |
| 7381 | ((cur[3] & 0xc0) != 0x80)) |
| 7382 | goto encoding_error; |
| 7383 | /* 4-byte code */ |
| 7384 | *len = 4; |
| 7385 | val = (cur[0] & 0x7) << 18; |
| 7386 | val |= (cur[1] & 0x3f) << 12; |
| 7387 | val |= (cur[2] & 0x3f) << 6; |
| 7388 | val |= cur[3] & 0x3f; |
| 7389 | } else { |
| 7390 | /* 3-byte code */ |
| 7391 | *len = 3; |
| 7392 | val = (cur[0] & 0xf) << 12; |
| 7393 | val |= (cur[1] & 0x3f) << 6; |
| 7394 | val |= cur[2] & 0x3f; |
| 7395 | } |
| 7396 | } else { |
| 7397 | /* 2-byte code */ |
| 7398 | *len = 2; |
| 7399 | val = (cur[0] & 0x1f) << 6; |
| 7400 | val |= cur[1] & 0x3f; |
| 7401 | } |
| 7402 | if (!IS_CHAR(val)) { |
| 7403 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7404 | } |
| 7405 | return(val); |
| 7406 | } else { |
| 7407 | /* 1-byte code */ |
| 7408 | *len = 1; |
| 7409 | return((int) *cur); |
| 7410 | } |
| 7411 | encoding_error: |
| 7412 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7413 | * If we detect an UTF8 error that probably means that the |
| 7414 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7415 | * declaration header. Report the error and switch the encoding |
| 7416 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7417 | * encoding !) |
| 7418 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7419 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7420 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7424 | * xmlXPathParseNCName: |
| 7425 | * @ctxt: the XPath Parser context |
| 7426 | * |
| 7427 | * parse an XML namespace non qualified name. |
| 7428 | * |
| 7429 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7430 | * |
| 7431 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7432 | * CombiningChar | Extender |
| 7433 | * |
| 7434 | * Returns the namespace name or NULL |
| 7435 | */ |
| 7436 | |
| 7437 | xmlChar * |
| 7438 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7439 | const xmlChar *in; |
| 7440 | xmlChar *ret; |
| 7441 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7442 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7443 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7444 | /* |
| 7445 | * Accelerator for simple ASCII names |
| 7446 | */ |
| 7447 | in = ctxt->cur; |
| 7448 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7449 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7450 | (*in == '_')) { |
| 7451 | in++; |
| 7452 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7453 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7454 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7455 | (*in == '_') || (*in == '.') || |
| 7456 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7457 | in++; |
| 7458 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7459 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7460 | (*in == '@') || (*in == '*')) { |
| 7461 | count = in - ctxt->cur; |
| 7462 | if (count == 0) |
| 7463 | return(NULL); |
| 7464 | ret = xmlStrndup(ctxt->cur, count); |
| 7465 | ctxt->cur = in; |
| 7466 | return(ret); |
| 7467 | } |
| 7468 | } |
| 7469 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7470 | } |
| 7471 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7472 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7473 | /** |
| 7474 | * xmlXPathParseQName: |
| 7475 | * @ctxt: the XPath Parser context |
| 7476 | * @prefix: a xmlChar ** |
| 7477 | * |
| 7478 | * parse an XML qualified name |
| 7479 | * |
| 7480 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7481 | * |
| 7482 | * [NS 6] Prefix ::= NCName |
| 7483 | * |
| 7484 | * [NS 7] LocalPart ::= NCName |
| 7485 | * |
| 7486 | * Returns the function returns the local part, and prefix is updated |
| 7487 | * to get the Prefix if any. |
| 7488 | */ |
| 7489 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7490 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7491 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7492 | xmlChar *ret = NULL; |
| 7493 | |
| 7494 | *prefix = NULL; |
| 7495 | ret = xmlXPathParseNCName(ctxt); |
| 7496 | if (CUR == ':') { |
| 7497 | *prefix = ret; |
| 7498 | NEXT; |
| 7499 | ret = xmlXPathParseNCName(ctxt); |
| 7500 | } |
| 7501 | return(ret); |
| 7502 | } |
| 7503 | |
| 7504 | /** |
| 7505 | * xmlXPathParseName: |
| 7506 | * @ctxt: the XPath Parser context |
| 7507 | * |
| 7508 | * parse an XML name |
| 7509 | * |
| 7510 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7511 | * CombiningChar | Extender |
| 7512 | * |
| 7513 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7514 | * |
| 7515 | * Returns the namespace name or NULL |
| 7516 | */ |
| 7517 | |
| 7518 | xmlChar * |
| 7519 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7520 | const xmlChar *in; |
| 7521 | xmlChar *ret; |
| 7522 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7523 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7524 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7525 | /* |
| 7526 | * Accelerator for simple ASCII names |
| 7527 | */ |
| 7528 | in = ctxt->cur; |
| 7529 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7530 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7531 | (*in == '_') || (*in == ':')) { |
| 7532 | in++; |
| 7533 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7534 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7535 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7536 | (*in == '_') || (*in == '-') || |
| 7537 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7538 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7539 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7540 | count = in - ctxt->cur; |
| 7541 | ret = xmlStrndup(ctxt->cur, count); |
| 7542 | ctxt->cur = in; |
| 7543 | return(ret); |
| 7544 | } |
| 7545 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7546 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7549 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7550 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7551 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7552 | int len = 0, l; |
| 7553 | int c; |
| 7554 | |
| 7555 | /* |
| 7556 | * Handler for more complex cases |
| 7557 | */ |
| 7558 | c = CUR_CHAR(l); |
| 7559 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7560 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7561 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7562 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7563 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7564 | return(NULL); |
| 7565 | } |
| 7566 | |
| 7567 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7568 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7569 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7570 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7571 | (IS_COMBINING(c)) || |
| 7572 | (IS_EXTENDER(c)))) { |
| 7573 | COPY_BUF(l,buf,len,c); |
| 7574 | NEXTL(l); |
| 7575 | c = CUR_CHAR(l); |
| 7576 | if (len >= XML_MAX_NAMELEN) { |
| 7577 | /* |
| 7578 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7579 | * for the processing speed. |
| 7580 | */ |
| 7581 | xmlChar *buffer; |
| 7582 | int max = len * 2; |
| 7583 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7584 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7585 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7586 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7587 | } |
| 7588 | memcpy(buffer, buf, len); |
| 7589 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7590 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7591 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7592 | (IS_COMBINING(c)) || |
| 7593 | (IS_EXTENDER(c))) { |
| 7594 | if (len + 10 > max) { |
| 7595 | max *= 2; |
| 7596 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7597 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7598 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7599 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7600 | } |
| 7601 | } |
| 7602 | COPY_BUF(l,buffer,len,c); |
| 7603 | NEXTL(l); |
| 7604 | c = CUR_CHAR(l); |
| 7605 | } |
| 7606 | buffer[len] = 0; |
| 7607 | return(buffer); |
| 7608 | } |
| 7609 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7610 | if (len == 0) |
| 7611 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7612 | return(xmlStrndup(buf, len)); |
| 7613 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7614 | |
| 7615 | #define MAX_FRAC 20 |
| 7616 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7617 | /* |
| 7618 | * These are used as divisors for the fractional part of a number. |
| 7619 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 7620 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 7621 | */ |
| 7622 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7623 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7624 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7625 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7626 | 100000000000000.0, |
| 7627 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7628 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7629 | }; |
| 7630 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7631 | /** |
| 7632 | * xmlXPathStringEvalNumber: |
| 7633 | * @str: A string to scan |
| 7634 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7635 | * [30a] Float ::= Number ('e' Digits?)? |
| 7636 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7637 | * [30] Number ::= Digits ('.' Digits?)? |
| 7638 | * | '.' Digits |
| 7639 | * [31] Digits ::= [0-9]+ |
| 7640 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7641 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7642 | * In complement of the Number expression, this function also handles |
| 7643 | * negative values : '-' Number. |
| 7644 | * |
| 7645 | * Returns the double value. |
| 7646 | */ |
| 7647 | double |
| 7648 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7649 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7650 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7651 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7652 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7653 | int exponent = 0; |
| 7654 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7655 | #ifdef __GNUC__ |
| 7656 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7657 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7658 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7659 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7660 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7661 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7662 | return(xmlXPathNAN); |
| 7663 | } |
| 7664 | if (*cur == '-') { |
| 7665 | isneg = 1; |
| 7666 | cur++; |
| 7667 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7668 | |
| 7669 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7670 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7671 | * tmp/temp is a workaround against a gcc compiler bug |
| 7672 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7673 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7674 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7675 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7676 | ret = ret * 10; |
| 7677 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7678 | ok = 1; |
| 7679 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7680 | temp = (double) tmp; |
| 7681 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7682 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7683 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7684 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7685 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7686 | ret = ret * 10 + (*cur - '0'); |
| 7687 | ok = 1; |
| 7688 | cur++; |
| 7689 | } |
| 7690 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7691 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7692 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7693 | int v, frac = 0; |
| 7694 | double fraction = 0; |
| 7695 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7696 | cur++; |
| 7697 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7698 | return(xmlXPathNAN); |
| 7699 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7700 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7701 | v = (*cur - '0'); |
| 7702 | fraction = fraction * 10 + v; |
| 7703 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7704 | cur++; |
| 7705 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7706 | fraction /= my_pow10[frac]; |
| 7707 | ret = ret + fraction; |
| 7708 | while ((*cur >= '0') && (*cur <= '9')) |
| 7709 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7710 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7711 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7712 | cur++; |
| 7713 | if (*cur == '-') { |
| 7714 | is_exponent_negative = 1; |
| 7715 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7716 | } else if (*cur == '+') { |
| 7717 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7718 | } |
| 7719 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7720 | exponent = exponent * 10 + (*cur - '0'); |
| 7721 | cur++; |
| 7722 | } |
| 7723 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7724 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7725 | if (*cur != 0) return(xmlXPathNAN); |
| 7726 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7727 | if (is_exponent_negative) exponent = -exponent; |
| 7728 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7729 | return(ret); |
| 7730 | } |
| 7731 | |
| 7732 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7733 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7734 | * @ctxt: the XPath Parser context |
| 7735 | * |
| 7736 | * [30] Number ::= Digits ('.' Digits?)? |
| 7737 | * | '.' Digits |
| 7738 | * [31] Digits ::= [0-9]+ |
| 7739 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7740 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7741 | * |
| 7742 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7743 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7744 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7745 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7746 | double ret = 0.0; |
| 7747 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7748 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7749 | int exponent = 0; |
| 7750 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7751 | #ifdef __GNUC__ |
| 7752 | unsigned long tmp = 0; |
| 7753 | double temp; |
| 7754 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7755 | |
| 7756 | CHECK_ERROR; |
| 7757 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7758 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7759 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7760 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7761 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7762 | * tmp/temp is a workaround against a gcc compiler bug |
| 7763 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7764 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7765 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7766 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7767 | ret = ret * 10; |
| 7768 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7769 | ok = 1; |
| 7770 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7771 | temp = (double) tmp; |
| 7772 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7773 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7774 | #else |
| 7775 | ret = 0; |
| 7776 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7777 | ret = ret * 10 + (CUR - '0'); |
| 7778 | ok = 1; |
| 7779 | NEXT; |
| 7780 | } |
| 7781 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7782 | if (CUR == '.') { |
| 7783 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7784 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7785 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7786 | } |
| 7787 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7788 | mult /= 10; |
| 7789 | ret = ret + (CUR - '0') * mult; |
| 7790 | NEXT; |
| 7791 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7792 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7793 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7794 | NEXT; |
| 7795 | if (CUR == '-') { |
| 7796 | is_exponent_negative = 1; |
| 7797 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7798 | } else if (CUR == '+') { |
| 7799 | NEXT; |
| 7800 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7801 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7802 | exponent = exponent * 10 + (CUR - '0'); |
| 7803 | NEXT; |
| 7804 | } |
| 7805 | if (is_exponent_negative) |
| 7806 | exponent = -exponent; |
| 7807 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7808 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7809 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7810 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7811 | } |
| 7812 | |
| 7813 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7814 | * xmlXPathParseLiteral: |
| 7815 | * @ctxt: the XPath Parser context |
| 7816 | * |
| 7817 | * Parse a Literal |
| 7818 | * |
| 7819 | * [29] Literal ::= '"' [^"]* '"' |
| 7820 | * | "'" [^']* "'" |
| 7821 | * |
| 7822 | * Returns the value found or NULL in case of error |
| 7823 | */ |
| 7824 | static xmlChar * |
| 7825 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7826 | const xmlChar *q; |
| 7827 | xmlChar *ret = NULL; |
| 7828 | |
| 7829 | if (CUR == '"') { |
| 7830 | NEXT; |
| 7831 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7832 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7833 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7834 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7835 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7836 | } else { |
| 7837 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7838 | NEXT; |
| 7839 | } |
| 7840 | } else if (CUR == '\'') { |
| 7841 | NEXT; |
| 7842 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7843 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7844 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7845 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7846 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7847 | } else { |
| 7848 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7849 | NEXT; |
| 7850 | } |
| 7851 | } else { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7852 | XP_ERRORNULL(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7853 | } |
| 7854 | return(ret); |
| 7855 | } |
| 7856 | |
| 7857 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7858 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7859 | * @ctxt: the XPath Parser context |
| 7860 | * |
| 7861 | * Parse a Literal and push it on the stack. |
| 7862 | * |
| 7863 | * [29] Literal ::= '"' [^"]* '"' |
| 7864 | * | "'" [^']* "'" |
| 7865 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7866 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7867 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7868 | static void |
| 7869 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7870 | const xmlChar *q; |
| 7871 | xmlChar *ret = NULL; |
| 7872 | |
| 7873 | if (CUR == '"') { |
| 7874 | NEXT; |
| 7875 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7876 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7877 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7878 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7879 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7880 | } else { |
| 7881 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7882 | NEXT; |
| 7883 | } |
| 7884 | } else if (CUR == '\'') { |
| 7885 | NEXT; |
| 7886 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7887 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7888 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7889 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7890 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7891 | } else { |
| 7892 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7893 | NEXT; |
| 7894 | } |
| 7895 | } else { |
| 7896 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7897 | } |
| 7898 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7899 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7900 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7901 | xmlFree(ret); |
| 7902 | } |
| 7903 | |
| 7904 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7905 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7906 | * @ctxt: the XPath Parser context |
| 7907 | * |
| 7908 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7909 | * |
| 7910 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7911 | * 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] | 7912 | * of any of the types that are possible for the value of an expression, |
| 7913 | * and may also be of additional types not specified here. |
| 7914 | * |
| 7915 | * Early evaluation is possible since: |
| 7916 | * The variable bindings [...] used to evaluate a subexpression are |
| 7917 | * always the same as those used to evaluate the containing expression. |
| 7918 | * |
| 7919 | * [36] VariableReference ::= '$' QName |
| 7920 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7921 | static void |
| 7922 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7923 | xmlChar *name; |
| 7924 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7925 | |
| 7926 | SKIP_BLANKS; |
| 7927 | if (CUR != '$') { |
| 7928 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7929 | } |
| 7930 | NEXT; |
| 7931 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7932 | if (name == NULL) { |
| 7933 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7934 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7935 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7936 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7937 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7938 | SKIP_BLANKS; |
| 7939 | } |
| 7940 | |
| 7941 | /** |
| 7942 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7943 | * @name: a name string |
| 7944 | * |
| 7945 | * Is the name given a NodeType one. |
| 7946 | * |
| 7947 | * [38] NodeType ::= 'comment' |
| 7948 | * | 'text' |
| 7949 | * | 'processing-instruction' |
| 7950 | * | 'node' |
| 7951 | * |
| 7952 | * Returns 1 if true 0 otherwise |
| 7953 | */ |
| 7954 | int |
| 7955 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7956 | if (name == NULL) |
| 7957 | return(0); |
| 7958 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7959 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7960 | return(1); |
| 7961 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7962 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7963 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7964 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7965 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7966 | return(1); |
| 7967 | return(0); |
| 7968 | } |
| 7969 | |
| 7970 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7971 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7972 | * @ctxt: the XPath Parser context |
| 7973 | * |
| 7974 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7975 | * [17] Argument ::= Expr |
| 7976 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7977 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7978 | * pushed on the stack |
| 7979 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7980 | static void |
| 7981 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7982 | xmlChar *name; |
| 7983 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7984 | int nbargs = 0; |
| 7985 | |
| 7986 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7987 | if (name == NULL) { |
| 7988 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7989 | } |
| 7990 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7991 | #ifdef DEBUG_EXPR |
| 7992 | if (prefix == NULL) |
| 7993 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7994 | name); |
| 7995 | else |
| 7996 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7997 | prefix, name); |
| 7998 | #endif |
| 7999 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8000 | if (CUR != '(') { |
| 8001 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8002 | } |
| 8003 | NEXT; |
| 8004 | SKIP_BLANKS; |
| 8005 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8006 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 8007 | if (CUR != ')') { |
| 8008 | while (CUR != 0) { |
| 8009 | int op1 = ctxt->comp->last; |
| 8010 | ctxt->comp->last = -1; |
| 8011 | xmlXPathCompileExpr(ctxt); |
| 8012 | CHECK_ERROR; |
| 8013 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 8014 | nbargs++; |
| 8015 | if (CUR == ')') break; |
| 8016 | if (CUR != ',') { |
| 8017 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8018 | } |
| 8019 | NEXT; |
| 8020 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8021 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8022 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8023 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 8024 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8025 | NEXT; |
| 8026 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8027 | } |
| 8028 | |
| 8029 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8030 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8031 | * @ctxt: the XPath Parser context |
| 8032 | * |
| 8033 | * [15] PrimaryExpr ::= VariableReference |
| 8034 | * | '(' Expr ')' |
| 8035 | * | Literal |
| 8036 | * | Number |
| 8037 | * | FunctionCall |
| 8038 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8039 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8040 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8041 | static void |
| 8042 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8043 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8044 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8045 | else if (CUR == '(') { |
| 8046 | NEXT; |
| 8047 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8048 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 8049 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8050 | if (CUR != ')') { |
| 8051 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8052 | } |
| 8053 | NEXT; |
| 8054 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8055 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8056 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8057 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8058 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8059 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8060 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8061 | } |
| 8062 | SKIP_BLANKS; |
| 8063 | } |
| 8064 | |
| 8065 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8066 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8067 | * @ctxt: the XPath Parser context |
| 8068 | * |
| 8069 | * [20] FilterExpr ::= PrimaryExpr |
| 8070 | * | FilterExpr Predicate |
| 8071 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8072 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8073 | * Square brackets are used to filter expressions in the same way that |
| 8074 | * they are used in location paths. It is an error if the expression to |
| 8075 | * be filtered does not evaluate to a node-set. The context node list |
| 8076 | * used for evaluating the expression in square brackets is the node-set |
| 8077 | * to be filtered listed in document order. |
| 8078 | */ |
| 8079 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8080 | static void |
| 8081 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 8082 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8083 | CHECK_ERROR; |
| 8084 | SKIP_BLANKS; |
| 8085 | |
| 8086 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8087 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8088 | SKIP_BLANKS; |
| 8089 | } |
| 8090 | |
| 8091 | |
| 8092 | } |
| 8093 | |
| 8094 | /** |
| 8095 | * xmlXPathScanName: |
| 8096 | * @ctxt: the XPath Parser context |
| 8097 | * |
| 8098 | * Trickery: parse an XML name but without consuming the input flow |
| 8099 | * Needed to avoid insanity in the parser state. |
| 8100 | * |
| 8101 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 8102 | * CombiningChar | Extender |
| 8103 | * |
| 8104 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 8105 | * |
| 8106 | * [6] Names ::= Name (S Name)* |
| 8107 | * |
| 8108 | * Returns the Name parsed or NULL |
| 8109 | */ |
| 8110 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8111 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8112 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8113 | int len = 0, l; |
| 8114 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8115 | const xmlChar *cur; |
| 8116 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8117 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8118 | cur = ctxt->cur; |
| 8119 | |
| 8120 | c = CUR_CHAR(l); |
| 8121 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 8122 | (!IS_LETTER(c) && (c != '_') && |
| 8123 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8124 | return(NULL); |
| 8125 | } |
| 8126 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8127 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 8128 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 8129 | (c == '.') || (c == '-') || |
| 8130 | (c == '_') || (c == ':') || |
| 8131 | (IS_COMBINING(c)) || |
| 8132 | (IS_EXTENDER(c)))) { |
| 8133 | len += l; |
| 8134 | NEXTL(l); |
| 8135 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8136 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8137 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 8138 | ctxt->cur = cur; |
| 8139 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8140 | } |
| 8141 | |
| 8142 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8143 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8144 | * @ctxt: the XPath Parser context |
| 8145 | * |
| 8146 | * [19] PathExpr ::= LocationPath |
| 8147 | * | FilterExpr |
| 8148 | * | FilterExpr '/' RelativeLocationPath |
| 8149 | * | FilterExpr '//' RelativeLocationPath |
| 8150 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8151 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8152 | * The / operator and // operators combine an arbitrary expression |
| 8153 | * and a relative location path. It is an error if the expression |
| 8154 | * does not evaluate to a node-set. |
| 8155 | * The / operator does composition in the same way as when / is |
| 8156 | * used in a location path. As in location paths, // is short for |
| 8157 | * /descendant-or-self::node()/. |
| 8158 | */ |
| 8159 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8160 | static void |
| 8161 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8162 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 8163 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 8164 | |
| 8165 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8166 | if ((CUR == '$') || (CUR == '(') || |
| 8167 | (IS_ASCII_DIGIT(CUR)) || |
| 8168 | (CUR == '\'') || (CUR == '"') || |
| 8169 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8170 | lc = 0; |
| 8171 | } else if (CUR == '*') { |
| 8172 | /* relative or absolute location path */ |
| 8173 | lc = 1; |
| 8174 | } else if (CUR == '/') { |
| 8175 | /* relative or absolute location path */ |
| 8176 | lc = 1; |
| 8177 | } else if (CUR == '@') { |
| 8178 | /* relative abbreviated attribute location path */ |
| 8179 | lc = 1; |
| 8180 | } else if (CUR == '.') { |
| 8181 | /* relative abbreviated attribute location path */ |
| 8182 | lc = 1; |
| 8183 | } else { |
| 8184 | /* |
| 8185 | * Problem is finding if we have a name here whether it's: |
| 8186 | * - a nodetype |
| 8187 | * - a function call in which case it's followed by '(' |
| 8188 | * - an axis in which case it's followed by ':' |
| 8189 | * - a element name |
| 8190 | * We do an a priori analysis here rather than having to |
| 8191 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8192 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8193 | * read/write/debug. |
| 8194 | */ |
| 8195 | SKIP_BLANKS; |
| 8196 | name = xmlXPathScanName(ctxt); |
| 8197 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8198 | #ifdef DEBUG_STEP |
| 8199 | xmlGenericError(xmlGenericErrorContext, |
| 8200 | "PathExpr: Axis\n"); |
| 8201 | #endif |
| 8202 | lc = 1; |
| 8203 | xmlFree(name); |
| 8204 | } else if (name != NULL) { |
| 8205 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8206 | |
| 8207 | |
| 8208 | while (NXT(len) != 0) { |
| 8209 | if (NXT(len) == '/') { |
| 8210 | /* element name */ |
| 8211 | #ifdef DEBUG_STEP |
| 8212 | xmlGenericError(xmlGenericErrorContext, |
| 8213 | "PathExpr: AbbrRelLocation\n"); |
| 8214 | #endif |
| 8215 | lc = 1; |
| 8216 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8217 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8218 | /* ignore blanks */ |
| 8219 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | } else if (NXT(len) == ':') { |
| 8221 | #ifdef DEBUG_STEP |
| 8222 | xmlGenericError(xmlGenericErrorContext, |
| 8223 | "PathExpr: AbbrRelLocation\n"); |
| 8224 | #endif |
| 8225 | lc = 1; |
| 8226 | break; |
| 8227 | } else if ((NXT(len) == '(')) { |
| 8228 | /* Note Type or Function */ |
| 8229 | if (xmlXPathIsNodeType(name)) { |
| 8230 | #ifdef DEBUG_STEP |
| 8231 | xmlGenericError(xmlGenericErrorContext, |
| 8232 | "PathExpr: Type search\n"); |
| 8233 | #endif |
| 8234 | lc = 1; |
| 8235 | } else { |
| 8236 | #ifdef DEBUG_STEP |
| 8237 | xmlGenericError(xmlGenericErrorContext, |
| 8238 | "PathExpr: function call\n"); |
| 8239 | #endif |
| 8240 | lc = 0; |
| 8241 | } |
| 8242 | break; |
| 8243 | } else if ((NXT(len) == '[')) { |
| 8244 | /* element name */ |
| 8245 | #ifdef DEBUG_STEP |
| 8246 | xmlGenericError(xmlGenericErrorContext, |
| 8247 | "PathExpr: AbbrRelLocation\n"); |
| 8248 | #endif |
| 8249 | lc = 1; |
| 8250 | break; |
| 8251 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8252 | (NXT(len) == '=')) { |
| 8253 | lc = 1; |
| 8254 | break; |
| 8255 | } else { |
| 8256 | lc = 1; |
| 8257 | break; |
| 8258 | } |
| 8259 | len++; |
| 8260 | } |
| 8261 | if (NXT(len) == 0) { |
| 8262 | #ifdef DEBUG_STEP |
| 8263 | xmlGenericError(xmlGenericErrorContext, |
| 8264 | "PathExpr: AbbrRelLocation\n"); |
| 8265 | #endif |
| 8266 | /* element name */ |
| 8267 | lc = 1; |
| 8268 | } |
| 8269 | xmlFree(name); |
| 8270 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8271 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8272 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8273 | } |
| 8274 | } |
| 8275 | |
| 8276 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8277 | if (CUR == '/') { |
| 8278 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8279 | } else { |
| 8280 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8281 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8282 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8283 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8284 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8285 | CHECK_ERROR; |
| 8286 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8287 | SKIP(2); |
| 8288 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8289 | |
| 8290 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8291 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8292 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8293 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8294 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8295 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8296 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8297 | } |
| 8298 | } |
| 8299 | SKIP_BLANKS; |
| 8300 | } |
| 8301 | |
| 8302 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8303 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8304 | * @ctxt: the XPath Parser context |
| 8305 | * |
| 8306 | * [18] UnionExpr ::= PathExpr |
| 8307 | * | UnionExpr '|' PathExpr |
| 8308 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8309 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8310 | */ |
| 8311 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8312 | static void |
| 8313 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8314 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8315 | CHECK_ERROR; |
| 8316 | SKIP_BLANKS; |
| 8317 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8318 | int op1 = ctxt->comp->last; |
| 8319 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8320 | |
| 8321 | NEXT; |
| 8322 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8323 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8324 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8325 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8326 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8327 | SKIP_BLANKS; |
| 8328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8329 | } |
| 8330 | |
| 8331 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8332 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | * @ctxt: the XPath Parser context |
| 8334 | * |
| 8335 | * [27] UnaryExpr ::= UnionExpr |
| 8336 | * | '-' UnaryExpr |
| 8337 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8338 | * Compile an unary 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 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8343 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8344 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8345 | |
| 8346 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8347 | while (CUR == '-') { |
| 8348 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8349 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8350 | NEXT; |
| 8351 | SKIP_BLANKS; |
| 8352 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8353 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8354 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8355 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8356 | if (found) { |
| 8357 | if (minus) |
| 8358 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8359 | else |
| 8360 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8361 | } |
| 8362 | } |
| 8363 | |
| 8364 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8365 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8366 | * @ctxt: the XPath Parser context |
| 8367 | * |
| 8368 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8369 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8370 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8371 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8372 | * [34] MultiplyOperator ::= '*' |
| 8373 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8374 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8375 | */ |
| 8376 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8377 | static void |
| 8378 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8379 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8380 | CHECK_ERROR; |
| 8381 | SKIP_BLANKS; |
| 8382 | while ((CUR == '*') || |
| 8383 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8384 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8385 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8386 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8387 | |
| 8388 | if (CUR == '*') { |
| 8389 | op = 0; |
| 8390 | NEXT; |
| 8391 | } else if (CUR == 'd') { |
| 8392 | op = 1; |
| 8393 | SKIP(3); |
| 8394 | } else if (CUR == 'm') { |
| 8395 | op = 2; |
| 8396 | SKIP(3); |
| 8397 | } |
| 8398 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8399 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8400 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8401 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8402 | SKIP_BLANKS; |
| 8403 | } |
| 8404 | } |
| 8405 | |
| 8406 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8407 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8408 | * @ctxt: the XPath Parser context |
| 8409 | * |
| 8410 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8411 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8412 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8413 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8414 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8415 | */ |
| 8416 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8417 | static void |
| 8418 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8419 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8420 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8421 | CHECK_ERROR; |
| 8422 | SKIP_BLANKS; |
| 8423 | while ((CUR == '+') || (CUR == '-')) { |
| 8424 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8425 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8426 | |
| 8427 | if (CUR == '+') plus = 1; |
| 8428 | else plus = 0; |
| 8429 | NEXT; |
| 8430 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8431 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8432 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8433 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8434 | SKIP_BLANKS; |
| 8435 | } |
| 8436 | } |
| 8437 | |
| 8438 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8439 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8440 | * @ctxt: the XPath Parser context |
| 8441 | * |
| 8442 | * [24] RelationalExpr ::= AdditiveExpr |
| 8443 | * | RelationalExpr '<' AdditiveExpr |
| 8444 | * | RelationalExpr '>' AdditiveExpr |
| 8445 | * | RelationalExpr '<=' AdditiveExpr |
| 8446 | * | RelationalExpr '>=' AdditiveExpr |
| 8447 | * |
| 8448 | * A <= B > C is allowed ? Answer from James, yes with |
| 8449 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8450 | * which is basically what got implemented. |
| 8451 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8452 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8453 | * on the stack |
| 8454 | */ |
| 8455 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8456 | static void |
| 8457 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8458 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8459 | CHECK_ERROR; |
| 8460 | SKIP_BLANKS; |
| 8461 | while ((CUR == '<') || |
| 8462 | (CUR == '>') || |
| 8463 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8464 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8465 | int inf, strict; |
| 8466 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8467 | |
| 8468 | if (CUR == '<') inf = 1; |
| 8469 | else inf = 0; |
| 8470 | if (NXT(1) == '=') strict = 0; |
| 8471 | else strict = 1; |
| 8472 | NEXT; |
| 8473 | if (!strict) NEXT; |
| 8474 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8475 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8476 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8477 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8478 | SKIP_BLANKS; |
| 8479 | } |
| 8480 | } |
| 8481 | |
| 8482 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8483 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | * @ctxt: the XPath Parser context |
| 8485 | * |
| 8486 | * [23] EqualityExpr ::= RelationalExpr |
| 8487 | * | EqualityExpr '=' RelationalExpr |
| 8488 | * | EqualityExpr '!=' RelationalExpr |
| 8489 | * |
| 8490 | * A != B != C is allowed ? Answer from James, yes with |
| 8491 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8492 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8493 | * which is basically what got implemented. |
| 8494 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8495 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8496 | * |
| 8497 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8498 | static void |
| 8499 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8500 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8501 | CHECK_ERROR; |
| 8502 | SKIP_BLANKS; |
| 8503 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8504 | int eq; |
| 8505 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8506 | |
| 8507 | if (CUR == '=') eq = 1; |
| 8508 | else eq = 0; |
| 8509 | NEXT; |
| 8510 | if (!eq) NEXT; |
| 8511 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8512 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8513 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8514 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8515 | SKIP_BLANKS; |
| 8516 | } |
| 8517 | } |
| 8518 | |
| 8519 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8520 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8521 | * @ctxt: the XPath Parser context |
| 8522 | * |
| 8523 | * [22] AndExpr ::= EqualityExpr |
| 8524 | * | AndExpr 'and' EqualityExpr |
| 8525 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8526 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8527 | * |
| 8528 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8529 | static void |
| 8530 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8531 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8532 | CHECK_ERROR; |
| 8533 | SKIP_BLANKS; |
| 8534 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8535 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8536 | SKIP(3); |
| 8537 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8538 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8539 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8540 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8541 | SKIP_BLANKS; |
| 8542 | } |
| 8543 | } |
| 8544 | |
| 8545 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8546 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8547 | * @ctxt: the XPath Parser context |
| 8548 | * |
| 8549 | * [14] Expr ::= OrExpr |
| 8550 | * [21] OrExpr ::= AndExpr |
| 8551 | * | OrExpr 'or' AndExpr |
| 8552 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8553 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8554 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8555 | static void |
| 8556 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 8557 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8558 | CHECK_ERROR; |
| 8559 | SKIP_BLANKS; |
| 8560 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8561 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8562 | SKIP(2); |
| 8563 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8564 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8565 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8566 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8567 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8568 | SKIP_BLANKS; |
| 8569 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8570 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 8571 | /* more ops could be optimized too */ |
| 8572 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8573 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8574 | } |
| 8575 | |
| 8576 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8577 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8578 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8579 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | * |
| 8581 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8582 | * [9] PredicateExpr ::= Expr |
| 8583 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8584 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8585 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8586 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8587 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8588 | int op1 = ctxt->comp->last; |
| 8589 | |
| 8590 | SKIP_BLANKS; |
| 8591 | if (CUR != '[') { |
| 8592 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8593 | } |
| 8594 | NEXT; |
| 8595 | SKIP_BLANKS; |
| 8596 | |
| 8597 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8598 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8599 | CHECK_ERROR; |
| 8600 | |
| 8601 | if (CUR != ']') { |
| 8602 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8603 | } |
| 8604 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8605 | if (filter) |
| 8606 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8607 | else |
| 8608 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8609 | |
| 8610 | NEXT; |
| 8611 | SKIP_BLANKS; |
| 8612 | } |
| 8613 | |
| 8614 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8615 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8616 | * @ctxt: the XPath Parser context |
| 8617 | * @test: pointer to a xmlXPathTestVal |
| 8618 | * @type: pointer to a xmlXPathTypeVal |
| 8619 | * @prefix: placeholder for a possible name prefix |
| 8620 | * |
| 8621 | * [7] NodeTest ::= NameTest |
| 8622 | * | NodeType '(' ')' |
| 8623 | * | 'processing-instruction' '(' Literal ')' |
| 8624 | * |
| 8625 | * [37] NameTest ::= '*' |
| 8626 | * | NCName ':' '*' |
| 8627 | * | QName |
| 8628 | * [38] NodeType ::= 'comment' |
| 8629 | * | 'text' |
| 8630 | * | 'processing-instruction' |
| 8631 | * | 'node' |
| 8632 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8633 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8634 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8635 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8636 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8637 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8638 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8639 | int blanks; |
| 8640 | |
| 8641 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8642 | STRANGE; |
| 8643 | return(NULL); |
| 8644 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8645 | *type = (xmlXPathTypeVal) 0; |
| 8646 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8647 | *prefix = NULL; |
| 8648 | SKIP_BLANKS; |
| 8649 | |
| 8650 | if ((name == NULL) && (CUR == '*')) { |
| 8651 | /* |
| 8652 | * All elements |
| 8653 | */ |
| 8654 | NEXT; |
| 8655 | *test = NODE_TEST_ALL; |
| 8656 | return(NULL); |
| 8657 | } |
| 8658 | |
| 8659 | if (name == NULL) |
| 8660 | name = xmlXPathParseNCName(ctxt); |
| 8661 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8662 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8663 | } |
| 8664 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8665 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8666 | SKIP_BLANKS; |
| 8667 | if (CUR == '(') { |
| 8668 | NEXT; |
| 8669 | /* |
| 8670 | * NodeType or PI search |
| 8671 | */ |
| 8672 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8673 | *type = NODE_TYPE_COMMENT; |
| 8674 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8675 | *type = NODE_TYPE_NODE; |
| 8676 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8677 | *type = NODE_TYPE_PI; |
| 8678 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8679 | *type = NODE_TYPE_TEXT; |
| 8680 | else { |
| 8681 | if (name != NULL) |
| 8682 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8683 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8684 | } |
| 8685 | |
| 8686 | *test = NODE_TEST_TYPE; |
| 8687 | |
| 8688 | SKIP_BLANKS; |
| 8689 | if (*type == NODE_TYPE_PI) { |
| 8690 | /* |
| 8691 | * Specific case: search a PI by name. |
| 8692 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8693 | if (name != NULL) |
| 8694 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8695 | name = NULL; |
| 8696 | if (CUR != ')') { |
| 8697 | name = xmlXPathParseLiteral(ctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8698 | CHECK_ERROR NULL; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8699 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8700 | SKIP_BLANKS; |
| 8701 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8702 | } |
| 8703 | if (CUR != ')') { |
| 8704 | if (name != NULL) |
| 8705 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8706 | XP_ERRORNULL(XPATH_UNCLOSED_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8707 | } |
| 8708 | NEXT; |
| 8709 | return(name); |
| 8710 | } |
| 8711 | *test = NODE_TEST_NAME; |
| 8712 | if ((!blanks) && (CUR == ':')) { |
| 8713 | NEXT; |
| 8714 | |
| 8715 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8716 | * Since currently the parser context don't have a |
| 8717 | * namespace list associated: |
| 8718 | * The namespace name for this prefix can be computed |
| 8719 | * only at evaluation time. The compilation is done |
| 8720 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8721 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8722 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8723 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8724 | if (name != NULL) |
| 8725 | xmlFree(name); |
| 8726 | if (*prefix == NULL) { |
| 8727 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8728 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8729 | #else |
| 8730 | *prefix = name; |
| 8731 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8732 | |
| 8733 | if (CUR == '*') { |
| 8734 | /* |
| 8735 | * All elements |
| 8736 | */ |
| 8737 | NEXT; |
| 8738 | *test = NODE_TEST_ALL; |
| 8739 | return(NULL); |
| 8740 | } |
| 8741 | |
| 8742 | name = xmlXPathParseNCName(ctxt); |
| 8743 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8744 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8745 | } |
| 8746 | } |
| 8747 | return(name); |
| 8748 | } |
| 8749 | |
| 8750 | /** |
| 8751 | * xmlXPathIsAxisName: |
| 8752 | * @name: a preparsed name token |
| 8753 | * |
| 8754 | * [6] AxisName ::= 'ancestor' |
| 8755 | * | 'ancestor-or-self' |
| 8756 | * | 'attribute' |
| 8757 | * | 'child' |
| 8758 | * | 'descendant' |
| 8759 | * | 'descendant-or-self' |
| 8760 | * | 'following' |
| 8761 | * | 'following-sibling' |
| 8762 | * | 'namespace' |
| 8763 | * | 'parent' |
| 8764 | * | 'preceding' |
| 8765 | * | 'preceding-sibling' |
| 8766 | * | 'self' |
| 8767 | * |
| 8768 | * Returns the axis or 0 |
| 8769 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8770 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8771 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8772 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8773 | switch (name[0]) { |
| 8774 | case 'a': |
| 8775 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8776 | ret = AXIS_ANCESTOR; |
| 8777 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8778 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8779 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8780 | ret = AXIS_ATTRIBUTE; |
| 8781 | break; |
| 8782 | case 'c': |
| 8783 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8784 | ret = AXIS_CHILD; |
| 8785 | break; |
| 8786 | case 'd': |
| 8787 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8788 | ret = AXIS_DESCENDANT; |
| 8789 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8790 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8791 | break; |
| 8792 | case 'f': |
| 8793 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8794 | ret = AXIS_FOLLOWING; |
| 8795 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8796 | ret = AXIS_FOLLOWING_SIBLING; |
| 8797 | break; |
| 8798 | case 'n': |
| 8799 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8800 | ret = AXIS_NAMESPACE; |
| 8801 | break; |
| 8802 | case 'p': |
| 8803 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8804 | ret = AXIS_PARENT; |
| 8805 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8806 | ret = AXIS_PRECEDING; |
| 8807 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8808 | ret = AXIS_PRECEDING_SIBLING; |
| 8809 | break; |
| 8810 | case 's': |
| 8811 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8812 | ret = AXIS_SELF; |
| 8813 | break; |
| 8814 | } |
| 8815 | return(ret); |
| 8816 | } |
| 8817 | |
| 8818 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8819 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8820 | * @ctxt: the XPath Parser context |
| 8821 | * |
| 8822 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8823 | * | AbbreviatedStep |
| 8824 | * |
| 8825 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8826 | * |
| 8827 | * [5] AxisSpecifier ::= AxisName '::' |
| 8828 | * | AbbreviatedAxisSpecifier |
| 8829 | * |
| 8830 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8831 | * |
| 8832 | * Modified for XPtr range support as: |
| 8833 | * |
| 8834 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8835 | * | AbbreviatedStep |
| 8836 | * | 'range-to' '(' Expr ')' Predicate* |
| 8837 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8838 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8839 | * A location step of . is short for self::node(). This is |
| 8840 | * particularly useful in conjunction with //. For example, the |
| 8841 | * location path .//para is short for |
| 8842 | * self::node()/descendant-or-self::node()/child::para |
| 8843 | * and so will select all para descendant elements of the context |
| 8844 | * node. |
| 8845 | * Similarly, a location step of .. is short for parent::node(). |
| 8846 | * For example, ../title is short for parent::node()/child::title |
| 8847 | * and so will select the title children of the parent of the context |
| 8848 | * node. |
| 8849 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8850 | static void |
| 8851 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8852 | #ifdef LIBXML_XPTR_ENABLED |
| 8853 | int rangeto = 0; |
| 8854 | int op2 = -1; |
| 8855 | #endif |
| 8856 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8857 | SKIP_BLANKS; |
| 8858 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8859 | SKIP(2); |
| 8860 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8861 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8862 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8863 | } else if (CUR == '.') { |
| 8864 | NEXT; |
| 8865 | SKIP_BLANKS; |
| 8866 | } else { |
| 8867 | xmlChar *name = NULL; |
| 8868 | const xmlChar *prefix = NULL; |
| 8869 | xmlXPathTestVal test; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8870 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8871 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8872 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8873 | |
| 8874 | /* |
| 8875 | * The modification needed for XPointer change to the production |
| 8876 | */ |
| 8877 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8878 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8879 | name = xmlXPathParseNCName(ctxt); |
| 8880 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8881 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8882 | xmlFree(name); |
| 8883 | SKIP_BLANKS; |
| 8884 | if (CUR != '(') { |
| 8885 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8886 | } |
| 8887 | NEXT; |
| 8888 | SKIP_BLANKS; |
| 8889 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8890 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8891 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8892 | CHECK_ERROR; |
| 8893 | |
| 8894 | SKIP_BLANKS; |
| 8895 | if (CUR != ')') { |
| 8896 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8897 | } |
| 8898 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8899 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8900 | goto eval_predicates; |
| 8901 | } |
| 8902 | } |
| 8903 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8904 | if (CUR == '*') { |
| 8905 | axis = AXIS_CHILD; |
| 8906 | } else { |
| 8907 | if (name == NULL) |
| 8908 | name = xmlXPathParseNCName(ctxt); |
| 8909 | if (name != NULL) { |
| 8910 | axis = xmlXPathIsAxisName(name); |
| 8911 | if (axis != 0) { |
| 8912 | SKIP_BLANKS; |
| 8913 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8914 | SKIP(2); |
| 8915 | xmlFree(name); |
| 8916 | name = NULL; |
| 8917 | } else { |
| 8918 | /* an element name can conflict with an axis one :-\ */ |
| 8919 | axis = AXIS_CHILD; |
| 8920 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8921 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8922 | axis = AXIS_CHILD; |
| 8923 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8924 | } else if (CUR == '@') { |
| 8925 | NEXT; |
| 8926 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8927 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8928 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8929 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8930 | } |
| 8931 | |
| 8932 | CHECK_ERROR; |
| 8933 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8934 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8935 | if (test == 0) |
| 8936 | return; |
| 8937 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 8938 | if ((prefix != NULL) && (ctxt->context != NULL) && |
| 8939 | (ctxt->context->flags & XML_XPATH_CHECKNS)) { |
| 8940 | if (xmlXPathNsLookup(ctxt->context, prefix) == NULL) { |
| 8941 | xmlXPathErr(ctxt, XPATH_UNDEF_PREFIX_ERROR); |
| 8942 | } |
| 8943 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8944 | #ifdef DEBUG_STEP |
| 8945 | xmlGenericError(xmlGenericErrorContext, |
| 8946 | "Basis : computing new set\n"); |
| 8947 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8948 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8949 | #ifdef DEBUG_STEP |
| 8950 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8951 | if (ctxt->value == NULL) |
| 8952 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8953 | else if (ctxt->value->nodesetval == NULL) |
| 8954 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8955 | else |
| 8956 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8957 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8958 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8959 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8960 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8961 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8962 | op1 = ctxt->comp->last; |
| 8963 | ctxt->comp->last = -1; |
| 8964 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8965 | SKIP_BLANKS; |
| 8966 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8967 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8968 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8969 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8970 | #ifdef LIBXML_XPTR_ENABLED |
| 8971 | if (rangeto) { |
| 8972 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8973 | } else |
| 8974 | #endif |
| 8975 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8976 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8977 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8978 | } |
| 8979 | #ifdef DEBUG_STEP |
| 8980 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8981 | if (ctxt->value == NULL) |
| 8982 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8983 | else if (ctxt->value->nodesetval == NULL) |
| 8984 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8985 | else |
| 8986 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8987 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8988 | #endif |
| 8989 | } |
| 8990 | |
| 8991 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8992 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8993 | * @ctxt: the XPath Parser context |
| 8994 | * |
| 8995 | * [3] RelativeLocationPath ::= Step |
| 8996 | * | RelativeLocationPath '/' Step |
| 8997 | * | AbbreviatedRelativeLocationPath |
| 8998 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8999 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9000 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9001 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9002 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9003 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9004 | (xmlXPathParserContextPtr ctxt) { |
| 9005 | SKIP_BLANKS; |
| 9006 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9007 | SKIP(2); |
| 9008 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9009 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 9010 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9011 | } else if (CUR == '/') { |
| 9012 | NEXT; |
| 9013 | SKIP_BLANKS; |
| 9014 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9015 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9016 | SKIP_BLANKS; |
| 9017 | while (CUR == '/') { |
| 9018 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9019 | SKIP(2); |
| 9020 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9021 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9022 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9023 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9024 | } else if (CUR == '/') { |
| 9025 | NEXT; |
| 9026 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9027 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9028 | } |
| 9029 | SKIP_BLANKS; |
| 9030 | } |
| 9031 | } |
| 9032 | |
| 9033 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9034 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9035 | * @ctxt: the XPath Parser context |
| 9036 | * |
| 9037 | * [1] LocationPath ::= RelativeLocationPath |
| 9038 | * | AbsoluteLocationPath |
| 9039 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 9040 | * | AbbreviatedAbsoluteLocationPath |
| 9041 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 9042 | * '//' RelativeLocationPath |
| 9043 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9044 | * Compile a location path |
| 9045 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9046 | * // is short for /descendant-or-self::node()/. For example, |
| 9047 | * //para is short for /descendant-or-self::node()/child::para and |
| 9048 | * so will select any para element in the document (even a para element |
| 9049 | * that is a document element will be selected by //para since the |
| 9050 | * document element node is a child of the root node); div//para is |
| 9051 | * short for div/descendant-or-self::node()/child::para and so will |
| 9052 | * select all para descendants of div children. |
| 9053 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9054 | static void |
| 9055 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9056 | SKIP_BLANKS; |
| 9057 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9058 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9059 | } else { |
| 9060 | while (CUR == '/') { |
| 9061 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9062 | SKIP(2); |
| 9063 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9064 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 9065 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9066 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9067 | } else if (CUR == '/') { |
| 9068 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9069 | SKIP_BLANKS; |
| 9070 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 9071 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9072 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9073 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9074 | } |
| 9075 | } |
| 9076 | } |
| 9077 | } |
| 9078 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9079 | /************************************************************************ |
| 9080 | * * |
| 9081 | * XPath precompiled expression evaluation * |
| 9082 | * * |
| 9083 | ************************************************************************/ |
| 9084 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9085 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9086 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 9087 | |
| 9088 | /** |
| 9089 | * xmlXPathNodeCollectAndTest: |
| 9090 | * @ctxt: the XPath Parser context |
| 9091 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9092 | * @first: pointer to the first element in document order |
| 9093 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9094 | * |
| 9095 | * This is the function implementing a step: based on the current list |
| 9096 | * 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] | 9097 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9098 | * |
| 9099 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9100 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9101 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9102 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9103 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9104 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9105 | xmlXPathStepOpPtr op, |
| 9106 | xmlNodePtr * first, xmlNodePtr * last) |
| 9107 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9108 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9109 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9110 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9111 | const xmlChar *prefix = op->value4; |
| 9112 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9113 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9114 | |
| 9115 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9116 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9117 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9118 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9119 | xmlNodeSetPtr ret, list; |
| 9120 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9121 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9122 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9123 | xmlNodePtr cur = NULL; |
| 9124 | xmlXPathObjectPtr obj; |
| 9125 | xmlNodeSetPtr nodelist; |
| 9126 | xmlNodePtr tmp; |
| 9127 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9128 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9129 | obj = valuePop(ctxt); |
| 9130 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9131 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9132 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9133 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9134 | if (URI == NULL) { |
| 9135 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9136 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9137 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9138 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9139 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9140 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9141 | #endif |
| 9142 | switch (axis) { |
| 9143 | case AXIS_ANCESTOR: |
| 9144 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9145 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9146 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9147 | first = NULL; |
| 9148 | next = xmlXPathNextAncestor; |
| 9149 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9150 | case AXIS_ANCESTOR_OR_SELF: |
| 9151 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9152 | xmlGenericError(xmlGenericErrorContext, |
| 9153 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9154 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9155 | first = NULL; |
| 9156 | next = xmlXPathNextAncestorOrSelf; |
| 9157 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9158 | case AXIS_ATTRIBUTE: |
| 9159 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9160 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
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 | first = NULL; |
| 9163 | last = NULL; |
| 9164 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9165 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9166 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9167 | case AXIS_CHILD: |
| 9168 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9169 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
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 | last = NULL; |
| 9172 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9173 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9174 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9175 | case AXIS_DESCENDANT: |
| 9176 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9177 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9178 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9179 | last = NULL; |
| 9180 | next = xmlXPathNextDescendant; |
| 9181 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9182 | case AXIS_DESCENDANT_OR_SELF: |
| 9183 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9184 | xmlGenericError(xmlGenericErrorContext, |
| 9185 | "axis 'descendant-or-self' "); |
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 | last = NULL; |
| 9188 | next = xmlXPathNextDescendantOrSelf; |
| 9189 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9190 | case AXIS_FOLLOWING: |
| 9191 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9192 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9193 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9194 | last = NULL; |
| 9195 | next = xmlXPathNextFollowing; |
| 9196 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9197 | case AXIS_FOLLOWING_SIBLING: |
| 9198 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9199 | xmlGenericError(xmlGenericErrorContext, |
| 9200 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9201 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9202 | last = NULL; |
| 9203 | next = xmlXPathNextFollowingSibling; |
| 9204 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9205 | case AXIS_NAMESPACE: |
| 9206 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9207 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9208 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9209 | first = NULL; |
| 9210 | last = NULL; |
| 9211 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9212 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9213 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9214 | case AXIS_PARENT: |
| 9215 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9216 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9217 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9218 | first = NULL; |
| 9219 | next = xmlXPathNextParent; |
| 9220 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9221 | case AXIS_PRECEDING: |
| 9222 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9223 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9224 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9225 | first = NULL; |
| 9226 | next = xmlXPathNextPrecedingInternal; |
| 9227 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9228 | case AXIS_PRECEDING_SIBLING: |
| 9229 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9230 | xmlGenericError(xmlGenericErrorContext, |
| 9231 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9232 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9233 | first = NULL; |
| 9234 | next = xmlXPathNextPrecedingSibling; |
| 9235 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9236 | case AXIS_SELF: |
| 9237 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9238 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9239 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9240 | first = NULL; |
| 9241 | last = NULL; |
| 9242 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9243 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9244 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9245 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9246 | if (next == NULL) { |
| 9247 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9248 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9249 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9250 | |
| 9251 | nodelist = obj->nodesetval; |
| 9252 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9253 | xmlXPathFreeObject(obj); |
| 9254 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9255 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9256 | } |
| 9257 | addNode = xmlXPathNodeSetAddUnique; |
| 9258 | ret = NULL; |
| 9259 | #ifdef DEBUG_STEP |
| 9260 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9261 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9262 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9263 | case NODE_TEST_NONE: |
| 9264 | xmlGenericError(xmlGenericErrorContext, |
| 9265 | " searching for none !!!\n"); |
| 9266 | break; |
| 9267 | case NODE_TEST_TYPE: |
| 9268 | xmlGenericError(xmlGenericErrorContext, |
| 9269 | " searching for type %d\n", type); |
| 9270 | break; |
| 9271 | case NODE_TEST_PI: |
| 9272 | xmlGenericError(xmlGenericErrorContext, |
| 9273 | " searching for PI !!!\n"); |
| 9274 | break; |
| 9275 | case NODE_TEST_ALL: |
| 9276 | xmlGenericError(xmlGenericErrorContext, |
| 9277 | " searching for *\n"); |
| 9278 | break; |
| 9279 | case NODE_TEST_NS: |
| 9280 | xmlGenericError(xmlGenericErrorContext, |
| 9281 | " searching for namespace %s\n", |
| 9282 | prefix); |
| 9283 | break; |
| 9284 | case NODE_TEST_NAME: |
| 9285 | xmlGenericError(xmlGenericErrorContext, |
| 9286 | " searching for name %s\n", name); |
| 9287 | if (prefix != NULL) |
| 9288 | xmlGenericError(xmlGenericErrorContext, |
| 9289 | " with namespace %s\n", prefix); |
| 9290 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9291 | } |
| 9292 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9293 | #endif |
| 9294 | /* |
| 9295 | * 2.3 Node Tests |
| 9296 | * - For the attribute axis, the principal node type is attribute. |
| 9297 | * - For the namespace axis, the principal node type is namespace. |
| 9298 | * - For other axes, the principal node type is element. |
| 9299 | * |
| 9300 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9301 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9302 | * select all element children of the context node |
| 9303 | */ |
| 9304 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9305 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9306 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9307 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9308 | cur = NULL; |
| 9309 | list = xmlXPathNodeSetCreate(NULL); |
| 9310 | do { |
| 9311 | cur = next(ctxt, cur); |
| 9312 | if (cur == NULL) |
| 9313 | break; |
| 9314 | if ((first != NULL) && (*first == cur)) |
| 9315 | break; |
| 9316 | if (((t % 256) == 0) && |
| 9317 | (first != NULL) && (*first != NULL) && |
| 9318 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9319 | break; |
| 9320 | if ((last != NULL) && (*last == cur)) |
| 9321 | break; |
| 9322 | if (((t % 256) == 0) && |
| 9323 | (last != NULL) && (*last != NULL) && |
| 9324 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9325 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9326 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9327 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9328 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9329 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9330 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9331 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9332 | ctxt->context->node = tmp; |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9333 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9334 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9335 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9336 | if ((cur->type == type) || |
| 9337 | ((type == NODE_TYPE_NODE) && |
| 9338 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9339 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9340 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9341 | (cur->type == XML_NAMESPACE_DECL) || |
| 9342 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9343 | (cur->type == XML_PI_NODE) || |
| 9344 | (cur->type == XML_COMMENT_NODE) || |
| 9345 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9346 | (cur->type == XML_TEXT_NODE))) || |
| 9347 | ((type == NODE_TYPE_TEXT) && |
| 9348 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9349 | #ifdef DEBUG_STEP |
| 9350 | n++; |
| 9351 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9352 | addNode(list, cur); |
| 9353 | } |
| 9354 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9355 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9356 | if (cur->type == XML_PI_NODE) { |
| 9357 | if ((name != NULL) && |
| 9358 | (!xmlStrEqual(name, cur->name))) |
| 9359 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9360 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9361 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9362 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9363 | addNode(list, cur); |
| 9364 | } |
| 9365 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9366 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9367 | if (axis == AXIS_ATTRIBUTE) { |
| 9368 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9369 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9370 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9371 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9372 | addNode(list, cur); |
| 9373 | } |
| 9374 | } else if (axis == AXIS_NAMESPACE) { |
| 9375 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9376 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9377 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9378 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9379 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9380 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9381 | } |
| 9382 | } else { |
| 9383 | if (cur->type == XML_ELEMENT_NODE) { |
| 9384 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9385 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9386 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9387 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9388 | addNode(list, cur); |
| 9389 | } else if ((cur->ns != NULL) && |
| 9390 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9391 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9392 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9393 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9394 | addNode(list, cur); |
| 9395 | } |
| 9396 | } |
| 9397 | } |
| 9398 | break; |
| 9399 | case NODE_TEST_NS:{ |
| 9400 | TODO; |
| 9401 | break; |
| 9402 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9403 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9404 | switch (cur->type) { |
| 9405 | case XML_ELEMENT_NODE: |
| 9406 | if (xmlStrEqual(name, cur->name)) { |
| 9407 | if (prefix == NULL) { |
| 9408 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9409 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9410 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9411 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9412 | addNode(list, cur); |
| 9413 | } |
| 9414 | } else { |
| 9415 | if ((cur->ns != NULL) && |
| 9416 | (xmlStrEqual(URI, |
| 9417 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9418 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9419 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9420 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9421 | addNode(list, cur); |
| 9422 | } |
| 9423 | } |
| 9424 | } |
| 9425 | break; |
| 9426 | case XML_ATTRIBUTE_NODE:{ |
| 9427 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9428 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9429 | if (xmlStrEqual(name, attr->name)) { |
| 9430 | if (prefix == NULL) { |
| 9431 | if ((attr->ns == NULL) || |
| 9432 | (attr->ns->prefix == NULL)) { |
| 9433 | #ifdef DEBUG_STEP |
| 9434 | n++; |
| 9435 | #endif |
| 9436 | addNode(list, |
| 9437 | (xmlNodePtr) attr); |
| 9438 | } |
| 9439 | } else { |
| 9440 | if ((attr->ns != NULL) && |
| 9441 | (xmlStrEqual(URI, |
| 9442 | attr->ns-> |
| 9443 | href))) { |
| 9444 | #ifdef DEBUG_STEP |
| 9445 | n++; |
| 9446 | #endif |
| 9447 | addNode(list, |
| 9448 | (xmlNodePtr) attr); |
| 9449 | } |
| 9450 | } |
| 9451 | } |
| 9452 | break; |
| 9453 | } |
| 9454 | case XML_NAMESPACE_DECL: |
| 9455 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9456 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9457 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9458 | if ((ns->prefix != NULL) && (name != NULL) |
| 9459 | && (xmlStrEqual(ns->prefix, name))) { |
| 9460 | #ifdef DEBUG_STEP |
| 9461 | n++; |
| 9462 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9463 | xmlXPathNodeSetAddNs(list, |
| 9464 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9465 | } |
| 9466 | } |
| 9467 | break; |
| 9468 | default: |
| 9469 | break; |
| 9470 | } |
| 9471 | break; |
| 9472 | break; |
| 9473 | } |
| 9474 | } while (cur != NULL); |
| 9475 | |
| 9476 | /* |
| 9477 | * If there is some predicate filtering do it now |
| 9478 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9479 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9480 | xmlXPathObjectPtr obj2; |
| 9481 | |
| 9482 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9483 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9484 | CHECK_TYPE0(XPATH_NODESET); |
| 9485 | obj2 = valuePop(ctxt); |
| 9486 | list = obj2->nodesetval; |
| 9487 | obj2->nodesetval = NULL; |
| 9488 | xmlXPathFreeObject(obj2); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9489 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 9490 | xmlXPathFreeObject(obj); |
| 9491 | xmlXPathFreeNodeSet(list); |
| 9492 | return(0); |
| 9493 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9494 | } |
| 9495 | if (ret == NULL) { |
| 9496 | ret = list; |
| 9497 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9498 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9499 | xmlXPathFreeNodeSet(list); |
| 9500 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9501 | } |
| 9502 | ctxt->context->node = tmp; |
| 9503 | #ifdef DEBUG_STEP |
| 9504 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9505 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9506 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9507 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9508 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9509 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9510 | ctxt->value->boolval = 1; |
| 9511 | ctxt->value->user = obj->user; |
| 9512 | obj->user = NULL; |
| 9513 | obj->boolval = 0; |
| 9514 | } |
| 9515 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9516 | return(t); |
| 9517 | } |
| 9518 | |
| 9519 | /** |
| 9520 | * xmlXPathNodeCollectAndTestNth: |
| 9521 | * @ctxt: the XPath Parser context |
| 9522 | * @op: the XPath precompiled step operation |
| 9523 | * @indx: the index to collect |
| 9524 | * @first: pointer to the first element in document order |
| 9525 | * @last: pointer to the last element in document order |
| 9526 | * |
| 9527 | * This is the function implementing a step: based on the current list |
| 9528 | * 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] | 9529 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9530 | * |
| 9531 | * Pushes the new NodeSet resulting from the search. |
| 9532 | * Returns the number of node traversed |
| 9533 | */ |
| 9534 | static int |
| 9535 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9536 | xmlXPathStepOpPtr op, int indx, |
| 9537 | xmlNodePtr * first, xmlNodePtr * last) |
| 9538 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9539 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9540 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9541 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9542 | const xmlChar *prefix = op->value4; |
| 9543 | const xmlChar *name = op->value5; |
| 9544 | const xmlChar *URI = NULL; |
| 9545 | int n = 0, t = 0; |
| 9546 | |
| 9547 | int i; |
| 9548 | xmlNodeSetPtr list; |
| 9549 | xmlXPathTraversalFunction next = NULL; |
| 9550 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9551 | xmlNodePtr cur = NULL; |
| 9552 | xmlXPathObjectPtr obj; |
| 9553 | xmlNodeSetPtr nodelist; |
| 9554 | xmlNodePtr tmp; |
| 9555 | |
| 9556 | CHECK_TYPE0(XPATH_NODESET); |
| 9557 | obj = valuePop(ctxt); |
| 9558 | addNode = xmlXPathNodeSetAdd; |
| 9559 | if (prefix != NULL) { |
| 9560 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9561 | if (URI == NULL) { |
| 9562 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9563 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9564 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9565 | } |
| 9566 | #ifdef DEBUG_STEP_NTH |
| 9567 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9568 | if (first != NULL) { |
| 9569 | if (*first != NULL) |
| 9570 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9571 | (*first)->name); |
| 9572 | else |
| 9573 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9574 | } |
| 9575 | if (last != NULL) { |
| 9576 | if (*last != NULL) |
| 9577 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9578 | (*last)->name); |
| 9579 | else |
| 9580 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9581 | } |
| 9582 | #endif |
| 9583 | switch (axis) { |
| 9584 | case AXIS_ANCESTOR: |
| 9585 | #ifdef DEBUG_STEP_NTH |
| 9586 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9587 | #endif |
| 9588 | first = NULL; |
| 9589 | next = xmlXPathNextAncestor; |
| 9590 | break; |
| 9591 | case AXIS_ANCESTOR_OR_SELF: |
| 9592 | #ifdef DEBUG_STEP_NTH |
| 9593 | xmlGenericError(xmlGenericErrorContext, |
| 9594 | "axis 'ancestors-or-self' "); |
| 9595 | #endif |
| 9596 | first = NULL; |
| 9597 | next = xmlXPathNextAncestorOrSelf; |
| 9598 | break; |
| 9599 | case AXIS_ATTRIBUTE: |
| 9600 | #ifdef DEBUG_STEP_NTH |
| 9601 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 9602 | #endif |
| 9603 | first = NULL; |
| 9604 | last = NULL; |
| 9605 | next = xmlXPathNextAttribute; |
| 9606 | break; |
| 9607 | case AXIS_CHILD: |
| 9608 | #ifdef DEBUG_STEP_NTH |
| 9609 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9610 | #endif |
| 9611 | last = NULL; |
| 9612 | next = xmlXPathNextChild; |
| 9613 | break; |
| 9614 | case AXIS_DESCENDANT: |
| 9615 | #ifdef DEBUG_STEP_NTH |
| 9616 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9617 | #endif |
| 9618 | last = NULL; |
| 9619 | next = xmlXPathNextDescendant; |
| 9620 | break; |
| 9621 | case AXIS_DESCENDANT_OR_SELF: |
| 9622 | #ifdef DEBUG_STEP_NTH |
| 9623 | xmlGenericError(xmlGenericErrorContext, |
| 9624 | "axis 'descendant-or-self' "); |
| 9625 | #endif |
| 9626 | last = NULL; |
| 9627 | next = xmlXPathNextDescendantOrSelf; |
| 9628 | break; |
| 9629 | case AXIS_FOLLOWING: |
| 9630 | #ifdef DEBUG_STEP_NTH |
| 9631 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9632 | #endif |
| 9633 | last = NULL; |
| 9634 | next = xmlXPathNextFollowing; |
| 9635 | break; |
| 9636 | case AXIS_FOLLOWING_SIBLING: |
| 9637 | #ifdef DEBUG_STEP_NTH |
| 9638 | xmlGenericError(xmlGenericErrorContext, |
| 9639 | "axis 'following-siblings' "); |
| 9640 | #endif |
| 9641 | last = NULL; |
| 9642 | next = xmlXPathNextFollowingSibling; |
| 9643 | break; |
| 9644 | case AXIS_NAMESPACE: |
| 9645 | #ifdef DEBUG_STEP_NTH |
| 9646 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9647 | #endif |
| 9648 | last = NULL; |
| 9649 | first = NULL; |
| 9650 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9651 | break; |
| 9652 | case AXIS_PARENT: |
| 9653 | #ifdef DEBUG_STEP_NTH |
| 9654 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9655 | #endif |
| 9656 | first = NULL; |
| 9657 | next = xmlXPathNextParent; |
| 9658 | break; |
| 9659 | case AXIS_PRECEDING: |
| 9660 | #ifdef DEBUG_STEP_NTH |
| 9661 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9662 | #endif |
| 9663 | first = NULL; |
| 9664 | next = xmlXPathNextPrecedingInternal; |
| 9665 | break; |
| 9666 | case AXIS_PRECEDING_SIBLING: |
| 9667 | #ifdef DEBUG_STEP_NTH |
| 9668 | xmlGenericError(xmlGenericErrorContext, |
| 9669 | "axis 'preceding-sibling' "); |
| 9670 | #endif |
| 9671 | first = NULL; |
| 9672 | next = xmlXPathNextPrecedingSibling; |
| 9673 | break; |
| 9674 | case AXIS_SELF: |
| 9675 | #ifdef DEBUG_STEP_NTH |
| 9676 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9677 | #endif |
| 9678 | first = NULL; |
| 9679 | last = NULL; |
| 9680 | next = xmlXPathNextSelf; |
| 9681 | break; |
| 9682 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9683 | if (next == NULL) { |
| 9684 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9685 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9686 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9687 | |
| 9688 | nodelist = obj->nodesetval; |
| 9689 | if (nodelist == NULL) { |
| 9690 | xmlXPathFreeObject(obj); |
| 9691 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9692 | return(0); |
| 9693 | } |
| 9694 | addNode = xmlXPathNodeSetAddUnique; |
| 9695 | #ifdef DEBUG_STEP_NTH |
| 9696 | xmlGenericError(xmlGenericErrorContext, |
| 9697 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9698 | switch (test) { |
| 9699 | case NODE_TEST_NONE: |
| 9700 | xmlGenericError(xmlGenericErrorContext, |
| 9701 | " searching for none !!!\n"); |
| 9702 | break; |
| 9703 | case NODE_TEST_TYPE: |
| 9704 | xmlGenericError(xmlGenericErrorContext, |
| 9705 | " searching for type %d\n", type); |
| 9706 | break; |
| 9707 | case NODE_TEST_PI: |
| 9708 | xmlGenericError(xmlGenericErrorContext, |
| 9709 | " searching for PI !!!\n"); |
| 9710 | break; |
| 9711 | case NODE_TEST_ALL: |
| 9712 | xmlGenericError(xmlGenericErrorContext, |
| 9713 | " searching for *\n"); |
| 9714 | break; |
| 9715 | case NODE_TEST_NS: |
| 9716 | xmlGenericError(xmlGenericErrorContext, |
| 9717 | " searching for namespace %s\n", |
| 9718 | prefix); |
| 9719 | break; |
| 9720 | case NODE_TEST_NAME: |
| 9721 | xmlGenericError(xmlGenericErrorContext, |
| 9722 | " searching for name %s\n", name); |
| 9723 | if (prefix != NULL) |
| 9724 | xmlGenericError(xmlGenericErrorContext, |
| 9725 | " with namespace %s\n", prefix); |
| 9726 | break; |
| 9727 | } |
| 9728 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9729 | #endif |
| 9730 | /* |
| 9731 | * 2.3 Node Tests |
| 9732 | * - For the attribute axis, the principal node type is attribute. |
| 9733 | * - For the namespace axis, the principal node type is namespace. |
| 9734 | * - For other axes, the principal node type is element. |
| 9735 | * |
| 9736 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9737 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9738 | * select all element children of the context node |
| 9739 | */ |
| 9740 | tmp = ctxt->context->node; |
| 9741 | list = xmlXPathNodeSetCreate(NULL); |
| 9742 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9743 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9744 | |
| 9745 | cur = NULL; |
| 9746 | n = 0; |
| 9747 | do { |
| 9748 | cur = next(ctxt, cur); |
| 9749 | if (cur == NULL) |
| 9750 | break; |
| 9751 | if ((first != NULL) && (*first == cur)) |
| 9752 | break; |
| 9753 | if (((t % 256) == 0) && |
| 9754 | (first != NULL) && (*first != NULL) && |
| 9755 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9756 | break; |
| 9757 | if ((last != NULL) && (*last == cur)) |
| 9758 | break; |
| 9759 | if (((t % 256) == 0) && |
| 9760 | (last != NULL) && (*last != NULL) && |
| 9761 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9762 | break; |
| 9763 | t++; |
| 9764 | switch (test) { |
| 9765 | case NODE_TEST_NONE: |
| 9766 | ctxt->context->node = tmp; |
| 9767 | STRANGE return(0); |
| 9768 | case NODE_TEST_TYPE: |
| 9769 | if ((cur->type == type) || |
| 9770 | ((type == NODE_TYPE_NODE) && |
| 9771 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9772 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9773 | (cur->type == XML_ELEMENT_NODE) || |
| 9774 | (cur->type == XML_PI_NODE) || |
| 9775 | (cur->type == XML_COMMENT_NODE) || |
| 9776 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 9777 | (cur->type == XML_TEXT_NODE))) || |
| 9778 | ((type == NODE_TYPE_TEXT) && |
| 9779 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9780 | n++; |
| 9781 | if (n == indx) |
| 9782 | addNode(list, cur); |
| 9783 | } |
| 9784 | break; |
| 9785 | case NODE_TEST_PI: |
| 9786 | if (cur->type == XML_PI_NODE) { |
| 9787 | if ((name != NULL) && |
| 9788 | (!xmlStrEqual(name, cur->name))) |
| 9789 | break; |
| 9790 | n++; |
| 9791 | if (n == indx) |
| 9792 | addNode(list, cur); |
| 9793 | } |
| 9794 | break; |
| 9795 | case NODE_TEST_ALL: |
| 9796 | if (axis == AXIS_ATTRIBUTE) { |
| 9797 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9798 | n++; |
| 9799 | if (n == indx) |
| 9800 | addNode(list, cur); |
| 9801 | } |
| 9802 | } else if (axis == AXIS_NAMESPACE) { |
| 9803 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9804 | n++; |
| 9805 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9806 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9807 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9808 | } |
| 9809 | } else { |
| 9810 | if (cur->type == XML_ELEMENT_NODE) { |
| 9811 | if (prefix == NULL) { |
| 9812 | n++; |
| 9813 | if (n == indx) |
| 9814 | addNode(list, cur); |
| 9815 | } else if ((cur->ns != NULL) && |
| 9816 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9817 | n++; |
| 9818 | if (n == indx) |
| 9819 | addNode(list, cur); |
| 9820 | } |
| 9821 | } |
| 9822 | } |
| 9823 | break; |
| 9824 | case NODE_TEST_NS:{ |
| 9825 | TODO; |
| 9826 | break; |
| 9827 | } |
| 9828 | case NODE_TEST_NAME: |
| 9829 | switch (cur->type) { |
| 9830 | case XML_ELEMENT_NODE: |
| 9831 | if (xmlStrEqual(name, cur->name)) { |
| 9832 | if (prefix == NULL) { |
| 9833 | if (cur->ns == NULL) { |
| 9834 | n++; |
| 9835 | if (n == indx) |
| 9836 | addNode(list, cur); |
| 9837 | } |
| 9838 | } else { |
| 9839 | if ((cur->ns != NULL) && |
| 9840 | (xmlStrEqual(URI, |
| 9841 | cur->ns->href))) { |
| 9842 | n++; |
| 9843 | if (n == indx) |
| 9844 | addNode(list, cur); |
| 9845 | } |
| 9846 | } |
| 9847 | } |
| 9848 | break; |
| 9849 | case XML_ATTRIBUTE_NODE:{ |
| 9850 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9851 | |
| 9852 | if (xmlStrEqual(name, attr->name)) { |
| 9853 | if (prefix == NULL) { |
| 9854 | if ((attr->ns == NULL) || |
| 9855 | (attr->ns->prefix == NULL)) { |
| 9856 | n++; |
| 9857 | if (n == indx) |
| 9858 | addNode(list, cur); |
| 9859 | } |
| 9860 | } else { |
| 9861 | if ((attr->ns != NULL) && |
| 9862 | (xmlStrEqual(URI, |
| 9863 | attr->ns-> |
| 9864 | href))) { |
| 9865 | n++; |
| 9866 | if (n == indx) |
| 9867 | addNode(list, cur); |
| 9868 | } |
| 9869 | } |
| 9870 | } |
| 9871 | break; |
| 9872 | } |
| 9873 | case XML_NAMESPACE_DECL: |
| 9874 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9875 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9876 | |
| 9877 | if ((ns->prefix != NULL) && (name != NULL) |
| 9878 | && (xmlStrEqual(ns->prefix, name))) { |
| 9879 | n++; |
| 9880 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9881 | xmlXPathNodeSetAddNs(list, |
| 9882 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9883 | } |
| 9884 | } |
| 9885 | break; |
| 9886 | default: |
| 9887 | break; |
| 9888 | } |
| 9889 | break; |
| 9890 | break; |
| 9891 | } |
| 9892 | } while (n < indx); |
| 9893 | } |
| 9894 | ctxt->context->node = tmp; |
| 9895 | #ifdef DEBUG_STEP_NTH |
| 9896 | xmlGenericError(xmlGenericErrorContext, |
| 9897 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9898 | t, list->nodeNr); |
| 9899 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9900 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9901 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9902 | ctxt->value->boolval = 1; |
| 9903 | ctxt->value->user = obj->user; |
| 9904 | obj->user = NULL; |
| 9905 | obj->boolval = 0; |
| 9906 | } |
| 9907 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9908 | return(t); |
| 9909 | } |
| 9910 | |
| 9911 | /** |
| 9912 | * xmlXPathCompOpEvalFirst: |
| 9913 | * @ctxt: the XPath parser context with the compiled expression |
| 9914 | * @op: an XPath compiled operation |
| 9915 | * @first: the first elem found so far |
| 9916 | * |
| 9917 | * Evaluate the Precompiled XPath operation searching only the first |
| 9918 | * element in document order |
| 9919 | * |
| 9920 | * Returns the number of examined objects. |
| 9921 | */ |
| 9922 | static int |
| 9923 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9924 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9925 | { |
| 9926 | int total = 0, cur; |
| 9927 | xmlXPathCompExprPtr comp; |
| 9928 | xmlXPathObjectPtr arg1, arg2; |
| 9929 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9930 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9931 | comp = ctxt->comp; |
| 9932 | switch (op->op) { |
| 9933 | case XPATH_OP_END: |
| 9934 | return (0); |
| 9935 | case XPATH_OP_UNION: |
| 9936 | total = |
| 9937 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9938 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9939 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9940 | if ((ctxt->value != NULL) |
| 9941 | && (ctxt->value->type == XPATH_NODESET) |
| 9942 | && (ctxt->value->nodesetval != NULL) |
| 9943 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9944 | /* |
| 9945 | * limit tree traversing to first node in the result |
| 9946 | */ |
| 9947 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9948 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9949 | } |
| 9950 | cur = |
| 9951 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9952 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9953 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9954 | CHECK_TYPE0(XPATH_NODESET); |
| 9955 | arg2 = valuePop(ctxt); |
| 9956 | |
| 9957 | CHECK_TYPE0(XPATH_NODESET); |
| 9958 | arg1 = valuePop(ctxt); |
| 9959 | |
| 9960 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9961 | arg2->nodesetval); |
| 9962 | valuePush(ctxt, arg1); |
| 9963 | xmlXPathFreeObject(arg2); |
| 9964 | /* optimizer */ |
| 9965 | if (total > cur) |
| 9966 | xmlXPathCompSwap(op); |
| 9967 | return (total + cur); |
| 9968 | case XPATH_OP_ROOT: |
| 9969 | xmlXPathRoot(ctxt); |
| 9970 | return (0); |
| 9971 | case XPATH_OP_NODE: |
| 9972 | if (op->ch1 != -1) |
| 9973 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9974 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9975 | if (op->ch2 != -1) |
| 9976 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9977 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9978 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9979 | return (total); |
| 9980 | case XPATH_OP_RESET: |
| 9981 | if (op->ch1 != -1) |
| 9982 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9983 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9984 | if (op->ch2 != -1) |
| 9985 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9986 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9987 | ctxt->context->node = NULL; |
| 9988 | return (total); |
| 9989 | case XPATH_OP_COLLECT:{ |
| 9990 | if (op->ch1 == -1) |
| 9991 | return (total); |
| 9992 | |
| 9993 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9994 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9995 | |
| 9996 | /* |
| 9997 | * Optimization for [n] selection where n is a number |
| 9998 | */ |
| 9999 | if ((op->ch2 != -1) && |
| 10000 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10001 | (comp->steps[op->ch2].ch1 == -1) && |
| 10002 | (comp->steps[op->ch2].ch2 != -1) && |
| 10003 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10004 | XPATH_OP_VALUE)) { |
| 10005 | xmlXPathObjectPtr val; |
| 10006 | |
| 10007 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10008 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10009 | int indx = (int) val->floatval; |
| 10010 | |
| 10011 | if (val->floatval == (float) indx) { |
| 10012 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 10013 | first, NULL); |
| 10014 | return (total); |
| 10015 | } |
| 10016 | } |
| 10017 | } |
| 10018 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 10019 | return (total); |
| 10020 | } |
| 10021 | case XPATH_OP_VALUE: |
| 10022 | valuePush(ctxt, |
| 10023 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10024 | return (0); |
| 10025 | case XPATH_OP_SORT: |
| 10026 | if (op->ch1 != -1) |
| 10027 | total += |
| 10028 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 10029 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10030 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10031 | if ((ctxt->value != NULL) |
| 10032 | && (ctxt->value->type == XPATH_NODESET) |
| 10033 | && (ctxt->value->nodesetval != NULL)) |
| 10034 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10035 | return (total); |
| 10036 | default: |
| 10037 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10038 | } |
| 10039 | } |
| 10040 | |
| 10041 | /** |
| 10042 | * xmlXPathCompOpEvalLast: |
| 10043 | * @ctxt: the XPath parser context with the compiled expression |
| 10044 | * @op: an XPath compiled operation |
| 10045 | * @last: the last elem found so far |
| 10046 | * |
| 10047 | * Evaluate the Precompiled XPath operation searching only the last |
| 10048 | * element in document order |
| 10049 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10050 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10051 | */ |
| 10052 | static int |
| 10053 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 10054 | xmlNodePtr * last) |
| 10055 | { |
| 10056 | int total = 0, cur; |
| 10057 | xmlXPathCompExprPtr comp; |
| 10058 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10059 | xmlNodePtr bak; |
| 10060 | xmlDocPtr bakd; |
| 10061 | int pp; |
| 10062 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10063 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10064 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10065 | comp = ctxt->comp; |
| 10066 | switch (op->op) { |
| 10067 | case XPATH_OP_END: |
| 10068 | return (0); |
| 10069 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10070 | bakd = ctxt->context->doc; |
| 10071 | bak = ctxt->context->node; |
| 10072 | pp = ctxt->context->proximityPosition; |
| 10073 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10074 | total = |
| 10075 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10076 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10077 | if ((ctxt->value != NULL) |
| 10078 | && (ctxt->value->type == XPATH_NODESET) |
| 10079 | && (ctxt->value->nodesetval != NULL) |
| 10080 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10081 | /* |
| 10082 | * limit tree traversing to first node in the result |
| 10083 | */ |
| 10084 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10085 | *last = |
| 10086 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 10087 | nodesetval->nodeNr - |
| 10088 | 1]; |
| 10089 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10090 | ctxt->context->doc = bakd; |
| 10091 | ctxt->context->node = bak; |
| 10092 | ctxt->context->proximityPosition = pp; |
| 10093 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10094 | cur = |
| 10095 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10096 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10097 | if ((ctxt->value != NULL) |
| 10098 | && (ctxt->value->type == XPATH_NODESET) |
| 10099 | && (ctxt->value->nodesetval != NULL) |
| 10100 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10101 | } |
| 10102 | CHECK_TYPE0(XPATH_NODESET); |
| 10103 | arg2 = valuePop(ctxt); |
| 10104 | |
| 10105 | CHECK_TYPE0(XPATH_NODESET); |
| 10106 | arg1 = valuePop(ctxt); |
| 10107 | |
| 10108 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10109 | arg2->nodesetval); |
| 10110 | valuePush(ctxt, arg1); |
| 10111 | xmlXPathFreeObject(arg2); |
| 10112 | /* optimizer */ |
| 10113 | if (total > cur) |
| 10114 | xmlXPathCompSwap(op); |
| 10115 | return (total + cur); |
| 10116 | case XPATH_OP_ROOT: |
| 10117 | xmlXPathRoot(ctxt); |
| 10118 | return (0); |
| 10119 | case XPATH_OP_NODE: |
| 10120 | if (op->ch1 != -1) |
| 10121 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10122 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10123 | if (op->ch2 != -1) |
| 10124 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10125 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10126 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10127 | return (total); |
| 10128 | case XPATH_OP_RESET: |
| 10129 | if (op->ch1 != -1) |
| 10130 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10131 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10132 | if (op->ch2 != -1) |
| 10133 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10134 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10135 | ctxt->context->node = NULL; |
| 10136 | return (total); |
| 10137 | case XPATH_OP_COLLECT:{ |
| 10138 | if (op->ch1 == -1) |
| 10139 | return (0); |
| 10140 | |
| 10141 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10142 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10143 | |
| 10144 | /* |
| 10145 | * Optimization for [n] selection where n is a number |
| 10146 | */ |
| 10147 | if ((op->ch2 != -1) && |
| 10148 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10149 | (comp->steps[op->ch2].ch1 == -1) && |
| 10150 | (comp->steps[op->ch2].ch2 != -1) && |
| 10151 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10152 | XPATH_OP_VALUE)) { |
| 10153 | xmlXPathObjectPtr val; |
| 10154 | |
| 10155 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10156 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10157 | int indx = (int) val->floatval; |
| 10158 | |
| 10159 | if (val->floatval == (float) indx) { |
| 10160 | total += |
| 10161 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10162 | indx, NULL, |
| 10163 | last); |
| 10164 | return (total); |
| 10165 | } |
| 10166 | } |
| 10167 | } |
| 10168 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 10169 | return (total); |
| 10170 | } |
| 10171 | case XPATH_OP_VALUE: |
| 10172 | valuePush(ctxt, |
| 10173 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10174 | return (0); |
| 10175 | case XPATH_OP_SORT: |
| 10176 | if (op->ch1 != -1) |
| 10177 | total += |
| 10178 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 10179 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10180 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10181 | if ((ctxt->value != NULL) |
| 10182 | && (ctxt->value->type == XPATH_NODESET) |
| 10183 | && (ctxt->value->nodesetval != NULL)) |
| 10184 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10185 | return (total); |
| 10186 | default: |
| 10187 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10188 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10189 | } |
| 10190 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10191 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10192 | * xmlXPathCompOpEval: |
| 10193 | * @ctxt: the XPath parser context with the compiled expression |
| 10194 | * @op: an XPath compiled operation |
| 10195 | * |
| 10196 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10197 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10198 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10199 | static int |
| 10200 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 10201 | { |
| 10202 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10203 | int equal, ret; |
| 10204 | xmlXPathCompExprPtr comp; |
| 10205 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10206 | xmlNodePtr bak; |
| 10207 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10208 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10209 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10210 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10211 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10212 | comp = ctxt->comp; |
| 10213 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10214 | case XPATH_OP_END: |
| 10215 | return (0); |
| 10216 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10217 | bakd = ctxt->context->doc; |
| 10218 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10219 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10220 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10221 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10222 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10223 | xmlXPathBooleanFunction(ctxt, 1); |
| 10224 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10225 | return (total); |
| 10226 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10227 | ctxt->context->doc = bakd; |
| 10228 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10229 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10230 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10231 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10232 | if (ctxt->error) { |
| 10233 | xmlXPathFreeObject(arg2); |
| 10234 | return(0); |
| 10235 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10236 | xmlXPathBooleanFunction(ctxt, 1); |
| 10237 | arg1 = valuePop(ctxt); |
| 10238 | arg1->boolval &= arg2->boolval; |
| 10239 | valuePush(ctxt, arg1); |
| 10240 | xmlXPathFreeObject(arg2); |
| 10241 | return (total); |
| 10242 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10243 | bakd = ctxt->context->doc; |
| 10244 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10245 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10246 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10247 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10248 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10249 | xmlXPathBooleanFunction(ctxt, 1); |
| 10250 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10251 | return (total); |
| 10252 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10253 | ctxt->context->doc = bakd; |
| 10254 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10255 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10256 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10257 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10258 | if (ctxt->error) { |
| 10259 | xmlXPathFreeObject(arg2); |
| 10260 | return(0); |
| 10261 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10262 | xmlXPathBooleanFunction(ctxt, 1); |
| 10263 | arg1 = valuePop(ctxt); |
| 10264 | arg1->boolval |= arg2->boolval; |
| 10265 | valuePush(ctxt, arg1); |
| 10266 | xmlXPathFreeObject(arg2); |
| 10267 | return (total); |
| 10268 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10269 | bakd = ctxt->context->doc; |
| 10270 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10271 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10272 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10273 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10274 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10275 | ctxt->context->doc = bakd; |
| 10276 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10277 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10278 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10279 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10280 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10281 | if (op->value) |
| 10282 | equal = xmlXPathEqualValues(ctxt); |
| 10283 | else |
| 10284 | equal = xmlXPathNotEqualValues(ctxt); |
| 10285 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10286 | return (total); |
| 10287 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10288 | bakd = ctxt->context->doc; |
| 10289 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10290 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10291 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10292 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10293 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10294 | ctxt->context->doc = bakd; |
| 10295 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10296 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10297 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10298 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10299 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10300 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 10301 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 10302 | return (total); |
| 10303 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10304 | bakd = ctxt->context->doc; |
| 10305 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10306 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10307 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10308 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10309 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10310 | if (op->ch2 != -1) { |
| 10311 | ctxt->context->doc = bakd; |
| 10312 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10313 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10314 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10315 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10316 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10317 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10318 | if (op->value == 0) |
| 10319 | xmlXPathSubValues(ctxt); |
| 10320 | else if (op->value == 1) |
| 10321 | xmlXPathAddValues(ctxt); |
| 10322 | else if (op->value == 2) |
| 10323 | xmlXPathValueFlipSign(ctxt); |
| 10324 | else if (op->value == 3) { |
| 10325 | CAST_TO_NUMBER; |
| 10326 | CHECK_TYPE0(XPATH_NUMBER); |
| 10327 | } |
| 10328 | return (total); |
| 10329 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10330 | bakd = ctxt->context->doc; |
| 10331 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10332 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10333 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10334 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10335 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10336 | ctxt->context->doc = bakd; |
| 10337 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10338 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10339 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10340 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10341 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10342 | if (op->value == 0) |
| 10343 | xmlXPathMultValues(ctxt); |
| 10344 | else if (op->value == 1) |
| 10345 | xmlXPathDivValues(ctxt); |
| 10346 | else if (op->value == 2) |
| 10347 | xmlXPathModValues(ctxt); |
| 10348 | return (total); |
| 10349 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10350 | bakd = ctxt->context->doc; |
| 10351 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10352 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10353 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10354 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10355 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10356 | ctxt->context->doc = bakd; |
| 10357 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10358 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10359 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10360 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10361 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10362 | CHECK_TYPE0(XPATH_NODESET); |
| 10363 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10364 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10365 | CHECK_TYPE0(XPATH_NODESET); |
| 10366 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10367 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10368 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10369 | arg2->nodesetval); |
| 10370 | valuePush(ctxt, arg1); |
| 10371 | xmlXPathFreeObject(arg2); |
| 10372 | return (total); |
| 10373 | case XPATH_OP_ROOT: |
| 10374 | xmlXPathRoot(ctxt); |
| 10375 | return (total); |
| 10376 | case XPATH_OP_NODE: |
| 10377 | if (op->ch1 != -1) |
| 10378 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10379 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10380 | if (op->ch2 != -1) |
| 10381 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10382 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10383 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10384 | return (total); |
| 10385 | case XPATH_OP_RESET: |
| 10386 | if (op->ch1 != -1) |
| 10387 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10388 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10389 | if (op->ch2 != -1) |
| 10390 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10391 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10392 | ctxt->context->node = NULL; |
| 10393 | return (total); |
| 10394 | case XPATH_OP_COLLECT:{ |
| 10395 | if (op->ch1 == -1) |
| 10396 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10397 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10398 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10399 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10400 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10401 | /* |
| 10402 | * Optimization for [n] selection where n is a number |
| 10403 | */ |
| 10404 | if ((op->ch2 != -1) && |
| 10405 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10406 | (comp->steps[op->ch2].ch1 == -1) && |
| 10407 | (comp->steps[op->ch2].ch2 != -1) && |
| 10408 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10409 | XPATH_OP_VALUE)) { |
| 10410 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 10411 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10412 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10413 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10414 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10415 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10416 | if (val->floatval == (float) indx) { |
| 10417 | total += |
| 10418 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10419 | indx, NULL, |
| 10420 | NULL); |
| 10421 | return (total); |
| 10422 | } |
| 10423 | } |
| 10424 | } |
| 10425 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 10426 | return (total); |
| 10427 | } |
| 10428 | case XPATH_OP_VALUE: |
| 10429 | valuePush(ctxt, |
| 10430 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10431 | return (total); |
| 10432 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10433 | xmlXPathObjectPtr val; |
| 10434 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10435 | if (op->ch1 != -1) |
| 10436 | total += |
| 10437 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10438 | if (op->value5 == NULL) { |
| 10439 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 10440 | if (val == NULL) { |
| 10441 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10442 | return(0); |
| 10443 | } |
| 10444 | valuePush(ctxt, val); |
| 10445 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10446 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10447 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10448 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10449 | if (URI == NULL) { |
| 10450 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10451 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10452 | op->value4, op->value5); |
| 10453 | return (total); |
| 10454 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10455 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 10456 | op->value4, URI); |
| 10457 | if (val == NULL) { |
| 10458 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10459 | return(0); |
| 10460 | } |
| 10461 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10462 | } |
| 10463 | return (total); |
| 10464 | } |
| 10465 | case XPATH_OP_FUNCTION:{ |
| 10466 | xmlXPathFunction func; |
| 10467 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10468 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10469 | |
| 10470 | if (op->ch1 != -1) |
| 10471 | total += |
| 10472 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10473 | if (ctxt->valueNr < op->value) { |
| 10474 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10475 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10476 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10477 | return (total); |
| 10478 | } |
| 10479 | for (i = 0; i < op->value; i++) |
| 10480 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 10481 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10482 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10483 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10484 | return (total); |
| 10485 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10486 | if (op->cache != NULL) |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 10487 | XML_CAST_FPTR(func) = op->cache; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10488 | else { |
| 10489 | const xmlChar *URI = NULL; |
| 10490 | |
| 10491 | if (op->value5 == NULL) |
| 10492 | func = |
| 10493 | xmlXPathFunctionLookup(ctxt->context, |
| 10494 | op->value4); |
| 10495 | else { |
| 10496 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10497 | if (URI == NULL) { |
| 10498 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10499 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10500 | op->value4, op->value5); |
| 10501 | return (total); |
| 10502 | } |
| 10503 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 10504 | op->value4, URI); |
| 10505 | } |
| 10506 | if (func == NULL) { |
| 10507 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10508 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10509 | op->value4); |
| 10510 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10511 | } |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 10512 | op->cache = XML_CAST_FPTR(func); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10513 | op->cacheURI = (void *) URI; |
| 10514 | } |
| 10515 | oldFunc = ctxt->context->function; |
| 10516 | oldFuncURI = ctxt->context->functionURI; |
| 10517 | ctxt->context->function = op->value4; |
| 10518 | ctxt->context->functionURI = op->cacheURI; |
| 10519 | func(ctxt, op->value); |
| 10520 | ctxt->context->function = oldFunc; |
| 10521 | ctxt->context->functionURI = oldFuncURI; |
| 10522 | return (total); |
| 10523 | } |
| 10524 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10525 | bakd = ctxt->context->doc; |
| 10526 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10527 | pp = ctxt->context->proximityPosition; |
| 10528 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10529 | if (op->ch1 != -1) |
| 10530 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10531 | ctxt->context->contextSize = cs; |
| 10532 | ctxt->context->proximityPosition = pp; |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10533 | ctxt->context->node = bak; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10534 | ctxt->context->doc = bakd; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10535 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10536 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10537 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10538 | ctxt->context->doc = bakd; |
| 10539 | ctxt->context->node = bak; |
| 10540 | CHECK_ERROR0; |
| 10541 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10542 | return (total); |
| 10543 | case XPATH_OP_PREDICATE: |
| 10544 | case XPATH_OP_FILTER:{ |
| 10545 | xmlXPathObjectPtr res; |
| 10546 | xmlXPathObjectPtr obj, tmp; |
| 10547 | xmlNodeSetPtr newset = NULL; |
| 10548 | xmlNodeSetPtr oldset; |
| 10549 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10550 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10551 | int i; |
| 10552 | |
| 10553 | /* |
| 10554 | * Optimization for ()[1] selection i.e. the first elem |
| 10555 | */ |
| 10556 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10557 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10558 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 10559 | xmlXPathObjectPtr val; |
| 10560 | |
| 10561 | val = comp->steps[op->ch2].value4; |
| 10562 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 10563 | (val->floatval == 1.0)) { |
| 10564 | xmlNodePtr first = NULL; |
| 10565 | |
| 10566 | total += |
| 10567 | xmlXPathCompOpEvalFirst(ctxt, |
| 10568 | &comp->steps[op->ch1], |
| 10569 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10570 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10571 | /* |
| 10572 | * The nodeset should be in document order, |
| 10573 | * Keep only the first value |
| 10574 | */ |
| 10575 | if ((ctxt->value != NULL) && |
| 10576 | (ctxt->value->type == XPATH_NODESET) && |
| 10577 | (ctxt->value->nodesetval != NULL) && |
| 10578 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 10579 | ctxt->value->nodesetval->nodeNr = 1; |
| 10580 | return (total); |
| 10581 | } |
| 10582 | } |
| 10583 | /* |
| 10584 | * Optimization for ()[last()] selection i.e. the last elem |
| 10585 | */ |
| 10586 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10587 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10588 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10589 | int f = comp->steps[op->ch2].ch1; |
| 10590 | |
| 10591 | if ((f != -1) && |
| 10592 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10593 | (comp->steps[f].value5 == NULL) && |
| 10594 | (comp->steps[f].value == 0) && |
| 10595 | (comp->steps[f].value4 != NULL) && |
| 10596 | (xmlStrEqual |
| 10597 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10598 | xmlNodePtr last = NULL; |
| 10599 | |
| 10600 | total += |
| 10601 | xmlXPathCompOpEvalLast(ctxt, |
| 10602 | &comp->steps[op->ch1], |
| 10603 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10604 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10605 | /* |
| 10606 | * The nodeset should be in document order, |
| 10607 | * Keep only the last value |
| 10608 | */ |
| 10609 | if ((ctxt->value != NULL) && |
| 10610 | (ctxt->value->type == XPATH_NODESET) && |
| 10611 | (ctxt->value->nodesetval != NULL) && |
| 10612 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10613 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10614 | ctxt->value->nodesetval->nodeTab[0] = |
| 10615 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10616 | value-> |
| 10617 | nodesetval-> |
| 10618 | nodeNr - |
| 10619 | 1]; |
| 10620 | ctxt->value->nodesetval->nodeNr = 1; |
| 10621 | } |
| 10622 | return (total); |
| 10623 | } |
| 10624 | } |
| 10625 | |
| 10626 | if (op->ch1 != -1) |
| 10627 | total += |
| 10628 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10629 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10630 | if (op->ch2 == -1) |
| 10631 | return (total); |
| 10632 | if (ctxt->value == NULL) |
| 10633 | return (total); |
| 10634 | |
| 10635 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10636 | |
| 10637 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10638 | /* |
| 10639 | * Hum are we filtering the result of an XPointer expression |
| 10640 | */ |
| 10641 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10642 | xmlLocationSetPtr newlocset = NULL; |
| 10643 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10644 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10645 | /* |
| 10646 | * Extract the old locset, and then evaluate the result of the |
| 10647 | * expression for all the element in the locset. use it to grow |
| 10648 | * up a new locset. |
| 10649 | */ |
| 10650 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10651 | obj = valuePop(ctxt); |
| 10652 | oldlocset = obj->user; |
| 10653 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10654 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10655 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10656 | ctxt->context->contextSize = 0; |
| 10657 | ctxt->context->proximityPosition = 0; |
| 10658 | if (op->ch2 != -1) |
| 10659 | total += |
| 10660 | xmlXPathCompOpEval(ctxt, |
| 10661 | &comp->steps[op->ch2]); |
| 10662 | res = valuePop(ctxt); |
| 10663 | if (res != NULL) |
| 10664 | xmlXPathFreeObject(res); |
| 10665 | valuePush(ctxt, obj); |
| 10666 | CHECK_ERROR0; |
| 10667 | return (total); |
| 10668 | } |
| 10669 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10670 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10671 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10672 | /* |
| 10673 | * Run the evaluation with a node list made of a |
| 10674 | * single item in the nodelocset. |
| 10675 | */ |
| 10676 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10677 | ctxt->context->contextSize = oldlocset->locNr; |
| 10678 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10679 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10680 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10681 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10682 | if (op->ch2 != -1) |
| 10683 | total += |
| 10684 | xmlXPathCompOpEval(ctxt, |
| 10685 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10686 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10687 | xmlXPathFreeObject(obj); |
| 10688 | return(0); |
| 10689 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10690 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10691 | /* |
| 10692 | * The result of the evaluation need to be tested to |
| 10693 | * decided whether the filter succeeded or not |
| 10694 | */ |
| 10695 | res = valuePop(ctxt); |
| 10696 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10697 | xmlXPtrLocationSetAdd(newlocset, |
| 10698 | xmlXPathObjectCopy |
| 10699 | (oldlocset->locTab[i])); |
| 10700 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10701 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10702 | /* |
| 10703 | * Cleanup |
| 10704 | */ |
| 10705 | if (res != NULL) |
| 10706 | xmlXPathFreeObject(res); |
| 10707 | if (ctxt->value == tmp) { |
| 10708 | res = valuePop(ctxt); |
| 10709 | xmlXPathFreeObject(res); |
| 10710 | } |
| 10711 | |
| 10712 | ctxt->context->node = NULL; |
| 10713 | } |
| 10714 | |
| 10715 | /* |
| 10716 | * The result is used as the new evaluation locset. |
| 10717 | */ |
| 10718 | xmlXPathFreeObject(obj); |
| 10719 | ctxt->context->node = NULL; |
| 10720 | ctxt->context->contextSize = -1; |
| 10721 | ctxt->context->proximityPosition = -1; |
| 10722 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10723 | ctxt->context->node = oldnode; |
| 10724 | return (total); |
| 10725 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10726 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10727 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10728 | /* |
| 10729 | * Extract the old set, and then evaluate the result of the |
| 10730 | * expression for all the element in the set. use it to grow |
| 10731 | * up a new set. |
| 10732 | */ |
| 10733 | CHECK_TYPE0(XPATH_NODESET); |
| 10734 | obj = valuePop(ctxt); |
| 10735 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10736 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10737 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10738 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10739 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10740 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10741 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10742 | ctxt->context->contextSize = 0; |
| 10743 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10744 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10745 | if (op->ch2 != -1) |
| 10746 | total += |
| 10747 | xmlXPathCompOpEval(ctxt, |
| 10748 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10749 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10750 | res = valuePop(ctxt); |
| 10751 | if (res != NULL) |
| 10752 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10753 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10754 | valuePush(ctxt, obj); |
| 10755 | ctxt->context->node = oldnode; |
| 10756 | CHECK_ERROR0; |
| 10757 | } else { |
| 10758 | /* |
| 10759 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10760 | * Also set the xpath document in case things like |
| 10761 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10762 | */ |
| 10763 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10764 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10765 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10766 | /* |
| 10767 | * Run the evaluation with a node list made of |
| 10768 | * a single item in the nodeset. |
| 10769 | */ |
| 10770 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10771 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 10772 | (oldset->nodeTab[i]->doc != NULL)) |
| 10773 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10774 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10775 | valuePush(ctxt, tmp); |
| 10776 | ctxt->context->contextSize = oldset->nodeNr; |
| 10777 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10778 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10779 | if (op->ch2 != -1) |
| 10780 | total += |
| 10781 | xmlXPathCompOpEval(ctxt, |
| 10782 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10783 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10784 | xmlXPathFreeNodeSet(newset); |
| 10785 | xmlXPathFreeObject(obj); |
| 10786 | return(0); |
| 10787 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10788 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10789 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10790 | * The result of the evaluation needs to be tested to |
| 10791 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10792 | */ |
| 10793 | res = valuePop(ctxt); |
| 10794 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10795 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10796 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10797 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10798 | /* |
| 10799 | * Cleanup |
| 10800 | */ |
| 10801 | if (res != NULL) |
| 10802 | xmlXPathFreeObject(res); |
| 10803 | if (ctxt->value == tmp) { |
| 10804 | res = valuePop(ctxt); |
| 10805 | xmlXPathFreeObject(res); |
| 10806 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10807 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10808 | ctxt->context->node = NULL; |
| 10809 | } |
| 10810 | |
| 10811 | /* |
| 10812 | * The result is used as the new evaluation set. |
| 10813 | */ |
| 10814 | xmlXPathFreeObject(obj); |
| 10815 | ctxt->context->node = NULL; |
| 10816 | ctxt->context->contextSize = -1; |
| 10817 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10818 | /* may want to move this past the '}' later */ |
| 10819 | ctxt->context->doc = oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10820 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10821 | } |
| 10822 | ctxt->context->node = oldnode; |
| 10823 | return (total); |
| 10824 | } |
| 10825 | case XPATH_OP_SORT: |
| 10826 | if (op->ch1 != -1) |
| 10827 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10828 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10829 | if ((ctxt->value != NULL) && |
| 10830 | (ctxt->value->type == XPATH_NODESET) && |
| 10831 | (ctxt->value->nodesetval != NULL)) |
| 10832 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10833 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10834 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10835 | case XPATH_OP_RANGETO:{ |
| 10836 | xmlXPathObjectPtr range; |
| 10837 | xmlXPathObjectPtr res, obj; |
| 10838 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10839 | xmlLocationSetPtr newlocset = NULL; |
| 10840 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10841 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10842 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10843 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10844 | if (op->ch1 != -1) |
| 10845 | total += |
| 10846 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10847 | if (op->ch2 == -1) |
| 10848 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10849 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10850 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10851 | /* |
| 10852 | * Extract the old locset, and then evaluate the result of the |
| 10853 | * expression for all the element in the locset. use it to grow |
| 10854 | * up a new locset. |
| 10855 | */ |
| 10856 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10857 | obj = valuePop(ctxt); |
| 10858 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10859 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10860 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10861 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10862 | ctxt->context->contextSize = 0; |
| 10863 | ctxt->context->proximityPosition = 0; |
| 10864 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 10865 | res = valuePop(ctxt); |
| 10866 | if (res != NULL) |
| 10867 | xmlXPathFreeObject(res); |
| 10868 | valuePush(ctxt, obj); |
| 10869 | CHECK_ERROR0; |
| 10870 | return (total); |
| 10871 | } |
| 10872 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10873 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10874 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10875 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10876 | * Run the evaluation with a node list made of a |
| 10877 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10878 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10879 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10880 | ctxt->context->contextSize = oldlocset->locNr; |
| 10881 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10882 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10883 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10884 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10885 | if (op->ch2 != -1) |
| 10886 | total += |
| 10887 | xmlXPathCompOpEval(ctxt, |
| 10888 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10889 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10890 | xmlXPathFreeObject(obj); |
| 10891 | return(0); |
| 10892 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10893 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10894 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10895 | if (res->type == XPATH_LOCATIONSET) { |
| 10896 | xmlLocationSetPtr rloc = |
| 10897 | (xmlLocationSetPtr)res->user; |
| 10898 | for (j=0; j<rloc->locNr; j++) { |
| 10899 | range = xmlXPtrNewRange( |
| 10900 | oldlocset->locTab[i]->user, |
| 10901 | oldlocset->locTab[i]->index, |
| 10902 | rloc->locTab[j]->user2, |
| 10903 | rloc->locTab[j]->index2); |
| 10904 | if (range != NULL) { |
| 10905 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10906 | } |
| 10907 | } |
| 10908 | } else { |
| 10909 | range = xmlXPtrNewRangeNodeObject( |
| 10910 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 10911 | if (range != NULL) { |
| 10912 | xmlXPtrLocationSetAdd(newlocset,range); |
| 10913 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10914 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10915 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10916 | /* |
| 10917 | * Cleanup |
| 10918 | */ |
| 10919 | if (res != NULL) |
| 10920 | xmlXPathFreeObject(res); |
| 10921 | if (ctxt->value == tmp) { |
| 10922 | res = valuePop(ctxt); |
| 10923 | xmlXPathFreeObject(res); |
| 10924 | } |
| 10925 | |
| 10926 | ctxt->context->node = NULL; |
| 10927 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10928 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10929 | CHECK_TYPE0(XPATH_NODESET); |
| 10930 | obj = valuePop(ctxt); |
| 10931 | oldset = obj->nodesetval; |
| 10932 | ctxt->context->node = NULL; |
| 10933 | |
| 10934 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10935 | |
| 10936 | if (oldset != NULL) { |
| 10937 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10938 | /* |
| 10939 | * Run the evaluation with a node list made of a single item |
| 10940 | * in the nodeset. |
| 10941 | */ |
| 10942 | ctxt->context->node = oldset->nodeTab[i]; |
| 10943 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10944 | valuePush(ctxt, tmp); |
| 10945 | |
| 10946 | if (op->ch2 != -1) |
| 10947 | total += |
| 10948 | xmlXPathCompOpEval(ctxt, |
| 10949 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10950 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10951 | xmlXPathFreeObject(obj); |
| 10952 | return(0); |
| 10953 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10954 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10955 | res = valuePop(ctxt); |
| 10956 | range = |
| 10957 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10958 | res); |
| 10959 | if (range != NULL) { |
| 10960 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10961 | } |
| 10962 | |
| 10963 | /* |
| 10964 | * Cleanup |
| 10965 | */ |
| 10966 | if (res != NULL) |
| 10967 | xmlXPathFreeObject(res); |
| 10968 | if (ctxt->value == tmp) { |
| 10969 | res = valuePop(ctxt); |
| 10970 | xmlXPathFreeObject(res); |
| 10971 | } |
| 10972 | |
| 10973 | ctxt->context->node = NULL; |
| 10974 | } |
| 10975 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10976 | } |
| 10977 | |
| 10978 | /* |
| 10979 | * The result is used as the new evaluation set. |
| 10980 | */ |
| 10981 | xmlXPathFreeObject(obj); |
| 10982 | ctxt->context->node = NULL; |
| 10983 | ctxt->context->contextSize = -1; |
| 10984 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10985 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10986 | return (total); |
| 10987 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10988 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10989 | } |
| 10990 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10991 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10992 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10993 | } |
| 10994 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 10995 | #ifdef XPATH_STREAMING |
| 10996 | /** |
| 10997 | * xmlXPathRunStreamEval: |
| 10998 | * @ctxt: the XPath parser context with the compiled expression |
| 10999 | * |
| 11000 | * Evaluate the Precompiled Streamable XPath expression in the given context. |
| 11001 | */ |
| 11002 | static xmlXPathObjectPtr |
| 11003 | xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp) { |
| 11004 | int max_depth; |
| 11005 | int from_root; |
| 11006 | int ret, depth; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 11007 | xmlNodePtr cur = NULL, limit = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11008 | xmlXPathObjectPtr retval; |
| 11009 | xmlStreamCtxtPtr patstream; |
| 11010 | |
| 11011 | int nb_nodes = 0; |
| 11012 | |
| 11013 | if ((ctxt == NULL) || (comp == NULL)) |
| 11014 | return(NULL); |
| 11015 | max_depth = xmlPatternMaxDepth(comp); |
| 11016 | if (max_depth == -1) |
| 11017 | return(NULL); |
| 11018 | if (max_depth == -2) |
| 11019 | max_depth = 10000; |
| 11020 | from_root = xmlPatternFromRoot(comp); |
| 11021 | if (from_root < 0) |
| 11022 | return(NULL); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11023 | #if 0 |
| 11024 | printf("stream eval: depth %d from root %d\n", max_depth, from_root); |
| 11025 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11026 | |
| 11027 | retval = xmlXPathNewNodeSet(NULL); |
| 11028 | if (retval == NULL) |
| 11029 | return(NULL); |
| 11030 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11031 | if ((from_root) && (max_depth == 0)) { |
| 11032 | xmlXPathNodeSetAddUnique(retval->nodesetval, (xmlNodePtr) ctxt->doc); |
| 11033 | return(retval); |
| 11034 | } else if (max_depth == 0) { |
| 11035 | xmlXPathNodeSetAddUnique(retval->nodesetval, ctxt->node); |
| 11036 | return(retval); |
| 11037 | } |
| 11038 | if (from_root) { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 11039 | cur = (xmlNodePtr)ctxt->doc; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11040 | } else if (ctxt->node != NULL) { |
| 11041 | switch (ctxt->node->type) { |
| 11042 | case XML_ELEMENT_NODE: |
| 11043 | case XML_DOCUMENT_NODE: |
| 11044 | case XML_DOCUMENT_FRAG_NODE: |
| 11045 | case XML_HTML_DOCUMENT_NODE: |
| 11046 | #ifdef LIBXML_DOCB_ENABLED |
| 11047 | case XML_DOCB_DOCUMENT_NODE: |
| 11048 | #endif |
| 11049 | cur = ctxt->node; |
| 11050 | break; |
| 11051 | case XML_ATTRIBUTE_NODE: |
| 11052 | case XML_TEXT_NODE: |
| 11053 | case XML_CDATA_SECTION_NODE: |
| 11054 | case XML_ENTITY_REF_NODE: |
| 11055 | case XML_ENTITY_NODE: |
| 11056 | case XML_PI_NODE: |
| 11057 | case XML_COMMENT_NODE: |
| 11058 | case XML_NOTATION_NODE: |
| 11059 | case XML_DTD_NODE: |
| 11060 | case XML_DOCUMENT_TYPE_NODE: |
| 11061 | case XML_ELEMENT_DECL: |
| 11062 | case XML_ATTRIBUTE_DECL: |
| 11063 | case XML_ENTITY_DECL: |
| 11064 | case XML_NAMESPACE_DECL: |
| 11065 | case XML_XINCLUDE_START: |
| 11066 | case XML_XINCLUDE_END: |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11067 | break; |
| 11068 | } |
| 11069 | limit = cur; |
| 11070 | } |
| 11071 | if (cur == NULL) |
| 11072 | return(retval); |
| 11073 | |
| 11074 | patstream = xmlPatternGetStreamCtxt(comp); |
| 11075 | if (patstream == NULL) { |
| 11076 | return(retval); |
| 11077 | } |
| 11078 | |
| 11079 | if (from_root) { |
| 11080 | ret = xmlStreamPush(patstream, NULL, NULL); |
| 11081 | if (ret < 0) { |
| 11082 | } else if (ret == 1) { |
| 11083 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11084 | } |
| 11085 | } |
| 11086 | |
| 11087 | depth = 0; |
| 11088 | goto scan_children; |
| 11089 | do { |
| 11090 | next_node: |
| 11091 | nb_nodes++; |
| 11092 | if (cur->type == XML_ELEMENT_NODE) { |
| 11093 | ret = xmlStreamPush(patstream, cur->name, |
| 11094 | (cur->ns ? cur->ns->href : NULL)); |
| 11095 | if (ret < 0) { |
| 11096 | } else if (ret == 1) { |
| 11097 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11098 | } |
| 11099 | if ((cur->children == NULL) || (depth >= max_depth)) { |
| 11100 | ret = xmlStreamPop(patstream); |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 11101 | while (cur->next != NULL) { |
| 11102 | cur = cur->next; |
| 11103 | if ((cur->type != XML_ENTITY_DECL) && |
| 11104 | (cur->type != XML_DTD_NODE)) |
| 11105 | goto next_node; |
| 11106 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11107 | } |
| 11108 | } |
| 11109 | |
| 11110 | scan_children: |
| 11111 | if ((cur->children != NULL) && (depth < max_depth)) { |
| 11112 | /* |
| 11113 | * Do not descend on entities declarations |
| 11114 | */ |
| 11115 | if (cur->children->type != XML_ENTITY_DECL) { |
| 11116 | cur = cur->children; |
| 11117 | depth++; |
| 11118 | /* |
| 11119 | * Skip DTDs |
| 11120 | */ |
| 11121 | if (cur->type != XML_DTD_NODE) |
| 11122 | continue; |
| 11123 | } |
| 11124 | } |
| 11125 | |
| 11126 | if (cur == limit) |
| 11127 | break; |
| 11128 | |
| 11129 | while (cur->next != NULL) { |
| 11130 | cur = cur->next; |
| 11131 | if ((cur->type != XML_ENTITY_DECL) && |
| 11132 | (cur->type != XML_DTD_NODE)) |
| 11133 | goto next_node; |
| 11134 | } |
| 11135 | |
| 11136 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11137 | cur = cur->parent; |
| 11138 | depth--; |
| 11139 | if ((cur == NULL) || (cur == limit)) |
| 11140 | goto done; |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 11141 | if (cur->type == XML_ELEMENT_NODE) |
| 11142 | ret = xmlStreamPop(patstream); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11143 | if (cur->next != NULL) { |
| 11144 | cur = cur->next; |
| 11145 | break; |
| 11146 | } |
| 11147 | } while (cur != NULL); |
| 11148 | |
| 11149 | } while ((cur != NULL) && (depth >= 0)); |
| 11150 | done: |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11151 | #if 0 |
| 11152 | printf("stream eval: checked %d nodes selected %d\n", |
| 11153 | nb_nodes, retval->nodesetval->nodeNr); |
| 11154 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11155 | xmlFreeStreamCtxt(patstream); |
| 11156 | return(retval); |
| 11157 | } |
| 11158 | #endif /* XPATH_STREAMING */ |
| 11159 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11160 | /** |
| 11161 | * xmlXPathRunEval: |
| 11162 | * @ctxt: the XPath parser context with the compiled expression |
| 11163 | * |
| 11164 | * Evaluate the Precompiled XPath expression in the given context. |
| 11165 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11166 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11167 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 11168 | xmlXPathCompExprPtr comp; |
| 11169 | |
| 11170 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 11171 | return; |
| 11172 | |
| 11173 | if (ctxt->valueTab == NULL) { |
| 11174 | /* Allocate the value stack */ |
| 11175 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 11176 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 11177 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 11178 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11179 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11180 | } |
| 11181 | ctxt->valueNr = 0; |
| 11182 | ctxt->valueMax = 10; |
| 11183 | ctxt->value = NULL; |
| 11184 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11185 | #ifdef XPATH_STREAMING |
| 11186 | if (ctxt->comp->stream) { |
| 11187 | xmlXPathObjectPtr ret; |
| 11188 | ret = xmlXPathRunStreamEval(ctxt->context, ctxt->comp->stream); |
| 11189 | if (ret != NULL) { |
| 11190 | valuePush(ctxt, ret); |
| 11191 | return; |
| 11192 | } |
| 11193 | } |
| 11194 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11195 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 11196 | if(comp->last < 0) { |
| 11197 | xmlGenericError(xmlGenericErrorContext, |
| 11198 | "xmlXPathRunEval: last is less than zero\n"); |
| 11199 | return; |
| 11200 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11201 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 11202 | } |
| 11203 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11204 | /************************************************************************ |
| 11205 | * * |
| 11206 | * Public interfaces * |
| 11207 | * * |
| 11208 | ************************************************************************/ |
| 11209 | |
| 11210 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11211 | * xmlXPathEvalPredicate: |
| 11212 | * @ctxt: the XPath context |
| 11213 | * @res: the Predicate Expression evaluation result |
| 11214 | * |
| 11215 | * Evaluate a predicate result for the current node. |
| 11216 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 11217 | * the result to a boolean. If the result is a number, the result will |
| 11218 | * be converted to true if the number is equal to the position of the |
| 11219 | * context node in the context node list (as returned by the position |
| 11220 | * function) and will be converted to false otherwise; if the result |
| 11221 | * is not a number, then the result will be converted as if by a call |
| 11222 | * to the boolean function. |
| 11223 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11224 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11225 | */ |
| 11226 | int |
| 11227 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 11228 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11229 | switch (res->type) { |
| 11230 | case XPATH_BOOLEAN: |
| 11231 | return(res->boolval); |
| 11232 | case XPATH_NUMBER: |
| 11233 | return(res->floatval == ctxt->proximityPosition); |
| 11234 | case XPATH_NODESET: |
| 11235 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11236 | if (res->nodesetval == NULL) |
| 11237 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11238 | return(res->nodesetval->nodeNr != 0); |
| 11239 | case XPATH_STRING: |
| 11240 | return((res->stringval != NULL) && |
| 11241 | (xmlStrlen(res->stringval) != 0)); |
| 11242 | default: |
| 11243 | STRANGE |
| 11244 | } |
| 11245 | return(0); |
| 11246 | } |
| 11247 | |
| 11248 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11249 | * xmlXPathEvaluatePredicateResult: |
| 11250 | * @ctxt: the XPath Parser context |
| 11251 | * @res: the Predicate Expression evaluation result |
| 11252 | * |
| 11253 | * Evaluate a predicate result for the current node. |
| 11254 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 11255 | * the result to a boolean. If the result is a number, the result will |
| 11256 | * be converted to true if the number is equal to the position of the |
| 11257 | * context node in the context node list (as returned by the position |
| 11258 | * function) and will be converted to false otherwise; if the result |
| 11259 | * is not a number, then the result will be converted as if by a call |
| 11260 | * to the boolean function. |
| 11261 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11262 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11263 | */ |
| 11264 | int |
| 11265 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 11266 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 11267 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11268 | switch (res->type) { |
| 11269 | case XPATH_BOOLEAN: |
| 11270 | return(res->boolval); |
| 11271 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 11272 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 11273 | return((res->floatval == ctxt->context->proximityPosition) && |
| 11274 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11275 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11276 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11277 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11278 | case XPATH_NODESET: |
| 11279 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 11280 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 11281 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11282 | return(res->nodesetval->nodeNr != 0); |
| 11283 | case XPATH_STRING: |
| 11284 | return((res->stringval != NULL) && |
| 11285 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11286 | #ifdef LIBXML_XPTR_ENABLED |
| 11287 | case XPATH_LOCATIONSET:{ |
| 11288 | xmlLocationSetPtr ptr = res->user; |
| 11289 | if (ptr == NULL) |
| 11290 | return(0); |
| 11291 | return (ptr->locNr != 0); |
| 11292 | } |
| 11293 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11294 | default: |
| 11295 | STRANGE |
| 11296 | } |
| 11297 | return(0); |
| 11298 | } |
| 11299 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11300 | #ifdef XPATH_STREAMING |
| 11301 | /** |
| 11302 | * xmlXPathTryStreamCompile: |
| 11303 | * @ctxt: an XPath context |
| 11304 | * @str: the XPath expression |
| 11305 | * |
| 11306 | * Try to compile the XPath expression as a streamable subset. |
| 11307 | * |
| 11308 | * Returns the compiled expression or NULL if failed to compile. |
| 11309 | */ |
| 11310 | static xmlXPathCompExprPtr |
| 11311 | xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 11312 | /* |
| 11313 | * Optimization: use streaming patterns when the XPath expression can |
| 11314 | * be compiled to a stream lookup |
| 11315 | */ |
| 11316 | xmlPatternPtr stream; |
| 11317 | xmlXPathCompExprPtr comp; |
| 11318 | xmlDictPtr dict = NULL; |
| 11319 | const xmlChar **namespaces = NULL; |
| 11320 | xmlNsPtr ns; |
| 11321 | int i, j; |
| 11322 | |
| 11323 | if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) && |
| 11324 | (!xmlStrchr(str, '@'))) { |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 11325 | const xmlChar *tmp; |
| 11326 | |
| 11327 | /* |
| 11328 | * We don't try to handle :: constructs, just the simplied form at |
| 11329 | * this point |
| 11330 | */ |
| 11331 | tmp = xmlStrchr(str, ':'); |
| 11332 | if ((tmp != NULL) && (tmp[1] == ':')) |
| 11333 | return(NULL); |
| 11334 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11335 | if (ctxt != NULL) { |
| 11336 | dict = ctxt->dict; |
| 11337 | if (ctxt->nsNr > 0) { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 11338 | namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*)); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11339 | if (namespaces == NULL) { |
| 11340 | xmlXPathErrMemory(ctxt, "allocating namespaces array\n"); |
| 11341 | return(NULL); |
| 11342 | } |
| 11343 | for (i = 0, j = 0; (j < ctxt->nsNr); j++) { |
| 11344 | ns = ctxt->namespaces[j]; |
| 11345 | namespaces[i++] = ns->href; |
| 11346 | namespaces[i++] = ns->prefix; |
| 11347 | } |
| 11348 | namespaces[i++] = NULL; |
| 11349 | namespaces[i++] = NULL; |
| 11350 | } |
| 11351 | } |
| 11352 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 11353 | stream = xmlPatterncompile(str, dict, XML_PATTERN_XPATH, |
| 11354 | &namespaces[0]); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11355 | if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) { |
| 11356 | comp = xmlXPathNewCompExpr(); |
| 11357 | if (comp == NULL) { |
| 11358 | xmlXPathErrMemory(ctxt, "allocating streamable expression\n"); |
| 11359 | return(NULL); |
| 11360 | } |
| 11361 | comp->stream = stream; |
| 11362 | comp->dict = dict; |
| 11363 | if (comp->dict) |
| 11364 | xmlDictReference(comp->dict); |
| 11365 | return(comp); |
| 11366 | } |
| 11367 | xmlFreePattern(stream); |
| 11368 | } |
| 11369 | return(NULL); |
| 11370 | } |
| 11371 | #endif /* XPATH_STREAMING */ |
| 11372 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11373 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11374 | * xmlXPathCtxtCompile: |
| 11375 | * @ctxt: an XPath context |
| 11376 | * @str: the XPath expression |
| 11377 | * |
| 11378 | * Compile an XPath expression |
| 11379 | * |
| 11380 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 11381 | * the caller has to free the object. |
| 11382 | */ |
| 11383 | xmlXPathCompExprPtr |
| 11384 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 11385 | xmlXPathParserContextPtr pctxt; |
| 11386 | xmlXPathCompExprPtr comp; |
| 11387 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11388 | #ifdef XPATH_STREAMING |
| 11389 | comp = xmlXPathTryStreamCompile(ctxt, str); |
| 11390 | if (comp != NULL) |
| 11391 | return(comp); |
| 11392 | #endif |
| 11393 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11394 | xmlXPathInit(); |
| 11395 | |
| 11396 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11397 | xmlXPathCompileExpr(pctxt); |
| 11398 | |
| 11399 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 11400 | { |
| 11401 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11402 | return(NULL); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11403 | } |
| 11404 | |
| 11405 | if (*pctxt->cur != 0) { |
| 11406 | /* |
| 11407 | * aleksey: in some cases this line prints *second* error message |
| 11408 | * (see bug #78858) and probably this should be fixed. |
| 11409 | * However, we are not sure that all error messages are printed |
| 11410 | * out in other places. It's not critical so we leave it as-is for now |
| 11411 | */ |
| 11412 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11413 | comp = NULL; |
| 11414 | } else { |
| 11415 | comp = pctxt->comp; |
| 11416 | pctxt->comp = NULL; |
| 11417 | } |
| 11418 | xmlXPathFreeParserContext(pctxt); |
| 11419 | if (comp != NULL) { |
| 11420 | comp->expr = xmlStrdup(str); |
| 11421 | #ifdef DEBUG_EVAL_COUNTS |
| 11422 | comp->string = xmlStrdup(str); |
| 11423 | comp->nb = 0; |
| 11424 | #endif |
| 11425 | } |
| 11426 | return(comp); |
| 11427 | } |
| 11428 | |
| 11429 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11430 | * xmlXPathCompile: |
| 11431 | * @str: the XPath expression |
| 11432 | * |
| 11433 | * Compile an XPath expression |
| 11434 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 11435 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11436 | * the caller has to free the object. |
| 11437 | */ |
| 11438 | xmlXPathCompExprPtr |
| 11439 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11440 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11441 | } |
| 11442 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11443 | /** |
| 11444 | * xmlXPathCompiledEval: |
| 11445 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11446 | * @ctx: the XPath context |
| 11447 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11448 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11449 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11450 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11451 | * the caller has to free the object. |
| 11452 | */ |
| 11453 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11454 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11455 | xmlXPathParserContextPtr ctxt; |
| 11456 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11457 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11458 | #ifndef LIBXML_THREAD_ENABLED |
| 11459 | static int reentance = 0; |
| 11460 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11461 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11462 | CHECK_CTXT(ctx) |
| 11463 | |
| 11464 | if (comp == NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11465 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11466 | xmlXPathInit(); |
| 11467 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11468 | #ifndef LIBXML_THREAD_ENABLED |
| 11469 | reentance++; |
| 11470 | if (reentance > 1) |
| 11471 | xmlXPathDisableOptimizer = 1; |
| 11472 | #endif |
| 11473 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11474 | #ifdef DEBUG_EVAL_COUNTS |
| 11475 | comp->nb++; |
| 11476 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 11477 | fprintf(stderr, "100 x %s\n", comp->string); |
| 11478 | comp->nb = 0; |
| 11479 | } |
| 11480 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11481 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 11482 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11483 | |
| 11484 | if (ctxt->value == NULL) { |
| 11485 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11486 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11487 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11488 | } else { |
| 11489 | res = valuePop(ctxt); |
| 11490 | } |
| 11491 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11492 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11493 | do { |
| 11494 | tmp = valuePop(ctxt); |
| 11495 | if (tmp != NULL) { |
| 11496 | if (tmp != init) |
| 11497 | stack++; |
| 11498 | xmlXPathFreeObject(tmp); |
| 11499 | } |
| 11500 | } while (tmp != NULL); |
| 11501 | if ((stack != 0) && (res != NULL)) { |
| 11502 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11503 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11504 | stack); |
| 11505 | } |
| 11506 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11507 | xmlXPathFreeObject(res); |
| 11508 | res = NULL; |
| 11509 | } |
| 11510 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11511 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11512 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11513 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11514 | #ifndef LIBXML_THREAD_ENABLED |
| 11515 | reentance--; |
| 11516 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11517 | return(res); |
| 11518 | } |
| 11519 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11520 | /** |
| 11521 | * xmlXPathEvalExpr: |
| 11522 | * @ctxt: the XPath Parser context |
| 11523 | * |
| 11524 | * Parse and evaluate an XPath expression in the given context, |
| 11525 | * then push the result on the context stack |
| 11526 | */ |
| 11527 | void |
| 11528 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11529 | #ifdef XPATH_STREAMING |
| 11530 | xmlXPathCompExprPtr comp; |
| 11531 | #endif |
| 11532 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 11533 | if (ctxt == NULL) return; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11534 | |
| 11535 | #ifdef XPATH_STREAMING |
| 11536 | comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base); |
| 11537 | if (comp != NULL) { |
| 11538 | if (ctxt->comp != NULL) |
| 11539 | xmlXPathFreeCompExpr(ctxt->comp); |
| 11540 | ctxt->comp = comp; |
| 11541 | if (ctxt->cur != NULL) |
| 11542 | while (*ctxt->cur != 0) ctxt->cur++; |
| 11543 | } else |
| 11544 | #endif |
| 11545 | { |
| 11546 | xmlXPathCompileExpr(ctxt); |
| 11547 | } |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 11548 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11549 | xmlXPathRunEval(ctxt); |
| 11550 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11551 | |
| 11552 | /** |
| 11553 | * xmlXPathEval: |
| 11554 | * @str: the XPath expression |
| 11555 | * @ctx: the XPath context |
| 11556 | * |
| 11557 | * Evaluate the XPath Location Path in the given context. |
| 11558 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11559 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11560 | * the caller has to free the object. |
| 11561 | */ |
| 11562 | xmlXPathObjectPtr |
| 11563 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 11564 | xmlXPathParserContextPtr ctxt; |
| 11565 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11566 | int stack = 0; |
| 11567 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11568 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11569 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11570 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11571 | |
| 11572 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 11573 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11574 | |
| 11575 | if (ctxt->value == NULL) { |
| 11576 | xmlGenericError(xmlGenericErrorContext, |
| 11577 | "xmlXPathEval: evaluation failed\n"); |
| 11578 | res = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11579 | } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL) |
| 11580 | #ifdef XPATH_STREAMING |
| 11581 | && (ctxt->comp->stream == NULL) |
| 11582 | #endif |
| 11583 | ) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11584 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11585 | res = NULL; |
| 11586 | } else { |
| 11587 | res = valuePop(ctxt); |
| 11588 | } |
| 11589 | |
| 11590 | do { |
| 11591 | tmp = valuePop(ctxt); |
| 11592 | if (tmp != NULL) { |
| 11593 | if (tmp != init) |
| 11594 | stack++; |
| 11595 | xmlXPathFreeObject(tmp); |
| 11596 | } |
| 11597 | } while (tmp != NULL); |
| 11598 | if ((stack != 0) && (res != NULL)) { |
| 11599 | xmlGenericError(xmlGenericErrorContext, |
| 11600 | "xmlXPathEval: %d object left on the stack\n", |
| 11601 | stack); |
| 11602 | } |
| 11603 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11604 | xmlXPathFreeObject(res); |
| 11605 | res = NULL; |
| 11606 | } |
| 11607 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11608 | xmlXPathFreeParserContext(ctxt); |
| 11609 | return(res); |
| 11610 | } |
| 11611 | |
| 11612 | /** |
| 11613 | * xmlXPathEvalExpression: |
| 11614 | * @str: the XPath expression |
| 11615 | * @ctxt: the XPath context |
| 11616 | * |
| 11617 | * Evaluate the XPath expression in the given context. |
| 11618 | * |
| 11619 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 11620 | * the caller has to free the object. |
| 11621 | */ |
| 11622 | xmlXPathObjectPtr |
| 11623 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 11624 | xmlXPathParserContextPtr pctxt; |
| 11625 | xmlXPathObjectPtr res, tmp; |
| 11626 | int stack = 0; |
| 11627 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11628 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11629 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11630 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11631 | |
| 11632 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11633 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11634 | |
| 11635 | if (*pctxt->cur != 0) { |
| 11636 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11637 | res = NULL; |
| 11638 | } else { |
| 11639 | res = valuePop(pctxt); |
| 11640 | } |
| 11641 | do { |
| 11642 | tmp = valuePop(pctxt); |
| 11643 | if (tmp != NULL) { |
| 11644 | xmlXPathFreeObject(tmp); |
| 11645 | stack++; |
| 11646 | } |
| 11647 | } while (tmp != NULL); |
| 11648 | if ((stack != 0) && (res != NULL)) { |
| 11649 | xmlGenericError(xmlGenericErrorContext, |
| 11650 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 11651 | stack); |
| 11652 | } |
| 11653 | xmlXPathFreeParserContext(pctxt); |
| 11654 | return(res); |
| 11655 | } |
| 11656 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11657 | /************************************************************************ |
| 11658 | * * |
| 11659 | * Extra functions not pertaining to the XPath spec * |
| 11660 | * * |
| 11661 | ************************************************************************/ |
| 11662 | /** |
| 11663 | * xmlXPathEscapeUriFunction: |
| 11664 | * @ctxt: the XPath Parser context |
| 11665 | * @nargs: the number of arguments |
| 11666 | * |
| 11667 | * Implement the escape-uri() XPath function |
| 11668 | * string escape-uri(string $str, bool $escape-reserved) |
| 11669 | * |
| 11670 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 11671 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 11672 | * or part of a URI. The effect of the function is to replace any special |
| 11673 | * character in the string by an escape sequence of the form %xx%yy..., |
| 11674 | * where xxyy... is the hexadecimal representation of the octets used to |
| 11675 | * represent the character in UTF-8. |
| 11676 | * |
| 11677 | * The set of characters that are escaped depends on the setting of the |
| 11678 | * boolean argument $escape-reserved. |
| 11679 | * |
| 11680 | * If $escape-reserved is true, all characters are escaped other than lower |
| 11681 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 11682 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 11683 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 11684 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 11685 | * A-F). |
| 11686 | * |
| 11687 | * If $escape-reserved is false, the behavior differs in that characters |
| 11688 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 11689 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 11690 | * |
| 11691 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 11692 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 11693 | * compared using string comparison functions, this function must always use |
| 11694 | * the upper-case letters A-F. |
| 11695 | * |
| 11696 | * Generally, $escape-reserved should be set to true when escaping a string |
| 11697 | * that is to form a single part of a URI, and to false when escaping an |
| 11698 | * entire URI or URI reference. |
| 11699 | * |
| 11700 | * In the case of non-ascii characters, the string is encoded according to |
| 11701 | * utf-8 and then converted according to RFC 2396. |
| 11702 | * |
| 11703 | * Examples |
| 11704 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 11705 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 11706 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 11707 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 11708 | * |
| 11709 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 11710 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11711 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 11712 | xmlXPathObjectPtr str; |
| 11713 | int escape_reserved; |
| 11714 | xmlBufferPtr target; |
| 11715 | xmlChar *cptr; |
| 11716 | xmlChar escape[4]; |
| 11717 | |
| 11718 | CHECK_ARITY(2); |
| 11719 | |
| 11720 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 11721 | |
| 11722 | CAST_TO_STRING; |
| 11723 | str = valuePop(ctxt); |
| 11724 | |
| 11725 | target = xmlBufferCreate(); |
| 11726 | |
| 11727 | escape[0] = '%'; |
| 11728 | escape[3] = 0; |
| 11729 | |
| 11730 | if (target) { |
| 11731 | for (cptr = str->stringval; *cptr; cptr++) { |
| 11732 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 11733 | (*cptr >= 'a' && *cptr <= 'z') || |
| 11734 | (*cptr >= '0' && *cptr <= '9') || |
| 11735 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 11736 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 11737 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 11738 | (*cptr == '%' && |
| 11739 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 11740 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 11741 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 11742 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 11743 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 11744 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 11745 | (!escape_reserved && |
| 11746 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 11747 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 11748 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 11749 | *cptr == ','))) { |
| 11750 | xmlBufferAdd(target, cptr, 1); |
| 11751 | } else { |
| 11752 | if ((*cptr >> 4) < 10) |
| 11753 | escape[1] = '0' + (*cptr >> 4); |
| 11754 | else |
| 11755 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 11756 | if ((*cptr & 0xF) < 10) |
| 11757 | escape[2] = '0' + (*cptr & 0xF); |
| 11758 | else |
| 11759 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 11760 | |
| 11761 | xmlBufferAdd(target, &escape[0], 3); |
| 11762 | } |
| 11763 | } |
| 11764 | } |
| 11765 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 11766 | xmlBufferFree(target); |
| 11767 | xmlXPathFreeObject(str); |
| 11768 | } |
| 11769 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11770 | /** |
| 11771 | * xmlXPathRegisterAllFunctions: |
| 11772 | * @ctxt: the XPath context |
| 11773 | * |
| 11774 | * Registers all default XPath functions in this context |
| 11775 | */ |
| 11776 | void |
| 11777 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 11778 | { |
| 11779 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 11780 | xmlXPathBooleanFunction); |
| 11781 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 11782 | xmlXPathCeilingFunction); |
| 11783 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 11784 | xmlXPathCountFunction); |
| 11785 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 11786 | xmlXPathConcatFunction); |
| 11787 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 11788 | xmlXPathContainsFunction); |
| 11789 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 11790 | xmlXPathIdFunction); |
| 11791 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 11792 | xmlXPathFalseFunction); |
| 11793 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 11794 | xmlXPathFloorFunction); |
| 11795 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 11796 | xmlXPathLastFunction); |
| 11797 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 11798 | xmlXPathLangFunction); |
| 11799 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 11800 | xmlXPathLocalNameFunction); |
| 11801 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 11802 | xmlXPathNotFunction); |
| 11803 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 11804 | xmlXPathNameFunction); |
| 11805 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 11806 | xmlXPathNamespaceURIFunction); |
| 11807 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 11808 | xmlXPathNormalizeFunction); |
| 11809 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 11810 | xmlXPathNumberFunction); |
| 11811 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 11812 | xmlXPathPositionFunction); |
| 11813 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 11814 | xmlXPathRoundFunction); |
| 11815 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 11816 | xmlXPathStringFunction); |
| 11817 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 11818 | xmlXPathStringLengthFunction); |
| 11819 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 11820 | xmlXPathStartsWithFunction); |
| 11821 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 11822 | xmlXPathSubstringFunction); |
| 11823 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 11824 | xmlXPathSubstringBeforeFunction); |
| 11825 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 11826 | xmlXPathSubstringAfterFunction); |
| 11827 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 11828 | xmlXPathSumFunction); |
| 11829 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 11830 | xmlXPathTrueFunction); |
| 11831 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 11832 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11833 | |
| 11834 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 11835 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 11836 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
| 11839 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 11840 | #define bottom_xpath |
| 11841 | #include "elfgcchack.h" |