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 | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 92 | double xmlXPathNZERO = 0; |
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 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 192 | /* |
| 193 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 194 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 195 | static const char *xmlXPathErrorMessages[] = { |
| 196 | "Ok\n", |
| 197 | "Number encoding\n", |
| 198 | "Unfinished literal\n", |
| 199 | "Start of literal\n", |
| 200 | "Expected $ for variable reference\n", |
| 201 | "Undefined variable\n", |
| 202 | "Invalid predicate\n", |
| 203 | "Invalid expression\n", |
| 204 | "Missing closing curly brace\n", |
| 205 | "Unregistered function\n", |
| 206 | "Invalid operand\n", |
| 207 | "Invalid type\n", |
| 208 | "Invalid number of arguments\n", |
| 209 | "Invalid context size\n", |
| 210 | "Invalid context position\n", |
| 211 | "Memory allocation error\n", |
| 212 | "Syntax error\n", |
| 213 | "Resource error\n", |
| 214 | "Sub resource error\n", |
| 215 | "Undefined namespace prefix\n", |
| 216 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 217 | "Char out of XML range\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 218 | "Invalid or incomplete context\n", |
| 219 | "?? Unknown error ??\n" /* Must be last in the list! */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 220 | }; |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 221 | #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \ |
| 222 | sizeof(xmlXPathErrorMessages[0])) - 1) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 223 | /** |
| 224 | * xmlXPathErrMemory: |
| 225 | * @ctxt: an XPath context |
| 226 | * @extra: extra informations |
| 227 | * |
| 228 | * Handle a redefinition of attribute error |
| 229 | */ |
| 230 | static void |
| 231 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 232 | { |
| 233 | if (ctxt != NULL) { |
| 234 | if (extra) { |
| 235 | xmlChar buf[200]; |
| 236 | |
| 237 | xmlStrPrintf(buf, 200, |
| 238 | BAD_CAST "Memory allocation failed : %s\n", |
| 239 | extra); |
| 240 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 241 | } else { |
| 242 | ctxt->lastError.message = (char *) |
| 243 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 244 | } |
| 245 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 246 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 247 | if (ctxt->error != NULL) |
| 248 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 249 | } else { |
| 250 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 251 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 252 | NULL, NULL, XML_FROM_XPATH, |
| 253 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 254 | extra, NULL, NULL, 0, 0, |
| 255 | "Memory allocation failed : %s\n", extra); |
| 256 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 257 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 258 | NULL, NULL, XML_FROM_XPATH, |
| 259 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 260 | NULL, NULL, NULL, 0, 0, |
| 261 | "Memory allocation failed\n"); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 266 | * xmlXPathPErrMemory: |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 267 | * @ctxt: an XPath parser context |
| 268 | * @extra: extra informations |
| 269 | * |
| 270 | * Handle a redefinition of attribute error |
| 271 | */ |
| 272 | static void |
| 273 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 274 | { |
| 275 | ctxt->error = XPATH_MEMORY_ERROR; |
| 276 | if (ctxt == NULL) |
| 277 | xmlXPathErrMemory(NULL, extra); |
| 278 | else |
| 279 | xmlXPathErrMemory(ctxt->context, extra); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * xmlXPathErr: |
| 284 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 285 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 286 | * |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 287 | * Handle an XPath error |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 288 | */ |
| 289 | void |
| 290 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 291 | { |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 292 | if ((error < 0) || (error > MAXERRNO)) |
| 293 | error = MAXERRNO; |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 294 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 295 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 296 | NULL, NULL, XML_FROM_XPATH, |
| 297 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 298 | XML_ERR_ERROR, NULL, 0, |
| 299 | NULL, NULL, NULL, 0, 0, |
| 300 | xmlXPathErrorMessages[error]); |
| 301 | return; |
| 302 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 303 | ctxt->error = error; |
| 304 | if (ctxt->context == NULL) { |
| 305 | __xmlRaiseError(NULL, NULL, NULL, |
| 306 | NULL, NULL, XML_FROM_XPATH, |
| 307 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 308 | XML_ERR_ERROR, NULL, 0, |
| 309 | (const char *) ctxt->base, NULL, NULL, |
| 310 | ctxt->cur - ctxt->base, 0, |
| 311 | xmlXPathErrorMessages[error]); |
| 312 | return; |
| 313 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 314 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 315 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 316 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 317 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 318 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 319 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 320 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 321 | if (ctxt->context->error != NULL) { |
| 322 | ctxt->context->error(ctxt->context->userData, |
| 323 | &ctxt->context->lastError); |
| 324 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 325 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 326 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 327 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 328 | XML_ERR_ERROR, NULL, 0, |
| 329 | (const char *) ctxt->base, NULL, NULL, |
| 330 | ctxt->cur - ctxt->base, 0, |
| 331 | xmlXPathErrorMessages[error]); |
| 332 | } |
| 333 | |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * xmlXPatherror: |
| 338 | * @ctxt: the XPath Parser context |
| 339 | * @file: the file name |
| 340 | * @line: the line number |
| 341 | * @no: the error number |
| 342 | * |
| 343 | * Formats an error message. |
| 344 | */ |
| 345 | void |
| 346 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 347 | int line ATTRIBUTE_UNUSED, int no) { |
| 348 | xmlXPathErr(ctxt, no); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 353 | * * |
| 354 | * Parser Types * |
| 355 | * * |
| 356 | ************************************************************************/ |
| 357 | |
| 358 | /* |
| 359 | * Types are private: |
| 360 | */ |
| 361 | |
| 362 | typedef enum { |
| 363 | XPATH_OP_END=0, |
| 364 | XPATH_OP_AND, |
| 365 | XPATH_OP_OR, |
| 366 | XPATH_OP_EQUAL, |
| 367 | XPATH_OP_CMP, |
| 368 | XPATH_OP_PLUS, |
| 369 | XPATH_OP_MULT, |
| 370 | XPATH_OP_UNION, |
| 371 | XPATH_OP_ROOT, |
| 372 | XPATH_OP_NODE, |
| 373 | XPATH_OP_RESET, |
| 374 | XPATH_OP_COLLECT, |
| 375 | XPATH_OP_VALUE, |
| 376 | XPATH_OP_VARIABLE, |
| 377 | XPATH_OP_FUNCTION, |
| 378 | XPATH_OP_ARG, |
| 379 | XPATH_OP_PREDICATE, |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 380 | XPATH_OP_FILTER, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 381 | XPATH_OP_SORT |
| 382 | #ifdef LIBXML_XPTR_ENABLED |
| 383 | ,XPATH_OP_RANGETO |
| 384 | #endif |
| 385 | } xmlXPathOp; |
| 386 | |
| 387 | typedef enum { |
| 388 | AXIS_ANCESTOR = 1, |
| 389 | AXIS_ANCESTOR_OR_SELF, |
| 390 | AXIS_ATTRIBUTE, |
| 391 | AXIS_CHILD, |
| 392 | AXIS_DESCENDANT, |
| 393 | AXIS_DESCENDANT_OR_SELF, |
| 394 | AXIS_FOLLOWING, |
| 395 | AXIS_FOLLOWING_SIBLING, |
| 396 | AXIS_NAMESPACE, |
| 397 | AXIS_PARENT, |
| 398 | AXIS_PRECEDING, |
| 399 | AXIS_PRECEDING_SIBLING, |
| 400 | AXIS_SELF |
| 401 | } xmlXPathAxisVal; |
| 402 | |
| 403 | typedef enum { |
| 404 | NODE_TEST_NONE = 0, |
| 405 | NODE_TEST_TYPE = 1, |
| 406 | NODE_TEST_PI = 2, |
| 407 | NODE_TEST_ALL = 3, |
| 408 | NODE_TEST_NS = 4, |
| 409 | NODE_TEST_NAME = 5 |
| 410 | } xmlXPathTestVal; |
| 411 | |
| 412 | typedef enum { |
| 413 | NODE_TYPE_NODE = 0, |
| 414 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 415 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 416 | NODE_TYPE_PI = XML_PI_NODE |
| 417 | } xmlXPathTypeVal; |
| 418 | |
| 419 | |
| 420 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 421 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 422 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 423 | xmlXPathOp op; /* The identifier of the operation */ |
| 424 | int ch1; /* First child */ |
| 425 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 426 | int value; |
| 427 | int value2; |
| 428 | int value3; |
| 429 | void *value4; |
| 430 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 431 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 432 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 436 | int nbStep; /* Number of steps in this expression */ |
| 437 | int maxStep; /* Maximum number of steps allocated */ |
| 438 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 439 | int last; /* index of last step in expression */ |
| 440 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 441 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 442 | #ifdef DEBUG_EVAL_COUNTS |
| 443 | int nb; |
| 444 | xmlChar *string; |
| 445 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 446 | #ifdef XPATH_STREAMING |
| 447 | xmlPatternPtr stream; |
| 448 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | /************************************************************************ |
| 452 | * * |
| 453 | * Parser Type functions * |
| 454 | * * |
| 455 | ************************************************************************/ |
| 456 | |
| 457 | /** |
| 458 | * xmlXPathNewCompExpr: |
| 459 | * |
| 460 | * Create a new Xpath component |
| 461 | * |
| 462 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 463 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 464 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 465 | xmlXPathNewCompExpr(void) { |
| 466 | xmlXPathCompExprPtr cur; |
| 467 | |
| 468 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 469 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 470 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 471 | return(NULL); |
| 472 | } |
| 473 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 474 | cur->maxStep = 10; |
| 475 | cur->nbStep = 0; |
| 476 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 477 | sizeof(xmlXPathStepOp)); |
| 478 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 479 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 480 | xmlFree(cur); |
| 481 | return(NULL); |
| 482 | } |
| 483 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 484 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 485 | #ifdef DEBUG_EVAL_COUNTS |
| 486 | cur->nb = 0; |
| 487 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 488 | return(cur); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * xmlXPathFreeCompExpr: |
| 493 | * @comp: an XPATH comp |
| 494 | * |
| 495 | * Free up the memory allocated by @comp |
| 496 | */ |
| 497 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 498 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 499 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 500 | xmlXPathStepOpPtr op; |
| 501 | int i; |
| 502 | |
| 503 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 504 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 505 | if (comp->dict == NULL) { |
| 506 | for (i = 0; i < comp->nbStep; i++) { |
| 507 | op = &comp->steps[i]; |
| 508 | if (op->value4 != NULL) { |
| 509 | if (op->op == XPATH_OP_VALUE) |
| 510 | xmlXPathFreeObject(op->value4); |
| 511 | else |
| 512 | xmlFree(op->value4); |
| 513 | } |
| 514 | if (op->value5 != NULL) |
| 515 | xmlFree(op->value5); |
| 516 | } |
| 517 | } else { |
| 518 | for (i = 0; i < comp->nbStep; i++) { |
| 519 | op = &comp->steps[i]; |
| 520 | if (op->value4 != NULL) { |
| 521 | if (op->op == XPATH_OP_VALUE) |
| 522 | xmlXPathFreeObject(op->value4); |
| 523 | } |
| 524 | } |
| 525 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 526 | } |
| 527 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 528 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 529 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 530 | #ifdef DEBUG_EVAL_COUNTS |
| 531 | if (comp->string != NULL) { |
| 532 | xmlFree(comp->string); |
| 533 | } |
| 534 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 535 | #ifdef XPATH_STREAMING |
| 536 | if (comp->stream != NULL) { |
| 537 | xmlFreePatternList(comp->stream); |
| 538 | } |
| 539 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 540 | if (comp->expr != NULL) { |
| 541 | xmlFree(comp->expr); |
| 542 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 543 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 544 | xmlFree(comp); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * xmlXPathCompExprAdd: |
| 549 | * @comp: the compiled expression |
| 550 | * @ch1: first child index |
| 551 | * @ch2: second child index |
| 552 | * @op: an op |
| 553 | * @value: the first int value |
| 554 | * @value2: the second int value |
| 555 | * @value3: the third int value |
| 556 | * @value4: the first string value |
| 557 | * @value5: the second string value |
| 558 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 559 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 560 | * |
| 561 | * Returns -1 in case of failure, the index otherwise |
| 562 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 563 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 564 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 565 | xmlXPathOp op, int value, |
| 566 | int value2, int value3, void *value4, void *value5) { |
| 567 | if (comp->nbStep >= comp->maxStep) { |
| 568 | xmlXPathStepOp *real; |
| 569 | |
| 570 | comp->maxStep *= 2; |
| 571 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 572 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 573 | if (real == NULL) { |
| 574 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 575 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 576 | return(-1); |
| 577 | } |
| 578 | comp->steps = real; |
| 579 | } |
| 580 | comp->last = comp->nbStep; |
| 581 | comp->steps[comp->nbStep].ch1 = ch1; |
| 582 | comp->steps[comp->nbStep].ch2 = ch2; |
| 583 | comp->steps[comp->nbStep].op = op; |
| 584 | comp->steps[comp->nbStep].value = value; |
| 585 | comp->steps[comp->nbStep].value2 = value2; |
| 586 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 587 | if ((comp->dict != NULL) && |
| 588 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 589 | (op == XPATH_OP_COLLECT))) { |
| 590 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 591 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 592 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 593 | xmlFree(value4); |
| 594 | } else |
| 595 | comp->steps[comp->nbStep].value4 = NULL; |
| 596 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 597 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 598 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 599 | xmlFree(value5); |
| 600 | } else |
| 601 | comp->steps[comp->nbStep].value5 = NULL; |
| 602 | } else { |
| 603 | comp->steps[comp->nbStep].value4 = value4; |
| 604 | comp->steps[comp->nbStep].value5 = value5; |
| 605 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 606 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 607 | return(comp->nbStep++); |
| 608 | } |
| 609 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 610 | /** |
| 611 | * xmlXPathCompSwap: |
| 612 | * @comp: the compiled expression |
| 613 | * @op: operation index |
| 614 | * |
| 615 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 616 | */ |
| 617 | static void |
| 618 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 619 | int tmp; |
| 620 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 621 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 622 | /* |
| 623 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 624 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 625 | * application |
| 626 | */ |
| 627 | if (xmlXPathDisableOptimizer) |
| 628 | return; |
| 629 | #endif |
| 630 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 631 | tmp = op->ch1; |
| 632 | op->ch1 = op->ch2; |
| 633 | op->ch2 = tmp; |
| 634 | } |
| 635 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 636 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 637 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 638 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 639 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 640 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 641 | (op), (val), (val2), (val3), (val4), (val5)) |
| 642 | |
| 643 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 644 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 645 | |
| 646 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 647 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 648 | |
| 649 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 650 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 651 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 652 | |
| 653 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 654 | * * |
| 655 | * Debugging related functions * |
| 656 | * * |
| 657 | ************************************************************************/ |
| 658 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 659 | #define STRANGE \ |
| 660 | xmlGenericError(xmlGenericErrorContext, \ |
| 661 | "Internal error at %s:%d\n", \ |
| 662 | __FILE__, __LINE__); |
| 663 | |
| 664 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 665 | static void |
| 666 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 667 | int i; |
| 668 | char shift[100]; |
| 669 | |
| 670 | for (i = 0;((i < depth) && (i < 25));i++) |
| 671 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 672 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 673 | if (cur == NULL) { |
| 674 | fprintf(output, shift); |
| 675 | fprintf(output, "Node is NULL !\n"); |
| 676 | return; |
| 677 | |
| 678 | } |
| 679 | |
| 680 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 681 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 682 | fprintf(output, shift); |
| 683 | fprintf(output, " /\n"); |
| 684 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 685 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 686 | else |
| 687 | xmlDebugDumpOneNode(output, cur, depth); |
| 688 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 689 | static void |
| 690 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 691 | xmlNodePtr tmp; |
| 692 | int i; |
| 693 | char shift[100]; |
| 694 | |
| 695 | for (i = 0;((i < depth) && (i < 25));i++) |
| 696 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 697 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 698 | if (cur == NULL) { |
| 699 | fprintf(output, shift); |
| 700 | fprintf(output, "Node is NULL !\n"); |
| 701 | return; |
| 702 | |
| 703 | } |
| 704 | |
| 705 | while (cur != NULL) { |
| 706 | tmp = cur; |
| 707 | cur = cur->next; |
| 708 | xmlDebugDumpOneNode(output, tmp, depth); |
| 709 | } |
| 710 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 711 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 712 | static void |
| 713 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 714 | int i; |
| 715 | char shift[100]; |
| 716 | |
| 717 | for (i = 0;((i < depth) && (i < 25));i++) |
| 718 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 719 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 720 | |
| 721 | if (cur == NULL) { |
| 722 | fprintf(output, shift); |
| 723 | fprintf(output, "NodeSet is NULL !\n"); |
| 724 | return; |
| 725 | |
| 726 | } |
| 727 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 728 | if (cur != NULL) { |
| 729 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 730 | for (i = 0;i < cur->nodeNr;i++) { |
| 731 | fprintf(output, shift); |
| 732 | fprintf(output, "%d", i + 1); |
| 733 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 734 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 738 | static void |
| 739 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 740 | int i; |
| 741 | char shift[100]; |
| 742 | |
| 743 | for (i = 0;((i < depth) && (i < 25));i++) |
| 744 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 745 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 746 | |
| 747 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 748 | fprintf(output, shift); |
| 749 | fprintf(output, "Value Tree is NULL !\n"); |
| 750 | return; |
| 751 | |
| 752 | } |
| 753 | |
| 754 | fprintf(output, shift); |
| 755 | fprintf(output, "%d", i + 1); |
| 756 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 757 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 758 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 759 | static void |
| 760 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | int i; |
| 762 | char shift[100]; |
| 763 | |
| 764 | for (i = 0;((i < depth) && (i < 25));i++) |
| 765 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 766 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 767 | |
| 768 | if (cur == NULL) { |
| 769 | fprintf(output, shift); |
| 770 | fprintf(output, "LocationSet is NULL !\n"); |
| 771 | return; |
| 772 | |
| 773 | } |
| 774 | |
| 775 | for (i = 0;i < cur->locNr;i++) { |
| 776 | fprintf(output, shift); |
| 777 | fprintf(output, "%d : ", i + 1); |
| 778 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 779 | } |
| 780 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 781 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 782 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 783 | /** |
| 784 | * xmlXPathDebugDumpObject: |
| 785 | * @output: the FILE * to dump the output |
| 786 | * @cur: the object to inspect |
| 787 | * @depth: indentation level |
| 788 | * |
| 789 | * Dump the content of the object for debugging purposes |
| 790 | */ |
| 791 | void |
| 792 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 793 | int i; |
| 794 | char shift[100]; |
| 795 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 796 | if (output == NULL) return; |
| 797 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 798 | for (i = 0;((i < depth) && (i < 25));i++) |
| 799 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 800 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 801 | |
| 802 | fprintf(output, shift); |
| 803 | |
| 804 | if (cur == NULL) { |
| 805 | fprintf(output, "Object is empty (NULL)\n"); |
| 806 | return; |
| 807 | } |
| 808 | switch(cur->type) { |
| 809 | case XPATH_UNDEFINED: |
| 810 | fprintf(output, "Object is uninitialized\n"); |
| 811 | break; |
| 812 | case XPATH_NODESET: |
| 813 | fprintf(output, "Object is a Node Set :\n"); |
| 814 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 815 | break; |
| 816 | case XPATH_XSLT_TREE: |
| 817 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 818 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 819 | break; |
| 820 | case XPATH_BOOLEAN: |
| 821 | fprintf(output, "Object is a Boolean : "); |
| 822 | if (cur->boolval) fprintf(output, "true\n"); |
| 823 | else fprintf(output, "false\n"); |
| 824 | break; |
| 825 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 826 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 827 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 828 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 829 | break; |
| 830 | case -1: |
| 831 | fprintf(output, "Object is a number : -Infinity\n"); |
| 832 | break; |
| 833 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 834 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 835 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 836 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 837 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 838 | } else { |
| 839 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 840 | } |
| 841 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 842 | break; |
| 843 | case XPATH_STRING: |
| 844 | fprintf(output, "Object is a string : "); |
| 845 | xmlDebugDumpString(output, cur->stringval); |
| 846 | fprintf(output, "\n"); |
| 847 | break; |
| 848 | case XPATH_POINT: |
| 849 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 850 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 851 | fprintf(output, "\n"); |
| 852 | break; |
| 853 | case XPATH_RANGE: |
| 854 | if ((cur->user2 == NULL) || |
| 855 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 856 | fprintf(output, "Object is a collapsed range :\n"); |
| 857 | fprintf(output, shift); |
| 858 | if (cur->index >= 0) |
| 859 | fprintf(output, "index %d in ", cur->index); |
| 860 | fprintf(output, "node\n"); |
| 861 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 862 | depth + 1); |
| 863 | } else { |
| 864 | fprintf(output, "Object is a range :\n"); |
| 865 | fprintf(output, shift); |
| 866 | fprintf(output, "From "); |
| 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 | fprintf(output, shift); |
| 873 | fprintf(output, "To "); |
| 874 | if (cur->index2 >= 0) |
| 875 | fprintf(output, "index %d in ", cur->index2); |
| 876 | fprintf(output, "node\n"); |
| 877 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 878 | depth + 1); |
| 879 | fprintf(output, "\n"); |
| 880 | } |
| 881 | break; |
| 882 | case XPATH_LOCATIONSET: |
| 883 | #if defined(LIBXML_XPTR_ENABLED) |
| 884 | fprintf(output, "Object is a Location Set:\n"); |
| 885 | xmlXPathDebugDumpLocationSet(output, |
| 886 | (xmlLocationSetPtr) cur->user, depth); |
| 887 | #endif |
| 888 | break; |
| 889 | case XPATH_USERS: |
| 890 | fprintf(output, "Object is user defined\n"); |
| 891 | break; |
| 892 | } |
| 893 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 894 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 895 | static void |
| 896 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 897 | xmlXPathStepOpPtr op, int depth) { |
| 898 | int i; |
| 899 | char shift[100]; |
| 900 | |
| 901 | for (i = 0;((i < depth) && (i < 25));i++) |
| 902 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 903 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 904 | |
| 905 | fprintf(output, shift); |
| 906 | if (op == NULL) { |
| 907 | fprintf(output, "Step is NULL\n"); |
| 908 | return; |
| 909 | } |
| 910 | switch (op->op) { |
| 911 | case XPATH_OP_END: |
| 912 | fprintf(output, "END"); break; |
| 913 | case XPATH_OP_AND: |
| 914 | fprintf(output, "AND"); break; |
| 915 | case XPATH_OP_OR: |
| 916 | fprintf(output, "OR"); break; |
| 917 | case XPATH_OP_EQUAL: |
| 918 | if (op->value) |
| 919 | fprintf(output, "EQUAL ="); |
| 920 | else |
| 921 | fprintf(output, "EQUAL !="); |
| 922 | break; |
| 923 | case XPATH_OP_CMP: |
| 924 | if (op->value) |
| 925 | fprintf(output, "CMP <"); |
| 926 | else |
| 927 | fprintf(output, "CMP >"); |
| 928 | if (!op->value2) |
| 929 | fprintf(output, "="); |
| 930 | break; |
| 931 | case XPATH_OP_PLUS: |
| 932 | if (op->value == 0) |
| 933 | fprintf(output, "PLUS -"); |
| 934 | else if (op->value == 1) |
| 935 | fprintf(output, "PLUS +"); |
| 936 | else if (op->value == 2) |
| 937 | fprintf(output, "PLUS unary -"); |
| 938 | else if (op->value == 3) |
| 939 | fprintf(output, "PLUS unary - -"); |
| 940 | break; |
| 941 | case XPATH_OP_MULT: |
| 942 | if (op->value == 0) |
| 943 | fprintf(output, "MULT *"); |
| 944 | else if (op->value == 1) |
| 945 | fprintf(output, "MULT div"); |
| 946 | else |
| 947 | fprintf(output, "MULT mod"); |
| 948 | break; |
| 949 | case XPATH_OP_UNION: |
| 950 | fprintf(output, "UNION"); break; |
| 951 | case XPATH_OP_ROOT: |
| 952 | fprintf(output, "ROOT"); break; |
| 953 | case XPATH_OP_NODE: |
| 954 | fprintf(output, "NODE"); break; |
| 955 | case XPATH_OP_RESET: |
| 956 | fprintf(output, "RESET"); break; |
| 957 | case XPATH_OP_SORT: |
| 958 | fprintf(output, "SORT"); break; |
| 959 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 960 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 961 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 962 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 963 | const xmlChar *prefix = op->value4; |
| 964 | const xmlChar *name = op->value5; |
| 965 | |
| 966 | fprintf(output, "COLLECT "); |
| 967 | switch (axis) { |
| 968 | case AXIS_ANCESTOR: |
| 969 | fprintf(output, " 'ancestors' "); break; |
| 970 | case AXIS_ANCESTOR_OR_SELF: |
| 971 | fprintf(output, " 'ancestors-or-self' "); break; |
| 972 | case AXIS_ATTRIBUTE: |
| 973 | fprintf(output, " 'attributes' "); break; |
| 974 | case AXIS_CHILD: |
| 975 | fprintf(output, " 'child' "); break; |
| 976 | case AXIS_DESCENDANT: |
| 977 | fprintf(output, " 'descendant' "); break; |
| 978 | case AXIS_DESCENDANT_OR_SELF: |
| 979 | fprintf(output, " 'descendant-or-self' "); break; |
| 980 | case AXIS_FOLLOWING: |
| 981 | fprintf(output, " 'following' "); break; |
| 982 | case AXIS_FOLLOWING_SIBLING: |
| 983 | fprintf(output, " 'following-siblings' "); break; |
| 984 | case AXIS_NAMESPACE: |
| 985 | fprintf(output, " 'namespace' "); break; |
| 986 | case AXIS_PARENT: |
| 987 | fprintf(output, " 'parent' "); break; |
| 988 | case AXIS_PRECEDING: |
| 989 | fprintf(output, " 'preceding' "); break; |
| 990 | case AXIS_PRECEDING_SIBLING: |
| 991 | fprintf(output, " 'preceding-sibling' "); break; |
| 992 | case AXIS_SELF: |
| 993 | fprintf(output, " 'self' "); break; |
| 994 | } |
| 995 | switch (test) { |
| 996 | case NODE_TEST_NONE: |
| 997 | fprintf(output, "'none' "); break; |
| 998 | case NODE_TEST_TYPE: |
| 999 | fprintf(output, "'type' "); break; |
| 1000 | case NODE_TEST_PI: |
| 1001 | fprintf(output, "'PI' "); break; |
| 1002 | case NODE_TEST_ALL: |
| 1003 | fprintf(output, "'all' "); break; |
| 1004 | case NODE_TEST_NS: |
| 1005 | fprintf(output, "'namespace' "); break; |
| 1006 | case NODE_TEST_NAME: |
| 1007 | fprintf(output, "'name' "); break; |
| 1008 | } |
| 1009 | switch (type) { |
| 1010 | case NODE_TYPE_NODE: |
| 1011 | fprintf(output, "'node' "); break; |
| 1012 | case NODE_TYPE_COMMENT: |
| 1013 | fprintf(output, "'comment' "); break; |
| 1014 | case NODE_TYPE_TEXT: |
| 1015 | fprintf(output, "'text' "); break; |
| 1016 | case NODE_TYPE_PI: |
| 1017 | fprintf(output, "'PI' "); break; |
| 1018 | } |
| 1019 | if (prefix != NULL) |
| 1020 | fprintf(output, "%s:", prefix); |
| 1021 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1022 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | |
| 1025 | } |
| 1026 | case XPATH_OP_VALUE: { |
| 1027 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1028 | |
| 1029 | fprintf(output, "ELEM "); |
| 1030 | xmlXPathDebugDumpObject(output, object, 0); |
| 1031 | goto finish; |
| 1032 | } |
| 1033 | case XPATH_OP_VARIABLE: { |
| 1034 | const xmlChar *prefix = op->value5; |
| 1035 | const xmlChar *name = op->value4; |
| 1036 | |
| 1037 | if (prefix != NULL) |
| 1038 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1039 | else |
| 1040 | fprintf(output, "VARIABLE %s", name); |
| 1041 | break; |
| 1042 | } |
| 1043 | case XPATH_OP_FUNCTION: { |
| 1044 | int nbargs = op->value; |
| 1045 | const xmlChar *prefix = op->value5; |
| 1046 | const xmlChar *name = op->value4; |
| 1047 | |
| 1048 | if (prefix != NULL) |
| 1049 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1050 | prefix, name, nbargs); |
| 1051 | else |
| 1052 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1053 | break; |
| 1054 | } |
| 1055 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1056 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1057 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1058 | #ifdef LIBXML_XPTR_ENABLED |
| 1059 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1060 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1061 | default: |
| 1062 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1063 | } |
| 1064 | fprintf(output, "\n"); |
| 1065 | finish: |
| 1066 | if (op->ch1 >= 0) |
| 1067 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1068 | if (op->ch2 >= 0) |
| 1069 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1070 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1071 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1072 | /** |
| 1073 | * xmlXPathDebugDumpCompExpr: |
| 1074 | * @output: the FILE * for the output |
| 1075 | * @comp: the precompiled XPath expression |
| 1076 | * @depth: the indentation level. |
| 1077 | * |
| 1078 | * Dumps the tree of the compiled XPath expression. |
| 1079 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1080 | void |
| 1081 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1082 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1083 | int i; |
| 1084 | char shift[100]; |
| 1085 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1086 | if ((output == NULL) || (comp == NULL)) return; |
| 1087 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1088 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1089 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1090 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1091 | |
| 1092 | fprintf(output, shift); |
| 1093 | |
| 1094 | if (comp == NULL) { |
| 1095 | fprintf(output, "Compiled Expression is NULL\n"); |
| 1096 | return; |
| 1097 | } |
| 1098 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1099 | comp->nbStep); |
| 1100 | i = comp->last; |
| 1101 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1102 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1103 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1104 | |
| 1105 | /************************************************************************ |
| 1106 | * * |
| 1107 | * Parser stacks related functions and macros * |
| 1108 | * * |
| 1109 | ************************************************************************/ |
| 1110 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1111 | /** |
| 1112 | * valuePop: |
| 1113 | * @ctxt: an XPath evaluation context |
| 1114 | * |
| 1115 | * Pops the top XPath object from the value stack |
| 1116 | * |
| 1117 | * Returns the XPath object just removed |
| 1118 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1119 | extern xmlXPathObjectPtr |
| 1120 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1121 | { |
| 1122 | xmlXPathObjectPtr ret; |
| 1123 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1124 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1125 | return (0); |
| 1126 | ctxt->valueNr--; |
| 1127 | if (ctxt->valueNr > 0) |
| 1128 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1129 | else |
| 1130 | ctxt->value = NULL; |
| 1131 | ret = ctxt->valueTab[ctxt->valueNr]; |
| 1132 | ctxt->valueTab[ctxt->valueNr] = 0; |
| 1133 | return (ret); |
| 1134 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1135 | /** |
| 1136 | * valuePush: |
| 1137 | * @ctxt: an XPath evaluation context |
| 1138 | * @value: the XPath object |
| 1139 | * |
| 1140 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1141 | * |
| 1142 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1143 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1144 | extern int |
| 1145 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1146 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1147 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1148 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1149 | xmlXPathObjectPtr *tmp; |
| 1150 | |
| 1151 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1152 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1153 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1154 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1155 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1156 | return (0); |
| 1157 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1158 | ctxt->valueMax *= 2; |
| 1159 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1160 | } |
| 1161 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1162 | ctxt->value = value; |
| 1163 | return (ctxt->valueNr++); |
| 1164 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1165 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1166 | /** |
| 1167 | * xmlXPathPopBoolean: |
| 1168 | * @ctxt: an XPath parser context |
| 1169 | * |
| 1170 | * Pops a boolean from the stack, handling conversion if needed. |
| 1171 | * Check error with #xmlXPathCheckError. |
| 1172 | * |
| 1173 | * Returns the boolean |
| 1174 | */ |
| 1175 | int |
| 1176 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1177 | xmlXPathObjectPtr obj; |
| 1178 | int ret; |
| 1179 | |
| 1180 | obj = valuePop(ctxt); |
| 1181 | if (obj == NULL) { |
| 1182 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1183 | return(0); |
| 1184 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1185 | if (obj->type != XPATH_BOOLEAN) |
| 1186 | ret = xmlXPathCastToBoolean(obj); |
| 1187 | else |
| 1188 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1189 | xmlXPathFreeObject(obj); |
| 1190 | return(ret); |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * xmlXPathPopNumber: |
| 1195 | * @ctxt: an XPath parser context |
| 1196 | * |
| 1197 | * Pops a number from the stack, handling conversion if needed. |
| 1198 | * Check error with #xmlXPathCheckError. |
| 1199 | * |
| 1200 | * Returns the number |
| 1201 | */ |
| 1202 | double |
| 1203 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1204 | xmlXPathObjectPtr obj; |
| 1205 | double ret; |
| 1206 | |
| 1207 | obj = valuePop(ctxt); |
| 1208 | if (obj == NULL) { |
| 1209 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1210 | return(0); |
| 1211 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1212 | if (obj->type != XPATH_NUMBER) |
| 1213 | ret = xmlXPathCastToNumber(obj); |
| 1214 | else |
| 1215 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1216 | xmlXPathFreeObject(obj); |
| 1217 | return(ret); |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * xmlXPathPopString: |
| 1222 | * @ctxt: an XPath parser context |
| 1223 | * |
| 1224 | * Pops a string from the stack, handling conversion if needed. |
| 1225 | * Check error with #xmlXPathCheckError. |
| 1226 | * |
| 1227 | * Returns the string |
| 1228 | */ |
| 1229 | xmlChar * |
| 1230 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1231 | xmlXPathObjectPtr obj; |
| 1232 | xmlChar * ret; |
| 1233 | |
| 1234 | obj = valuePop(ctxt); |
| 1235 | if (obj == NULL) { |
| 1236 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1237 | return(NULL); |
| 1238 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1239 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1240 | /* TODO: needs refactoring somewhere else */ |
| 1241 | if (obj->stringval == ret) |
| 1242 | obj->stringval = NULL; |
| 1243 | xmlXPathFreeObject(obj); |
| 1244 | return(ret); |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * xmlXPathPopNodeSet: |
| 1249 | * @ctxt: an XPath parser context |
| 1250 | * |
| 1251 | * Pops a node-set from the stack, handling conversion if needed. |
| 1252 | * Check error with #xmlXPathCheckError. |
| 1253 | * |
| 1254 | * Returns the node-set |
| 1255 | */ |
| 1256 | xmlNodeSetPtr |
| 1257 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1258 | xmlXPathObjectPtr obj; |
| 1259 | xmlNodeSetPtr ret; |
| 1260 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 1261 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1262 | if (ctxt->value == NULL) { |
| 1263 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1264 | return(NULL); |
| 1265 | } |
| 1266 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1267 | xmlXPathSetTypeError(ctxt); |
| 1268 | return(NULL); |
| 1269 | } |
| 1270 | obj = valuePop(ctxt); |
| 1271 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1272 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1273 | /* to fix memory leak of not clearing obj->user */ |
| 1274 | if (obj->boolval && obj->user != NULL) |
| 1275 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1276 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1277 | xmlXPathFreeNodeSetList(obj); |
| 1278 | return(ret); |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * xmlXPathPopExternal: |
| 1283 | * @ctxt: an XPath parser context |
| 1284 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1285 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1286 | * Check error with #xmlXPathCheckError. |
| 1287 | * |
| 1288 | * Returns the object |
| 1289 | */ |
| 1290 | void * |
| 1291 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1292 | xmlXPathObjectPtr obj; |
| 1293 | void * ret; |
| 1294 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1295 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1296 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1297 | return(NULL); |
| 1298 | } |
| 1299 | if (ctxt->value->type != XPATH_USERS) { |
| 1300 | xmlXPathSetTypeError(ctxt); |
| 1301 | return(NULL); |
| 1302 | } |
| 1303 | obj = valuePop(ctxt); |
| 1304 | ret = obj->user; |
| 1305 | xmlXPathFreeObject(obj); |
| 1306 | return(ret); |
| 1307 | } |
| 1308 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1309 | /* |
| 1310 | * Macros for accessing the content. Those should be used only by the parser, |
| 1311 | * and not exported. |
| 1312 | * |
| 1313 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1314 | * |
| 1315 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1316 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1317 | * in ISO-Latin or UTF-8. |
| 1318 | * This should be used internally by the parser |
| 1319 | * only to compare to ASCII values otherwise it would break when |
| 1320 | * running with UTF-8 encoding. |
| 1321 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1322 | * to compare on ASCII based substring. |
| 1323 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1324 | * strings within the parser. |
| 1325 | * CURRENT Returns the current char value, with the full decoding of |
| 1326 | * UTF-8 if we are using this mode. It returns an int. |
| 1327 | * NEXT Skip to the next character, this does the proper decoding |
| 1328 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1329 | * It returns the pointer to the current xmlChar. |
| 1330 | */ |
| 1331 | |
| 1332 | #define CUR (*ctxt->cur) |
| 1333 | #define SKIP(val) ctxt->cur += (val) |
| 1334 | #define NXT(val) ctxt->cur[(val)] |
| 1335 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1336 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1337 | |
| 1338 | #define COPY_BUF(l,b,i,v) \ |
| 1339 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1340 | else i += xmlCopyChar(l,&b[i],v) |
| 1341 | |
| 1342 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1343 | |
| 1344 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1345 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1346 | |
| 1347 | #define CURRENT (*ctxt->cur) |
| 1348 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1349 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1350 | |
| 1351 | #ifndef DBL_DIG |
| 1352 | #define DBL_DIG 16 |
| 1353 | #endif |
| 1354 | #ifndef DBL_EPSILON |
| 1355 | #define DBL_EPSILON 1E-9 |
| 1356 | #endif |
| 1357 | |
| 1358 | #define UPPER_DOUBLE 1E9 |
| 1359 | #define LOWER_DOUBLE 1E-5 |
| 1360 | |
| 1361 | #define INTEGER_DIGITS DBL_DIG |
| 1362 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1363 | #define EXPONENT_DIGITS (3 + 2) |
| 1364 | |
| 1365 | /** |
| 1366 | * xmlXPathFormatNumber: |
| 1367 | * @number: number to format |
| 1368 | * @buffer: output buffer |
| 1369 | * @buffersize: size of output buffer |
| 1370 | * |
| 1371 | * Convert the number into a string representation. |
| 1372 | */ |
| 1373 | static void |
| 1374 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1375 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1376 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1377 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1378 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1379 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | case -1: |
| 1382 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1383 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1384 | break; |
| 1385 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1386 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1387 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1388 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1389 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1390 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1391 | } else if (number == ((int) number)) { |
| 1392 | char work[30]; |
| 1393 | char *ptr, *cur; |
| 1394 | int res, value = (int) number; |
| 1395 | |
| 1396 | ptr = &buffer[0]; |
| 1397 | if (value < 0) { |
| 1398 | *ptr++ = '-'; |
| 1399 | value = -value; |
| 1400 | } |
| 1401 | if (value == 0) { |
| 1402 | *ptr++ = '0'; |
| 1403 | } else { |
| 1404 | cur = &work[0]; |
| 1405 | while (value != 0) { |
| 1406 | res = value % 10; |
| 1407 | value = value / 10; |
| 1408 | *cur++ = '0' + res; |
| 1409 | } |
| 1410 | cur--; |
| 1411 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1412 | *ptr++ = *cur--; |
| 1413 | } |
| 1414 | } |
| 1415 | if (ptr - buffer < buffersize) { |
| 1416 | *ptr = 0; |
| 1417 | } else if (buffersize > 0) { |
| 1418 | ptr--; |
| 1419 | *ptr = 0; |
| 1420 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1421 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1422 | /* 3 is sign, decimal point, and terminating zero */ |
| 1423 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1424 | int integer_place, fraction_place; |
| 1425 | char *ptr; |
| 1426 | char *after_fraction; |
| 1427 | double absolute_value; |
| 1428 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1429 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1430 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1431 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1432 | /* |
| 1433 | * First choose format - scientific or regular floating point. |
| 1434 | * In either case, result is in work, and after_fraction points |
| 1435 | * just past the fractional part. |
| 1436 | */ |
| 1437 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1438 | (absolute_value < LOWER_DOUBLE)) && |
| 1439 | (absolute_value != 0.0) ) { |
| 1440 | /* Use scientific notation */ |
| 1441 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1442 | fraction_place = DBL_DIG - 1; |
| 1443 | snprintf(work, sizeof(work),"%*.*e", |
| 1444 | integer_place, fraction_place, number); |
| 1445 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1446 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1447 | else { |
| 1448 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1449 | if (absolute_value > 0.0) |
| 1450 | integer_place = 1 + (int)log10(absolute_value); |
| 1451 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1452 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1453 | fraction_place = (integer_place > 0) |
| 1454 | ? DBL_DIG - integer_place |
| 1455 | : DBL_DIG; |
| 1456 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1457 | fraction_place, number); |
| 1458 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1461 | /* Remove fractional trailing zeroes */ |
| 1462 | ptr = after_fraction; |
| 1463 | while (*(--ptr) == '0') |
| 1464 | ; |
| 1465 | if (*ptr != '.') |
| 1466 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1467 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1468 | |
| 1469 | /* Finally copy result back to caller */ |
| 1470 | size = strlen(work) + 1; |
| 1471 | if (size > buffersize) { |
| 1472 | work[buffersize - 1] = 0; |
| 1473 | size = buffersize; |
| 1474 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1475 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1476 | } |
| 1477 | break; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1481 | |
| 1482 | /************************************************************************ |
| 1483 | * * |
| 1484 | * Routines to handle NodeSets * |
| 1485 | * * |
| 1486 | ************************************************************************/ |
| 1487 | |
| 1488 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1489 | * xmlXPathOrderDocElems: |
| 1490 | * @doc: an input document |
| 1491 | * |
| 1492 | * Call this routine to speed up XPath computation on static documents. |
| 1493 | * This stamps all the element nodes with the document order |
| 1494 | * 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] | 1495 | * field, the value stored is actually - the node number (starting at -1) |
| 1496 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1497 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1498 | * 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] | 1499 | * of error. |
| 1500 | */ |
| 1501 | long |
| 1502 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1503 | long count = 0; |
| 1504 | xmlNodePtr cur; |
| 1505 | |
| 1506 | if (doc == NULL) |
| 1507 | return(-1); |
| 1508 | cur = doc->children; |
| 1509 | while (cur != NULL) { |
| 1510 | if (cur->type == XML_ELEMENT_NODE) { |
| 1511 | cur->content = (void *) (-(++count)); |
| 1512 | if (cur->children != NULL) { |
| 1513 | cur = cur->children; |
| 1514 | continue; |
| 1515 | } |
| 1516 | } |
| 1517 | if (cur->next != NULL) { |
| 1518 | cur = cur->next; |
| 1519 | continue; |
| 1520 | } |
| 1521 | do { |
| 1522 | cur = cur->parent; |
| 1523 | if (cur == NULL) |
| 1524 | break; |
| 1525 | if (cur == (xmlNodePtr) doc) { |
| 1526 | cur = NULL; |
| 1527 | break; |
| 1528 | } |
| 1529 | if (cur->next != NULL) { |
| 1530 | cur = cur->next; |
| 1531 | break; |
| 1532 | } |
| 1533 | } while (cur != NULL); |
| 1534 | } |
| 1535 | return(count); |
| 1536 | } |
| 1537 | |
| 1538 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1539 | * xmlXPathCmpNodes: |
| 1540 | * @node1: the first node |
| 1541 | * @node2: the second node |
| 1542 | * |
| 1543 | * Compare two nodes w.r.t document order |
| 1544 | * |
| 1545 | * 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] | 1546 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1547 | */ |
| 1548 | int |
| 1549 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1550 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1551 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1552 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1553 | xmlNodePtr cur, root; |
| 1554 | |
| 1555 | if ((node1 == NULL) || (node2 == NULL)) |
| 1556 | return(-2); |
| 1557 | /* |
| 1558 | * a couple of optimizations which will avoid computations in most cases |
| 1559 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1560 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1561 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1562 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1563 | node1 = node1->parent; |
| 1564 | } |
| 1565 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1566 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1567 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1568 | node2 = node2->parent; |
| 1569 | } |
| 1570 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1571 | if (attr1 == attr2) { |
| 1572 | /* not required, but we keep attributes in order */ |
| 1573 | if (attr1 != 0) { |
| 1574 | cur = attrNode2->prev; |
| 1575 | while (cur != NULL) { |
| 1576 | if (cur == attrNode1) |
| 1577 | return (1); |
| 1578 | cur = cur->prev; |
| 1579 | } |
| 1580 | return (-1); |
| 1581 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1582 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1583 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1584 | if (attr2 == 1) |
| 1585 | return(1); |
| 1586 | return(-1); |
| 1587 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1588 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1589 | (node2->type == XML_NAMESPACE_DECL)) |
| 1590 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | if (node1 == node2->prev) |
| 1592 | return(1); |
| 1593 | if (node1 == node2->next) |
| 1594 | return(-1); |
| 1595 | |
| 1596 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1597 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1598 | */ |
| 1599 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1600 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1601 | (0 > (long) node1->content) && |
| 1602 | (0 > (long) node2->content) && |
| 1603 | (node1->doc == node2->doc)) { |
| 1604 | long l1, l2; |
| 1605 | |
| 1606 | l1 = -((long) node1->content); |
| 1607 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1608 | if (l1 < l2) |
| 1609 | return(1); |
| 1610 | if (l1 > l2) |
| 1611 | return(-1); |
| 1612 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1613 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1614 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1615 | * compute depth to root |
| 1616 | */ |
| 1617 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1618 | if (cur == node1) |
| 1619 | return(1); |
| 1620 | depth2++; |
| 1621 | } |
| 1622 | root = cur; |
| 1623 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1624 | if (cur == node2) |
| 1625 | return(-1); |
| 1626 | depth1++; |
| 1627 | } |
| 1628 | /* |
| 1629 | * Distinct document (or distinct entities :-( ) case. |
| 1630 | */ |
| 1631 | if (root != cur) { |
| 1632 | return(-2); |
| 1633 | } |
| 1634 | /* |
| 1635 | * get the nearest common ancestor. |
| 1636 | */ |
| 1637 | while (depth1 > depth2) { |
| 1638 | depth1--; |
| 1639 | node1 = node1->parent; |
| 1640 | } |
| 1641 | while (depth2 > depth1) { |
| 1642 | depth2--; |
| 1643 | node2 = node2->parent; |
| 1644 | } |
| 1645 | while (node1->parent != node2->parent) { |
| 1646 | node1 = node1->parent; |
| 1647 | node2 = node2->parent; |
| 1648 | /* should not happen but just in case ... */ |
| 1649 | if ((node1 == NULL) || (node2 == NULL)) |
| 1650 | return(-2); |
| 1651 | } |
| 1652 | /* |
| 1653 | * Find who's first. |
| 1654 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1655 | if (node1 == node2->prev) |
| 1656 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1657 | if (node1 == node2->next) |
| 1658 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1659 | /* |
| 1660 | * Speedup using document order if availble. |
| 1661 | */ |
| 1662 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1663 | (node2->type == XML_ELEMENT_NODE) && |
| 1664 | (0 > (long) node1->content) && |
| 1665 | (0 > (long) node2->content) && |
| 1666 | (node1->doc == node2->doc)) { |
| 1667 | long l1, l2; |
| 1668 | |
| 1669 | l1 = -((long) node1->content); |
| 1670 | l2 = -((long) node2->content); |
| 1671 | if (l1 < l2) |
| 1672 | return(1); |
| 1673 | if (l1 > l2) |
| 1674 | return(-1); |
| 1675 | } |
| 1676 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1677 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1678 | if (cur == node2) |
| 1679 | return(1); |
| 1680 | return(-1); /* assume there is no sibling list corruption */ |
| 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * xmlXPathNodeSetSort: |
| 1685 | * @set: the node set |
| 1686 | * |
| 1687 | * Sort the node set in document order |
| 1688 | */ |
| 1689 | void |
| 1690 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1691 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1692 | xmlNodePtr tmp; |
| 1693 | |
| 1694 | if (set == NULL) |
| 1695 | return; |
| 1696 | |
| 1697 | /* Use Shell's sort to sort the node-set */ |
| 1698 | len = set->nodeNr; |
| 1699 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1700 | for (i = incr; i < len; i++) { |
| 1701 | j = i - incr; |
| 1702 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1703 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1704 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1705 | tmp = set->nodeTab[j]; |
| 1706 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1707 | set->nodeTab[j + incr] = tmp; |
| 1708 | j -= incr; |
| 1709 | } else |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | #define XML_NODESET_DEFAULT 10 |
| 1717 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1718 | * xmlXPathNodeSetDupNs: |
| 1719 | * @node: the parent node of the namespace XPath node |
| 1720 | * @ns: the libxml namespace declaration node. |
| 1721 | * |
| 1722 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1723 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1724 | * parent node in the XPath semantic. |
| 1725 | * |
| 1726 | * Returns the newly created object. |
| 1727 | */ |
| 1728 | static xmlNodePtr |
| 1729 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1730 | xmlNsPtr cur; |
| 1731 | |
| 1732 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1733 | return(NULL); |
| 1734 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1735 | return((xmlNodePtr) ns); |
| 1736 | |
| 1737 | /* |
| 1738 | * Allocate a new Namespace and fill the fields. |
| 1739 | */ |
| 1740 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1741 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1742 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1743 | return(NULL); |
| 1744 | } |
| 1745 | memset(cur, 0, sizeof(xmlNs)); |
| 1746 | cur->type = XML_NAMESPACE_DECL; |
| 1747 | if (ns->href != NULL) |
| 1748 | cur->href = xmlStrdup(ns->href); |
| 1749 | if (ns->prefix != NULL) |
| 1750 | cur->prefix = xmlStrdup(ns->prefix); |
| 1751 | cur->next = (xmlNsPtr) node; |
| 1752 | return((xmlNodePtr) cur); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * xmlXPathNodeSetFreeNs: |
| 1757 | * @ns: the XPath namespace node found in a nodeset. |
| 1758 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1759 | * 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] | 1760 | * 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] | 1761 | * 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] | 1762 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1763 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1764 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1765 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1766 | return; |
| 1767 | |
| 1768 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1769 | if (ns->href != NULL) |
| 1770 | xmlFree((xmlChar *)ns->href); |
| 1771 | if (ns->prefix != NULL) |
| 1772 | xmlFree((xmlChar *)ns->prefix); |
| 1773 | xmlFree(ns); |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1778 | * xmlXPathNodeSetCreate: |
| 1779 | * @val: an initial xmlNodePtr, or NULL |
| 1780 | * |
| 1781 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1782 | * |
| 1783 | * Returns the newly created object. |
| 1784 | */ |
| 1785 | xmlNodeSetPtr |
| 1786 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1787 | xmlNodeSetPtr ret; |
| 1788 | |
| 1789 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1790 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1791 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1792 | return(NULL); |
| 1793 | } |
| 1794 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1795 | if (val != NULL) { |
| 1796 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1797 | sizeof(xmlNodePtr)); |
| 1798 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1799 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 1800 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1801 | return(NULL); |
| 1802 | } |
| 1803 | memset(ret->nodeTab, 0 , |
| 1804 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1805 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1806 | if (val->type == XML_NAMESPACE_DECL) { |
| 1807 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1808 | |
| 1809 | ret->nodeTab[ret->nodeNr++] = |
| 1810 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1811 | } else |
| 1812 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1813 | } |
| 1814 | return(ret); |
| 1815 | } |
| 1816 | |
| 1817 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1818 | * xmlXPathNodeSetContains: |
| 1819 | * @cur: the node-set |
| 1820 | * @val: the node |
| 1821 | * |
| 1822 | * checks whether @cur contains @val |
| 1823 | * |
| 1824 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1825 | */ |
| 1826 | int |
| 1827 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1828 | int i; |
| 1829 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1830 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1831 | if (val->type == XML_NAMESPACE_DECL) { |
| 1832 | for (i = 0; i < cur->nodeNr; i++) { |
| 1833 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1834 | xmlNsPtr ns1, ns2; |
| 1835 | |
| 1836 | ns1 = (xmlNsPtr) val; |
| 1837 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1838 | if (ns1 == ns2) |
| 1839 | return(1); |
| 1840 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1841 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1842 | return(1); |
| 1843 | } |
| 1844 | } |
| 1845 | } else { |
| 1846 | for (i = 0; i < cur->nodeNr; i++) { |
| 1847 | if (cur->nodeTab[i] == val) |
| 1848 | return(1); |
| 1849 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1850 | } |
| 1851 | return(0); |
| 1852 | } |
| 1853 | |
| 1854 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1855 | * xmlXPathNodeSetAddNs: |
| 1856 | * @cur: the initial node set |
| 1857 | * @node: the hosting node |
| 1858 | * @ns: a the namespace node |
| 1859 | * |
| 1860 | * add a new namespace node to an existing NodeSet |
| 1861 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1862 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1863 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1864 | int i; |
| 1865 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1866 | |
| 1867 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 1868 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1869 | (node->type != XML_ELEMENT_NODE)) |
| 1870 | return; |
| 1871 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1872 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1873 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1874 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1875 | */ |
| 1876 | for (i = 0;i < cur->nodeNr;i++) { |
| 1877 | if ((cur->nodeTab[i] != NULL) && |
| 1878 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1879 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1880 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * grow the nodeTab if needed |
| 1886 | */ |
| 1887 | if (cur->nodeMax == 0) { |
| 1888 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1889 | sizeof(xmlNodePtr)); |
| 1890 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1891 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1892 | return; |
| 1893 | } |
| 1894 | memset(cur->nodeTab, 0 , |
| 1895 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1896 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1897 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1898 | xmlNodePtr *temp; |
| 1899 | |
| 1900 | cur->nodeMax *= 2; |
| 1901 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1902 | sizeof(xmlNodePtr)); |
| 1903 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1904 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1905 | return; |
| 1906 | } |
| 1907 | cur->nodeTab = temp; |
| 1908 | } |
| 1909 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | * xmlXPathNodeSetAdd: |
| 1914 | * @cur: the initial node set |
| 1915 | * @val: a new xmlNodePtr |
| 1916 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1917 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1918 | */ |
| 1919 | void |
| 1920 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1921 | int i; |
| 1922 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1923 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1924 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1925 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1926 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1927 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1928 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1929 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1930 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1931 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1932 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1933 | */ |
| 1934 | for (i = 0;i < cur->nodeNr;i++) |
| 1935 | if (cur->nodeTab[i] == val) return; |
| 1936 | |
| 1937 | /* |
| 1938 | * grow the nodeTab if needed |
| 1939 | */ |
| 1940 | if (cur->nodeMax == 0) { |
| 1941 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1942 | sizeof(xmlNodePtr)); |
| 1943 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1944 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1945 | return; |
| 1946 | } |
| 1947 | memset(cur->nodeTab, 0 , |
| 1948 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1949 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1950 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1951 | xmlNodePtr *temp; |
| 1952 | |
| 1953 | cur->nodeMax *= 2; |
| 1954 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1955 | sizeof(xmlNodePtr)); |
| 1956 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1957 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1958 | return; |
| 1959 | } |
| 1960 | cur->nodeTab = temp; |
| 1961 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1962 | if (val->type == XML_NAMESPACE_DECL) { |
| 1963 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1964 | |
| 1965 | cur->nodeTab[cur->nodeNr++] = |
| 1966 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1967 | } else |
| 1968 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * xmlXPathNodeSetAddUnique: |
| 1973 | * @cur: the initial node set |
| 1974 | * @val: a new xmlNodePtr |
| 1975 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1976 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1977 | * when we are sure the node is not already in the set. |
| 1978 | */ |
| 1979 | void |
| 1980 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1981 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1983 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1984 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1985 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1986 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1987 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1988 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1989 | /* |
| 1990 | * grow the nodeTab if needed |
| 1991 | */ |
| 1992 | if (cur->nodeMax == 0) { |
| 1993 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1994 | sizeof(xmlNodePtr)); |
| 1995 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1996 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1997 | return; |
| 1998 | } |
| 1999 | memset(cur->nodeTab, 0 , |
| 2000 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2001 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 2002 | } else if (cur->nodeNr == cur->nodeMax) { |
| 2003 | xmlNodePtr *temp; |
| 2004 | |
| 2005 | cur->nodeMax *= 2; |
| 2006 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 2007 | sizeof(xmlNodePtr)); |
| 2008 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2009 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
| 2012 | cur->nodeTab = temp; |
| 2013 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2014 | if (val->type == XML_NAMESPACE_DECL) { |
| 2015 | xmlNsPtr ns = (xmlNsPtr) val; |
| 2016 | |
| 2017 | cur->nodeTab[cur->nodeNr++] = |
| 2018 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2019 | } else |
| 2020 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * xmlXPathNodeSetMerge: |
| 2025 | * @val1: the first NodeSet or NULL |
| 2026 | * @val2: the second NodeSet |
| 2027 | * |
| 2028 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2029 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2030 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2031 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2032 | */ |
| 2033 | xmlNodeSetPtr |
| 2034 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2035 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2036 | |
| 2037 | if (val2 == NULL) return(val1); |
| 2038 | if (val1 == NULL) { |
| 2039 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2040 | } |
| 2041 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2042 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2043 | initNr = val1->nodeNr; |
| 2044 | |
| 2045 | for (i = 0;i < val2->nodeNr;i++) { |
| 2046 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2047 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2048 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2049 | skip = 0; |
| 2050 | for (j = 0; j < initNr; j++) { |
| 2051 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 2052 | skip = 1; |
| 2053 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2054 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 2055 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 2056 | xmlNsPtr ns1, ns2; |
| 2057 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 2058 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 2059 | if ((ns1->next == ns2->next) && |
| 2060 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 2061 | skip = 1; |
| 2062 | break; |
| 2063 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | if (skip) |
| 2067 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2068 | |
| 2069 | /* |
| 2070 | * grow the nodeTab if needed |
| 2071 | */ |
| 2072 | if (val1->nodeMax == 0) { |
| 2073 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2074 | sizeof(xmlNodePtr)); |
| 2075 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2076 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2077 | return(NULL); |
| 2078 | } |
| 2079 | memset(val1->nodeTab, 0 , |
| 2080 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2081 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2082 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2083 | xmlNodePtr *temp; |
| 2084 | |
| 2085 | val1->nodeMax *= 2; |
| 2086 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2087 | sizeof(xmlNodePtr)); |
| 2088 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2089 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2090 | return(NULL); |
| 2091 | } |
| 2092 | val1->nodeTab = temp; |
| 2093 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2094 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2095 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2096 | |
| 2097 | val1->nodeTab[val1->nodeNr++] = |
| 2098 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2099 | } else |
| 2100 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | return(val1); |
| 2104 | } |
| 2105 | |
| 2106 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2107 | * xmlXPathNodeSetMergeUnique: |
| 2108 | * @val1: the first NodeSet or NULL |
| 2109 | * @val2: the second NodeSet |
| 2110 | * |
| 2111 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2112 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2113 | * |
| 2114 | * Returns @val1 once extended or NULL in case of error. |
| 2115 | */ |
| 2116 | static xmlNodeSetPtr |
| 2117 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2118 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2119 | |
| 2120 | if (val2 == NULL) return(val1); |
| 2121 | if (val1 == NULL) { |
| 2122 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2123 | } |
| 2124 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2125 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2126 | |
| 2127 | for (i = 0;i < val2->nodeNr;i++) { |
| 2128 | /* |
| 2129 | * grow the nodeTab if needed |
| 2130 | */ |
| 2131 | if (val1->nodeMax == 0) { |
| 2132 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2133 | sizeof(xmlNodePtr)); |
| 2134 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2135 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2136 | return(NULL); |
| 2137 | } |
| 2138 | memset(val1->nodeTab, 0 , |
| 2139 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2140 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2141 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2142 | xmlNodePtr *temp; |
| 2143 | |
| 2144 | val1->nodeMax *= 2; |
| 2145 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2146 | sizeof(xmlNodePtr)); |
| 2147 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2148 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2149 | return(NULL); |
| 2150 | } |
| 2151 | val1->nodeTab = temp; |
| 2152 | } |
| 2153 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2154 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2155 | |
| 2156 | val1->nodeTab[val1->nodeNr++] = |
| 2157 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2158 | } else |
| 2159 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2160 | } |
| 2161 | |
| 2162 | return(val1); |
| 2163 | } |
| 2164 | |
| 2165 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2166 | * xmlXPathNodeSetDel: |
| 2167 | * @cur: the initial node set |
| 2168 | * @val: an xmlNodePtr |
| 2169 | * |
| 2170 | * Removes an xmlNodePtr from an existing NodeSet |
| 2171 | */ |
| 2172 | void |
| 2173 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2174 | int i; |
| 2175 | |
| 2176 | if (cur == NULL) return; |
| 2177 | if (val == NULL) return; |
| 2178 | |
| 2179 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2180 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2181 | */ |
| 2182 | for (i = 0;i < cur->nodeNr;i++) |
| 2183 | if (cur->nodeTab[i] == val) break; |
| 2184 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2185 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2186 | #ifdef DEBUG |
| 2187 | xmlGenericError(xmlGenericErrorContext, |
| 2188 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2189 | val->name); |
| 2190 | #endif |
| 2191 | return; |
| 2192 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2193 | if ((cur->nodeTab[i] != NULL) && |
| 2194 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2195 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | cur->nodeNr--; |
| 2197 | for (;i < cur->nodeNr;i++) |
| 2198 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2199 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2200 | } |
| 2201 | |
| 2202 | /** |
| 2203 | * xmlXPathNodeSetRemove: |
| 2204 | * @cur: the initial node set |
| 2205 | * @val: the index to remove |
| 2206 | * |
| 2207 | * Removes an entry from an existing NodeSet list. |
| 2208 | */ |
| 2209 | void |
| 2210 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2211 | if (cur == NULL) return; |
| 2212 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2213 | if ((cur->nodeTab[val] != NULL) && |
| 2214 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2215 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2216 | cur->nodeNr--; |
| 2217 | for (;val < cur->nodeNr;val++) |
| 2218 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2219 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2220 | } |
| 2221 | |
| 2222 | /** |
| 2223 | * xmlXPathFreeNodeSet: |
| 2224 | * @obj: the xmlNodeSetPtr to free |
| 2225 | * |
| 2226 | * Free the NodeSet compound (not the actual nodes !). |
| 2227 | */ |
| 2228 | void |
| 2229 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2230 | if (obj == NULL) return; |
| 2231 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2232 | int i; |
| 2233 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2234 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2235 | for (i = 0;i < obj->nodeNr;i++) |
| 2236 | if ((obj->nodeTab[i] != NULL) && |
| 2237 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2238 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2239 | xmlFree(obj->nodeTab); |
| 2240 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2241 | xmlFree(obj); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
| 2245 | * xmlXPathFreeValueTree: |
| 2246 | * @obj: the xmlNodeSetPtr to free |
| 2247 | * |
| 2248 | * Free the NodeSet compound and the actual tree, this is different |
| 2249 | * from xmlXPathFreeNodeSet() |
| 2250 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2251 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2252 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2253 | int i; |
| 2254 | |
| 2255 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2256 | |
| 2257 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2258 | for (i = 0;i < obj->nodeNr;i++) { |
| 2259 | if (obj->nodeTab[i] != NULL) { |
| 2260 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2261 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2262 | } else { |
| 2263 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2264 | } |
| 2265 | } |
| 2266 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2267 | xmlFree(obj->nodeTab); |
| 2268 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2269 | xmlFree(obj); |
| 2270 | } |
| 2271 | |
| 2272 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2273 | /** |
| 2274 | * xmlGenericErrorContextNodeSet: |
| 2275 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2276 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2277 | * |
| 2278 | * Quick display of a NodeSet |
| 2279 | */ |
| 2280 | void |
| 2281 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2282 | int i; |
| 2283 | |
| 2284 | if (output == NULL) output = xmlGenericErrorContext; |
| 2285 | if (obj == NULL) { |
| 2286 | fprintf(output, "NodeSet == NULL !\n"); |
| 2287 | return; |
| 2288 | } |
| 2289 | if (obj->nodeNr == 0) { |
| 2290 | fprintf(output, "NodeSet is empty\n"); |
| 2291 | return; |
| 2292 | } |
| 2293 | if (obj->nodeTab == NULL) { |
| 2294 | fprintf(output, " nodeTab == NULL !\n"); |
| 2295 | return; |
| 2296 | } |
| 2297 | for (i = 0; i < obj->nodeNr; i++) { |
| 2298 | if (obj->nodeTab[i] == NULL) { |
| 2299 | fprintf(output, " NULL !\n"); |
| 2300 | return; |
| 2301 | } |
| 2302 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2303 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2304 | fprintf(output, " /"); |
| 2305 | else if (obj->nodeTab[i]->name == NULL) |
| 2306 | fprintf(output, " noname!"); |
| 2307 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2308 | } |
| 2309 | fprintf(output, "\n"); |
| 2310 | } |
| 2311 | #endif |
| 2312 | |
| 2313 | /** |
| 2314 | * xmlXPathNewNodeSet: |
| 2315 | * @val: the NodePtr value |
| 2316 | * |
| 2317 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2318 | * it with the single Node @val |
| 2319 | * |
| 2320 | * Returns the newly created object. |
| 2321 | */ |
| 2322 | xmlXPathObjectPtr |
| 2323 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2324 | xmlXPathObjectPtr ret; |
| 2325 | |
| 2326 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2327 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2328 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2329 | return(NULL); |
| 2330 | } |
| 2331 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2332 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2333 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2334 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2335 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2336 | return(ret); |
| 2337 | } |
| 2338 | |
| 2339 | /** |
| 2340 | * xmlXPathNewValueTree: |
| 2341 | * @val: the NodePtr value |
| 2342 | * |
| 2343 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2344 | * it with the tree root @val |
| 2345 | * |
| 2346 | * Returns the newly created object. |
| 2347 | */ |
| 2348 | xmlXPathObjectPtr |
| 2349 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2350 | xmlXPathObjectPtr ret; |
| 2351 | |
| 2352 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2353 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2354 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2355 | return(NULL); |
| 2356 | } |
| 2357 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2358 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2359 | ret->boolval = 1; |
| 2360 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2361 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2362 | return(ret); |
| 2363 | } |
| 2364 | |
| 2365 | /** |
| 2366 | * xmlXPathNewNodeSetList: |
| 2367 | * @val: an existing NodeSet |
| 2368 | * |
| 2369 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2370 | * it with the Nodeset @val |
| 2371 | * |
| 2372 | * Returns the newly created object. |
| 2373 | */ |
| 2374 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2375 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2376 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2377 | xmlXPathObjectPtr ret; |
| 2378 | int i; |
| 2379 | |
| 2380 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2381 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2382 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2383 | ret = xmlXPathNewNodeSet(NULL); |
| 2384 | else { |
| 2385 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2386 | for (i = 1; i < val->nodeNr; ++i) |
| 2387 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2388 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2389 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2390 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | /** |
| 2394 | * xmlXPathWrapNodeSet: |
| 2395 | * @val: the NodePtr value |
| 2396 | * |
| 2397 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2398 | * |
| 2399 | * Returns the newly created object. |
| 2400 | */ |
| 2401 | xmlXPathObjectPtr |
| 2402 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2403 | xmlXPathObjectPtr ret; |
| 2404 | |
| 2405 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2406 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2407 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2408 | return(NULL); |
| 2409 | } |
| 2410 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2411 | ret->type = XPATH_NODESET; |
| 2412 | ret->nodesetval = val; |
| 2413 | return(ret); |
| 2414 | } |
| 2415 | |
| 2416 | /** |
| 2417 | * xmlXPathFreeNodeSetList: |
| 2418 | * @obj: an existing NodeSetList object |
| 2419 | * |
| 2420 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2421 | * the list contrary to xmlXPathFreeObject(). |
| 2422 | */ |
| 2423 | void |
| 2424 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2425 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2426 | xmlFree(obj); |
| 2427 | } |
| 2428 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2429 | /** |
| 2430 | * xmlXPathDifference: |
| 2431 | * @nodes1: a node-set |
| 2432 | * @nodes2: a node-set |
| 2433 | * |
| 2434 | * Implements the EXSLT - Sets difference() function: |
| 2435 | * node-set set:difference (node-set, node-set) |
| 2436 | * |
| 2437 | * Returns the difference between the two node sets, or nodes1 if |
| 2438 | * nodes2 is empty |
| 2439 | */ |
| 2440 | xmlNodeSetPtr |
| 2441 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2442 | xmlNodeSetPtr ret; |
| 2443 | int i, l1; |
| 2444 | xmlNodePtr cur; |
| 2445 | |
| 2446 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2447 | return(nodes1); |
| 2448 | |
| 2449 | ret = xmlXPathNodeSetCreate(NULL); |
| 2450 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2451 | return(ret); |
| 2452 | |
| 2453 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2454 | |
| 2455 | for (i = 0; i < l1; i++) { |
| 2456 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2457 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2458 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2459 | } |
| 2460 | return(ret); |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * xmlXPathIntersection: |
| 2465 | * @nodes1: a node-set |
| 2466 | * @nodes2: a node-set |
| 2467 | * |
| 2468 | * Implements the EXSLT - Sets intersection() function: |
| 2469 | * node-set set:intersection (node-set, node-set) |
| 2470 | * |
| 2471 | * Returns a node set comprising the nodes that are within both the |
| 2472 | * node sets passed as arguments |
| 2473 | */ |
| 2474 | xmlNodeSetPtr |
| 2475 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2476 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2477 | int i, l1; |
| 2478 | xmlNodePtr cur; |
| 2479 | |
| 2480 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2481 | return(ret); |
| 2482 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2483 | return(ret); |
| 2484 | |
| 2485 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2486 | |
| 2487 | for (i = 0; i < l1; i++) { |
| 2488 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2489 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2490 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2491 | } |
| 2492 | return(ret); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
| 2496 | * xmlXPathDistinctSorted: |
| 2497 | * @nodes: a node-set, sorted by document order |
| 2498 | * |
| 2499 | * Implements the EXSLT - Sets distinct() function: |
| 2500 | * node-set set:distinct (node-set) |
| 2501 | * |
| 2502 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2503 | * it is empty |
| 2504 | */ |
| 2505 | xmlNodeSetPtr |
| 2506 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2507 | xmlNodeSetPtr ret; |
| 2508 | xmlHashTablePtr hash; |
| 2509 | int i, l; |
| 2510 | xmlChar * strval; |
| 2511 | xmlNodePtr cur; |
| 2512 | |
| 2513 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2514 | return(nodes); |
| 2515 | |
| 2516 | ret = xmlXPathNodeSetCreate(NULL); |
| 2517 | l = xmlXPathNodeSetGetLength(nodes); |
| 2518 | hash = xmlHashCreate (l); |
| 2519 | for (i = 0; i < l; i++) { |
| 2520 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2521 | strval = xmlXPathCastNodeToString(cur); |
| 2522 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2523 | xmlHashAddEntry(hash, strval, strval); |
| 2524 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2525 | } else { |
| 2526 | xmlFree(strval); |
| 2527 | } |
| 2528 | } |
| 2529 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2530 | return(ret); |
| 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * xmlXPathDistinct: |
| 2535 | * @nodes: a node-set |
| 2536 | * |
| 2537 | * Implements the EXSLT - Sets distinct() function: |
| 2538 | * node-set set:distinct (node-set) |
| 2539 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2540 | * is called with the sorted node-set |
| 2541 | * |
| 2542 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2543 | * it is empty |
| 2544 | */ |
| 2545 | xmlNodeSetPtr |
| 2546 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2547 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2548 | return(nodes); |
| 2549 | |
| 2550 | xmlXPathNodeSetSort(nodes); |
| 2551 | return(xmlXPathDistinctSorted(nodes)); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
| 2555 | * xmlXPathHasSameNodes: |
| 2556 | * @nodes1: a node-set |
| 2557 | * @nodes2: a node-set |
| 2558 | * |
| 2559 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2560 | * boolean set:has-same-node(node-set, node-set) |
| 2561 | * |
| 2562 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2563 | * otherwise |
| 2564 | */ |
| 2565 | int |
| 2566 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2567 | int i, l; |
| 2568 | xmlNodePtr cur; |
| 2569 | |
| 2570 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2571 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2572 | return(0); |
| 2573 | |
| 2574 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2575 | for (i = 0; i < l; i++) { |
| 2576 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2577 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2578 | return(1); |
| 2579 | } |
| 2580 | return(0); |
| 2581 | } |
| 2582 | |
| 2583 | /** |
| 2584 | * xmlXPathNodeLeadingSorted: |
| 2585 | * @nodes: a node-set, sorted by document order |
| 2586 | * @node: a node |
| 2587 | * |
| 2588 | * Implements the EXSLT - Sets leading() function: |
| 2589 | * node-set set:leading (node-set, node-set) |
| 2590 | * |
| 2591 | * Returns the nodes in @nodes that precede @node in document order, |
| 2592 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2593 | * doesn't contain @node |
| 2594 | */ |
| 2595 | xmlNodeSetPtr |
| 2596 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2597 | int i, l; |
| 2598 | xmlNodePtr cur; |
| 2599 | xmlNodeSetPtr ret; |
| 2600 | |
| 2601 | if (node == NULL) |
| 2602 | return(nodes); |
| 2603 | |
| 2604 | ret = xmlXPathNodeSetCreate(NULL); |
| 2605 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2606 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2607 | return(ret); |
| 2608 | |
| 2609 | l = xmlXPathNodeSetGetLength(nodes); |
| 2610 | for (i = 0; i < l; i++) { |
| 2611 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2612 | if (cur == node) |
| 2613 | break; |
| 2614 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2615 | } |
| 2616 | return(ret); |
| 2617 | } |
| 2618 | |
| 2619 | /** |
| 2620 | * xmlXPathNodeLeading: |
| 2621 | * @nodes: a node-set |
| 2622 | * @node: a node |
| 2623 | * |
| 2624 | * Implements the EXSLT - Sets leading() function: |
| 2625 | * node-set set:leading (node-set, node-set) |
| 2626 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2627 | * is called. |
| 2628 | * |
| 2629 | * Returns the nodes in @nodes that precede @node in document order, |
| 2630 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2631 | * doesn't contain @node |
| 2632 | */ |
| 2633 | xmlNodeSetPtr |
| 2634 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2635 | xmlXPathNodeSetSort(nodes); |
| 2636 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * xmlXPathLeadingSorted: |
| 2641 | * @nodes1: a node-set, sorted by document order |
| 2642 | * @nodes2: a node-set, sorted by document order |
| 2643 | * |
| 2644 | * Implements the EXSLT - Sets leading() function: |
| 2645 | * node-set set:leading (node-set, node-set) |
| 2646 | * |
| 2647 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2648 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2649 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2650 | */ |
| 2651 | xmlNodeSetPtr |
| 2652 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2653 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2654 | return(nodes1); |
| 2655 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2656 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlXPathLeading: |
| 2661 | * @nodes1: a node-set |
| 2662 | * @nodes2: a node-set |
| 2663 | * |
| 2664 | * Implements the EXSLT - Sets leading() function: |
| 2665 | * node-set set:leading (node-set, node-set) |
| 2666 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2667 | * #exslSetsLeadingSorted is called. |
| 2668 | * |
| 2669 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2670 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2671 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2672 | */ |
| 2673 | xmlNodeSetPtr |
| 2674 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2675 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2676 | return(nodes1); |
| 2677 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2678 | return(xmlXPathNodeSetCreate(NULL)); |
| 2679 | xmlXPathNodeSetSort(nodes1); |
| 2680 | xmlXPathNodeSetSort(nodes2); |
| 2681 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2682 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2683 | } |
| 2684 | |
| 2685 | /** |
| 2686 | * xmlXPathNodeTrailingSorted: |
| 2687 | * @nodes: a node-set, sorted by document order |
| 2688 | * @node: a node |
| 2689 | * |
| 2690 | * Implements the EXSLT - Sets trailing() function: |
| 2691 | * node-set set:trailing (node-set, node-set) |
| 2692 | * |
| 2693 | * Returns the nodes in @nodes that follow @node in document order, |
| 2694 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2695 | * doesn't contain @node |
| 2696 | */ |
| 2697 | xmlNodeSetPtr |
| 2698 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2699 | int i, l; |
| 2700 | xmlNodePtr cur; |
| 2701 | xmlNodeSetPtr ret; |
| 2702 | |
| 2703 | if (node == NULL) |
| 2704 | return(nodes); |
| 2705 | |
| 2706 | ret = xmlXPathNodeSetCreate(NULL); |
| 2707 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2708 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2709 | return(ret); |
| 2710 | |
| 2711 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2712 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2713 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2714 | if (cur == node) |
| 2715 | break; |
| 2716 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2717 | } |
| 2718 | return(ret); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * xmlXPathNodeTrailing: |
| 2723 | * @nodes: a node-set |
| 2724 | * @node: a node |
| 2725 | * |
| 2726 | * Implements the EXSLT - Sets trailing() function: |
| 2727 | * node-set set:trailing (node-set, node-set) |
| 2728 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2729 | * is called. |
| 2730 | * |
| 2731 | * Returns the nodes in @nodes that follow @node in document order, |
| 2732 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2733 | * doesn't contain @node |
| 2734 | */ |
| 2735 | xmlNodeSetPtr |
| 2736 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2737 | xmlXPathNodeSetSort(nodes); |
| 2738 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2739 | } |
| 2740 | |
| 2741 | /** |
| 2742 | * xmlXPathTrailingSorted: |
| 2743 | * @nodes1: a node-set, sorted by document order |
| 2744 | * @nodes2: a node-set, sorted by document order |
| 2745 | * |
| 2746 | * Implements the EXSLT - Sets trailing() function: |
| 2747 | * node-set set:trailing (node-set, node-set) |
| 2748 | * |
| 2749 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2750 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2751 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2752 | */ |
| 2753 | xmlNodeSetPtr |
| 2754 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2755 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2756 | return(nodes1); |
| 2757 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2758 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2759 | } |
| 2760 | |
| 2761 | /** |
| 2762 | * xmlXPathTrailing: |
| 2763 | * @nodes1: a node-set |
| 2764 | * @nodes2: a node-set |
| 2765 | * |
| 2766 | * Implements the EXSLT - Sets trailing() function: |
| 2767 | * node-set set:trailing (node-set, node-set) |
| 2768 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2769 | * #xmlXPathTrailingSorted is called. |
| 2770 | * |
| 2771 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2772 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2773 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2774 | */ |
| 2775 | xmlNodeSetPtr |
| 2776 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2777 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2778 | return(nodes1); |
| 2779 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2780 | return(xmlXPathNodeSetCreate(NULL)); |
| 2781 | xmlXPathNodeSetSort(nodes1); |
| 2782 | xmlXPathNodeSetSort(nodes2); |
| 2783 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2784 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2785 | } |
| 2786 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2787 | /************************************************************************ |
| 2788 | * * |
| 2789 | * Routines to handle extra functions * |
| 2790 | * * |
| 2791 | ************************************************************************/ |
| 2792 | |
| 2793 | /** |
| 2794 | * xmlXPathRegisterFunc: |
| 2795 | * @ctxt: the XPath context |
| 2796 | * @name: the function name |
| 2797 | * @f: the function implementation or NULL |
| 2798 | * |
| 2799 | * Register a new function. If @f is NULL it unregisters the function |
| 2800 | * |
| 2801 | * Returns 0 in case of success, -1 in case of error |
| 2802 | */ |
| 2803 | int |
| 2804 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2805 | xmlXPathFunction f) { |
| 2806 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2807 | } |
| 2808 | |
| 2809 | /** |
| 2810 | * xmlXPathRegisterFuncNS: |
| 2811 | * @ctxt: the XPath context |
| 2812 | * @name: the function name |
| 2813 | * @ns_uri: the function namespace URI |
| 2814 | * @f: the function implementation or NULL |
| 2815 | * |
| 2816 | * Register a new function. If @f is NULL it unregisters the function |
| 2817 | * |
| 2818 | * Returns 0 in case of success, -1 in case of error |
| 2819 | */ |
| 2820 | int |
| 2821 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2822 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2823 | if (ctxt == NULL) |
| 2824 | return(-1); |
| 2825 | if (name == NULL) |
| 2826 | return(-1); |
| 2827 | |
| 2828 | if (ctxt->funcHash == NULL) |
| 2829 | ctxt->funcHash = xmlHashCreate(0); |
| 2830 | if (ctxt->funcHash == NULL) |
| 2831 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2832 | if (f == NULL) |
| 2833 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2834 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2838 | * xmlXPathRegisterFuncLookup: |
| 2839 | * @ctxt: the XPath context |
| 2840 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2841 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2842 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2843 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2844 | */ |
| 2845 | void |
| 2846 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2847 | xmlXPathFuncLookupFunc f, |
| 2848 | void *funcCtxt) { |
| 2849 | if (ctxt == NULL) |
| 2850 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2851 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2852 | ctxt->funcLookupData = funcCtxt; |
| 2853 | } |
| 2854 | |
| 2855 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2856 | * xmlXPathFunctionLookup: |
| 2857 | * @ctxt: the XPath context |
| 2858 | * @name: the function name |
| 2859 | * |
| 2860 | * Search in the Function array of the context for the given |
| 2861 | * function. |
| 2862 | * |
| 2863 | * Returns the xmlXPathFunction or NULL if not found |
| 2864 | */ |
| 2865 | xmlXPathFunction |
| 2866 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2867 | if (ctxt == NULL) |
| 2868 | return (NULL); |
| 2869 | |
| 2870 | if (ctxt->funcLookupFunc != NULL) { |
| 2871 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2872 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2873 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2874 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2875 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2876 | if (ret != NULL) |
| 2877 | return(ret); |
| 2878 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2879 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * xmlXPathFunctionLookupNS: |
| 2884 | * @ctxt: the XPath context |
| 2885 | * @name: the function name |
| 2886 | * @ns_uri: the function namespace URI |
| 2887 | * |
| 2888 | * Search in the Function array of the context for the given |
| 2889 | * function. |
| 2890 | * |
| 2891 | * Returns the xmlXPathFunction or NULL if not found |
| 2892 | */ |
| 2893 | xmlXPathFunction |
| 2894 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2895 | const xmlChar *ns_uri) { |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2896 | xmlXPathFunction ret; |
| 2897 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | if (ctxt == NULL) |
| 2899 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2900 | if (name == NULL) |
| 2901 | return(NULL); |
| 2902 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2903 | if (ctxt->funcLookupFunc != NULL) { |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2904 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2905 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2906 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2907 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2908 | if (ret != NULL) |
| 2909 | return(ret); |
| 2910 | } |
| 2911 | |
| 2912 | if (ctxt->funcHash == NULL) |
| 2913 | return(NULL); |
| 2914 | |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 2915 | XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri); |
| 2916 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | /** |
| 2920 | * xmlXPathRegisteredFuncsCleanup: |
| 2921 | * @ctxt: the XPath context |
| 2922 | * |
| 2923 | * Cleanup the XPath context data associated to registered functions |
| 2924 | */ |
| 2925 | void |
| 2926 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2927 | if (ctxt == NULL) |
| 2928 | return; |
| 2929 | |
| 2930 | xmlHashFree(ctxt->funcHash, NULL); |
| 2931 | ctxt->funcHash = NULL; |
| 2932 | } |
| 2933 | |
| 2934 | /************************************************************************ |
| 2935 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2936 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2937 | * * |
| 2938 | ************************************************************************/ |
| 2939 | |
| 2940 | /** |
| 2941 | * xmlXPathRegisterVariable: |
| 2942 | * @ctxt: the XPath context |
| 2943 | * @name: the variable name |
| 2944 | * @value: the variable value or NULL |
| 2945 | * |
| 2946 | * Register a new variable value. If @value is NULL it unregisters |
| 2947 | * the variable |
| 2948 | * |
| 2949 | * Returns 0 in case of success, -1 in case of error |
| 2950 | */ |
| 2951 | int |
| 2952 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2953 | xmlXPathObjectPtr value) { |
| 2954 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2955 | } |
| 2956 | |
| 2957 | /** |
| 2958 | * xmlXPathRegisterVariableNS: |
| 2959 | * @ctxt: the XPath context |
| 2960 | * @name: the variable name |
| 2961 | * @ns_uri: the variable namespace URI |
| 2962 | * @value: the variable value or NULL |
| 2963 | * |
| 2964 | * Register a new variable value. If @value is NULL it unregisters |
| 2965 | * the variable |
| 2966 | * |
| 2967 | * Returns 0 in case of success, -1 in case of error |
| 2968 | */ |
| 2969 | int |
| 2970 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2971 | const xmlChar *ns_uri, |
| 2972 | xmlXPathObjectPtr value) { |
| 2973 | if (ctxt == NULL) |
| 2974 | return(-1); |
| 2975 | if (name == NULL) |
| 2976 | return(-1); |
| 2977 | |
| 2978 | if (ctxt->varHash == NULL) |
| 2979 | ctxt->varHash = xmlHashCreate(0); |
| 2980 | if (ctxt->varHash == NULL) |
| 2981 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2982 | if (value == NULL) |
| 2983 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 2984 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2985 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2986 | (void *) value, |
| 2987 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2988 | } |
| 2989 | |
| 2990 | /** |
| 2991 | * xmlXPathRegisterVariableLookup: |
| 2992 | * @ctxt: the XPath context |
| 2993 | * @f: the lookup function |
| 2994 | * @data: the lookup data |
| 2995 | * |
| 2996 | * register an external mechanism to do variable lookup |
| 2997 | */ |
| 2998 | void |
| 2999 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 3000 | xmlXPathVariableLookupFunc f, void *data) { |
| 3001 | if (ctxt == NULL) |
| 3002 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3003 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | ctxt->varLookupData = data; |
| 3005 | } |
| 3006 | |
| 3007 | /** |
| 3008 | * xmlXPathVariableLookup: |
| 3009 | * @ctxt: the XPath context |
| 3010 | * @name: the variable name |
| 3011 | * |
| 3012 | * Search in the Variable array of the context for the given |
| 3013 | * variable value. |
| 3014 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3015 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3016 | */ |
| 3017 | xmlXPathObjectPtr |
| 3018 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 3019 | if (ctxt == NULL) |
| 3020 | return(NULL); |
| 3021 | |
| 3022 | if (ctxt->varLookupFunc != NULL) { |
| 3023 | xmlXPathObjectPtr ret; |
| 3024 | |
| 3025 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3026 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 3027 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3028 | } |
| 3029 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 3030 | } |
| 3031 | |
| 3032 | /** |
| 3033 | * xmlXPathVariableLookupNS: |
| 3034 | * @ctxt: the XPath context |
| 3035 | * @name: the variable name |
| 3036 | * @ns_uri: the variable namespace URI |
| 3037 | * |
| 3038 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3039 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3040 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3041 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3042 | */ |
| 3043 | xmlXPathObjectPtr |
| 3044 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3045 | const xmlChar *ns_uri) { |
| 3046 | if (ctxt == NULL) |
| 3047 | return(NULL); |
| 3048 | |
| 3049 | if (ctxt->varLookupFunc != NULL) { |
| 3050 | xmlXPathObjectPtr ret; |
| 3051 | |
| 3052 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3053 | (ctxt->varLookupData, name, ns_uri); |
| 3054 | if (ret != NULL) return(ret); |
| 3055 | } |
| 3056 | |
| 3057 | if (ctxt->varHash == NULL) |
| 3058 | return(NULL); |
| 3059 | if (name == NULL) |
| 3060 | return(NULL); |
| 3061 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 3062 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 3063 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | /** |
| 3067 | * xmlXPathRegisteredVariablesCleanup: |
| 3068 | * @ctxt: the XPath context |
| 3069 | * |
| 3070 | * Cleanup the XPath context data associated to registered variables |
| 3071 | */ |
| 3072 | void |
| 3073 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 3074 | if (ctxt == NULL) |
| 3075 | return; |
| 3076 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3077 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3078 | ctxt->varHash = NULL; |
| 3079 | } |
| 3080 | |
| 3081 | /** |
| 3082 | * xmlXPathRegisterNs: |
| 3083 | * @ctxt: the XPath context |
| 3084 | * @prefix: the namespace prefix |
| 3085 | * @ns_uri: the namespace name |
| 3086 | * |
| 3087 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 3088 | * the namespace |
| 3089 | * |
| 3090 | * Returns 0 in case of success, -1 in case of error |
| 3091 | */ |
| 3092 | int |
| 3093 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3094 | const xmlChar *ns_uri) { |
| 3095 | if (ctxt == NULL) |
| 3096 | return(-1); |
| 3097 | if (prefix == NULL) |
| 3098 | return(-1); |
| 3099 | |
| 3100 | if (ctxt->nsHash == NULL) |
| 3101 | ctxt->nsHash = xmlHashCreate(10); |
| 3102 | if (ctxt->nsHash == NULL) |
| 3103 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3104 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3105 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3106 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3107 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3108 | (xmlHashDeallocator)xmlFree)); |
| 3109 | } |
| 3110 | |
| 3111 | /** |
| 3112 | * xmlXPathNsLookup: |
| 3113 | * @ctxt: the XPath context |
| 3114 | * @prefix: the namespace prefix value |
| 3115 | * |
| 3116 | * Search in the namespace declaration array of the context for the given |
| 3117 | * namespace name associated to the given prefix |
| 3118 | * |
| 3119 | * Returns the value or NULL if not found |
| 3120 | */ |
| 3121 | const xmlChar * |
| 3122 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3123 | if (ctxt == NULL) |
| 3124 | return(NULL); |
| 3125 | if (prefix == NULL) |
| 3126 | return(NULL); |
| 3127 | |
| 3128 | #ifdef XML_XML_NAMESPACE |
| 3129 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3130 | return(XML_XML_NAMESPACE); |
| 3131 | #endif |
| 3132 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3133 | if (ctxt->namespaces != NULL) { |
| 3134 | int i; |
| 3135 | |
| 3136 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3137 | if ((ctxt->namespaces[i] != NULL) && |
| 3138 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3139 | return(ctxt->namespaces[i]->href); |
| 3140 | } |
| 3141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3142 | |
| 3143 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3144 | } |
| 3145 | |
| 3146 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3147 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3148 | * @ctxt: the XPath context |
| 3149 | * |
| 3150 | * Cleanup the XPath context data associated to registered variables |
| 3151 | */ |
| 3152 | void |
| 3153 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3154 | if (ctxt == NULL) |
| 3155 | return; |
| 3156 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3157 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3158 | ctxt->nsHash = NULL; |
| 3159 | } |
| 3160 | |
| 3161 | /************************************************************************ |
| 3162 | * * |
| 3163 | * Routines to handle Values * |
| 3164 | * * |
| 3165 | ************************************************************************/ |
| 3166 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3167 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3168 | |
| 3169 | /** |
| 3170 | * xmlXPathNewFloat: |
| 3171 | * @val: the double value |
| 3172 | * |
| 3173 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3174 | * |
| 3175 | * Returns the newly created object. |
| 3176 | */ |
| 3177 | xmlXPathObjectPtr |
| 3178 | xmlXPathNewFloat(double val) { |
| 3179 | xmlXPathObjectPtr ret; |
| 3180 | |
| 3181 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3182 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3183 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3184 | return(NULL); |
| 3185 | } |
| 3186 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3187 | ret->type = XPATH_NUMBER; |
| 3188 | ret->floatval = val; |
| 3189 | return(ret); |
| 3190 | } |
| 3191 | |
| 3192 | /** |
| 3193 | * xmlXPathNewBoolean: |
| 3194 | * @val: the boolean value |
| 3195 | * |
| 3196 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3197 | * |
| 3198 | * Returns the newly created object. |
| 3199 | */ |
| 3200 | xmlXPathObjectPtr |
| 3201 | xmlXPathNewBoolean(int val) { |
| 3202 | xmlXPathObjectPtr ret; |
| 3203 | |
| 3204 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3205 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3206 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3207 | return(NULL); |
| 3208 | } |
| 3209 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3210 | ret->type = XPATH_BOOLEAN; |
| 3211 | ret->boolval = (val != 0); |
| 3212 | return(ret); |
| 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * xmlXPathNewString: |
| 3217 | * @val: the xmlChar * value |
| 3218 | * |
| 3219 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3220 | * |
| 3221 | * Returns the newly created object. |
| 3222 | */ |
| 3223 | xmlXPathObjectPtr |
| 3224 | xmlXPathNewString(const xmlChar *val) { |
| 3225 | xmlXPathObjectPtr ret; |
| 3226 | |
| 3227 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3228 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3229 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3230 | return(NULL); |
| 3231 | } |
| 3232 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3233 | ret->type = XPATH_STRING; |
| 3234 | if (val != NULL) |
| 3235 | ret->stringval = xmlStrdup(val); |
| 3236 | else |
| 3237 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3238 | return(ret); |
| 3239 | } |
| 3240 | |
| 3241 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3242 | * xmlXPathWrapString: |
| 3243 | * @val: the xmlChar * value |
| 3244 | * |
| 3245 | * Wraps the @val string into an XPath object. |
| 3246 | * |
| 3247 | * Returns the newly created object. |
| 3248 | */ |
| 3249 | xmlXPathObjectPtr |
| 3250 | xmlXPathWrapString (xmlChar *val) { |
| 3251 | xmlXPathObjectPtr ret; |
| 3252 | |
| 3253 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3254 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3255 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3256 | return(NULL); |
| 3257 | } |
| 3258 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3259 | ret->type = XPATH_STRING; |
| 3260 | ret->stringval = val; |
| 3261 | return(ret); |
| 3262 | } |
| 3263 | |
| 3264 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3265 | * xmlXPathNewCString: |
| 3266 | * @val: the char * value |
| 3267 | * |
| 3268 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3269 | * |
| 3270 | * Returns the newly created object. |
| 3271 | */ |
| 3272 | xmlXPathObjectPtr |
| 3273 | xmlXPathNewCString(const char *val) { |
| 3274 | xmlXPathObjectPtr ret; |
| 3275 | |
| 3276 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3277 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3278 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3279 | return(NULL); |
| 3280 | } |
| 3281 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3282 | ret->type = XPATH_STRING; |
| 3283 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3284 | return(ret); |
| 3285 | } |
| 3286 | |
| 3287 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3288 | * xmlXPathWrapCString: |
| 3289 | * @val: the char * value |
| 3290 | * |
| 3291 | * Wraps a string into an XPath object. |
| 3292 | * |
| 3293 | * Returns the newly created object. |
| 3294 | */ |
| 3295 | xmlXPathObjectPtr |
| 3296 | xmlXPathWrapCString (char * val) { |
| 3297 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3298 | } |
| 3299 | |
| 3300 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3301 | * xmlXPathWrapExternal: |
| 3302 | * @val: the user data |
| 3303 | * |
| 3304 | * Wraps the @val data into an XPath object. |
| 3305 | * |
| 3306 | * Returns the newly created object. |
| 3307 | */ |
| 3308 | xmlXPathObjectPtr |
| 3309 | xmlXPathWrapExternal (void *val) { |
| 3310 | xmlXPathObjectPtr ret; |
| 3311 | |
| 3312 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3313 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3314 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3315 | return(NULL); |
| 3316 | } |
| 3317 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3318 | ret->type = XPATH_USERS; |
| 3319 | ret->user = val; |
| 3320 | return(ret); |
| 3321 | } |
| 3322 | |
| 3323 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3324 | * xmlXPathObjectCopy: |
| 3325 | * @val: the original object |
| 3326 | * |
| 3327 | * allocate a new copy of a given object |
| 3328 | * |
| 3329 | * Returns the newly created object. |
| 3330 | */ |
| 3331 | xmlXPathObjectPtr |
| 3332 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3333 | xmlXPathObjectPtr ret; |
| 3334 | |
| 3335 | if (val == NULL) |
| 3336 | return(NULL); |
| 3337 | |
| 3338 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3339 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3340 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3341 | return(NULL); |
| 3342 | } |
| 3343 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3344 | switch (val->type) { |
| 3345 | case XPATH_BOOLEAN: |
| 3346 | case XPATH_NUMBER: |
| 3347 | case XPATH_POINT: |
| 3348 | case XPATH_RANGE: |
| 3349 | break; |
| 3350 | case XPATH_STRING: |
| 3351 | ret->stringval = xmlStrdup(val->stringval); |
| 3352 | break; |
| 3353 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3354 | #if 0 |
| 3355 | /* |
| 3356 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 3357 | this previous handling is no longer correct, and can cause some serious |
| 3358 | problems (ref. bug 145547) |
| 3359 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3360 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3361 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3362 | xmlNodePtr cur, tmp; |
| 3363 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3364 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3365 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3366 | top = xmlNewDoc(NULL); |
| 3367 | top->name = (char *) |
| 3368 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3369 | ret->user = top; |
| 3370 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3371 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3372 | cur = val->nodesetval->nodeTab[0]->children; |
| 3373 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3374 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3375 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3376 | cur = cur->next; |
| 3377 | } |
| 3378 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3379 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3380 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3381 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3383 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3384 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3385 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3386 | case XPATH_NODESET: |
| 3387 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3388 | /* Do not deallocate the copied tree value */ |
| 3389 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | case XPATH_LOCATIONSET: |
| 3392 | #ifdef LIBXML_XPTR_ENABLED |
| 3393 | { |
| 3394 | xmlLocationSetPtr loc = val->user; |
| 3395 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3396 | break; |
| 3397 | } |
| 3398 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3399 | case XPATH_USERS: |
| 3400 | ret->user = val->user; |
| 3401 | break; |
| 3402 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3403 | xmlGenericError(xmlGenericErrorContext, |
| 3404 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3405 | val->type); |
| 3406 | break; |
| 3407 | } |
| 3408 | return(ret); |
| 3409 | } |
| 3410 | |
| 3411 | /** |
| 3412 | * xmlXPathFreeObject: |
| 3413 | * @obj: the object to free |
| 3414 | * |
| 3415 | * Free up an xmlXPathObjectPtr object. |
| 3416 | */ |
| 3417 | void |
| 3418 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3419 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3420 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3421 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3422 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3423 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3424 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3425 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3426 | } else |
| 3427 | #endif |
| 3428 | if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3429 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3430 | } else { |
| 3431 | if (obj->nodesetval != NULL) |
| 3432 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3433 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3434 | #ifdef LIBXML_XPTR_ENABLED |
| 3435 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3436 | if (obj->user != NULL) |
| 3437 | xmlXPtrFreeLocationSet(obj->user); |
| 3438 | #endif |
| 3439 | } else if (obj->type == XPATH_STRING) { |
| 3440 | if (obj->stringval != NULL) |
| 3441 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3444 | xmlFree(obj); |
| 3445 | } |
| 3446 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3447 | |
| 3448 | /************************************************************************ |
| 3449 | * * |
| 3450 | * Type Casting Routines * |
| 3451 | * * |
| 3452 | ************************************************************************/ |
| 3453 | |
| 3454 | /** |
| 3455 | * xmlXPathCastBooleanToString: |
| 3456 | * @val: a boolean |
| 3457 | * |
| 3458 | * Converts a boolean to its string value. |
| 3459 | * |
| 3460 | * Returns a newly allocated string. |
| 3461 | */ |
| 3462 | xmlChar * |
| 3463 | xmlXPathCastBooleanToString (int val) { |
| 3464 | xmlChar *ret; |
| 3465 | if (val) |
| 3466 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3467 | else |
| 3468 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3469 | return(ret); |
| 3470 | } |
| 3471 | |
| 3472 | /** |
| 3473 | * xmlXPathCastNumberToString: |
| 3474 | * @val: a number |
| 3475 | * |
| 3476 | * Converts a number to its string value. |
| 3477 | * |
| 3478 | * Returns a newly allocated string. |
| 3479 | */ |
| 3480 | xmlChar * |
| 3481 | xmlXPathCastNumberToString (double val) { |
| 3482 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3483 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3484 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3485 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3486 | break; |
| 3487 | case -1: |
| 3488 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3489 | break; |
| 3490 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3491 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3492 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3493 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3494 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3495 | } else { |
| 3496 | /* could be improved */ |
| 3497 | char buf[100]; |
| 3498 | xmlXPathFormatNumber(val, buf, 100); |
| 3499 | ret = xmlStrdup((const xmlChar *) buf); |
| 3500 | } |
| 3501 | } |
| 3502 | return(ret); |
| 3503 | } |
| 3504 | |
| 3505 | /** |
| 3506 | * xmlXPathCastNodeToString: |
| 3507 | * @node: a node |
| 3508 | * |
| 3509 | * Converts a node to its string value. |
| 3510 | * |
| 3511 | * Returns a newly allocated string. |
| 3512 | */ |
| 3513 | xmlChar * |
| 3514 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3515 | return(xmlNodeGetContent(node)); |
| 3516 | } |
| 3517 | |
| 3518 | /** |
| 3519 | * xmlXPathCastNodeSetToString: |
| 3520 | * @ns: a node-set |
| 3521 | * |
| 3522 | * Converts a node-set to its string value. |
| 3523 | * |
| 3524 | * Returns a newly allocated string. |
| 3525 | */ |
| 3526 | xmlChar * |
| 3527 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3528 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3529 | return(xmlStrdup((const xmlChar *) "")); |
| 3530 | |
| 3531 | xmlXPathNodeSetSort(ns); |
| 3532 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3533 | } |
| 3534 | |
| 3535 | /** |
| 3536 | * xmlXPathCastToString: |
| 3537 | * @val: an XPath object |
| 3538 | * |
| 3539 | * Converts an existing object to its string() equivalent |
| 3540 | * |
| 3541 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3542 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3543 | * string object). |
| 3544 | */ |
| 3545 | xmlChar * |
| 3546 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3547 | xmlChar *ret = NULL; |
| 3548 | |
| 3549 | if (val == NULL) |
| 3550 | return(xmlStrdup((const xmlChar *) "")); |
| 3551 | switch (val->type) { |
| 3552 | case XPATH_UNDEFINED: |
| 3553 | #ifdef DEBUG_EXPR |
| 3554 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3555 | #endif |
| 3556 | ret = xmlStrdup((const xmlChar *) ""); |
| 3557 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3558 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3559 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3560 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3561 | break; |
| 3562 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3563 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3564 | case XPATH_BOOLEAN: |
| 3565 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3566 | break; |
| 3567 | case XPATH_NUMBER: { |
| 3568 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3569 | break; |
| 3570 | } |
| 3571 | case XPATH_USERS: |
| 3572 | case XPATH_POINT: |
| 3573 | case XPATH_RANGE: |
| 3574 | case XPATH_LOCATIONSET: |
| 3575 | TODO |
| 3576 | ret = xmlStrdup((const xmlChar *) ""); |
| 3577 | break; |
| 3578 | } |
| 3579 | return(ret); |
| 3580 | } |
| 3581 | |
| 3582 | /** |
| 3583 | * xmlXPathConvertString: |
| 3584 | * @val: an XPath object |
| 3585 | * |
| 3586 | * Converts an existing object to its string() equivalent |
| 3587 | * |
| 3588 | * Returns the new object, the old one is freed (or the operation |
| 3589 | * is done directly on @val) |
| 3590 | */ |
| 3591 | xmlXPathObjectPtr |
| 3592 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3593 | xmlChar *res = NULL; |
| 3594 | |
| 3595 | if (val == NULL) |
| 3596 | return(xmlXPathNewCString("")); |
| 3597 | |
| 3598 | switch (val->type) { |
| 3599 | case XPATH_UNDEFINED: |
| 3600 | #ifdef DEBUG_EXPR |
| 3601 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3602 | #endif |
| 3603 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3604 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3605 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3606 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3607 | break; |
| 3608 | case XPATH_STRING: |
| 3609 | return(val); |
| 3610 | case XPATH_BOOLEAN: |
| 3611 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3612 | break; |
| 3613 | case XPATH_NUMBER: |
| 3614 | res = xmlXPathCastNumberToString(val->floatval); |
| 3615 | break; |
| 3616 | case XPATH_USERS: |
| 3617 | case XPATH_POINT: |
| 3618 | case XPATH_RANGE: |
| 3619 | case XPATH_LOCATIONSET: |
| 3620 | TODO; |
| 3621 | break; |
| 3622 | } |
| 3623 | xmlXPathFreeObject(val); |
| 3624 | if (res == NULL) |
| 3625 | return(xmlXPathNewCString("")); |
| 3626 | return(xmlXPathWrapString(res)); |
| 3627 | } |
| 3628 | |
| 3629 | /** |
| 3630 | * xmlXPathCastBooleanToNumber: |
| 3631 | * @val: a boolean |
| 3632 | * |
| 3633 | * Converts a boolean to its number value |
| 3634 | * |
| 3635 | * Returns the number value |
| 3636 | */ |
| 3637 | double |
| 3638 | xmlXPathCastBooleanToNumber(int val) { |
| 3639 | if (val) |
| 3640 | return(1.0); |
| 3641 | return(0.0); |
| 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * xmlXPathCastStringToNumber: |
| 3646 | * @val: a string |
| 3647 | * |
| 3648 | * Converts a string to its number value |
| 3649 | * |
| 3650 | * Returns the number value |
| 3651 | */ |
| 3652 | double |
| 3653 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3654 | return(xmlXPathStringEvalNumber(val)); |
| 3655 | } |
| 3656 | |
| 3657 | /** |
| 3658 | * xmlXPathCastNodeToNumber: |
| 3659 | * @node: a node |
| 3660 | * |
| 3661 | * Converts a node to its number value |
| 3662 | * |
| 3663 | * Returns the number value |
| 3664 | */ |
| 3665 | double |
| 3666 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3667 | xmlChar *strval; |
| 3668 | double ret; |
| 3669 | |
| 3670 | if (node == NULL) |
| 3671 | return(xmlXPathNAN); |
| 3672 | strval = xmlXPathCastNodeToString(node); |
| 3673 | if (strval == NULL) |
| 3674 | return(xmlXPathNAN); |
| 3675 | ret = xmlXPathCastStringToNumber(strval); |
| 3676 | xmlFree(strval); |
| 3677 | |
| 3678 | return(ret); |
| 3679 | } |
| 3680 | |
| 3681 | /** |
| 3682 | * xmlXPathCastNodeSetToNumber: |
| 3683 | * @ns: a node-set |
| 3684 | * |
| 3685 | * Converts a node-set to its number value |
| 3686 | * |
| 3687 | * Returns the number value |
| 3688 | */ |
| 3689 | double |
| 3690 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3691 | xmlChar *str; |
| 3692 | double ret; |
| 3693 | |
| 3694 | if (ns == NULL) |
| 3695 | return(xmlXPathNAN); |
| 3696 | str = xmlXPathCastNodeSetToString(ns); |
| 3697 | ret = xmlXPathCastStringToNumber(str); |
| 3698 | xmlFree(str); |
| 3699 | return(ret); |
| 3700 | } |
| 3701 | |
| 3702 | /** |
| 3703 | * xmlXPathCastToNumber: |
| 3704 | * @val: an XPath object |
| 3705 | * |
| 3706 | * Converts an XPath object to its number value |
| 3707 | * |
| 3708 | * Returns the number value |
| 3709 | */ |
| 3710 | double |
| 3711 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3712 | double ret = 0.0; |
| 3713 | |
| 3714 | if (val == NULL) |
| 3715 | return(xmlXPathNAN); |
| 3716 | switch (val->type) { |
| 3717 | case XPATH_UNDEFINED: |
| 3718 | #ifdef DEGUB_EXPR |
| 3719 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3720 | #endif |
| 3721 | ret = xmlXPathNAN; |
| 3722 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3723 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3724 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3725 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3726 | break; |
| 3727 | case XPATH_STRING: |
| 3728 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3729 | break; |
| 3730 | case XPATH_NUMBER: |
| 3731 | ret = val->floatval; |
| 3732 | break; |
| 3733 | case XPATH_BOOLEAN: |
| 3734 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3735 | break; |
| 3736 | case XPATH_USERS: |
| 3737 | case XPATH_POINT: |
| 3738 | case XPATH_RANGE: |
| 3739 | case XPATH_LOCATIONSET: |
| 3740 | TODO; |
| 3741 | ret = xmlXPathNAN; |
| 3742 | break; |
| 3743 | } |
| 3744 | return(ret); |
| 3745 | } |
| 3746 | |
| 3747 | /** |
| 3748 | * xmlXPathConvertNumber: |
| 3749 | * @val: an XPath object |
| 3750 | * |
| 3751 | * Converts an existing object to its number() equivalent |
| 3752 | * |
| 3753 | * Returns the new object, the old one is freed (or the operation |
| 3754 | * is done directly on @val) |
| 3755 | */ |
| 3756 | xmlXPathObjectPtr |
| 3757 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3758 | xmlXPathObjectPtr ret; |
| 3759 | |
| 3760 | if (val == NULL) |
| 3761 | return(xmlXPathNewFloat(0.0)); |
| 3762 | if (val->type == XPATH_NUMBER) |
| 3763 | return(val); |
| 3764 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3765 | xmlXPathFreeObject(val); |
| 3766 | return(ret); |
| 3767 | } |
| 3768 | |
| 3769 | /** |
| 3770 | * xmlXPathCastNumberToBoolean: |
| 3771 | * @val: a number |
| 3772 | * |
| 3773 | * Converts a number to its boolean value |
| 3774 | * |
| 3775 | * Returns the boolean value |
| 3776 | */ |
| 3777 | int |
| 3778 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3779 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3780 | return(0); |
| 3781 | return(1); |
| 3782 | } |
| 3783 | |
| 3784 | /** |
| 3785 | * xmlXPathCastStringToBoolean: |
| 3786 | * @val: a string |
| 3787 | * |
| 3788 | * Converts a string to its boolean value |
| 3789 | * |
| 3790 | * Returns the boolean value |
| 3791 | */ |
| 3792 | int |
| 3793 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3794 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3795 | return(0); |
| 3796 | return(1); |
| 3797 | } |
| 3798 | |
| 3799 | /** |
| 3800 | * xmlXPathCastNodeSetToBoolean: |
| 3801 | * @ns: a node-set |
| 3802 | * |
| 3803 | * Converts a node-set to its boolean value |
| 3804 | * |
| 3805 | * Returns the boolean value |
| 3806 | */ |
| 3807 | int |
| 3808 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3809 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3810 | return(0); |
| 3811 | return(1); |
| 3812 | } |
| 3813 | |
| 3814 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3815 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3816 | * @val: an XPath object |
| 3817 | * |
| 3818 | * Converts an XPath object to its boolean value |
| 3819 | * |
| 3820 | * Returns the boolean value |
| 3821 | */ |
| 3822 | int |
| 3823 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3824 | int ret = 0; |
| 3825 | |
| 3826 | if (val == NULL) |
| 3827 | return(0); |
| 3828 | switch (val->type) { |
| 3829 | case XPATH_UNDEFINED: |
| 3830 | #ifdef DEBUG_EXPR |
| 3831 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3832 | #endif |
| 3833 | ret = 0; |
| 3834 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3835 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3836 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3837 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3838 | break; |
| 3839 | case XPATH_STRING: |
| 3840 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3841 | break; |
| 3842 | case XPATH_NUMBER: |
| 3843 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3844 | break; |
| 3845 | case XPATH_BOOLEAN: |
| 3846 | ret = val->boolval; |
| 3847 | break; |
| 3848 | case XPATH_USERS: |
| 3849 | case XPATH_POINT: |
| 3850 | case XPATH_RANGE: |
| 3851 | case XPATH_LOCATIONSET: |
| 3852 | TODO; |
| 3853 | ret = 0; |
| 3854 | break; |
| 3855 | } |
| 3856 | return(ret); |
| 3857 | } |
| 3858 | |
| 3859 | |
| 3860 | /** |
| 3861 | * xmlXPathConvertBoolean: |
| 3862 | * @val: an XPath object |
| 3863 | * |
| 3864 | * Converts an existing object to its boolean() equivalent |
| 3865 | * |
| 3866 | * Returns the new object, the old one is freed (or the operation |
| 3867 | * is done directly on @val) |
| 3868 | */ |
| 3869 | xmlXPathObjectPtr |
| 3870 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3871 | xmlXPathObjectPtr ret; |
| 3872 | |
| 3873 | if (val == NULL) |
| 3874 | return(xmlXPathNewBoolean(0)); |
| 3875 | if (val->type == XPATH_BOOLEAN) |
| 3876 | return(val); |
| 3877 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3878 | xmlXPathFreeObject(val); |
| 3879 | return(ret); |
| 3880 | } |
| 3881 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3882 | /************************************************************************ |
| 3883 | * * |
| 3884 | * Routines to handle XPath contexts * |
| 3885 | * * |
| 3886 | ************************************************************************/ |
| 3887 | |
| 3888 | /** |
| 3889 | * xmlXPathNewContext: |
| 3890 | * @doc: the XML document |
| 3891 | * |
| 3892 | * Create a new xmlXPathContext |
| 3893 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3894 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3895 | */ |
| 3896 | xmlXPathContextPtr |
| 3897 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3898 | xmlXPathContextPtr ret; |
| 3899 | |
| 3900 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3901 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3902 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3903 | return(NULL); |
| 3904 | } |
| 3905 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3906 | ret->doc = doc; |
| 3907 | ret->node = NULL; |
| 3908 | |
| 3909 | ret->varHash = NULL; |
| 3910 | |
| 3911 | ret->nb_types = 0; |
| 3912 | ret->max_types = 0; |
| 3913 | ret->types = NULL; |
| 3914 | |
| 3915 | ret->funcHash = xmlHashCreate(0); |
| 3916 | |
| 3917 | ret->nb_axis = 0; |
| 3918 | ret->max_axis = 0; |
| 3919 | ret->axis = NULL; |
| 3920 | |
| 3921 | ret->nsHash = NULL; |
| 3922 | ret->user = NULL; |
| 3923 | |
| 3924 | ret->contextSize = -1; |
| 3925 | ret->proximityPosition = -1; |
| 3926 | |
| 3927 | xmlXPathRegisterAllFunctions(ret); |
| 3928 | |
| 3929 | return(ret); |
| 3930 | } |
| 3931 | |
| 3932 | /** |
| 3933 | * xmlXPathFreeContext: |
| 3934 | * @ctxt: the context to free |
| 3935 | * |
| 3936 | * Free up an xmlXPathContext |
| 3937 | */ |
| 3938 | void |
| 3939 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 3940 | if (ctxt == NULL) return; |
| 3941 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3942 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3943 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3944 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 3945 | xmlResetError(&ctxt->lastError); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3946 | xmlFree(ctxt); |
| 3947 | } |
| 3948 | |
| 3949 | /************************************************************************ |
| 3950 | * * |
| 3951 | * Routines to handle XPath parser contexts * |
| 3952 | * * |
| 3953 | ************************************************************************/ |
| 3954 | |
| 3955 | #define CHECK_CTXT(ctxt) \ |
| 3956 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 3957 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 3958 | NULL, NULL, XML_FROM_XPATH, \ |
| 3959 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 3960 | __FILE__, __LINE__, \ |
| 3961 | NULL, NULL, NULL, 0, 0, \ |
| 3962 | "NULL context pointer\n"); \ |
| 3963 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3964 | } \ |
| 3965 | |
| 3966 | |
| 3967 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3968 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
| 3969 | (ctxt->doc->children == NULL)) { \ |
| 3970 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 3971 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3972 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3973 | |
| 3974 | |
| 3975 | /** |
| 3976 | * xmlXPathNewParserContext: |
| 3977 | * @str: the XPath expression |
| 3978 | * @ctxt: the XPath context |
| 3979 | * |
| 3980 | * Create a new xmlXPathParserContext |
| 3981 | * |
| 3982 | * Returns the xmlXPathParserContext just allocated. |
| 3983 | */ |
| 3984 | xmlXPathParserContextPtr |
| 3985 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3986 | xmlXPathParserContextPtr ret; |
| 3987 | |
| 3988 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3989 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3990 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3991 | return(NULL); |
| 3992 | } |
| 3993 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3994 | ret->cur = ret->base = str; |
| 3995 | ret->context = ctxt; |
| 3996 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3997 | ret->comp = xmlXPathNewCompExpr(); |
| 3998 | if (ret->comp == NULL) { |
| 3999 | xmlFree(ret->valueTab); |
| 4000 | xmlFree(ret); |
| 4001 | return(NULL); |
| 4002 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 4003 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 4004 | ret->comp->dict = ctxt->dict; |
| 4005 | xmlDictReference(ret->comp->dict); |
| 4006 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4007 | |
| 4008 | return(ret); |
| 4009 | } |
| 4010 | |
| 4011 | /** |
| 4012 | * xmlXPathCompParserContext: |
| 4013 | * @comp: the XPath compiled expression |
| 4014 | * @ctxt: the XPath context |
| 4015 | * |
| 4016 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 4017 | * |
| 4018 | * Returns the xmlXPathParserContext just allocated. |
| 4019 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4020 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4021 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 4022 | xmlXPathParserContextPtr ret; |
| 4023 | |
| 4024 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 4025 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4026 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4027 | return(NULL); |
| 4028 | } |
| 4029 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4030 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4031 | /* Allocate the value stack */ |
| 4032 | ret->valueTab = (xmlXPathObjectPtr *) |
| 4033 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4034 | if (ret->valueTab == NULL) { |
| 4035 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4036 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4037 | return(NULL); |
| 4038 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4039 | ret->valueNr = 0; |
| 4040 | ret->valueMax = 10; |
| 4041 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4042 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 4043 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4044 | ret->comp = comp; |
| 4045 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4046 | return(ret); |
| 4047 | } |
| 4048 | |
| 4049 | /** |
| 4050 | * xmlXPathFreeParserContext: |
| 4051 | * @ctxt: the context to free |
| 4052 | * |
| 4053 | * Free up an xmlXPathParserContext |
| 4054 | */ |
| 4055 | void |
| 4056 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 4057 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4058 | xmlFree(ctxt->valueTab); |
| 4059 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4060 | if (ctxt->comp != NULL) { |
| 4061 | #ifdef XPATH_STREAMING |
| 4062 | if (ctxt->comp->stream != NULL) { |
| 4063 | xmlFreePatternList(ctxt->comp->stream); |
| 4064 | ctxt->comp->stream = NULL; |
| 4065 | } |
| 4066 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4067 | xmlXPathFreeCompExpr(ctxt->comp); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4068 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4069 | xmlFree(ctxt); |
| 4070 | } |
| 4071 | |
| 4072 | /************************************************************************ |
| 4073 | * * |
| 4074 | * The implicit core function library * |
| 4075 | * * |
| 4076 | ************************************************************************/ |
| 4077 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4078 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4079 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4080 | * @node: a node pointer |
| 4081 | * |
| 4082 | * Function computing the beginning of the string value of the node, |
| 4083 | * used to speed up comparisons |
| 4084 | * |
| 4085 | * Returns an int usable as a hash |
| 4086 | */ |
| 4087 | static unsigned int |
| 4088 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 4089 | int len = 2; |
| 4090 | const xmlChar * string = NULL; |
| 4091 | xmlNodePtr tmp = NULL; |
| 4092 | unsigned int ret = 0; |
| 4093 | |
| 4094 | if (node == NULL) |
| 4095 | return(0); |
| 4096 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4097 | if (node->type == XML_DOCUMENT_NODE) { |
| 4098 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 4099 | if (tmp == NULL) |
| 4100 | node = node->children; |
| 4101 | else |
| 4102 | node = tmp; |
| 4103 | |
| 4104 | if (node == NULL) |
| 4105 | return(0); |
| 4106 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4107 | |
| 4108 | switch (node->type) { |
| 4109 | case XML_COMMENT_NODE: |
| 4110 | case XML_PI_NODE: |
| 4111 | case XML_CDATA_SECTION_NODE: |
| 4112 | case XML_TEXT_NODE: |
| 4113 | string = node->content; |
| 4114 | if (string == NULL) |
| 4115 | return(0); |
| 4116 | if (string[0] == 0) |
| 4117 | return(0); |
| 4118 | return(((unsigned int) string[0]) + |
| 4119 | (((unsigned int) string[1]) << 8)); |
| 4120 | case XML_NAMESPACE_DECL: |
| 4121 | string = ((xmlNsPtr)node)->href; |
| 4122 | if (string == NULL) |
| 4123 | return(0); |
| 4124 | if (string[0] == 0) |
| 4125 | return(0); |
| 4126 | return(((unsigned int) string[0]) + |
| 4127 | (((unsigned int) string[1]) << 8)); |
| 4128 | case XML_ATTRIBUTE_NODE: |
| 4129 | tmp = ((xmlAttrPtr) node)->children; |
| 4130 | break; |
| 4131 | case XML_ELEMENT_NODE: |
| 4132 | tmp = node->children; |
| 4133 | break; |
| 4134 | default: |
| 4135 | return(0); |
| 4136 | } |
| 4137 | while (tmp != NULL) { |
| 4138 | switch (tmp->type) { |
| 4139 | case XML_COMMENT_NODE: |
| 4140 | case XML_PI_NODE: |
| 4141 | case XML_CDATA_SECTION_NODE: |
| 4142 | case XML_TEXT_NODE: |
| 4143 | string = tmp->content; |
| 4144 | break; |
| 4145 | case XML_NAMESPACE_DECL: |
| 4146 | string = ((xmlNsPtr)tmp)->href; |
| 4147 | break; |
| 4148 | default: |
| 4149 | break; |
| 4150 | } |
| 4151 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4152 | if (len == 1) { |
| 4153 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4154 | } |
| 4155 | if (string[1] == 0) { |
| 4156 | len = 1; |
| 4157 | ret = (unsigned int) string[0]; |
| 4158 | } else { |
| 4159 | return(((unsigned int) string[0]) + |
| 4160 | (((unsigned int) string[1]) << 8)); |
| 4161 | } |
| 4162 | } |
| 4163 | /* |
| 4164 | * Skip to next node |
| 4165 | */ |
| 4166 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4167 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4168 | tmp = tmp->children; |
| 4169 | continue; |
| 4170 | } |
| 4171 | } |
| 4172 | if (tmp == node) |
| 4173 | break; |
| 4174 | |
| 4175 | if (tmp->next != NULL) { |
| 4176 | tmp = tmp->next; |
| 4177 | continue; |
| 4178 | } |
| 4179 | |
| 4180 | do { |
| 4181 | tmp = tmp->parent; |
| 4182 | if (tmp == NULL) |
| 4183 | break; |
| 4184 | if (tmp == node) { |
| 4185 | tmp = NULL; |
| 4186 | break; |
| 4187 | } |
| 4188 | if (tmp->next != NULL) { |
| 4189 | tmp = tmp->next; |
| 4190 | break; |
| 4191 | } |
| 4192 | } while (tmp != NULL); |
| 4193 | } |
| 4194 | return(ret); |
| 4195 | } |
| 4196 | |
| 4197 | /** |
| 4198 | * xmlXPathStringHash: |
| 4199 | * @string: a string |
| 4200 | * |
| 4201 | * Function computing the beginning of the string value of the node, |
| 4202 | * used to speed up comparisons |
| 4203 | * |
| 4204 | * Returns an int usable as a hash |
| 4205 | */ |
| 4206 | static unsigned int |
| 4207 | xmlXPathStringHash(const xmlChar * string) { |
| 4208 | if (string == NULL) |
| 4209 | return((unsigned int) 0); |
| 4210 | if (string[0] == 0) |
| 4211 | return(0); |
| 4212 | return(((unsigned int) string[0]) + |
| 4213 | (((unsigned int) string[1]) << 8)); |
| 4214 | } |
| 4215 | |
| 4216 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4217 | * xmlXPathCompareNodeSetFloat: |
| 4218 | * @ctxt: the XPath Parser context |
| 4219 | * @inf: less than (1) or greater than (0) |
| 4220 | * @strict: is the comparison strict |
| 4221 | * @arg: the node set |
| 4222 | * @f: the value |
| 4223 | * |
| 4224 | * Implement the compare operation between a nodeset and a number |
| 4225 | * @ns < @val (1, 1, ... |
| 4226 | * @ns <= @val (1, 0, ... |
| 4227 | * @ns > @val (0, 1, ... |
| 4228 | * @ns >= @val (0, 0, ... |
| 4229 | * |
| 4230 | * If one object to be compared is a node-set and the other is a number, |
| 4231 | * then the comparison will be true if and only if there is a node in the |
| 4232 | * node-set such that the result of performing the comparison on the number |
| 4233 | * to be compared and on the result of converting the string-value of that |
| 4234 | * node to a number using the number function is true. |
| 4235 | * |
| 4236 | * Returns 0 or 1 depending on the results of the test. |
| 4237 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4238 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4239 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4240 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4241 | int i, ret = 0; |
| 4242 | xmlNodeSetPtr ns; |
| 4243 | xmlChar *str2; |
| 4244 | |
| 4245 | if ((f == NULL) || (arg == NULL) || |
| 4246 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4247 | xmlXPathFreeObject(arg); |
| 4248 | xmlXPathFreeObject(f); |
| 4249 | return(0); |
| 4250 | } |
| 4251 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4252 | if (ns != NULL) { |
| 4253 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4254 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4255 | if (str2 != NULL) { |
| 4256 | valuePush(ctxt, |
| 4257 | xmlXPathNewString(str2)); |
| 4258 | xmlFree(str2); |
| 4259 | xmlXPathNumberFunction(ctxt, 1); |
| 4260 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4261 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4262 | if (ret) |
| 4263 | break; |
| 4264 | } |
| 4265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4266 | } |
| 4267 | xmlXPathFreeObject(arg); |
| 4268 | xmlXPathFreeObject(f); |
| 4269 | return(ret); |
| 4270 | } |
| 4271 | |
| 4272 | /** |
| 4273 | * xmlXPathCompareNodeSetString: |
| 4274 | * @ctxt: the XPath Parser context |
| 4275 | * @inf: less than (1) or greater than (0) |
| 4276 | * @strict: is the comparison strict |
| 4277 | * @arg: the node set |
| 4278 | * @s: the value |
| 4279 | * |
| 4280 | * Implement the compare operation between a nodeset and a string |
| 4281 | * @ns < @val (1, 1, ... |
| 4282 | * @ns <= @val (1, 0, ... |
| 4283 | * @ns > @val (0, 1, ... |
| 4284 | * @ns >= @val (0, 0, ... |
| 4285 | * |
| 4286 | * If one object to be compared is a node-set and the other is a string, |
| 4287 | * then the comparison will be true if and only if there is a node in |
| 4288 | * the node-set such that the result of performing the comparison on the |
| 4289 | * string-value of the node and the other string is true. |
| 4290 | * |
| 4291 | * Returns 0 or 1 depending on the results of the test. |
| 4292 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4293 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4294 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4295 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4296 | int i, ret = 0; |
| 4297 | xmlNodeSetPtr ns; |
| 4298 | xmlChar *str2; |
| 4299 | |
| 4300 | if ((s == NULL) || (arg == NULL) || |
| 4301 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4302 | xmlXPathFreeObject(arg); |
| 4303 | xmlXPathFreeObject(s); |
| 4304 | return(0); |
| 4305 | } |
| 4306 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4307 | if (ns != NULL) { |
| 4308 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4309 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4310 | if (str2 != NULL) { |
| 4311 | valuePush(ctxt, |
| 4312 | xmlXPathNewString(str2)); |
| 4313 | xmlFree(str2); |
| 4314 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4315 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4316 | if (ret) |
| 4317 | break; |
| 4318 | } |
| 4319 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4320 | } |
| 4321 | xmlXPathFreeObject(arg); |
| 4322 | xmlXPathFreeObject(s); |
| 4323 | return(ret); |
| 4324 | } |
| 4325 | |
| 4326 | /** |
| 4327 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4328 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4329 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4330 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | * @arg2: the second node set object |
| 4332 | * |
| 4333 | * Implement the compare operation on nodesets: |
| 4334 | * |
| 4335 | * If both objects to be compared are node-sets, then the comparison |
| 4336 | * will be true if and only if there is a node in the first node-set |
| 4337 | * and a node in the second node-set such that the result of performing |
| 4338 | * the comparison on the string-values of the two nodes is true. |
| 4339 | * .... |
| 4340 | * When neither object to be compared is a node-set and the operator |
| 4341 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4342 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4343 | * .... |
| 4344 | * The number function converts its argument to a number as follows: |
| 4345 | * - a string that consists of optional whitespace followed by an |
| 4346 | * optional minus sign followed by a Number followed by whitespace |
| 4347 | * is converted to the IEEE 754 number that is nearest (according |
| 4348 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4349 | * represented by the string; any other string is converted to NaN |
| 4350 | * |
| 4351 | * Conclusion all nodes need to be converted first to their string value |
| 4352 | * and then the comparison must be done when possible |
| 4353 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4354 | static int |
| 4355 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4356 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4357 | int i, j, init = 0; |
| 4358 | double val1; |
| 4359 | double *values2; |
| 4360 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4361 | xmlNodeSetPtr ns1; |
| 4362 | xmlNodeSetPtr ns2; |
| 4363 | |
| 4364 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4365 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4366 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4367 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4368 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4369 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4370 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4371 | xmlXPathFreeObject(arg1); |
| 4372 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4374 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4375 | |
| 4376 | ns1 = arg1->nodesetval; |
| 4377 | ns2 = arg2->nodesetval; |
| 4378 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4379 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 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 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4384 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4385 | xmlXPathFreeObject(arg1); |
| 4386 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4387 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4388 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4389 | |
| 4390 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4391 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4392 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4393 | xmlXPathFreeObject(arg1); |
| 4394 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4395 | return(0); |
| 4396 | } |
| 4397 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4398 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4399 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4400 | continue; |
| 4401 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4402 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4403 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4404 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4405 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4406 | continue; |
| 4407 | if (inf && strict) |
| 4408 | ret = (val1 < values2[j]); |
| 4409 | else if (inf && !strict) |
| 4410 | ret = (val1 <= values2[j]); |
| 4411 | else if (!inf && strict) |
| 4412 | ret = (val1 > values2[j]); |
| 4413 | else if (!inf && !strict) |
| 4414 | ret = (val1 >= values2[j]); |
| 4415 | if (ret) |
| 4416 | break; |
| 4417 | } |
| 4418 | if (ret) |
| 4419 | break; |
| 4420 | init = 1; |
| 4421 | } |
| 4422 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4423 | xmlXPathFreeObject(arg1); |
| 4424 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4425 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /** |
| 4429 | * xmlXPathCompareNodeSetValue: |
| 4430 | * @ctxt: the XPath Parser context |
| 4431 | * @inf: less than (1) or greater than (0) |
| 4432 | * @strict: is the comparison strict |
| 4433 | * @arg: the node set |
| 4434 | * @val: the value |
| 4435 | * |
| 4436 | * Implement the compare operation between a nodeset and a value |
| 4437 | * @ns < @val (1, 1, ... |
| 4438 | * @ns <= @val (1, 0, ... |
| 4439 | * @ns > @val (0, 1, ... |
| 4440 | * @ns >= @val (0, 0, ... |
| 4441 | * |
| 4442 | * If one object to be compared is a node-set and the other is a boolean, |
| 4443 | * then the comparison will be true if and only if the result of performing |
| 4444 | * the comparison on the boolean and on the result of converting |
| 4445 | * the node-set to a boolean using the boolean function is true. |
| 4446 | * |
| 4447 | * Returns 0 or 1 depending on the results of the test. |
| 4448 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4449 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4450 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4451 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4452 | if ((val == NULL) || (arg == NULL) || |
| 4453 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4454 | return(0); |
| 4455 | |
| 4456 | switch(val->type) { |
| 4457 | case XPATH_NUMBER: |
| 4458 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4459 | case XPATH_NODESET: |
| 4460 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4461 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4462 | case XPATH_STRING: |
| 4463 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4464 | case XPATH_BOOLEAN: |
| 4465 | valuePush(ctxt, arg); |
| 4466 | xmlXPathBooleanFunction(ctxt, 1); |
| 4467 | valuePush(ctxt, val); |
| 4468 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4469 | default: |
| 4470 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4471 | } |
| 4472 | return(0); |
| 4473 | } |
| 4474 | |
| 4475 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4476 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4477 | * @arg: the nodeset object argument |
| 4478 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4479 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4480 | * |
| 4481 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4482 | * If one object to be compared is a node-set and the other is a string, |
| 4483 | * then the comparison will be true if and only if there is a node in |
| 4484 | * the node-set such that the result of performing the comparison on the |
| 4485 | * string-value of the node and the other string is true. |
| 4486 | * |
| 4487 | * Returns 0 or 1 depending on the results of the test. |
| 4488 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4489 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4490 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4491 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4492 | int i; |
| 4493 | xmlNodeSetPtr ns; |
| 4494 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4495 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4496 | |
| 4497 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4498 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4499 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4500 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4501 | /* |
| 4502 | * A NULL nodeset compared with a string is always false |
| 4503 | * (since there is no node equal, and no node not equal) |
| 4504 | */ |
| 4505 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4506 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4507 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4508 | for (i = 0; i < ns->nodeNr; i++) { |
| 4509 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4510 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4511 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4512 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4513 | if (neq) |
| 4514 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4515 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4516 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4517 | if (neq) |
| 4518 | continue; |
| 4519 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4520 | } else if (neq) { |
| 4521 | if (str2 != NULL) |
| 4522 | xmlFree(str2); |
| 4523 | return (1); |
| 4524 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4525 | if (str2 != NULL) |
| 4526 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4527 | } else if (neq) |
| 4528 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4529 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4530 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4534 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4535 | * @arg: the nodeset object argument |
| 4536 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4537 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | * |
| 4539 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4540 | * If one object to be compared is a node-set and the other is a number, |
| 4541 | * then the comparison will be true if and only if there is a node in |
| 4542 | * the node-set such that the result of performing the comparison on the |
| 4543 | * number to be compared and on the result of converting the string-value |
| 4544 | * of that node to a number using the number function is true. |
| 4545 | * |
| 4546 | * Returns 0 or 1 depending on the results of the test. |
| 4547 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4548 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4549 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4550 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4551 | int i, ret=0; |
| 4552 | xmlNodeSetPtr ns; |
| 4553 | xmlChar *str2; |
| 4554 | xmlXPathObjectPtr val; |
| 4555 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4556 | |
| 4557 | if ((arg == NULL) || |
| 4558 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4559 | return(0); |
| 4560 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4561 | ns = arg->nodesetval; |
| 4562 | if (ns != NULL) { |
| 4563 | for (i=0;i<ns->nodeNr;i++) { |
| 4564 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4565 | if (str2 != NULL) { |
| 4566 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4567 | xmlFree(str2); |
| 4568 | xmlXPathNumberFunction(ctxt, 1); |
| 4569 | val = valuePop(ctxt); |
| 4570 | v = val->floatval; |
| 4571 | xmlXPathFreeObject(val); |
| 4572 | if (!xmlXPathIsNaN(v)) { |
| 4573 | if ((!neq) && (v==f)) { |
| 4574 | ret = 1; |
| 4575 | break; |
| 4576 | } else if ((neq) && (v!=f)) { |
| 4577 | ret = 1; |
| 4578 | break; |
| 4579 | } |
| 4580 | } |
| 4581 | } |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | |
| 4589 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4590 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4591 | * @arg1: first nodeset object argument |
| 4592 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4593 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4595 | * Implement the equal / not equal operation on XPath nodesets: |
| 4596 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4597 | * If both objects to be compared are node-sets, then the comparison |
| 4598 | * will be true if and only if there is a node in the first node-set and |
| 4599 | * a node in the second node-set such that the result of performing the |
| 4600 | * comparison on the string-values of the two nodes is true. |
| 4601 | * |
| 4602 | * (needless to say, this is a costly operation) |
| 4603 | * |
| 4604 | * Returns 0 or 1 depending on the results of the test. |
| 4605 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4606 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4607 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4608 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4609 | unsigned int *hashs1; |
| 4610 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4611 | xmlChar **values1; |
| 4612 | xmlChar **values2; |
| 4613 | int ret = 0; |
| 4614 | xmlNodeSetPtr ns1; |
| 4615 | xmlNodeSetPtr ns2; |
| 4616 | |
| 4617 | if ((arg1 == NULL) || |
| 4618 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4619 | return(0); |
| 4620 | if ((arg2 == NULL) || |
| 4621 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4622 | return(0); |
| 4623 | |
| 4624 | ns1 = arg1->nodesetval; |
| 4625 | ns2 = arg2->nodesetval; |
| 4626 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4627 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4628 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4629 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4630 | return(0); |
| 4631 | |
| 4632 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4633 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4634 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4635 | if (neq == 0) |
| 4636 | for (i = 0;i < ns1->nodeNr;i++) |
| 4637 | for (j = 0;j < ns2->nodeNr;j++) |
| 4638 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4639 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | |
| 4641 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4642 | if (values1 == NULL) { |
| 4643 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4644 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4645 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4646 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4647 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4648 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4649 | xmlFree(values1); |
| 4650 | return(0); |
| 4651 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4652 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4653 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4654 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4655 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4656 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4657 | xmlFree(values1); |
| 4658 | return(0); |
| 4659 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4660 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4661 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4662 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4663 | xmlFree(hashs1); |
| 4664 | xmlFree(values1); |
| 4665 | xmlFree(values2); |
| 4666 | return(0); |
| 4667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4668 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4669 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4670 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4671 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4672 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4673 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4674 | if (hashs1[i] != hashs2[j]) { |
| 4675 | if (neq) { |
| 4676 | ret = 1; |
| 4677 | break; |
| 4678 | } |
| 4679 | } |
| 4680 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4681 | if (values1[i] == NULL) |
| 4682 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4683 | if (values2[j] == NULL) |
| 4684 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4685 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4686 | if (ret) |
| 4687 | break; |
| 4688 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4689 | } |
| 4690 | if (ret) |
| 4691 | break; |
| 4692 | } |
| 4693 | for (i = 0;i < ns1->nodeNr;i++) |
| 4694 | if (values1[i] != NULL) |
| 4695 | xmlFree(values1[i]); |
| 4696 | for (j = 0;j < ns2->nodeNr;j++) |
| 4697 | if (values2[j] != NULL) |
| 4698 | xmlFree(values2[j]); |
| 4699 | xmlFree(values1); |
| 4700 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4701 | xmlFree(hashs1); |
| 4702 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4703 | return(ret); |
| 4704 | } |
| 4705 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4706 | static int |
| 4707 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 4708 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4709 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4710 | /* |
| 4711 | *At this point we are assured neither arg1 nor arg2 |
| 4712 | *is a nodeset, so we can just pick the appropriate routine. |
| 4713 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4714 | switch (arg1->type) { |
| 4715 | case XPATH_UNDEFINED: |
| 4716 | #ifdef DEBUG_EXPR |
| 4717 | xmlGenericError(xmlGenericErrorContext, |
| 4718 | "Equal: undefined\n"); |
| 4719 | #endif |
| 4720 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4721 | case XPATH_BOOLEAN: |
| 4722 | switch (arg2->type) { |
| 4723 | case XPATH_UNDEFINED: |
| 4724 | #ifdef DEBUG_EXPR |
| 4725 | xmlGenericError(xmlGenericErrorContext, |
| 4726 | "Equal: undefined\n"); |
| 4727 | #endif |
| 4728 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4729 | case XPATH_BOOLEAN: |
| 4730 | #ifdef DEBUG_EXPR |
| 4731 | xmlGenericError(xmlGenericErrorContext, |
| 4732 | "Equal: %d boolean %d \n", |
| 4733 | arg1->boolval, arg2->boolval); |
| 4734 | #endif |
| 4735 | ret = (arg1->boolval == arg2->boolval); |
| 4736 | break; |
| 4737 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4738 | ret = (arg1->boolval == |
| 4739 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4740 | break; |
| 4741 | case XPATH_STRING: |
| 4742 | if ((arg2->stringval == NULL) || |
| 4743 | (arg2->stringval[0] == 0)) ret = 0; |
| 4744 | else |
| 4745 | ret = 1; |
| 4746 | ret = (arg1->boolval == ret); |
| 4747 | break; |
| 4748 | case XPATH_USERS: |
| 4749 | case XPATH_POINT: |
| 4750 | case XPATH_RANGE: |
| 4751 | case XPATH_LOCATIONSET: |
| 4752 | TODO |
| 4753 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4754 | case XPATH_NODESET: |
| 4755 | case XPATH_XSLT_TREE: |
| 4756 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4757 | } |
| 4758 | break; |
| 4759 | case XPATH_NUMBER: |
| 4760 | switch (arg2->type) { |
| 4761 | case XPATH_UNDEFINED: |
| 4762 | #ifdef DEBUG_EXPR |
| 4763 | xmlGenericError(xmlGenericErrorContext, |
| 4764 | "Equal: undefined\n"); |
| 4765 | #endif |
| 4766 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4767 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4768 | ret = (arg2->boolval== |
| 4769 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | case XPATH_STRING: |
| 4772 | valuePush(ctxt, arg2); |
| 4773 | xmlXPathNumberFunction(ctxt, 1); |
| 4774 | arg2 = valuePop(ctxt); |
| 4775 | /* no break on purpose */ |
| 4776 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4777 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4778 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4779 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4780 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4781 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4782 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4783 | ret = 1; |
| 4784 | else |
| 4785 | ret = 0; |
| 4786 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4787 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4788 | ret = 1; |
| 4789 | else |
| 4790 | ret = 0; |
| 4791 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4792 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4793 | ret = 1; |
| 4794 | else |
| 4795 | ret = 0; |
| 4796 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4797 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4798 | ret = 1; |
| 4799 | else |
| 4800 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4801 | } else { |
| 4802 | ret = (arg1->floatval == arg2->floatval); |
| 4803 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4804 | break; |
| 4805 | case XPATH_USERS: |
| 4806 | case XPATH_POINT: |
| 4807 | case XPATH_RANGE: |
| 4808 | case XPATH_LOCATIONSET: |
| 4809 | TODO |
| 4810 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4811 | case XPATH_NODESET: |
| 4812 | case XPATH_XSLT_TREE: |
| 4813 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4814 | } |
| 4815 | break; |
| 4816 | case XPATH_STRING: |
| 4817 | switch (arg2->type) { |
| 4818 | case XPATH_UNDEFINED: |
| 4819 | #ifdef DEBUG_EXPR |
| 4820 | xmlGenericError(xmlGenericErrorContext, |
| 4821 | "Equal: undefined\n"); |
| 4822 | #endif |
| 4823 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4824 | case XPATH_BOOLEAN: |
| 4825 | if ((arg1->stringval == NULL) || |
| 4826 | (arg1->stringval[0] == 0)) ret = 0; |
| 4827 | else |
| 4828 | ret = 1; |
| 4829 | ret = (arg2->boolval == ret); |
| 4830 | break; |
| 4831 | case XPATH_STRING: |
| 4832 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4833 | break; |
| 4834 | case XPATH_NUMBER: |
| 4835 | valuePush(ctxt, arg1); |
| 4836 | xmlXPathNumberFunction(ctxt, 1); |
| 4837 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4838 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4839 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4840 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4841 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4842 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4843 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4844 | ret = 1; |
| 4845 | else |
| 4846 | ret = 0; |
| 4847 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4848 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4849 | ret = 1; |
| 4850 | else |
| 4851 | ret = 0; |
| 4852 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4853 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4854 | ret = 1; |
| 4855 | else |
| 4856 | ret = 0; |
| 4857 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4858 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4859 | ret = 1; |
| 4860 | else |
| 4861 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4862 | } else { |
| 4863 | ret = (arg1->floatval == arg2->floatval); |
| 4864 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4865 | break; |
| 4866 | case XPATH_USERS: |
| 4867 | case XPATH_POINT: |
| 4868 | case XPATH_RANGE: |
| 4869 | case XPATH_LOCATIONSET: |
| 4870 | TODO |
| 4871 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4872 | case XPATH_NODESET: |
| 4873 | case XPATH_XSLT_TREE: |
| 4874 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4875 | } |
| 4876 | break; |
| 4877 | case XPATH_USERS: |
| 4878 | case XPATH_POINT: |
| 4879 | case XPATH_RANGE: |
| 4880 | case XPATH_LOCATIONSET: |
| 4881 | TODO |
| 4882 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4883 | case XPATH_NODESET: |
| 4884 | case XPATH_XSLT_TREE: |
| 4885 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4886 | } |
| 4887 | xmlXPathFreeObject(arg1); |
| 4888 | xmlXPathFreeObject(arg2); |
| 4889 | return(ret); |
| 4890 | } |
| 4891 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4892 | /** |
| 4893 | * xmlXPathEqualValues: |
| 4894 | * @ctxt: the XPath Parser context |
| 4895 | * |
| 4896 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4897 | * |
| 4898 | * Returns 0 or 1 depending on the results of the test. |
| 4899 | */ |
| 4900 | int |
| 4901 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4902 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4903 | int ret = 0; |
| 4904 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 4905 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4906 | arg2 = valuePop(ctxt); |
| 4907 | arg1 = valuePop(ctxt); |
| 4908 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4909 | if (arg1 != NULL) |
| 4910 | xmlXPathFreeObject(arg1); |
| 4911 | else |
| 4912 | xmlXPathFreeObject(arg2); |
| 4913 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4914 | } |
| 4915 | |
| 4916 | if (arg1 == arg2) { |
| 4917 | #ifdef DEBUG_EXPR |
| 4918 | xmlGenericError(xmlGenericErrorContext, |
| 4919 | "Equal: by pointer\n"); |
| 4920 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 4921 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4922 | return(1); |
| 4923 | } |
| 4924 | |
| 4925 | /* |
| 4926 | *If either argument is a nodeset, it's a 'special case' |
| 4927 | */ |
| 4928 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4929 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4930 | /* |
| 4931 | *Hack it to assure arg1 is the nodeset |
| 4932 | */ |
| 4933 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4934 | argtmp = arg2; |
| 4935 | arg2 = arg1; |
| 4936 | arg1 = argtmp; |
| 4937 | } |
| 4938 | switch (arg2->type) { |
| 4939 | case XPATH_UNDEFINED: |
| 4940 | #ifdef DEBUG_EXPR |
| 4941 | xmlGenericError(xmlGenericErrorContext, |
| 4942 | "Equal: undefined\n"); |
| 4943 | #endif |
| 4944 | break; |
| 4945 | case XPATH_NODESET: |
| 4946 | case XPATH_XSLT_TREE: |
| 4947 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 4948 | break; |
| 4949 | case XPATH_BOOLEAN: |
| 4950 | if ((arg1->nodesetval == NULL) || |
| 4951 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4952 | else |
| 4953 | ret = 1; |
| 4954 | ret = (ret == arg2->boolval); |
| 4955 | break; |
| 4956 | case XPATH_NUMBER: |
| 4957 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 4958 | break; |
| 4959 | case XPATH_STRING: |
| 4960 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 4961 | break; |
| 4962 | case XPATH_USERS: |
| 4963 | case XPATH_POINT: |
| 4964 | case XPATH_RANGE: |
| 4965 | case XPATH_LOCATIONSET: |
| 4966 | TODO |
| 4967 | break; |
| 4968 | } |
| 4969 | xmlXPathFreeObject(arg1); |
| 4970 | xmlXPathFreeObject(arg2); |
| 4971 | return(ret); |
| 4972 | } |
| 4973 | |
| 4974 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4975 | } |
| 4976 | |
| 4977 | /** |
| 4978 | * xmlXPathNotEqualValues: |
| 4979 | * @ctxt: the XPath Parser context |
| 4980 | * |
| 4981 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4982 | * |
| 4983 | * Returns 0 or 1 depending on the results of the test. |
| 4984 | */ |
| 4985 | int |
| 4986 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4987 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4988 | int ret = 0; |
| 4989 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 4990 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4991 | arg2 = valuePop(ctxt); |
| 4992 | arg1 = valuePop(ctxt); |
| 4993 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4994 | if (arg1 != NULL) |
| 4995 | xmlXPathFreeObject(arg1); |
| 4996 | else |
| 4997 | xmlXPathFreeObject(arg2); |
| 4998 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4999 | } |
| 5000 | |
| 5001 | if (arg1 == arg2) { |
| 5002 | #ifdef DEBUG_EXPR |
| 5003 | xmlGenericError(xmlGenericErrorContext, |
| 5004 | "NotEqual: by pointer\n"); |
| 5005 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 5006 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5007 | return(0); |
| 5008 | } |
| 5009 | |
| 5010 | /* |
| 5011 | *If either argument is a nodeset, it's a 'special case' |
| 5012 | */ |
| 5013 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5014 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5015 | /* |
| 5016 | *Hack it to assure arg1 is the nodeset |
| 5017 | */ |
| 5018 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 5019 | argtmp = arg2; |
| 5020 | arg2 = arg1; |
| 5021 | arg1 = argtmp; |
| 5022 | } |
| 5023 | switch (arg2->type) { |
| 5024 | case XPATH_UNDEFINED: |
| 5025 | #ifdef DEBUG_EXPR |
| 5026 | xmlGenericError(xmlGenericErrorContext, |
| 5027 | "NotEqual: undefined\n"); |
| 5028 | #endif |
| 5029 | break; |
| 5030 | case XPATH_NODESET: |
| 5031 | case XPATH_XSLT_TREE: |
| 5032 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 5033 | break; |
| 5034 | case XPATH_BOOLEAN: |
| 5035 | if ((arg1->nodesetval == NULL) || |
| 5036 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 5037 | else |
| 5038 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5039 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5040 | break; |
| 5041 | case XPATH_NUMBER: |
| 5042 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 5043 | break; |
| 5044 | case XPATH_STRING: |
| 5045 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 5046 | break; |
| 5047 | case XPATH_USERS: |
| 5048 | case XPATH_POINT: |
| 5049 | case XPATH_RANGE: |
| 5050 | case XPATH_LOCATIONSET: |
| 5051 | TODO |
| 5052 | break; |
| 5053 | } |
| 5054 | xmlXPathFreeObject(arg1); |
| 5055 | xmlXPathFreeObject(arg2); |
| 5056 | return(ret); |
| 5057 | } |
| 5058 | |
| 5059 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5060 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5061 | |
| 5062 | /** |
| 5063 | * xmlXPathCompareValues: |
| 5064 | * @ctxt: the XPath Parser context |
| 5065 | * @inf: less than (1) or greater than (0) |
| 5066 | * @strict: is the comparison strict |
| 5067 | * |
| 5068 | * Implement the compare operation on XPath objects: |
| 5069 | * @arg1 < @arg2 (1, 1, ... |
| 5070 | * @arg1 <= @arg2 (1, 0, ... |
| 5071 | * @arg1 > @arg2 (0, 1, ... |
| 5072 | * @arg1 >= @arg2 (0, 0, ... |
| 5073 | * |
| 5074 | * When neither object to be compared is a node-set and the operator is |
| 5075 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 5076 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 5077 | * comparison will be true if and only if the first number is less than the |
| 5078 | * second number. The <= comparison will be true if and only if the first |
| 5079 | * number is less than or equal to the second number. The > comparison |
| 5080 | * will be true if and only if the first number is greater than the second |
| 5081 | * number. The >= comparison will be true if and only if the first number |
| 5082 | * is greater than or equal to the second number. |
| 5083 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5084 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5085 | */ |
| 5086 | int |
| 5087 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5088 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5089 | xmlXPathObjectPtr arg1, arg2; |
| 5090 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5091 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5092 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5093 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5094 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5095 | if (arg1 != NULL) |
| 5096 | xmlXPathFreeObject(arg1); |
| 5097 | else |
| 5098 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5099 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5100 | } |
| 5101 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5102 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5103 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 5104 | /* |
| 5105 | * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments |
| 5106 | * are not freed from within this routine; they will be freed from the |
| 5107 | * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue |
| 5108 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5109 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 5110 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5111 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5112 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5113 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5114 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 5115 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5116 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5117 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 5118 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | return(ret); |
| 5122 | } |
| 5123 | |
| 5124 | if (arg1->type != XPATH_NUMBER) { |
| 5125 | valuePush(ctxt, arg1); |
| 5126 | xmlXPathNumberFunction(ctxt, 1); |
| 5127 | arg1 = valuePop(ctxt); |
| 5128 | } |
| 5129 | if (arg1->type != XPATH_NUMBER) { |
| 5130 | xmlXPathFreeObject(arg1); |
| 5131 | xmlXPathFreeObject(arg2); |
| 5132 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5133 | } |
| 5134 | if (arg2->type != XPATH_NUMBER) { |
| 5135 | valuePush(ctxt, arg2); |
| 5136 | xmlXPathNumberFunction(ctxt, 1); |
| 5137 | arg2 = valuePop(ctxt); |
| 5138 | } |
| 5139 | if (arg2->type != XPATH_NUMBER) { |
| 5140 | xmlXPathFreeObject(arg1); |
| 5141 | xmlXPathFreeObject(arg2); |
| 5142 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5143 | } |
| 5144 | /* |
| 5145 | * Add tests for infinity and nan |
| 5146 | * => feedback on 3.4 for Inf and NaN |
| 5147 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5148 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5149 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5150 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5151 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5152 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5153 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5154 | if (inf && strict) { |
| 5155 | if ((arg1i == -1 && arg2i != -1) || |
| 5156 | (arg2i == 1 && arg1i != 1)) { |
| 5157 | ret = 1; |
| 5158 | } else if (arg1i == 0 && arg2i == 0) { |
| 5159 | ret = (arg1->floatval < arg2->floatval); |
| 5160 | } else { |
| 5161 | ret = 0; |
| 5162 | } |
| 5163 | } |
| 5164 | else if (inf && !strict) { |
| 5165 | if (arg1i == -1 || arg2i == 1) { |
| 5166 | ret = 1; |
| 5167 | } else if (arg1i == 0 && arg2i == 0) { |
| 5168 | ret = (arg1->floatval <= arg2->floatval); |
| 5169 | } else { |
| 5170 | ret = 0; |
| 5171 | } |
| 5172 | } |
| 5173 | else if (!inf && strict) { |
| 5174 | if ((arg1i == 1 && arg2i != 1) || |
| 5175 | (arg2i == -1 && arg1i != -1)) { |
| 5176 | ret = 1; |
| 5177 | } else if (arg1i == 0 && arg2i == 0) { |
| 5178 | ret = (arg1->floatval > arg2->floatval); |
| 5179 | } else { |
| 5180 | ret = 0; |
| 5181 | } |
| 5182 | } |
| 5183 | else if (!inf && !strict) { |
| 5184 | if (arg1i == 1 || arg2i == -1) { |
| 5185 | ret = 1; |
| 5186 | } else if (arg1i == 0 && arg2i == 0) { |
| 5187 | ret = (arg1->floatval >= arg2->floatval); |
| 5188 | } else { |
| 5189 | ret = 0; |
| 5190 | } |
| 5191 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5192 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5193 | xmlXPathFreeObject(arg1); |
| 5194 | xmlXPathFreeObject(arg2); |
| 5195 | return(ret); |
| 5196 | } |
| 5197 | |
| 5198 | /** |
| 5199 | * xmlXPathValueFlipSign: |
| 5200 | * @ctxt: the XPath Parser context |
| 5201 | * |
| 5202 | * Implement the unary - operation on an XPath object |
| 5203 | * The numeric operators convert their operands to numbers as if |
| 5204 | * by calling the number function. |
| 5205 | */ |
| 5206 | void |
| 5207 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5208 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5209 | CAST_TO_NUMBER; |
| 5210 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5211 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5212 | ctxt->value->floatval=xmlXPathNAN; |
| 5213 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5214 | ctxt->value->floatval=xmlXPathNINF; |
| 5215 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5216 | ctxt->value->floatval=xmlXPathPINF; |
| 5217 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5218 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5219 | ctxt->value->floatval = xmlXPathNZERO; |
| 5220 | else |
| 5221 | ctxt->value->floatval = 0; |
| 5222 | } |
| 5223 | else |
| 5224 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | /** |
| 5228 | * xmlXPathAddValues: |
| 5229 | * @ctxt: the XPath Parser context |
| 5230 | * |
| 5231 | * Implement the add operation on XPath objects: |
| 5232 | * The numeric operators convert their operands to numbers as if |
| 5233 | * by calling the number function. |
| 5234 | */ |
| 5235 | void |
| 5236 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5237 | xmlXPathObjectPtr arg; |
| 5238 | double val; |
| 5239 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5240 | arg = valuePop(ctxt); |
| 5241 | if (arg == NULL) |
| 5242 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5243 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5244 | xmlXPathFreeObject(arg); |
| 5245 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5246 | CAST_TO_NUMBER; |
| 5247 | CHECK_TYPE(XPATH_NUMBER); |
| 5248 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | /** |
| 5252 | * xmlXPathSubValues: |
| 5253 | * @ctxt: the XPath Parser context |
| 5254 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5255 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5256 | * The numeric operators convert their operands to numbers as if |
| 5257 | * by calling the number function. |
| 5258 | */ |
| 5259 | void |
| 5260 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5261 | xmlXPathObjectPtr arg; |
| 5262 | double val; |
| 5263 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5264 | arg = valuePop(ctxt); |
| 5265 | if (arg == NULL) |
| 5266 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5267 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5268 | xmlXPathFreeObject(arg); |
| 5269 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5270 | CAST_TO_NUMBER; |
| 5271 | CHECK_TYPE(XPATH_NUMBER); |
| 5272 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | /** |
| 5276 | * xmlXPathMultValues: |
| 5277 | * @ctxt: the XPath Parser context |
| 5278 | * |
| 5279 | * Implement the multiply operation on XPath objects: |
| 5280 | * The numeric operators convert their operands to numbers as if |
| 5281 | * by calling the number function. |
| 5282 | */ |
| 5283 | void |
| 5284 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5285 | xmlXPathObjectPtr arg; |
| 5286 | double val; |
| 5287 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5288 | arg = valuePop(ctxt); |
| 5289 | if (arg == NULL) |
| 5290 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5291 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5292 | xmlXPathFreeObject(arg); |
| 5293 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5294 | CAST_TO_NUMBER; |
| 5295 | CHECK_TYPE(XPATH_NUMBER); |
| 5296 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * xmlXPathDivValues: |
| 5301 | * @ctxt: the XPath Parser context |
| 5302 | * |
| 5303 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5304 | * The numeric operators convert their operands to numbers as if |
| 5305 | * by calling the number function. |
| 5306 | */ |
| 5307 | void |
| 5308 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5309 | xmlXPathObjectPtr arg; |
| 5310 | double val; |
| 5311 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5312 | arg = valuePop(ctxt); |
| 5313 | if (arg == NULL) |
| 5314 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5315 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5316 | xmlXPathFreeObject(arg); |
| 5317 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5318 | CAST_TO_NUMBER; |
| 5319 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5320 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5321 | ctxt->value->floatval = xmlXPathNAN; |
| 5322 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5323 | if (ctxt->value->floatval == 0) |
| 5324 | ctxt->value->floatval = xmlXPathNAN; |
| 5325 | else if (ctxt->value->floatval > 0) |
| 5326 | ctxt->value->floatval = xmlXPathNINF; |
| 5327 | else if (ctxt->value->floatval < 0) |
| 5328 | ctxt->value->floatval = xmlXPathPINF; |
| 5329 | } |
| 5330 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5331 | if (ctxt->value->floatval == 0) |
| 5332 | ctxt->value->floatval = xmlXPathNAN; |
| 5333 | else if (ctxt->value->floatval > 0) |
| 5334 | ctxt->value->floatval = xmlXPathPINF; |
| 5335 | else if (ctxt->value->floatval < 0) |
| 5336 | ctxt->value->floatval = xmlXPathNINF; |
| 5337 | } else |
| 5338 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | /** |
| 5342 | * xmlXPathModValues: |
| 5343 | * @ctxt: the XPath Parser context |
| 5344 | * |
| 5345 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5346 | * The numeric operators convert their operands to numbers as if |
| 5347 | * by calling the number function. |
| 5348 | */ |
| 5349 | void |
| 5350 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5351 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5352 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5353 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5354 | arg = valuePop(ctxt); |
| 5355 | if (arg == NULL) |
| 5356 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5357 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5358 | xmlXPathFreeObject(arg); |
| 5359 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5360 | CAST_TO_NUMBER; |
| 5361 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5362 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5363 | if (arg2 == 0) |
| 5364 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5365 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5366 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5367 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | /************************************************************************ |
| 5371 | * * |
| 5372 | * The traversal functions * |
| 5373 | * * |
| 5374 | ************************************************************************/ |
| 5375 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5376 | /* |
| 5377 | * A traversal function enumerates nodes along an axis. |
| 5378 | * Initially it must be called with NULL, and it indicates |
| 5379 | * termination on the axis by returning NULL. |
| 5380 | */ |
| 5381 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5382 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5383 | |
| 5384 | /** |
| 5385 | * xmlXPathNextSelf: |
| 5386 | * @ctxt: the XPath Parser context |
| 5387 | * @cur: the current node in the traversal |
| 5388 | * |
| 5389 | * Traversal function for the "self" direction |
| 5390 | * The self axis contains just the context node itself |
| 5391 | * |
| 5392 | * Returns the next element following that axis |
| 5393 | */ |
| 5394 | xmlNodePtr |
| 5395 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5396 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5397 | if (cur == NULL) |
| 5398 | return(ctxt->context->node); |
| 5399 | return(NULL); |
| 5400 | } |
| 5401 | |
| 5402 | /** |
| 5403 | * xmlXPathNextChild: |
| 5404 | * @ctxt: the XPath Parser context |
| 5405 | * @cur: the current node in the traversal |
| 5406 | * |
| 5407 | * Traversal function for the "child" direction |
| 5408 | * The child axis contains the children of the context node in document order. |
| 5409 | * |
| 5410 | * Returns the next element following that axis |
| 5411 | */ |
| 5412 | xmlNodePtr |
| 5413 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5414 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5415 | if (cur == NULL) { |
| 5416 | if (ctxt->context->node == NULL) return(NULL); |
| 5417 | switch (ctxt->context->node->type) { |
| 5418 | case XML_ELEMENT_NODE: |
| 5419 | case XML_TEXT_NODE: |
| 5420 | case XML_CDATA_SECTION_NODE: |
| 5421 | case XML_ENTITY_REF_NODE: |
| 5422 | case XML_ENTITY_NODE: |
| 5423 | case XML_PI_NODE: |
| 5424 | case XML_COMMENT_NODE: |
| 5425 | case XML_NOTATION_NODE: |
| 5426 | case XML_DTD_NODE: |
| 5427 | return(ctxt->context->node->children); |
| 5428 | case XML_DOCUMENT_NODE: |
| 5429 | case XML_DOCUMENT_TYPE_NODE: |
| 5430 | case XML_DOCUMENT_FRAG_NODE: |
| 5431 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5432 | #ifdef LIBXML_DOCB_ENABLED |
| 5433 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | #endif |
| 5435 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5436 | case XML_ELEMENT_DECL: |
| 5437 | case XML_ATTRIBUTE_DECL: |
| 5438 | case XML_ENTITY_DECL: |
| 5439 | case XML_ATTRIBUTE_NODE: |
| 5440 | case XML_NAMESPACE_DECL: |
| 5441 | case XML_XINCLUDE_START: |
| 5442 | case XML_XINCLUDE_END: |
| 5443 | return(NULL); |
| 5444 | } |
| 5445 | return(NULL); |
| 5446 | } |
| 5447 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5448 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5449 | return(NULL); |
| 5450 | return(cur->next); |
| 5451 | } |
| 5452 | |
| 5453 | /** |
| 5454 | * xmlXPathNextDescendant: |
| 5455 | * @ctxt: the XPath Parser context |
| 5456 | * @cur: the current node in the traversal |
| 5457 | * |
| 5458 | * Traversal function for the "descendant" direction |
| 5459 | * the descendant axis contains the descendants of the context node in document |
| 5460 | * order; a descendant is a child or a child of a child and so on. |
| 5461 | * |
| 5462 | * Returns the next element following that axis |
| 5463 | */ |
| 5464 | xmlNodePtr |
| 5465 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5466 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5467 | if (cur == NULL) { |
| 5468 | if (ctxt->context->node == NULL) |
| 5469 | return(NULL); |
| 5470 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5471 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5472 | return(NULL); |
| 5473 | |
| 5474 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5475 | return(ctxt->context->doc->children); |
| 5476 | return(ctxt->context->node->children); |
| 5477 | } |
| 5478 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5479 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5480 | /* |
| 5481 | * Do not descend on entities declarations |
| 5482 | */ |
| 5483 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5484 | cur = cur->children; |
| 5485 | /* |
| 5486 | * Skip DTDs |
| 5487 | */ |
| 5488 | if (cur->type != XML_DTD_NODE) |
| 5489 | return(cur); |
| 5490 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | if (cur == ctxt->context->node) return(NULL); |
| 5494 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5495 | while (cur->next != NULL) { |
| 5496 | cur = cur->next; |
| 5497 | if ((cur->type != XML_ENTITY_DECL) && |
| 5498 | (cur->type != XML_DTD_NODE)) |
| 5499 | return(cur); |
| 5500 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5501 | |
| 5502 | do { |
| 5503 | cur = cur->parent; |
| 5504 | if (cur == NULL) return(NULL); |
| 5505 | if (cur == ctxt->context->node) return(NULL); |
| 5506 | if (cur->next != NULL) { |
| 5507 | cur = cur->next; |
| 5508 | return(cur); |
| 5509 | } |
| 5510 | } while (cur != NULL); |
| 5511 | return(cur); |
| 5512 | } |
| 5513 | |
| 5514 | /** |
| 5515 | * xmlXPathNextDescendantOrSelf: |
| 5516 | * @ctxt: the XPath Parser context |
| 5517 | * @cur: the current node in the traversal |
| 5518 | * |
| 5519 | * Traversal function for the "descendant-or-self" direction |
| 5520 | * the descendant-or-self axis contains the context node and the descendants |
| 5521 | * of the context node in document order; thus the context node is the first |
| 5522 | * node on the axis, and the first child of the context node is the second node |
| 5523 | * on the axis |
| 5524 | * |
| 5525 | * Returns the next element following that axis |
| 5526 | */ |
| 5527 | xmlNodePtr |
| 5528 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5529 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5530 | if (cur == NULL) { |
| 5531 | if (ctxt->context->node == NULL) |
| 5532 | return(NULL); |
| 5533 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5534 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5535 | return(NULL); |
| 5536 | return(ctxt->context->node); |
| 5537 | } |
| 5538 | |
| 5539 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5540 | } |
| 5541 | |
| 5542 | /** |
| 5543 | * xmlXPathNextParent: |
| 5544 | * @ctxt: the XPath Parser context |
| 5545 | * @cur: the current node in the traversal |
| 5546 | * |
| 5547 | * Traversal function for the "parent" direction |
| 5548 | * The parent axis contains the parent of the context node, if there is one. |
| 5549 | * |
| 5550 | * Returns the next element following that axis |
| 5551 | */ |
| 5552 | xmlNodePtr |
| 5553 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5554 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5555 | /* |
| 5556 | * the parent of an attribute or namespace node is the element |
| 5557 | * to which the attribute or namespace node is attached |
| 5558 | * Namespace handling !!! |
| 5559 | */ |
| 5560 | if (cur == NULL) { |
| 5561 | if (ctxt->context->node == NULL) return(NULL); |
| 5562 | switch (ctxt->context->node->type) { |
| 5563 | case XML_ELEMENT_NODE: |
| 5564 | case XML_TEXT_NODE: |
| 5565 | case XML_CDATA_SECTION_NODE: |
| 5566 | case XML_ENTITY_REF_NODE: |
| 5567 | case XML_ENTITY_NODE: |
| 5568 | case XML_PI_NODE: |
| 5569 | case XML_COMMENT_NODE: |
| 5570 | case XML_NOTATION_NODE: |
| 5571 | case XML_DTD_NODE: |
| 5572 | case XML_ELEMENT_DECL: |
| 5573 | case XML_ATTRIBUTE_DECL: |
| 5574 | case XML_XINCLUDE_START: |
| 5575 | case XML_XINCLUDE_END: |
| 5576 | case XML_ENTITY_DECL: |
| 5577 | if (ctxt->context->node->parent == NULL) |
| 5578 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5579 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5580 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5581 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5582 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5583 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5584 | return(ctxt->context->node->parent); |
| 5585 | case XML_ATTRIBUTE_NODE: { |
| 5586 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5587 | |
| 5588 | return(att->parent); |
| 5589 | } |
| 5590 | case XML_DOCUMENT_NODE: |
| 5591 | case XML_DOCUMENT_TYPE_NODE: |
| 5592 | case XML_DOCUMENT_FRAG_NODE: |
| 5593 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5594 | #ifdef LIBXML_DOCB_ENABLED |
| 5595 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | #endif |
| 5597 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5598 | case XML_NAMESPACE_DECL: { |
| 5599 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5600 | |
| 5601 | if ((ns->next != NULL) && |
| 5602 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5603 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5604 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5605 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | } |
| 5607 | } |
| 5608 | return(NULL); |
| 5609 | } |
| 5610 | |
| 5611 | /** |
| 5612 | * xmlXPathNextAncestor: |
| 5613 | * @ctxt: the XPath Parser context |
| 5614 | * @cur: the current node in the traversal |
| 5615 | * |
| 5616 | * Traversal function for the "ancestor" direction |
| 5617 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5618 | * of the context node consist of the parent of context node and the parent's |
| 5619 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5620 | * parent is the first node on the axis, and the parent's parent is the second |
| 5621 | * node on the axis |
| 5622 | * |
| 5623 | * Returns the next element following that axis |
| 5624 | */ |
| 5625 | xmlNodePtr |
| 5626 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5627 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | /* |
| 5629 | * the parent of an attribute or namespace node is the element |
| 5630 | * to which the attribute or namespace node is attached |
| 5631 | * !!!!!!!!!!!!! |
| 5632 | */ |
| 5633 | if (cur == NULL) { |
| 5634 | if (ctxt->context->node == NULL) return(NULL); |
| 5635 | switch (ctxt->context->node->type) { |
| 5636 | case XML_ELEMENT_NODE: |
| 5637 | case XML_TEXT_NODE: |
| 5638 | case XML_CDATA_SECTION_NODE: |
| 5639 | case XML_ENTITY_REF_NODE: |
| 5640 | case XML_ENTITY_NODE: |
| 5641 | case XML_PI_NODE: |
| 5642 | case XML_COMMENT_NODE: |
| 5643 | case XML_DTD_NODE: |
| 5644 | case XML_ELEMENT_DECL: |
| 5645 | case XML_ATTRIBUTE_DECL: |
| 5646 | case XML_ENTITY_DECL: |
| 5647 | case XML_NOTATION_NODE: |
| 5648 | case XML_XINCLUDE_START: |
| 5649 | case XML_XINCLUDE_END: |
| 5650 | if (ctxt->context->node->parent == NULL) |
| 5651 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5652 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5653 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5654 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5655 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5656 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | return(ctxt->context->node->parent); |
| 5658 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5659 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5660 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5661 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5662 | } |
| 5663 | case XML_DOCUMENT_NODE: |
| 5664 | case XML_DOCUMENT_TYPE_NODE: |
| 5665 | case XML_DOCUMENT_FRAG_NODE: |
| 5666 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5667 | #ifdef LIBXML_DOCB_ENABLED |
| 5668 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | #endif |
| 5670 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5671 | case XML_NAMESPACE_DECL: { |
| 5672 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5673 | |
| 5674 | if ((ns->next != NULL) && |
| 5675 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5676 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5677 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5678 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5679 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5680 | } |
| 5681 | return(NULL); |
| 5682 | } |
| 5683 | if (cur == ctxt->context->doc->children) |
| 5684 | return((xmlNodePtr) ctxt->context->doc); |
| 5685 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5686 | return(NULL); |
| 5687 | switch (cur->type) { |
| 5688 | case XML_ELEMENT_NODE: |
| 5689 | case XML_TEXT_NODE: |
| 5690 | case XML_CDATA_SECTION_NODE: |
| 5691 | case XML_ENTITY_REF_NODE: |
| 5692 | case XML_ENTITY_NODE: |
| 5693 | case XML_PI_NODE: |
| 5694 | case XML_COMMENT_NODE: |
| 5695 | case XML_NOTATION_NODE: |
| 5696 | case XML_DTD_NODE: |
| 5697 | case XML_ELEMENT_DECL: |
| 5698 | case XML_ATTRIBUTE_DECL: |
| 5699 | case XML_ENTITY_DECL: |
| 5700 | case XML_XINCLUDE_START: |
| 5701 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5702 | if (cur->parent == NULL) |
| 5703 | return(NULL); |
| 5704 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5705 | ((cur->parent->name[0] == ' ') || |
| 5706 | (xmlStrEqual(cur->parent->name, |
| 5707 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5708 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5709 | return(cur->parent); |
| 5710 | case XML_ATTRIBUTE_NODE: { |
| 5711 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5712 | |
| 5713 | return(att->parent); |
| 5714 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5715 | case XML_NAMESPACE_DECL: { |
| 5716 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5717 | |
| 5718 | if ((ns->next != NULL) && |
| 5719 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5720 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5721 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5722 | return(NULL); |
| 5723 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5724 | case XML_DOCUMENT_NODE: |
| 5725 | case XML_DOCUMENT_TYPE_NODE: |
| 5726 | case XML_DOCUMENT_FRAG_NODE: |
| 5727 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5728 | #ifdef LIBXML_DOCB_ENABLED |
| 5729 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5730 | #endif |
| 5731 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5732 | } |
| 5733 | return(NULL); |
| 5734 | } |
| 5735 | |
| 5736 | /** |
| 5737 | * xmlXPathNextAncestorOrSelf: |
| 5738 | * @ctxt: the XPath Parser context |
| 5739 | * @cur: the current node in the traversal |
| 5740 | * |
| 5741 | * Traversal function for the "ancestor-or-self" direction |
| 5742 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5743 | * the context node in reverse document order; thus the context node is |
| 5744 | * the first node on the axis, and the context node's parent the second; |
| 5745 | * parent here is defined the same as with the parent axis. |
| 5746 | * |
| 5747 | * Returns the next element following that axis |
| 5748 | */ |
| 5749 | xmlNodePtr |
| 5750 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5751 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5752 | if (cur == NULL) |
| 5753 | return(ctxt->context->node); |
| 5754 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5755 | } |
| 5756 | |
| 5757 | /** |
| 5758 | * xmlXPathNextFollowingSibling: |
| 5759 | * @ctxt: the XPath Parser context |
| 5760 | * @cur: the current node in the traversal |
| 5761 | * |
| 5762 | * Traversal function for the "following-sibling" direction |
| 5763 | * The following-sibling axis contains the following siblings of the context |
| 5764 | * node in document order. |
| 5765 | * |
| 5766 | * Returns the next element following that axis |
| 5767 | */ |
| 5768 | xmlNodePtr |
| 5769 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5770 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5771 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5772 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5773 | return(NULL); |
| 5774 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5775 | return(NULL); |
| 5776 | if (cur == NULL) |
| 5777 | return(ctxt->context->node->next); |
| 5778 | return(cur->next); |
| 5779 | } |
| 5780 | |
| 5781 | /** |
| 5782 | * xmlXPathNextPrecedingSibling: |
| 5783 | * @ctxt: the XPath Parser context |
| 5784 | * @cur: the current node in the traversal |
| 5785 | * |
| 5786 | * Traversal function for the "preceding-sibling" direction |
| 5787 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5788 | * node in reverse document order; the first preceding sibling is first on the |
| 5789 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5790 | * |
| 5791 | * Returns the next element following that axis |
| 5792 | */ |
| 5793 | xmlNodePtr |
| 5794 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5795 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5796 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5797 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5798 | return(NULL); |
| 5799 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5800 | return(NULL); |
| 5801 | if (cur == NULL) |
| 5802 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5803 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5804 | cur = cur->prev; |
| 5805 | if (cur == NULL) |
| 5806 | return(ctxt->context->node->prev); |
| 5807 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5808 | return(cur->prev); |
| 5809 | } |
| 5810 | |
| 5811 | /** |
| 5812 | * xmlXPathNextFollowing: |
| 5813 | * @ctxt: the XPath Parser context |
| 5814 | * @cur: the current node in the traversal |
| 5815 | * |
| 5816 | * Traversal function for the "following" direction |
| 5817 | * The following axis contains all nodes in the same document as the context |
| 5818 | * node that are after the context node in document order, excluding any |
| 5819 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5820 | * are ordered in document order |
| 5821 | * |
| 5822 | * Returns the next element following that axis |
| 5823 | */ |
| 5824 | xmlNodePtr |
| 5825 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5826 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5827 | if (cur != NULL && cur->children != NULL) |
| 5828 | return cur->children ; |
| 5829 | if (cur == NULL) cur = ctxt->context->node; |
| 5830 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5831 | if (cur->next != NULL) return(cur->next) ; |
| 5832 | do { |
| 5833 | cur = cur->parent; |
| 5834 | if (cur == NULL) return(NULL); |
| 5835 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5836 | if (cur->next != NULL) return(cur->next); |
| 5837 | } while (cur != NULL); |
| 5838 | return(cur); |
| 5839 | } |
| 5840 | |
| 5841 | /* |
| 5842 | * xmlXPathIsAncestor: |
| 5843 | * @ancestor: the ancestor node |
| 5844 | * @node: the current node |
| 5845 | * |
| 5846 | * Check that @ancestor is a @node's ancestor |
| 5847 | * |
| 5848 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5849 | */ |
| 5850 | static int |
| 5851 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5852 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5853 | /* nodes need to be in the same document */ |
| 5854 | if (ancestor->doc != node->doc) return(0); |
| 5855 | /* avoid searching if ancestor or node is the root node */ |
| 5856 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5857 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5858 | while (node->parent != NULL) { |
| 5859 | if (node->parent == ancestor) |
| 5860 | return(1); |
| 5861 | node = node->parent; |
| 5862 | } |
| 5863 | return(0); |
| 5864 | } |
| 5865 | |
| 5866 | /** |
| 5867 | * xmlXPathNextPreceding: |
| 5868 | * @ctxt: the XPath Parser context |
| 5869 | * @cur: the current node in the traversal |
| 5870 | * |
| 5871 | * Traversal function for the "preceding" direction |
| 5872 | * the preceding axis contains all nodes in the same document as the context |
| 5873 | * node that are before the context node in document order, excluding any |
| 5874 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5875 | * ordered in reverse document order |
| 5876 | * |
| 5877 | * Returns the next element following that axis |
| 5878 | */ |
| 5879 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5880 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5881 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5882 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5883 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5884 | cur = ctxt->context->node; |
| 5885 | if (cur == NULL) |
| 5886 | return (NULL); |
| 5887 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5888 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5889 | do { |
| 5890 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5891 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5892 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5896 | if (cur == NULL) |
| 5897 | return (NULL); |
| 5898 | if (cur == ctxt->context->doc->children) |
| 5899 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5900 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5901 | return (cur); |
| 5902 | } |
| 5903 | |
| 5904 | /** |
| 5905 | * xmlXPathNextPrecedingInternal: |
| 5906 | * @ctxt: the XPath Parser context |
| 5907 | * @cur: the current node in the traversal |
| 5908 | * |
| 5909 | * Traversal function for the "preceding" direction |
| 5910 | * the preceding axis contains all nodes in the same document as the context |
| 5911 | * node that are before the context node in document order, excluding any |
| 5912 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5913 | * ordered in reverse document order |
| 5914 | * This is a faster implementation but internal only since it requires a |
| 5915 | * state kept in the parser context: ctxt->ancestor. |
| 5916 | * |
| 5917 | * Returns the next element following that axis |
| 5918 | */ |
| 5919 | static xmlNodePtr |
| 5920 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5921 | xmlNodePtr cur) |
| 5922 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5923 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5924 | if (cur == NULL) { |
| 5925 | cur = ctxt->context->node; |
| 5926 | if (cur == NULL) |
| 5927 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 5928 | if (cur->type == XML_NAMESPACE_DECL) |
| 5929 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5930 | ctxt->ancestor = cur->parent; |
| 5931 | } |
| 5932 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5933 | cur = cur->prev; |
| 5934 | while (cur->prev == NULL) { |
| 5935 | cur = cur->parent; |
| 5936 | if (cur == NULL) |
| 5937 | return (NULL); |
| 5938 | if (cur == ctxt->context->doc->children) |
| 5939 | return (NULL); |
| 5940 | if (cur != ctxt->ancestor) |
| 5941 | return (cur); |
| 5942 | ctxt->ancestor = cur->parent; |
| 5943 | } |
| 5944 | cur = cur->prev; |
| 5945 | while (cur->last != NULL) |
| 5946 | cur = cur->last; |
| 5947 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | /** |
| 5951 | * xmlXPathNextNamespace: |
| 5952 | * @ctxt: the XPath Parser context |
| 5953 | * @cur: the current attribute in the traversal |
| 5954 | * |
| 5955 | * Traversal function for the "namespace" direction |
| 5956 | * the namespace axis contains the namespace nodes of the context node; |
| 5957 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5958 | * be empty unless the context node is an element |
| 5959 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5960 | * We keep the XML namespace node at the end of the list. |
| 5961 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5962 | * Returns the next element following that axis |
| 5963 | */ |
| 5964 | xmlNodePtr |
| 5965 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5966 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5968 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5969 | if (ctxt->context->tmpNsList != NULL) |
| 5970 | xmlFree(ctxt->context->tmpNsList); |
| 5971 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5972 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5973 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5974 | if (ctxt->context->tmpNsList != NULL) { |
| 5975 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 5976 | ctxt->context->tmpNsNr++; |
| 5977 | } |
| 5978 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5979 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5980 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5981 | if (ctxt->context->tmpNsNr > 0) { |
| 5982 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 5983 | } else { |
| 5984 | if (ctxt->context->tmpNsList != NULL) |
| 5985 | xmlFree(ctxt->context->tmpNsList); |
| 5986 | ctxt->context->tmpNsList = NULL; |
| 5987 | return(NULL); |
| 5988 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | /** |
| 5992 | * xmlXPathNextAttribute: |
| 5993 | * @ctxt: the XPath Parser context |
| 5994 | * @cur: the current attribute in the traversal |
| 5995 | * |
| 5996 | * Traversal function for the "attribute" direction |
| 5997 | * TODO: support DTD inherited default attributes |
| 5998 | * |
| 5999 | * Returns the next element following that axis |
| 6000 | */ |
| 6001 | xmlNodePtr |
| 6002 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6003 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6004 | if (ctxt->context->node == NULL) |
| 6005 | return(NULL); |
| 6006 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 6007 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6008 | if (cur == NULL) { |
| 6009 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 6010 | return(NULL); |
| 6011 | return((xmlNodePtr)ctxt->context->node->properties); |
| 6012 | } |
| 6013 | return((xmlNodePtr)cur->next); |
| 6014 | } |
| 6015 | |
| 6016 | /************************************************************************ |
| 6017 | * * |
| 6018 | * NodeTest Functions * |
| 6019 | * * |
| 6020 | ************************************************************************/ |
| 6021 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6022 | #define IS_FUNCTION 200 |
| 6023 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6024 | |
| 6025 | /************************************************************************ |
| 6026 | * * |
| 6027 | * Implicit tree core function library * |
| 6028 | * * |
| 6029 | ************************************************************************/ |
| 6030 | |
| 6031 | /** |
| 6032 | * xmlXPathRoot: |
| 6033 | * @ctxt: the XPath Parser context |
| 6034 | * |
| 6035 | * Initialize the context to the root of the document |
| 6036 | */ |
| 6037 | void |
| 6038 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6039 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6040 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 6041 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6042 | } |
| 6043 | |
| 6044 | /************************************************************************ |
| 6045 | * * |
| 6046 | * The explicit core function library * |
| 6047 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 6048 | * * |
| 6049 | ************************************************************************/ |
| 6050 | |
| 6051 | |
| 6052 | /** |
| 6053 | * xmlXPathLastFunction: |
| 6054 | * @ctxt: the XPath Parser context |
| 6055 | * @nargs: the number of arguments |
| 6056 | * |
| 6057 | * Implement the last() XPath function |
| 6058 | * number last() |
| 6059 | * The last function returns the number of nodes in the context node list. |
| 6060 | */ |
| 6061 | void |
| 6062 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6063 | CHECK_ARITY(0); |
| 6064 | if (ctxt->context->contextSize >= 0) { |
| 6065 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 6066 | #ifdef DEBUG_EXPR |
| 6067 | xmlGenericError(xmlGenericErrorContext, |
| 6068 | "last() : %d\n", ctxt->context->contextSize); |
| 6069 | #endif |
| 6070 | } else { |
| 6071 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | /** |
| 6076 | * xmlXPathPositionFunction: |
| 6077 | * @ctxt: the XPath Parser context |
| 6078 | * @nargs: the number of arguments |
| 6079 | * |
| 6080 | * Implement the position() XPath function |
| 6081 | * number position() |
| 6082 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6083 | * 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] | 6084 | * will be equal to last(). |
| 6085 | */ |
| 6086 | void |
| 6087 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6088 | CHECK_ARITY(0); |
| 6089 | if (ctxt->context->proximityPosition >= 0) { |
| 6090 | valuePush(ctxt, |
| 6091 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 6092 | #ifdef DEBUG_EXPR |
| 6093 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 6094 | ctxt->context->proximityPosition); |
| 6095 | #endif |
| 6096 | } else { |
| 6097 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | /** |
| 6102 | * xmlXPathCountFunction: |
| 6103 | * @ctxt: the XPath Parser context |
| 6104 | * @nargs: the number of arguments |
| 6105 | * |
| 6106 | * Implement the count() XPath function |
| 6107 | * number count(node-set) |
| 6108 | */ |
| 6109 | void |
| 6110 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6111 | xmlXPathObjectPtr cur; |
| 6112 | |
| 6113 | CHECK_ARITY(1); |
| 6114 | if ((ctxt->value == NULL) || |
| 6115 | ((ctxt->value->type != XPATH_NODESET) && |
| 6116 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6117 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6118 | cur = valuePop(ctxt); |
| 6119 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6120 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 6121 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6122 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6123 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 6124 | } else { |
| 6125 | if ((cur->nodesetval->nodeNr != 1) || |
| 6126 | (cur->nodesetval->nodeTab == NULL)) { |
| 6127 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 6128 | } else { |
| 6129 | xmlNodePtr tmp; |
| 6130 | int i = 0; |
| 6131 | |
| 6132 | tmp = cur->nodesetval->nodeTab[0]; |
| 6133 | if (tmp != NULL) { |
| 6134 | tmp = tmp->children; |
| 6135 | while (tmp != NULL) { |
| 6136 | tmp = tmp->next; |
| 6137 | i++; |
| 6138 | } |
| 6139 | } |
| 6140 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6141 | } |
| 6142 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6143 | xmlXPathFreeObject(cur); |
| 6144 | } |
| 6145 | |
| 6146 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6147 | * xmlXPathGetElementsByIds: |
| 6148 | * @doc: the document |
| 6149 | * @ids: a whitespace separated list of IDs |
| 6150 | * |
| 6151 | * Selects elements by their unique ID. |
| 6152 | * |
| 6153 | * Returns a node-set of selected elements. |
| 6154 | */ |
| 6155 | static xmlNodeSetPtr |
| 6156 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6157 | xmlNodeSetPtr ret; |
| 6158 | const xmlChar *cur = ids; |
| 6159 | xmlChar *ID; |
| 6160 | xmlAttrPtr attr; |
| 6161 | xmlNodePtr elem = NULL; |
| 6162 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6163 | if (ids == NULL) return(NULL); |
| 6164 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6165 | ret = xmlXPathNodeSetCreate(NULL); |
| 6166 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6167 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6168 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6169 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6170 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6171 | |
| 6172 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6173 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 6174 | /* |
| 6175 | * We used to check the fact that the value passed |
| 6176 | * was an NCName, but this generated much troubles for |
| 6177 | * me and Aleksey Sanin, people blatantly violated that |
| 6178 | * constaint, like Visa3D spec. |
| 6179 | * if (xmlValidateNCName(ID, 1) == 0) |
| 6180 | */ |
| 6181 | attr = xmlGetID(doc, ID); |
| 6182 | if (attr != NULL) { |
| 6183 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6184 | elem = attr->parent; |
| 6185 | else if (attr->type == XML_ELEMENT_NODE) |
| 6186 | elem = (xmlNodePtr) attr; |
| 6187 | else |
| 6188 | elem = NULL; |
| 6189 | if (elem != NULL) |
| 6190 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6191 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6192 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6193 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6194 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6195 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6196 | ids = cur; |
| 6197 | } |
| 6198 | return(ret); |
| 6199 | } |
| 6200 | |
| 6201 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6202 | * xmlXPathIdFunction: |
| 6203 | * @ctxt: the XPath Parser context |
| 6204 | * @nargs: the number of arguments |
| 6205 | * |
| 6206 | * Implement the id() XPath function |
| 6207 | * node-set id(object) |
| 6208 | * The id function selects elements by their unique ID |
| 6209 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6210 | * then the result is the union of the result of applying id to the |
| 6211 | * string value of each of the nodes in the argument node-set. When the |
| 6212 | * argument to id is of any other type, the argument is converted to a |
| 6213 | * string as if by a call to the string function; the string is split |
| 6214 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6215 | * of characters matching the production S); the result is a node-set |
| 6216 | * containing the elements in the same document as the context node that |
| 6217 | * have a unique ID equal to any of the tokens in the list. |
| 6218 | */ |
| 6219 | void |
| 6220 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6221 | xmlChar *tokens; |
| 6222 | xmlNodeSetPtr ret; |
| 6223 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6224 | |
| 6225 | CHECK_ARITY(1); |
| 6226 | obj = valuePop(ctxt); |
| 6227 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6228 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6229 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6230 | int i; |
| 6231 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6232 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6234 | if (obj->nodesetval != NULL) { |
| 6235 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6236 | tokens = |
| 6237 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6238 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6239 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6240 | xmlXPathFreeNodeSet(ns); |
| 6241 | if (tokens != NULL) |
| 6242 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6243 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
| 6246 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6247 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6248 | return; |
| 6249 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6250 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6251 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6252 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6253 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6255 | xmlXPathFreeObject(obj); |
| 6256 | return; |
| 6257 | } |
| 6258 | |
| 6259 | /** |
| 6260 | * xmlXPathLocalNameFunction: |
| 6261 | * @ctxt: the XPath Parser context |
| 6262 | * @nargs: the number of arguments |
| 6263 | * |
| 6264 | * Implement the local-name() XPath function |
| 6265 | * string local-name(node-set?) |
| 6266 | * The local-name function returns a string containing the local part |
| 6267 | * of the name of the node in the argument node-set that is first in |
| 6268 | * document order. If the node-set is empty or the first node has no |
| 6269 | * name, an empty string is returned. If the argument is omitted it |
| 6270 | * defaults to the context node. |
| 6271 | */ |
| 6272 | void |
| 6273 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6274 | xmlXPathObjectPtr cur; |
| 6275 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6276 | if (ctxt == NULL) return; |
| 6277 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6278 | if (nargs == 0) { |
| 6279 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6280 | nargs = 1; |
| 6281 | } |
| 6282 | |
| 6283 | CHECK_ARITY(1); |
| 6284 | if ((ctxt->value == NULL) || |
| 6285 | ((ctxt->value->type != XPATH_NODESET) && |
| 6286 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6287 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6288 | cur = valuePop(ctxt); |
| 6289 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6290 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6291 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6292 | } else { |
| 6293 | int i = 0; /* Should be first in document order !!!!! */ |
| 6294 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6295 | case XML_ELEMENT_NODE: |
| 6296 | case XML_ATTRIBUTE_NODE: |
| 6297 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6298 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6299 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6300 | else |
| 6301 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6302 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6303 | break; |
| 6304 | case XML_NAMESPACE_DECL: |
| 6305 | valuePush(ctxt, xmlXPathNewString( |
| 6306 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6307 | break; |
| 6308 | default: |
| 6309 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6310 | } |
| 6311 | } |
| 6312 | xmlXPathFreeObject(cur); |
| 6313 | } |
| 6314 | |
| 6315 | /** |
| 6316 | * xmlXPathNamespaceURIFunction: |
| 6317 | * @ctxt: the XPath Parser context |
| 6318 | * @nargs: the number of arguments |
| 6319 | * |
| 6320 | * Implement the namespace-uri() XPath function |
| 6321 | * string namespace-uri(node-set?) |
| 6322 | * The namespace-uri function returns a string containing the |
| 6323 | * namespace URI of the expanded name of the node in the argument |
| 6324 | * node-set that is first in document order. If the node-set is empty, |
| 6325 | * the first node has no name, or the expanded name has no namespace |
| 6326 | * URI, an empty string is returned. If the argument is omitted it |
| 6327 | * defaults to the context node. |
| 6328 | */ |
| 6329 | void |
| 6330 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6331 | xmlXPathObjectPtr cur; |
| 6332 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6333 | if (ctxt == NULL) return; |
| 6334 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6335 | if (nargs == 0) { |
| 6336 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6337 | nargs = 1; |
| 6338 | } |
| 6339 | CHECK_ARITY(1); |
| 6340 | if ((ctxt->value == NULL) || |
| 6341 | ((ctxt->value->type != XPATH_NODESET) && |
| 6342 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6343 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6344 | cur = valuePop(ctxt); |
| 6345 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6346 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6347 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6348 | } else { |
| 6349 | int i = 0; /* Should be first in document order !!!!! */ |
| 6350 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6351 | case XML_ELEMENT_NODE: |
| 6352 | case XML_ATTRIBUTE_NODE: |
| 6353 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6354 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6355 | else |
| 6356 | valuePush(ctxt, xmlXPathNewString( |
| 6357 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6358 | break; |
| 6359 | default: |
| 6360 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6361 | } |
| 6362 | } |
| 6363 | xmlXPathFreeObject(cur); |
| 6364 | } |
| 6365 | |
| 6366 | /** |
| 6367 | * xmlXPathNameFunction: |
| 6368 | * @ctxt: the XPath Parser context |
| 6369 | * @nargs: the number of arguments |
| 6370 | * |
| 6371 | * Implement the name() XPath function |
| 6372 | * string name(node-set?) |
| 6373 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6374 | * 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] | 6375 | * order. The QName must represent the name with respect to the namespace |
| 6376 | * declarations in effect on the node whose name is being represented. |
| 6377 | * Typically, this will be the form in which the name occurred in the XML |
| 6378 | * source. This need not be the case if there are namespace declarations |
| 6379 | * in effect on the node that associate multiple prefixes with the same |
| 6380 | * namespace. However, an implementation may include information about |
| 6381 | * the original prefix in its representation of nodes; in this case, an |
| 6382 | * implementation can ensure that the returned string is always the same |
| 6383 | * as the QName used in the XML source. If the argument it omitted it |
| 6384 | * defaults to the context node. |
| 6385 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6386 | * returned. |
| 6387 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6388 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6389 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6390 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6391 | xmlXPathObjectPtr cur; |
| 6392 | |
| 6393 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6394 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6395 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6399 | if ((ctxt->value == NULL) || |
| 6400 | ((ctxt->value->type != XPATH_NODESET) && |
| 6401 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6402 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | cur = valuePop(ctxt); |
| 6404 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6405 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6406 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6407 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6408 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6409 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6410 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6411 | case XML_ELEMENT_NODE: |
| 6412 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6413 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6414 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6415 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6416 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6417 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6418 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6419 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6420 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6421 | xmlChar *fullname; |
| 6422 | |
| 6423 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6424 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6425 | NULL, 0); |
| 6426 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6427 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6428 | if (fullname == NULL) { |
| 6429 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6430 | } |
| 6431 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6432 | } |
| 6433 | break; |
| 6434 | default: |
| 6435 | valuePush(ctxt, |
| 6436 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6437 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6438 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6439 | } |
| 6440 | xmlXPathFreeObject(cur); |
| 6441 | } |
| 6442 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6443 | |
| 6444 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6445 | * xmlXPathStringFunction: |
| 6446 | * @ctxt: the XPath Parser context |
| 6447 | * @nargs: the number of arguments |
| 6448 | * |
| 6449 | * Implement the string() XPath function |
| 6450 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6451 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6452 | * - A node-set is converted to a string by returning the value of |
| 6453 | * the node in the node-set that is first in document order. |
| 6454 | * If the node-set is empty, an empty string is returned. |
| 6455 | * - A number is converted to a string as follows |
| 6456 | * + NaN is converted to the string NaN |
| 6457 | * + positive zero is converted to the string 0 |
| 6458 | * + negative zero is converted to the string 0 |
| 6459 | * + positive infinity is converted to the string Infinity |
| 6460 | * + negative infinity is converted to the string -Infinity |
| 6461 | * + if the number is an integer, the number is represented in |
| 6462 | * decimal form as a Number with no decimal point and no leading |
| 6463 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6464 | * + otherwise, the number is represented in decimal form as a |
| 6465 | * Number including a decimal point with at least one digit |
| 6466 | * before the decimal point and at least one digit after the |
| 6467 | * decimal point, preceded by a minus sign (-) if the number |
| 6468 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6469 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6470 | * before the decimal point; beyond the one required digit |
| 6471 | * after the decimal point there must be as many, but only as |
| 6472 | * many, more digits as are needed to uniquely distinguish the |
| 6473 | * number from all other IEEE 754 numeric values. |
| 6474 | * - The boolean false value is converted to the string false. |
| 6475 | * The boolean true value is converted to the string true. |
| 6476 | * |
| 6477 | * If the argument is omitted, it defaults to a node-set with the |
| 6478 | * context node as its only member. |
| 6479 | */ |
| 6480 | void |
| 6481 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6482 | xmlXPathObjectPtr cur; |
| 6483 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6484 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6485 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6486 | valuePush(ctxt, |
| 6487 | xmlXPathWrapString( |
| 6488 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6489 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
| 6492 | CHECK_ARITY(1); |
| 6493 | cur = valuePop(ctxt); |
| 6494 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6495 | cur = xmlXPathConvertString(cur); |
| 6496 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | /** |
| 6500 | * xmlXPathStringLengthFunction: |
| 6501 | * @ctxt: the XPath Parser context |
| 6502 | * @nargs: the number of arguments |
| 6503 | * |
| 6504 | * Implement the string-length() XPath function |
| 6505 | * number string-length(string?) |
| 6506 | * The string-length returns the number of characters in the string |
| 6507 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6508 | * the context node converted to a string, in other words the value |
| 6509 | * of the context node. |
| 6510 | */ |
| 6511 | void |
| 6512 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6513 | xmlXPathObjectPtr cur; |
| 6514 | |
| 6515 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6516 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 6517 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6518 | if (ctxt->context->node == NULL) { |
| 6519 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6520 | } else { |
| 6521 | xmlChar *content; |
| 6522 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6523 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6524 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6525 | xmlFree(content); |
| 6526 | } |
| 6527 | return; |
| 6528 | } |
| 6529 | CHECK_ARITY(1); |
| 6530 | CAST_TO_STRING; |
| 6531 | CHECK_TYPE(XPATH_STRING); |
| 6532 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6533 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6534 | xmlXPathFreeObject(cur); |
| 6535 | } |
| 6536 | |
| 6537 | /** |
| 6538 | * xmlXPathConcatFunction: |
| 6539 | * @ctxt: the XPath Parser context |
| 6540 | * @nargs: the number of arguments |
| 6541 | * |
| 6542 | * Implement the concat() XPath function |
| 6543 | * string concat(string, string, string*) |
| 6544 | * The concat function returns the concatenation of its arguments. |
| 6545 | */ |
| 6546 | void |
| 6547 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6548 | xmlXPathObjectPtr cur, newobj; |
| 6549 | xmlChar *tmp; |
| 6550 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6551 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6552 | if (nargs < 2) { |
| 6553 | CHECK_ARITY(2); |
| 6554 | } |
| 6555 | |
| 6556 | CAST_TO_STRING; |
| 6557 | cur = valuePop(ctxt); |
| 6558 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6559 | xmlXPathFreeObject(cur); |
| 6560 | return; |
| 6561 | } |
| 6562 | nargs--; |
| 6563 | |
| 6564 | while (nargs > 0) { |
| 6565 | CAST_TO_STRING; |
| 6566 | newobj = valuePop(ctxt); |
| 6567 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6568 | xmlXPathFreeObject(newobj); |
| 6569 | xmlXPathFreeObject(cur); |
| 6570 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6571 | } |
| 6572 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6573 | newobj->stringval = cur->stringval; |
| 6574 | cur->stringval = tmp; |
| 6575 | |
| 6576 | xmlXPathFreeObject(newobj); |
| 6577 | nargs--; |
| 6578 | } |
| 6579 | valuePush(ctxt, cur); |
| 6580 | } |
| 6581 | |
| 6582 | /** |
| 6583 | * xmlXPathContainsFunction: |
| 6584 | * @ctxt: the XPath Parser context |
| 6585 | * @nargs: the number of arguments |
| 6586 | * |
| 6587 | * Implement the contains() XPath function |
| 6588 | * boolean contains(string, string) |
| 6589 | * The contains function returns true if the first argument string |
| 6590 | * contains the second argument string, and otherwise returns false. |
| 6591 | */ |
| 6592 | void |
| 6593 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6594 | xmlXPathObjectPtr hay, needle; |
| 6595 | |
| 6596 | CHECK_ARITY(2); |
| 6597 | CAST_TO_STRING; |
| 6598 | CHECK_TYPE(XPATH_STRING); |
| 6599 | needle = valuePop(ctxt); |
| 6600 | CAST_TO_STRING; |
| 6601 | hay = valuePop(ctxt); |
| 6602 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6603 | xmlXPathFreeObject(hay); |
| 6604 | xmlXPathFreeObject(needle); |
| 6605 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6606 | } |
| 6607 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6608 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6609 | else |
| 6610 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6611 | xmlXPathFreeObject(hay); |
| 6612 | xmlXPathFreeObject(needle); |
| 6613 | } |
| 6614 | |
| 6615 | /** |
| 6616 | * xmlXPathStartsWithFunction: |
| 6617 | * @ctxt: the XPath Parser context |
| 6618 | * @nargs: the number of arguments |
| 6619 | * |
| 6620 | * Implement the starts-with() XPath function |
| 6621 | * boolean starts-with(string, string) |
| 6622 | * The starts-with function returns true if the first argument string |
| 6623 | * starts with the second argument string, and otherwise returns false. |
| 6624 | */ |
| 6625 | void |
| 6626 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6627 | xmlXPathObjectPtr hay, needle; |
| 6628 | int n; |
| 6629 | |
| 6630 | CHECK_ARITY(2); |
| 6631 | CAST_TO_STRING; |
| 6632 | CHECK_TYPE(XPATH_STRING); |
| 6633 | needle = valuePop(ctxt); |
| 6634 | CAST_TO_STRING; |
| 6635 | hay = valuePop(ctxt); |
| 6636 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6637 | xmlXPathFreeObject(hay); |
| 6638 | xmlXPathFreeObject(needle); |
| 6639 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6640 | } |
| 6641 | n = xmlStrlen(needle->stringval); |
| 6642 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6643 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6644 | else |
| 6645 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6646 | xmlXPathFreeObject(hay); |
| 6647 | xmlXPathFreeObject(needle); |
| 6648 | } |
| 6649 | |
| 6650 | /** |
| 6651 | * xmlXPathSubstringFunction: |
| 6652 | * @ctxt: the XPath Parser context |
| 6653 | * @nargs: the number of arguments |
| 6654 | * |
| 6655 | * Implement the substring() XPath function |
| 6656 | * string substring(string, number, number?) |
| 6657 | * The substring function returns the substring of the first argument |
| 6658 | * starting at the position specified in the second argument with |
| 6659 | * length specified in the third argument. For example, |
| 6660 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6661 | * specified, it returns the substring starting at the position specified |
| 6662 | * in the second argument and continuing to the end of the string. For |
| 6663 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6664 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6665 | * numeric position: the position of the first character is 1, the position |
| 6666 | * of the second character is 2 and so on. The returned substring contains |
| 6667 | * those characters for which the position of the character is greater than |
| 6668 | * or equal to the second argument and, if the third argument is specified, |
| 6669 | * less than the sum of the second and third arguments; the comparisons |
| 6670 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6671 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6672 | * - substring("12345", 0, 3) returns "12" |
| 6673 | * - substring("12345", 0 div 0, 3) returns "" |
| 6674 | * - substring("12345", 1, 0 div 0) returns "" |
| 6675 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6676 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6677 | */ |
| 6678 | void |
| 6679 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6680 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6681 | double le=0, in; |
| 6682 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6683 | xmlChar *ret; |
| 6684 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6685 | if (nargs < 2) { |
| 6686 | CHECK_ARITY(2); |
| 6687 | } |
| 6688 | if (nargs > 3) { |
| 6689 | CHECK_ARITY(3); |
| 6690 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6691 | /* |
| 6692 | * take care of possible last (position) argument |
| 6693 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6694 | if (nargs == 3) { |
| 6695 | CAST_TO_NUMBER; |
| 6696 | CHECK_TYPE(XPATH_NUMBER); |
| 6697 | len = valuePop(ctxt); |
| 6698 | le = len->floatval; |
| 6699 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6701 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6702 | CAST_TO_NUMBER; |
| 6703 | CHECK_TYPE(XPATH_NUMBER); |
| 6704 | start = valuePop(ctxt); |
| 6705 | in = start->floatval; |
| 6706 | xmlXPathFreeObject(start); |
| 6707 | CAST_TO_STRING; |
| 6708 | CHECK_TYPE(XPATH_STRING); |
| 6709 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6710 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6711 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6712 | /* |
| 6713 | * If last pos not present, calculate last position |
| 6714 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6715 | if (nargs != 3) { |
| 6716 | le = (double)m; |
| 6717 | if (in < 1.0) |
| 6718 | in = 1.0; |
| 6719 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6720 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6721 | /* Need to check for the special cases where either |
| 6722 | * the index is NaN, the length is NaN, or both |
| 6723 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6724 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6725 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6726 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6727 | * To meet the requirements of the spec, the arguments |
| 6728 | * must be converted to integer format before |
| 6729 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6730 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6731 | * First we go to integer form, rounding up |
| 6732 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6733 | */ |
| 6734 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6735 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6736 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6737 | if (xmlXPathIsInf(le) == 1) { |
| 6738 | l = m; |
| 6739 | if (i < 1) |
| 6740 | i = 1; |
| 6741 | } |
| 6742 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 6743 | l = 0; |
| 6744 | else { |
| 6745 | l = (int) le; |
| 6746 | if (((double)l)+0.5 <= le) l++; |
| 6747 | } |
| 6748 | |
| 6749 | /* Now we normalize inidices */ |
| 6750 | i -= 1; |
| 6751 | l += i; |
| 6752 | if (i < 0) |
| 6753 | i = 0; |
| 6754 | if (l > m) |
| 6755 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6756 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6757 | /* number of chars to copy */ |
| 6758 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6759 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6760 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6761 | } |
| 6762 | else { |
| 6763 | ret = NULL; |
| 6764 | } |
| 6765 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6766 | if (ret == NULL) |
| 6767 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6768 | else { |
| 6769 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6770 | xmlFree(ret); |
| 6771 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6772 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | xmlXPathFreeObject(str); |
| 6774 | } |
| 6775 | |
| 6776 | /** |
| 6777 | * xmlXPathSubstringBeforeFunction: |
| 6778 | * @ctxt: the XPath Parser context |
| 6779 | * @nargs: the number of arguments |
| 6780 | * |
| 6781 | * Implement the substring-before() XPath function |
| 6782 | * string substring-before(string, string) |
| 6783 | * The substring-before function returns the substring of the first |
| 6784 | * argument string that precedes the first occurrence of the second |
| 6785 | * argument string in the first argument string, or the empty string |
| 6786 | * if the first argument string does not contain the second argument |
| 6787 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6788 | */ |
| 6789 | void |
| 6790 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6791 | xmlXPathObjectPtr str; |
| 6792 | xmlXPathObjectPtr find; |
| 6793 | xmlBufferPtr target; |
| 6794 | const xmlChar *point; |
| 6795 | int offset; |
| 6796 | |
| 6797 | CHECK_ARITY(2); |
| 6798 | CAST_TO_STRING; |
| 6799 | find = valuePop(ctxt); |
| 6800 | CAST_TO_STRING; |
| 6801 | str = valuePop(ctxt); |
| 6802 | |
| 6803 | target = xmlBufferCreate(); |
| 6804 | if (target) { |
| 6805 | point = xmlStrstr(str->stringval, find->stringval); |
| 6806 | if (point) { |
| 6807 | offset = (int)(point - str->stringval); |
| 6808 | xmlBufferAdd(target, str->stringval, offset); |
| 6809 | } |
| 6810 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6811 | xmlBufferFree(target); |
| 6812 | } |
| 6813 | |
| 6814 | xmlXPathFreeObject(str); |
| 6815 | xmlXPathFreeObject(find); |
| 6816 | } |
| 6817 | |
| 6818 | /** |
| 6819 | * xmlXPathSubstringAfterFunction: |
| 6820 | * @ctxt: the XPath Parser context |
| 6821 | * @nargs: the number of arguments |
| 6822 | * |
| 6823 | * Implement the substring-after() XPath function |
| 6824 | * string substring-after(string, string) |
| 6825 | * The substring-after function returns the substring of the first |
| 6826 | * argument string that follows the first occurrence of the second |
| 6827 | * argument string in the first argument string, or the empty stringi |
| 6828 | * if the first argument string does not contain the second argument |
| 6829 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6830 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6831 | */ |
| 6832 | void |
| 6833 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6834 | xmlXPathObjectPtr str; |
| 6835 | xmlXPathObjectPtr find; |
| 6836 | xmlBufferPtr target; |
| 6837 | const xmlChar *point; |
| 6838 | int offset; |
| 6839 | |
| 6840 | CHECK_ARITY(2); |
| 6841 | CAST_TO_STRING; |
| 6842 | find = valuePop(ctxt); |
| 6843 | CAST_TO_STRING; |
| 6844 | str = valuePop(ctxt); |
| 6845 | |
| 6846 | target = xmlBufferCreate(); |
| 6847 | if (target) { |
| 6848 | point = xmlStrstr(str->stringval, find->stringval); |
| 6849 | if (point) { |
| 6850 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6851 | xmlBufferAdd(target, &str->stringval[offset], |
| 6852 | xmlStrlen(str->stringval) - offset); |
| 6853 | } |
| 6854 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6855 | xmlBufferFree(target); |
| 6856 | } |
| 6857 | |
| 6858 | xmlXPathFreeObject(str); |
| 6859 | xmlXPathFreeObject(find); |
| 6860 | } |
| 6861 | |
| 6862 | /** |
| 6863 | * xmlXPathNormalizeFunction: |
| 6864 | * @ctxt: the XPath Parser context |
| 6865 | * @nargs: the number of arguments |
| 6866 | * |
| 6867 | * Implement the normalize-space() XPath function |
| 6868 | * string normalize-space(string?) |
| 6869 | * The normalize-space function returns the argument string with white |
| 6870 | * space normalized by stripping leading and trailing whitespace |
| 6871 | * and replacing sequences of whitespace characters by a single |
| 6872 | * space. Whitespace characters are the same allowed by the S production |
| 6873 | * in XML. If the argument is omitted, it defaults to the context |
| 6874 | * node converted to a string, in other words the value of the context node. |
| 6875 | */ |
| 6876 | void |
| 6877 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6878 | xmlXPathObjectPtr obj = NULL; |
| 6879 | xmlChar *source = NULL; |
| 6880 | xmlBufferPtr target; |
| 6881 | xmlChar blank; |
| 6882 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6883 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6884 | if (nargs == 0) { |
| 6885 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6886 | valuePush(ctxt, |
| 6887 | xmlXPathWrapString( |
| 6888 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6889 | nargs = 1; |
| 6890 | } |
| 6891 | |
| 6892 | CHECK_ARITY(1); |
| 6893 | CAST_TO_STRING; |
| 6894 | CHECK_TYPE(XPATH_STRING); |
| 6895 | obj = valuePop(ctxt); |
| 6896 | source = obj->stringval; |
| 6897 | |
| 6898 | target = xmlBufferCreate(); |
| 6899 | if (target && source) { |
| 6900 | |
| 6901 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6902 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6903 | source++; |
| 6904 | |
| 6905 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6906 | blank = 0; |
| 6907 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6908 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6909 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6910 | } else { |
| 6911 | if (blank) { |
| 6912 | xmlBufferAdd(target, &blank, 1); |
| 6913 | blank = 0; |
| 6914 | } |
| 6915 | xmlBufferAdd(target, source, 1); |
| 6916 | } |
| 6917 | source++; |
| 6918 | } |
| 6919 | |
| 6920 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6921 | xmlBufferFree(target); |
| 6922 | } |
| 6923 | xmlXPathFreeObject(obj); |
| 6924 | } |
| 6925 | |
| 6926 | /** |
| 6927 | * xmlXPathTranslateFunction: |
| 6928 | * @ctxt: the XPath Parser context |
| 6929 | * @nargs: the number of arguments |
| 6930 | * |
| 6931 | * Implement the translate() XPath function |
| 6932 | * string translate(string, string, string) |
| 6933 | * The translate function returns the first argument string with |
| 6934 | * occurrences of characters in the second argument string replaced |
| 6935 | * by the character at the corresponding position in the third argument |
| 6936 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6937 | * BAr. If there is a character in the second argument string with no |
| 6938 | * character at a corresponding position in the third argument string |
| 6939 | * (because the second argument string is longer than the third argument |
| 6940 | * string), then occurrences of that character in the first argument |
| 6941 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6942 | * returns "AAA". If a character occurs more than once in second |
| 6943 | * argument string, then the first occurrence determines the replacement |
| 6944 | * character. If the third argument string is longer than the second |
| 6945 | * argument string, then excess characters are ignored. |
| 6946 | */ |
| 6947 | void |
| 6948 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6949 | xmlXPathObjectPtr str; |
| 6950 | xmlXPathObjectPtr from; |
| 6951 | xmlXPathObjectPtr to; |
| 6952 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6953 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6954 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 6955 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6956 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6957 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6958 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6959 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6960 | CAST_TO_STRING; |
| 6961 | to = valuePop(ctxt); |
| 6962 | CAST_TO_STRING; |
| 6963 | from = valuePop(ctxt); |
| 6964 | CAST_TO_STRING; |
| 6965 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6967 | target = xmlBufferCreate(); |
| 6968 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6969 | max = xmlUTF8Strlen(to->stringval); |
| 6970 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6971 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6972 | if (offset >= 0) { |
| 6973 | if (offset < max) { |
| 6974 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6975 | if (point) |
| 6976 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6977 | } |
| 6978 | } else |
| 6979 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6980 | |
| 6981 | /* Step to next character in input */ |
| 6982 | cptr++; |
| 6983 | if ( ch & 0x80 ) { |
| 6984 | /* if not simple ascii, verify proper format */ |
| 6985 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6986 | xmlGenericError(xmlGenericErrorContext, |
| 6987 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6988 | break; |
| 6989 | } |
| 6990 | /* then skip over remaining bytes for this char */ |
| 6991 | while ( (ch <<= 1) & 0x80 ) |
| 6992 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 6993 | xmlGenericError(xmlGenericErrorContext, |
| 6994 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6995 | break; |
| 6996 | } |
| 6997 | if (ch & 0x80) /* must have had error encountered */ |
| 6998 | break; |
| 6999 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7000 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7001 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7002 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7003 | xmlBufferFree(target); |
| 7004 | xmlXPathFreeObject(str); |
| 7005 | xmlXPathFreeObject(from); |
| 7006 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7007 | } |
| 7008 | |
| 7009 | /** |
| 7010 | * xmlXPathBooleanFunction: |
| 7011 | * @ctxt: the XPath Parser context |
| 7012 | * @nargs: the number of arguments |
| 7013 | * |
| 7014 | * Implement the boolean() XPath function |
| 7015 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7016 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7017 | * - a number is true if and only if it is neither positive or |
| 7018 | * negative zero nor NaN |
| 7019 | * - a node-set is true if and only if it is non-empty |
| 7020 | * - a string is true if and only if its length is non-zero |
| 7021 | */ |
| 7022 | void |
| 7023 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7024 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7025 | |
| 7026 | CHECK_ARITY(1); |
| 7027 | cur = valuePop(ctxt); |
| 7028 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7029 | cur = xmlXPathConvertBoolean(cur); |
| 7030 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | /** |
| 7034 | * xmlXPathNotFunction: |
| 7035 | * @ctxt: the XPath Parser context |
| 7036 | * @nargs: the number of arguments |
| 7037 | * |
| 7038 | * Implement the not() XPath function |
| 7039 | * boolean not(boolean) |
| 7040 | * The not function returns true if its argument is false, |
| 7041 | * and false otherwise. |
| 7042 | */ |
| 7043 | void |
| 7044 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7045 | CHECK_ARITY(1); |
| 7046 | CAST_TO_BOOLEAN; |
| 7047 | CHECK_TYPE(XPATH_BOOLEAN); |
| 7048 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 7049 | } |
| 7050 | |
| 7051 | /** |
| 7052 | * xmlXPathTrueFunction: |
| 7053 | * @ctxt: the XPath Parser context |
| 7054 | * @nargs: the number of arguments |
| 7055 | * |
| 7056 | * Implement the true() XPath function |
| 7057 | * boolean true() |
| 7058 | */ |
| 7059 | void |
| 7060 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7061 | CHECK_ARITY(0); |
| 7062 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7063 | } |
| 7064 | |
| 7065 | /** |
| 7066 | * xmlXPathFalseFunction: |
| 7067 | * @ctxt: the XPath Parser context |
| 7068 | * @nargs: the number of arguments |
| 7069 | * |
| 7070 | * Implement the false() XPath function |
| 7071 | * boolean false() |
| 7072 | */ |
| 7073 | void |
| 7074 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7075 | CHECK_ARITY(0); |
| 7076 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7077 | } |
| 7078 | |
| 7079 | /** |
| 7080 | * xmlXPathLangFunction: |
| 7081 | * @ctxt: the XPath Parser context |
| 7082 | * @nargs: the number of arguments |
| 7083 | * |
| 7084 | * Implement the lang() XPath function |
| 7085 | * boolean lang(string) |
| 7086 | * The lang function returns true or false depending on whether the |
| 7087 | * language of the context node as specified by xml:lang attributes |
| 7088 | * is the same as or is a sublanguage of the language specified by |
| 7089 | * the argument string. The language of the context node is determined |
| 7090 | * by the value of the xml:lang attribute on the context node, or, if |
| 7091 | * the context node has no xml:lang attribute, by the value of the |
| 7092 | * xml:lang attribute on the nearest ancestor of the context node that |
| 7093 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 7094 | * returns false. If there is such an attribute, then lang returns |
| 7095 | * true if the attribute value is equal to the argument ignoring case, |
| 7096 | * or if there is some suffix starting with - such that the attribute |
| 7097 | * value is equal to the argument ignoring that suffix of the attribute |
| 7098 | * value and ignoring case. |
| 7099 | */ |
| 7100 | void |
| 7101 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7102 | xmlXPathObjectPtr val = NULL; |
| 7103 | const xmlChar *theLang = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7104 | const xmlChar *lang; |
| 7105 | int ret = 0; |
| 7106 | int i; |
| 7107 | |
| 7108 | CHECK_ARITY(1); |
| 7109 | CAST_TO_STRING; |
| 7110 | CHECK_TYPE(XPATH_STRING); |
| 7111 | val = valuePop(ctxt); |
| 7112 | lang = val->stringval; |
| 7113 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 7114 | if ((theLang != NULL) && (lang != NULL)) { |
| 7115 | for (i = 0;lang[i] != 0;i++) |
| 7116 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 7117 | goto not_equal; |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7118 | if ((theLang[i] == 0) || (theLang[i] == '-')) |
| 7119 | ret = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7120 | } |
| 7121 | not_equal: |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7122 | if (theLang != NULL) |
| 7123 | xmlFree((void *)theLang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7124 | xmlXPathFreeObject(val); |
| 7125 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 7126 | } |
| 7127 | |
| 7128 | /** |
| 7129 | * xmlXPathNumberFunction: |
| 7130 | * @ctxt: the XPath Parser context |
| 7131 | * @nargs: the number of arguments |
| 7132 | * |
| 7133 | * Implement the number() XPath function |
| 7134 | * number number(object?) |
| 7135 | */ |
| 7136 | void |
| 7137 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7138 | xmlXPathObjectPtr cur; |
| 7139 | double res; |
| 7140 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7141 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7142 | if (nargs == 0) { |
| 7143 | if (ctxt->context->node == NULL) { |
| 7144 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 7145 | } else { |
| 7146 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 7147 | |
| 7148 | res = xmlXPathStringEvalNumber(content); |
| 7149 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 7150 | xmlFree(content); |
| 7151 | } |
| 7152 | return; |
| 7153 | } |
| 7154 | |
| 7155 | CHECK_ARITY(1); |
| 7156 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7157 | cur = xmlXPathConvertNumber(cur); |
| 7158 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | /** |
| 7162 | * xmlXPathSumFunction: |
| 7163 | * @ctxt: the XPath Parser context |
| 7164 | * @nargs: the number of arguments |
| 7165 | * |
| 7166 | * Implement the sum() XPath function |
| 7167 | * number sum(node-set) |
| 7168 | * The sum function returns the sum of the values of the nodes in |
| 7169 | * the argument node-set. |
| 7170 | */ |
| 7171 | void |
| 7172 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7173 | xmlXPathObjectPtr cur; |
| 7174 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7175 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7176 | |
| 7177 | CHECK_ARITY(1); |
| 7178 | if ((ctxt->value == NULL) || |
| 7179 | ((ctxt->value->type != XPATH_NODESET) && |
| 7180 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7181 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7182 | cur = valuePop(ctxt); |
| 7183 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7184 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7185 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7186 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7187 | } |
| 7188 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7189 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7190 | xmlXPathFreeObject(cur); |
| 7191 | } |
| 7192 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7193 | /* |
| 7194 | * To assure working code on multiple platforms, we want to only depend |
| 7195 | * upon the characteristic truncation of converting a floating point value |
| 7196 | * to an integer. Unfortunately, because of the different storage sizes |
| 7197 | * of our internal floating point value (double) and integer (int), we |
| 7198 | * can't directly convert (see bug 301162). This macro is a messy |
| 7199 | * 'workaround' |
| 7200 | */ |
| 7201 | #define XTRUNC(f, v) \ |
| 7202 | f = fmod((v), INT_MAX); \ |
| 7203 | f = (v) - (f) + (double)((int)(f)); |
| 7204 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7205 | /** |
| 7206 | * xmlXPathFloorFunction: |
| 7207 | * @ctxt: the XPath Parser context |
| 7208 | * @nargs: the number of arguments |
| 7209 | * |
| 7210 | * Implement the floor() XPath function |
| 7211 | * number floor(number) |
| 7212 | * The floor function returns the largest (closest to positive infinity) |
| 7213 | * number that is not greater than the argument and that is an integer. |
| 7214 | */ |
| 7215 | void |
| 7216 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7217 | double f; |
| 7218 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7219 | CHECK_ARITY(1); |
| 7220 | CAST_TO_NUMBER; |
| 7221 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7222 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7223 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7224 | if (f != ctxt->value->floatval) { |
| 7225 | if (ctxt->value->floatval > 0) |
| 7226 | ctxt->value->floatval = f; |
| 7227 | else |
| 7228 | ctxt->value->floatval = f - 1; |
| 7229 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
| 7232 | /** |
| 7233 | * xmlXPathCeilingFunction: |
| 7234 | * @ctxt: the XPath Parser context |
| 7235 | * @nargs: the number of arguments |
| 7236 | * |
| 7237 | * Implement the ceiling() XPath function |
| 7238 | * number ceiling(number) |
| 7239 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7240 | * number that is not less than the argument and that is an integer. |
| 7241 | */ |
| 7242 | void |
| 7243 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7244 | double f; |
| 7245 | |
| 7246 | CHECK_ARITY(1); |
| 7247 | CAST_TO_NUMBER; |
| 7248 | CHECK_TYPE(XPATH_NUMBER); |
| 7249 | |
| 7250 | #if 0 |
| 7251 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7252 | #else |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7253 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7254 | if (f != ctxt->value->floatval) { |
| 7255 | if (ctxt->value->floatval > 0) |
| 7256 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7257 | else { |
| 7258 | if (ctxt->value->floatval < 0 && f == 0) |
| 7259 | ctxt->value->floatval = xmlXPathNZERO; |
| 7260 | else |
| 7261 | ctxt->value->floatval = f; |
| 7262 | } |
| 7263 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7264 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | #endif |
| 7266 | } |
| 7267 | |
| 7268 | /** |
| 7269 | * xmlXPathRoundFunction: |
| 7270 | * @ctxt: the XPath Parser context |
| 7271 | * @nargs: the number of arguments |
| 7272 | * |
| 7273 | * Implement the round() XPath function |
| 7274 | * number round(number) |
| 7275 | * The round function returns the number that is closest to the |
| 7276 | * argument and that is an integer. If there are two such numbers, |
| 7277 | * then the one that is even is returned. |
| 7278 | */ |
| 7279 | void |
| 7280 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7281 | double f; |
| 7282 | |
| 7283 | CHECK_ARITY(1); |
| 7284 | CAST_TO_NUMBER; |
| 7285 | CHECK_TYPE(XPATH_NUMBER); |
| 7286 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7287 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7288 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7289 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7290 | (ctxt->value->floatval == 0.0)) |
| 7291 | return; |
| 7292 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7293 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7294 | if (ctxt->value->floatval < 0) { |
| 7295 | if (ctxt->value->floatval < f - 0.5) |
| 7296 | ctxt->value->floatval = f - 1; |
| 7297 | else |
| 7298 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7299 | if (ctxt->value->floatval == 0) |
| 7300 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7301 | } else { |
| 7302 | if (ctxt->value->floatval < f + 0.5) |
| 7303 | ctxt->value->floatval = f; |
| 7304 | else |
| 7305 | ctxt->value->floatval = f + 1; |
| 7306 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | /************************************************************************ |
| 7310 | * * |
| 7311 | * The Parser * |
| 7312 | * * |
| 7313 | ************************************************************************/ |
| 7314 | |
| 7315 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7316 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7317 | * implementation. |
| 7318 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7319 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7320 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7321 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7322 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7323 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7324 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7325 | |
| 7326 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7327 | * xmlXPathCurrentChar: |
| 7328 | * @ctxt: the XPath parser context |
| 7329 | * @cur: pointer to the beginning of the char |
| 7330 | * @len: pointer to the length of the char read |
| 7331 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7332 | * 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] | 7333 | * bytes in the input buffer. |
| 7334 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7335 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7336 | */ |
| 7337 | |
| 7338 | static int |
| 7339 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7340 | unsigned char c; |
| 7341 | unsigned int val; |
| 7342 | const xmlChar *cur; |
| 7343 | |
| 7344 | if (ctxt == NULL) |
| 7345 | return(0); |
| 7346 | cur = ctxt->cur; |
| 7347 | |
| 7348 | /* |
| 7349 | * We are supposed to handle UTF8, check it's valid |
| 7350 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7351 | * |
| 7352 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7353 | * 0000 0000-0000 007F 0xxxxxxx |
| 7354 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7355 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7356 | * |
| 7357 | * Check for the 0x110000 limit too |
| 7358 | */ |
| 7359 | c = *cur; |
| 7360 | if (c & 0x80) { |
| 7361 | if ((cur[1] & 0xc0) != 0x80) |
| 7362 | goto encoding_error; |
| 7363 | if ((c & 0xe0) == 0xe0) { |
| 7364 | |
| 7365 | if ((cur[2] & 0xc0) != 0x80) |
| 7366 | goto encoding_error; |
| 7367 | if ((c & 0xf0) == 0xf0) { |
| 7368 | if (((c & 0xf8) != 0xf0) || |
| 7369 | ((cur[3] & 0xc0) != 0x80)) |
| 7370 | goto encoding_error; |
| 7371 | /* 4-byte code */ |
| 7372 | *len = 4; |
| 7373 | val = (cur[0] & 0x7) << 18; |
| 7374 | val |= (cur[1] & 0x3f) << 12; |
| 7375 | val |= (cur[2] & 0x3f) << 6; |
| 7376 | val |= cur[3] & 0x3f; |
| 7377 | } else { |
| 7378 | /* 3-byte code */ |
| 7379 | *len = 3; |
| 7380 | val = (cur[0] & 0xf) << 12; |
| 7381 | val |= (cur[1] & 0x3f) << 6; |
| 7382 | val |= cur[2] & 0x3f; |
| 7383 | } |
| 7384 | } else { |
| 7385 | /* 2-byte code */ |
| 7386 | *len = 2; |
| 7387 | val = (cur[0] & 0x1f) << 6; |
| 7388 | val |= cur[1] & 0x3f; |
| 7389 | } |
| 7390 | if (!IS_CHAR(val)) { |
| 7391 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7392 | } |
| 7393 | return(val); |
| 7394 | } else { |
| 7395 | /* 1-byte code */ |
| 7396 | *len = 1; |
| 7397 | return((int) *cur); |
| 7398 | } |
| 7399 | encoding_error: |
| 7400 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7401 | * If we detect an UTF8 error that probably means that the |
| 7402 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7403 | * declaration header. Report the error and switch the encoding |
| 7404 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7405 | * encoding !) |
| 7406 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7407 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7408 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
| 7411 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7412 | * xmlXPathParseNCName: |
| 7413 | * @ctxt: the XPath Parser context |
| 7414 | * |
| 7415 | * parse an XML namespace non qualified name. |
| 7416 | * |
| 7417 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7418 | * |
| 7419 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7420 | * CombiningChar | Extender |
| 7421 | * |
| 7422 | * Returns the namespace name or NULL |
| 7423 | */ |
| 7424 | |
| 7425 | xmlChar * |
| 7426 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7427 | const xmlChar *in; |
| 7428 | xmlChar *ret; |
| 7429 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7430 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7431 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7432 | /* |
| 7433 | * Accelerator for simple ASCII names |
| 7434 | */ |
| 7435 | in = ctxt->cur; |
| 7436 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7437 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7438 | (*in == '_')) { |
| 7439 | in++; |
| 7440 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7441 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7442 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7443 | (*in == '_') || (*in == '.') || |
| 7444 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7445 | in++; |
| 7446 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7447 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7448 | (*in == '@') || (*in == '*')) { |
| 7449 | count = in - ctxt->cur; |
| 7450 | if (count == 0) |
| 7451 | return(NULL); |
| 7452 | ret = xmlStrndup(ctxt->cur, count); |
| 7453 | ctxt->cur = in; |
| 7454 | return(ret); |
| 7455 | } |
| 7456 | } |
| 7457 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7460 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7461 | /** |
| 7462 | * xmlXPathParseQName: |
| 7463 | * @ctxt: the XPath Parser context |
| 7464 | * @prefix: a xmlChar ** |
| 7465 | * |
| 7466 | * parse an XML qualified name |
| 7467 | * |
| 7468 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7469 | * |
| 7470 | * [NS 6] Prefix ::= NCName |
| 7471 | * |
| 7472 | * [NS 7] LocalPart ::= NCName |
| 7473 | * |
| 7474 | * Returns the function returns the local part, and prefix is updated |
| 7475 | * to get the Prefix if any. |
| 7476 | */ |
| 7477 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7478 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7479 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7480 | xmlChar *ret = NULL; |
| 7481 | |
| 7482 | *prefix = NULL; |
| 7483 | ret = xmlXPathParseNCName(ctxt); |
| 7484 | if (CUR == ':') { |
| 7485 | *prefix = ret; |
| 7486 | NEXT; |
| 7487 | ret = xmlXPathParseNCName(ctxt); |
| 7488 | } |
| 7489 | return(ret); |
| 7490 | } |
| 7491 | |
| 7492 | /** |
| 7493 | * xmlXPathParseName: |
| 7494 | * @ctxt: the XPath Parser context |
| 7495 | * |
| 7496 | * parse an XML name |
| 7497 | * |
| 7498 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7499 | * CombiningChar | Extender |
| 7500 | * |
| 7501 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7502 | * |
| 7503 | * Returns the namespace name or NULL |
| 7504 | */ |
| 7505 | |
| 7506 | xmlChar * |
| 7507 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7508 | const xmlChar *in; |
| 7509 | xmlChar *ret; |
| 7510 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7511 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7512 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7513 | /* |
| 7514 | * Accelerator for simple ASCII names |
| 7515 | */ |
| 7516 | in = ctxt->cur; |
| 7517 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7518 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7519 | (*in == '_') || (*in == ':')) { |
| 7520 | in++; |
| 7521 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7522 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7523 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7524 | (*in == '_') || (*in == '-') || |
| 7525 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7526 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7527 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7528 | count = in - ctxt->cur; |
| 7529 | ret = xmlStrndup(ctxt->cur, count); |
| 7530 | ctxt->cur = in; |
| 7531 | return(ret); |
| 7532 | } |
| 7533 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7534 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7535 | } |
| 7536 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7537 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7538 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7539 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7540 | int len = 0, l; |
| 7541 | int c; |
| 7542 | |
| 7543 | /* |
| 7544 | * Handler for more complex cases |
| 7545 | */ |
| 7546 | c = CUR_CHAR(l); |
| 7547 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7548 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7549 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7550 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7551 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7552 | return(NULL); |
| 7553 | } |
| 7554 | |
| 7555 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7556 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7557 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7558 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7559 | (IS_COMBINING(c)) || |
| 7560 | (IS_EXTENDER(c)))) { |
| 7561 | COPY_BUF(l,buf,len,c); |
| 7562 | NEXTL(l); |
| 7563 | c = CUR_CHAR(l); |
| 7564 | if (len >= XML_MAX_NAMELEN) { |
| 7565 | /* |
| 7566 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7567 | * for the processing speed. |
| 7568 | */ |
| 7569 | xmlChar *buffer; |
| 7570 | int max = len * 2; |
| 7571 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7572 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7573 | if (buffer == NULL) { |
| 7574 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7575 | } |
| 7576 | memcpy(buffer, buf, len); |
| 7577 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7578 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7579 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7580 | (IS_COMBINING(c)) || |
| 7581 | (IS_EXTENDER(c))) { |
| 7582 | if (len + 10 > max) { |
| 7583 | max *= 2; |
| 7584 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7585 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7586 | if (buffer == NULL) { |
| 7587 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7588 | } |
| 7589 | } |
| 7590 | COPY_BUF(l,buffer,len,c); |
| 7591 | NEXTL(l); |
| 7592 | c = CUR_CHAR(l); |
| 7593 | } |
| 7594 | buffer[len] = 0; |
| 7595 | return(buffer); |
| 7596 | } |
| 7597 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7598 | if (len == 0) |
| 7599 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7600 | return(xmlStrndup(buf, len)); |
| 7601 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7602 | |
| 7603 | #define MAX_FRAC 20 |
| 7604 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7605 | /* |
| 7606 | * These are used as divisors for the fractional part of a number. |
| 7607 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 7608 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 7609 | */ |
| 7610 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7611 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7612 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7613 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7614 | 100000000000000.0, |
| 7615 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7616 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7617 | }; |
| 7618 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7619 | /** |
| 7620 | * xmlXPathStringEvalNumber: |
| 7621 | * @str: A string to scan |
| 7622 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7623 | * [30a] Float ::= Number ('e' Digits?)? |
| 7624 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7625 | * [30] Number ::= Digits ('.' Digits?)? |
| 7626 | * | '.' Digits |
| 7627 | * [31] Digits ::= [0-9]+ |
| 7628 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7629 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7630 | * In complement of the Number expression, this function also handles |
| 7631 | * negative values : '-' Number. |
| 7632 | * |
| 7633 | * Returns the double value. |
| 7634 | */ |
| 7635 | double |
| 7636 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7637 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7638 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7639 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7640 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7641 | int exponent = 0; |
| 7642 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7643 | #ifdef __GNUC__ |
| 7644 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7645 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7646 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7647 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7648 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7649 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7650 | return(xmlXPathNAN); |
| 7651 | } |
| 7652 | if (*cur == '-') { |
| 7653 | isneg = 1; |
| 7654 | cur++; |
| 7655 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7656 | |
| 7657 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7658 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7659 | * tmp/temp is a workaround against a gcc compiler bug |
| 7660 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7661 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7662 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7663 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7664 | ret = ret * 10; |
| 7665 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7666 | ok = 1; |
| 7667 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7668 | temp = (double) tmp; |
| 7669 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7670 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7671 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7672 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7673 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7674 | ret = ret * 10 + (*cur - '0'); |
| 7675 | ok = 1; |
| 7676 | cur++; |
| 7677 | } |
| 7678 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7679 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7680 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7681 | int v, frac = 0; |
| 7682 | double fraction = 0; |
| 7683 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7684 | cur++; |
| 7685 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7686 | return(xmlXPathNAN); |
| 7687 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7688 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7689 | v = (*cur - '0'); |
| 7690 | fraction = fraction * 10 + v; |
| 7691 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7692 | cur++; |
| 7693 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7694 | fraction /= my_pow10[frac]; |
| 7695 | ret = ret + fraction; |
| 7696 | while ((*cur >= '0') && (*cur <= '9')) |
| 7697 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7698 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7699 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7700 | cur++; |
| 7701 | if (*cur == '-') { |
| 7702 | is_exponent_negative = 1; |
| 7703 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7704 | } else if (*cur == '+') { |
| 7705 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7706 | } |
| 7707 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7708 | exponent = exponent * 10 + (*cur - '0'); |
| 7709 | cur++; |
| 7710 | } |
| 7711 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7712 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7713 | if (*cur != 0) return(xmlXPathNAN); |
| 7714 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7715 | if (is_exponent_negative) exponent = -exponent; |
| 7716 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7717 | return(ret); |
| 7718 | } |
| 7719 | |
| 7720 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7721 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7722 | * @ctxt: the XPath Parser context |
| 7723 | * |
| 7724 | * [30] Number ::= Digits ('.' Digits?)? |
| 7725 | * | '.' Digits |
| 7726 | * [31] Digits ::= [0-9]+ |
| 7727 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7728 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7729 | * |
| 7730 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7731 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7732 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7733 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7734 | double ret = 0.0; |
| 7735 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7736 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7737 | int exponent = 0; |
| 7738 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7739 | #ifdef __GNUC__ |
| 7740 | unsigned long tmp = 0; |
| 7741 | double temp; |
| 7742 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7743 | |
| 7744 | CHECK_ERROR; |
| 7745 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7746 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7747 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7748 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7749 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7750 | * tmp/temp is a workaround against a gcc compiler bug |
| 7751 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7752 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7753 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7754 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7755 | ret = ret * 10; |
| 7756 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7757 | ok = 1; |
| 7758 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7759 | temp = (double) tmp; |
| 7760 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7761 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7762 | #else |
| 7763 | ret = 0; |
| 7764 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7765 | ret = ret * 10 + (CUR - '0'); |
| 7766 | ok = 1; |
| 7767 | NEXT; |
| 7768 | } |
| 7769 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7770 | if (CUR == '.') { |
| 7771 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7772 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7773 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7774 | } |
| 7775 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7776 | mult /= 10; |
| 7777 | ret = ret + (CUR - '0') * mult; |
| 7778 | NEXT; |
| 7779 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7780 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7781 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7782 | NEXT; |
| 7783 | if (CUR == '-') { |
| 7784 | is_exponent_negative = 1; |
| 7785 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7786 | } else if (CUR == '+') { |
| 7787 | NEXT; |
| 7788 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7789 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7790 | exponent = exponent * 10 + (CUR - '0'); |
| 7791 | NEXT; |
| 7792 | } |
| 7793 | if (is_exponent_negative) |
| 7794 | exponent = -exponent; |
| 7795 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7796 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7797 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7798 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7802 | * xmlXPathParseLiteral: |
| 7803 | * @ctxt: the XPath Parser context |
| 7804 | * |
| 7805 | * Parse a Literal |
| 7806 | * |
| 7807 | * [29] Literal ::= '"' [^"]* '"' |
| 7808 | * | "'" [^']* "'" |
| 7809 | * |
| 7810 | * Returns the value found or NULL in case of error |
| 7811 | */ |
| 7812 | static xmlChar * |
| 7813 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7814 | const xmlChar *q; |
| 7815 | xmlChar *ret = NULL; |
| 7816 | |
| 7817 | if (CUR == '"') { |
| 7818 | NEXT; |
| 7819 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7820 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7821 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7822 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7823 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7824 | } else { |
| 7825 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7826 | NEXT; |
| 7827 | } |
| 7828 | } else if (CUR == '\'') { |
| 7829 | NEXT; |
| 7830 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7831 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7832 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7833 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7834 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7835 | } else { |
| 7836 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7837 | NEXT; |
| 7838 | } |
| 7839 | } else { |
| 7840 | XP_ERROR0(XPATH_START_LITERAL_ERROR); |
| 7841 | } |
| 7842 | return(ret); |
| 7843 | } |
| 7844 | |
| 7845 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7846 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7847 | * @ctxt: the XPath Parser context |
| 7848 | * |
| 7849 | * Parse a Literal and push it on the stack. |
| 7850 | * |
| 7851 | * [29] Literal ::= '"' [^"]* '"' |
| 7852 | * | "'" [^']* "'" |
| 7853 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7854 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7855 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7856 | static void |
| 7857 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7858 | const xmlChar *q; |
| 7859 | xmlChar *ret = NULL; |
| 7860 | |
| 7861 | if (CUR == '"') { |
| 7862 | NEXT; |
| 7863 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7864 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7865 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7866 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7867 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7868 | } else { |
| 7869 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7870 | NEXT; |
| 7871 | } |
| 7872 | } else if (CUR == '\'') { |
| 7873 | NEXT; |
| 7874 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7875 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7876 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7877 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7878 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7879 | } else { |
| 7880 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7881 | NEXT; |
| 7882 | } |
| 7883 | } else { |
| 7884 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7885 | } |
| 7886 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7887 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7888 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7889 | xmlFree(ret); |
| 7890 | } |
| 7891 | |
| 7892 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7893 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7894 | * @ctxt: the XPath Parser context |
| 7895 | * |
| 7896 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7897 | * |
| 7898 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7899 | * 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] | 7900 | * of any of the types that are possible for the value of an expression, |
| 7901 | * and may also be of additional types not specified here. |
| 7902 | * |
| 7903 | * Early evaluation is possible since: |
| 7904 | * The variable bindings [...] used to evaluate a subexpression are |
| 7905 | * always the same as those used to evaluate the containing expression. |
| 7906 | * |
| 7907 | * [36] VariableReference ::= '$' QName |
| 7908 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7909 | static void |
| 7910 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7911 | xmlChar *name; |
| 7912 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7913 | |
| 7914 | SKIP_BLANKS; |
| 7915 | if (CUR != '$') { |
| 7916 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7917 | } |
| 7918 | NEXT; |
| 7919 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7920 | if (name == NULL) { |
| 7921 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7922 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7923 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7924 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7925 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7926 | SKIP_BLANKS; |
| 7927 | } |
| 7928 | |
| 7929 | /** |
| 7930 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7931 | * @name: a name string |
| 7932 | * |
| 7933 | * Is the name given a NodeType one. |
| 7934 | * |
| 7935 | * [38] NodeType ::= 'comment' |
| 7936 | * | 'text' |
| 7937 | * | 'processing-instruction' |
| 7938 | * | 'node' |
| 7939 | * |
| 7940 | * Returns 1 if true 0 otherwise |
| 7941 | */ |
| 7942 | int |
| 7943 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7944 | if (name == NULL) |
| 7945 | return(0); |
| 7946 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7947 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7948 | return(1); |
| 7949 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7950 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7951 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7952 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7953 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7954 | return(1); |
| 7955 | return(0); |
| 7956 | } |
| 7957 | |
| 7958 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7959 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7960 | * @ctxt: the XPath Parser context |
| 7961 | * |
| 7962 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7963 | * [17] Argument ::= Expr |
| 7964 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7965 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7966 | * pushed on the stack |
| 7967 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7968 | static void |
| 7969 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7970 | xmlChar *name; |
| 7971 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7972 | int nbargs = 0; |
| 7973 | |
| 7974 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7975 | if (name == NULL) { |
| 7976 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7977 | } |
| 7978 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7979 | #ifdef DEBUG_EXPR |
| 7980 | if (prefix == NULL) |
| 7981 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7982 | name); |
| 7983 | else |
| 7984 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7985 | prefix, name); |
| 7986 | #endif |
| 7987 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7988 | if (CUR != '(') { |
| 7989 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7990 | } |
| 7991 | NEXT; |
| 7992 | SKIP_BLANKS; |
| 7993 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7994 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 7995 | if (CUR != ')') { |
| 7996 | while (CUR != 0) { |
| 7997 | int op1 = ctxt->comp->last; |
| 7998 | ctxt->comp->last = -1; |
| 7999 | xmlXPathCompileExpr(ctxt); |
| 8000 | CHECK_ERROR; |
| 8001 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 8002 | nbargs++; |
| 8003 | if (CUR == ')') break; |
| 8004 | if (CUR != ',') { |
| 8005 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8006 | } |
| 8007 | NEXT; |
| 8008 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8009 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8010 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8011 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 8012 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8013 | NEXT; |
| 8014 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8015 | } |
| 8016 | |
| 8017 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8018 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8019 | * @ctxt: the XPath Parser context |
| 8020 | * |
| 8021 | * [15] PrimaryExpr ::= VariableReference |
| 8022 | * | '(' Expr ')' |
| 8023 | * | Literal |
| 8024 | * | Number |
| 8025 | * | FunctionCall |
| 8026 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8027 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8028 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8029 | static void |
| 8030 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8031 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8032 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8033 | else if (CUR == '(') { |
| 8034 | NEXT; |
| 8035 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8036 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 8037 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8038 | if (CUR != ')') { |
| 8039 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8040 | } |
| 8041 | NEXT; |
| 8042 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8043 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8044 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8045 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8046 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8047 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8048 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8049 | } |
| 8050 | SKIP_BLANKS; |
| 8051 | } |
| 8052 | |
| 8053 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8054 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8055 | * @ctxt: the XPath Parser context |
| 8056 | * |
| 8057 | * [20] FilterExpr ::= PrimaryExpr |
| 8058 | * | FilterExpr Predicate |
| 8059 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8060 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8061 | * Square brackets are used to filter expressions in the same way that |
| 8062 | * they are used in location paths. It is an error if the expression to |
| 8063 | * be filtered does not evaluate to a node-set. The context node list |
| 8064 | * used for evaluating the expression in square brackets is the node-set |
| 8065 | * to be filtered listed in document order. |
| 8066 | */ |
| 8067 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8068 | static void |
| 8069 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 8070 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8071 | CHECK_ERROR; |
| 8072 | SKIP_BLANKS; |
| 8073 | |
| 8074 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8075 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8076 | SKIP_BLANKS; |
| 8077 | } |
| 8078 | |
| 8079 | |
| 8080 | } |
| 8081 | |
| 8082 | /** |
| 8083 | * xmlXPathScanName: |
| 8084 | * @ctxt: the XPath Parser context |
| 8085 | * |
| 8086 | * Trickery: parse an XML name but without consuming the input flow |
| 8087 | * Needed to avoid insanity in the parser state. |
| 8088 | * |
| 8089 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 8090 | * CombiningChar | Extender |
| 8091 | * |
| 8092 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 8093 | * |
| 8094 | * [6] Names ::= Name (S Name)* |
| 8095 | * |
| 8096 | * Returns the Name parsed or NULL |
| 8097 | */ |
| 8098 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8099 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8100 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8101 | int len = 0, l; |
| 8102 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8103 | const xmlChar *cur; |
| 8104 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8105 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8106 | cur = ctxt->cur; |
| 8107 | |
| 8108 | c = CUR_CHAR(l); |
| 8109 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 8110 | (!IS_LETTER(c) && (c != '_') && |
| 8111 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8112 | return(NULL); |
| 8113 | } |
| 8114 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8115 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 8116 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 8117 | (c == '.') || (c == '-') || |
| 8118 | (c == '_') || (c == ':') || |
| 8119 | (IS_COMBINING(c)) || |
| 8120 | (IS_EXTENDER(c)))) { |
| 8121 | len += l; |
| 8122 | NEXTL(l); |
| 8123 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8124 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8125 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 8126 | ctxt->cur = cur; |
| 8127 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8131 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8132 | * @ctxt: the XPath Parser context |
| 8133 | * |
| 8134 | * [19] PathExpr ::= LocationPath |
| 8135 | * | FilterExpr |
| 8136 | * | FilterExpr '/' RelativeLocationPath |
| 8137 | * | FilterExpr '//' RelativeLocationPath |
| 8138 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8139 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8140 | * The / operator and // operators combine an arbitrary expression |
| 8141 | * and a relative location path. It is an error if the expression |
| 8142 | * does not evaluate to a node-set. |
| 8143 | * The / operator does composition in the same way as when / is |
| 8144 | * used in a location path. As in location paths, // is short for |
| 8145 | * /descendant-or-self::node()/. |
| 8146 | */ |
| 8147 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8148 | static void |
| 8149 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8150 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 8151 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 8152 | |
| 8153 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8154 | if ((CUR == '$') || (CUR == '(') || |
| 8155 | (IS_ASCII_DIGIT(CUR)) || |
| 8156 | (CUR == '\'') || (CUR == '"') || |
| 8157 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8158 | lc = 0; |
| 8159 | } else if (CUR == '*') { |
| 8160 | /* relative or absolute location path */ |
| 8161 | lc = 1; |
| 8162 | } else if (CUR == '/') { |
| 8163 | /* relative or absolute location path */ |
| 8164 | lc = 1; |
| 8165 | } else if (CUR == '@') { |
| 8166 | /* relative abbreviated attribute location path */ |
| 8167 | lc = 1; |
| 8168 | } else if (CUR == '.') { |
| 8169 | /* relative abbreviated attribute location path */ |
| 8170 | lc = 1; |
| 8171 | } else { |
| 8172 | /* |
| 8173 | * Problem is finding if we have a name here whether it's: |
| 8174 | * - a nodetype |
| 8175 | * - a function call in which case it's followed by '(' |
| 8176 | * - an axis in which case it's followed by ':' |
| 8177 | * - a element name |
| 8178 | * We do an a priori analysis here rather than having to |
| 8179 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8180 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8181 | * read/write/debug. |
| 8182 | */ |
| 8183 | SKIP_BLANKS; |
| 8184 | name = xmlXPathScanName(ctxt); |
| 8185 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8186 | #ifdef DEBUG_STEP |
| 8187 | xmlGenericError(xmlGenericErrorContext, |
| 8188 | "PathExpr: Axis\n"); |
| 8189 | #endif |
| 8190 | lc = 1; |
| 8191 | xmlFree(name); |
| 8192 | } else if (name != NULL) { |
| 8193 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8194 | |
| 8195 | |
| 8196 | while (NXT(len) != 0) { |
| 8197 | if (NXT(len) == '/') { |
| 8198 | /* element name */ |
| 8199 | #ifdef DEBUG_STEP |
| 8200 | xmlGenericError(xmlGenericErrorContext, |
| 8201 | "PathExpr: AbbrRelLocation\n"); |
| 8202 | #endif |
| 8203 | lc = 1; |
| 8204 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8205 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8206 | /* ignore blanks */ |
| 8207 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8208 | } else if (NXT(len) == ':') { |
| 8209 | #ifdef DEBUG_STEP |
| 8210 | xmlGenericError(xmlGenericErrorContext, |
| 8211 | "PathExpr: AbbrRelLocation\n"); |
| 8212 | #endif |
| 8213 | lc = 1; |
| 8214 | break; |
| 8215 | } else if ((NXT(len) == '(')) { |
| 8216 | /* Note Type or Function */ |
| 8217 | if (xmlXPathIsNodeType(name)) { |
| 8218 | #ifdef DEBUG_STEP |
| 8219 | xmlGenericError(xmlGenericErrorContext, |
| 8220 | "PathExpr: Type search\n"); |
| 8221 | #endif |
| 8222 | lc = 1; |
| 8223 | } else { |
| 8224 | #ifdef DEBUG_STEP |
| 8225 | xmlGenericError(xmlGenericErrorContext, |
| 8226 | "PathExpr: function call\n"); |
| 8227 | #endif |
| 8228 | lc = 0; |
| 8229 | } |
| 8230 | break; |
| 8231 | } else if ((NXT(len) == '[')) { |
| 8232 | /* element name */ |
| 8233 | #ifdef DEBUG_STEP |
| 8234 | xmlGenericError(xmlGenericErrorContext, |
| 8235 | "PathExpr: AbbrRelLocation\n"); |
| 8236 | #endif |
| 8237 | lc = 1; |
| 8238 | break; |
| 8239 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8240 | (NXT(len) == '=')) { |
| 8241 | lc = 1; |
| 8242 | break; |
| 8243 | } else { |
| 8244 | lc = 1; |
| 8245 | break; |
| 8246 | } |
| 8247 | len++; |
| 8248 | } |
| 8249 | if (NXT(len) == 0) { |
| 8250 | #ifdef DEBUG_STEP |
| 8251 | xmlGenericError(xmlGenericErrorContext, |
| 8252 | "PathExpr: AbbrRelLocation\n"); |
| 8253 | #endif |
| 8254 | /* element name */ |
| 8255 | lc = 1; |
| 8256 | } |
| 8257 | xmlFree(name); |
| 8258 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8259 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8260 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8261 | } |
| 8262 | } |
| 8263 | |
| 8264 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8265 | if (CUR == '/') { |
| 8266 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8267 | } else { |
| 8268 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8269 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8270 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8271 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8272 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8273 | CHECK_ERROR; |
| 8274 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8275 | SKIP(2); |
| 8276 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8277 | |
| 8278 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8279 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8280 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8281 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8282 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8283 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8284 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8285 | } |
| 8286 | } |
| 8287 | SKIP_BLANKS; |
| 8288 | } |
| 8289 | |
| 8290 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8291 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8292 | * @ctxt: the XPath Parser context |
| 8293 | * |
| 8294 | * [18] UnionExpr ::= PathExpr |
| 8295 | * | UnionExpr '|' PathExpr |
| 8296 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8297 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8298 | */ |
| 8299 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8300 | static void |
| 8301 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8302 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8303 | CHECK_ERROR; |
| 8304 | SKIP_BLANKS; |
| 8305 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8306 | int op1 = ctxt->comp->last; |
| 8307 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8308 | |
| 8309 | NEXT; |
| 8310 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8311 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8313 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8314 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8315 | SKIP_BLANKS; |
| 8316 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8317 | } |
| 8318 | |
| 8319 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8320 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8321 | * @ctxt: the XPath Parser context |
| 8322 | * |
| 8323 | * [27] UnaryExpr ::= UnionExpr |
| 8324 | * | '-' UnaryExpr |
| 8325 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8326 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8327 | */ |
| 8328 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8329 | static void |
| 8330 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8331 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8332 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | |
| 8334 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8335 | while (CUR == '-') { |
| 8336 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8337 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8338 | NEXT; |
| 8339 | SKIP_BLANKS; |
| 8340 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8341 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8342 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8343 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8344 | if (found) { |
| 8345 | if (minus) |
| 8346 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8347 | else |
| 8348 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8349 | } |
| 8350 | } |
| 8351 | |
| 8352 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8353 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8354 | * @ctxt: the XPath Parser context |
| 8355 | * |
| 8356 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8357 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8358 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8359 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8360 | * [34] MultiplyOperator ::= '*' |
| 8361 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8362 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8363 | */ |
| 8364 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8365 | static void |
| 8366 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8367 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8368 | CHECK_ERROR; |
| 8369 | SKIP_BLANKS; |
| 8370 | while ((CUR == '*') || |
| 8371 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8372 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8373 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8374 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8375 | |
| 8376 | if (CUR == '*') { |
| 8377 | op = 0; |
| 8378 | NEXT; |
| 8379 | } else if (CUR == 'd') { |
| 8380 | op = 1; |
| 8381 | SKIP(3); |
| 8382 | } else if (CUR == 'm') { |
| 8383 | op = 2; |
| 8384 | SKIP(3); |
| 8385 | } |
| 8386 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8387 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8388 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8389 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8390 | SKIP_BLANKS; |
| 8391 | } |
| 8392 | } |
| 8393 | |
| 8394 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8395 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8396 | * @ctxt: the XPath Parser context |
| 8397 | * |
| 8398 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8399 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8400 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8401 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8402 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8403 | */ |
| 8404 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8405 | static void |
| 8406 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8407 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8408 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8409 | CHECK_ERROR; |
| 8410 | SKIP_BLANKS; |
| 8411 | while ((CUR == '+') || (CUR == '-')) { |
| 8412 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8413 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8414 | |
| 8415 | if (CUR == '+') plus = 1; |
| 8416 | else plus = 0; |
| 8417 | NEXT; |
| 8418 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8419 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8420 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8421 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8422 | SKIP_BLANKS; |
| 8423 | } |
| 8424 | } |
| 8425 | |
| 8426 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8427 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8428 | * @ctxt: the XPath Parser context |
| 8429 | * |
| 8430 | * [24] RelationalExpr ::= AdditiveExpr |
| 8431 | * | RelationalExpr '<' AdditiveExpr |
| 8432 | * | RelationalExpr '>' AdditiveExpr |
| 8433 | * | RelationalExpr '<=' AdditiveExpr |
| 8434 | * | RelationalExpr '>=' AdditiveExpr |
| 8435 | * |
| 8436 | * A <= B > C is allowed ? Answer from James, yes with |
| 8437 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8438 | * which is basically what got implemented. |
| 8439 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8440 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8441 | * on the stack |
| 8442 | */ |
| 8443 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8444 | static void |
| 8445 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8446 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8447 | CHECK_ERROR; |
| 8448 | SKIP_BLANKS; |
| 8449 | while ((CUR == '<') || |
| 8450 | (CUR == '>') || |
| 8451 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8452 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8453 | int inf, strict; |
| 8454 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8455 | |
| 8456 | if (CUR == '<') inf = 1; |
| 8457 | else inf = 0; |
| 8458 | if (NXT(1) == '=') strict = 0; |
| 8459 | else strict = 1; |
| 8460 | NEXT; |
| 8461 | if (!strict) NEXT; |
| 8462 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8463 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8464 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8465 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8466 | SKIP_BLANKS; |
| 8467 | } |
| 8468 | } |
| 8469 | |
| 8470 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8471 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8472 | * @ctxt: the XPath Parser context |
| 8473 | * |
| 8474 | * [23] EqualityExpr ::= RelationalExpr |
| 8475 | * | EqualityExpr '=' RelationalExpr |
| 8476 | * | EqualityExpr '!=' RelationalExpr |
| 8477 | * |
| 8478 | * A != B != C is allowed ? Answer from James, yes with |
| 8479 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8480 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8481 | * which is basically what got implemented. |
| 8482 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8483 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | * |
| 8485 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8486 | static void |
| 8487 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8488 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8489 | CHECK_ERROR; |
| 8490 | SKIP_BLANKS; |
| 8491 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8492 | int eq; |
| 8493 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8494 | |
| 8495 | if (CUR == '=') eq = 1; |
| 8496 | else eq = 0; |
| 8497 | NEXT; |
| 8498 | if (!eq) NEXT; |
| 8499 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8500 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8501 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8502 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8503 | SKIP_BLANKS; |
| 8504 | } |
| 8505 | } |
| 8506 | |
| 8507 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8508 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8509 | * @ctxt: the XPath Parser context |
| 8510 | * |
| 8511 | * [22] AndExpr ::= EqualityExpr |
| 8512 | * | AndExpr 'and' EqualityExpr |
| 8513 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8514 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8515 | * |
| 8516 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8517 | static void |
| 8518 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8519 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8520 | CHECK_ERROR; |
| 8521 | SKIP_BLANKS; |
| 8522 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8523 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8524 | SKIP(3); |
| 8525 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8526 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8527 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8528 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8529 | SKIP_BLANKS; |
| 8530 | } |
| 8531 | } |
| 8532 | |
| 8533 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8534 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8535 | * @ctxt: the XPath Parser context |
| 8536 | * |
| 8537 | * [14] Expr ::= OrExpr |
| 8538 | * [21] OrExpr ::= AndExpr |
| 8539 | * | OrExpr 'or' AndExpr |
| 8540 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8541 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8542 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8543 | static void |
| 8544 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 8545 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8546 | CHECK_ERROR; |
| 8547 | SKIP_BLANKS; |
| 8548 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8549 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8550 | SKIP(2); |
| 8551 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8552 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8553 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8554 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8555 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8556 | SKIP_BLANKS; |
| 8557 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8558 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 8559 | /* more ops could be optimized too */ |
| 8560 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8561 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
| 8564 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8565 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8566 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8567 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8568 | * |
| 8569 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8570 | * [9] PredicateExpr ::= Expr |
| 8571 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8572 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8573 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8574 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8575 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8576 | int op1 = ctxt->comp->last; |
| 8577 | |
| 8578 | SKIP_BLANKS; |
| 8579 | if (CUR != '[') { |
| 8580 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8581 | } |
| 8582 | NEXT; |
| 8583 | SKIP_BLANKS; |
| 8584 | |
| 8585 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8586 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8587 | CHECK_ERROR; |
| 8588 | |
| 8589 | if (CUR != ']') { |
| 8590 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8591 | } |
| 8592 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8593 | if (filter) |
| 8594 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8595 | else |
| 8596 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8597 | |
| 8598 | NEXT; |
| 8599 | SKIP_BLANKS; |
| 8600 | } |
| 8601 | |
| 8602 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8603 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8604 | * @ctxt: the XPath Parser context |
| 8605 | * @test: pointer to a xmlXPathTestVal |
| 8606 | * @type: pointer to a xmlXPathTypeVal |
| 8607 | * @prefix: placeholder for a possible name prefix |
| 8608 | * |
| 8609 | * [7] NodeTest ::= NameTest |
| 8610 | * | NodeType '(' ')' |
| 8611 | * | 'processing-instruction' '(' Literal ')' |
| 8612 | * |
| 8613 | * [37] NameTest ::= '*' |
| 8614 | * | NCName ':' '*' |
| 8615 | * | QName |
| 8616 | * [38] NodeType ::= 'comment' |
| 8617 | * | 'text' |
| 8618 | * | 'processing-instruction' |
| 8619 | * | 'node' |
| 8620 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8621 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8622 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8623 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8624 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8625 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8626 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8627 | int blanks; |
| 8628 | |
| 8629 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8630 | STRANGE; |
| 8631 | return(NULL); |
| 8632 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8633 | *type = (xmlXPathTypeVal) 0; |
| 8634 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8635 | *prefix = NULL; |
| 8636 | SKIP_BLANKS; |
| 8637 | |
| 8638 | if ((name == NULL) && (CUR == '*')) { |
| 8639 | /* |
| 8640 | * All elements |
| 8641 | */ |
| 8642 | NEXT; |
| 8643 | *test = NODE_TEST_ALL; |
| 8644 | return(NULL); |
| 8645 | } |
| 8646 | |
| 8647 | if (name == NULL) |
| 8648 | name = xmlXPathParseNCName(ctxt); |
| 8649 | if (name == NULL) { |
| 8650 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8651 | } |
| 8652 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8653 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8654 | SKIP_BLANKS; |
| 8655 | if (CUR == '(') { |
| 8656 | NEXT; |
| 8657 | /* |
| 8658 | * NodeType or PI search |
| 8659 | */ |
| 8660 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8661 | *type = NODE_TYPE_COMMENT; |
| 8662 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8663 | *type = NODE_TYPE_NODE; |
| 8664 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8665 | *type = NODE_TYPE_PI; |
| 8666 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8667 | *type = NODE_TYPE_TEXT; |
| 8668 | else { |
| 8669 | if (name != NULL) |
| 8670 | xmlFree(name); |
| 8671 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8672 | } |
| 8673 | |
| 8674 | *test = NODE_TEST_TYPE; |
| 8675 | |
| 8676 | SKIP_BLANKS; |
| 8677 | if (*type == NODE_TYPE_PI) { |
| 8678 | /* |
| 8679 | * Specific case: search a PI by name. |
| 8680 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8681 | if (name != NULL) |
| 8682 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8683 | name = NULL; |
| 8684 | if (CUR != ')') { |
| 8685 | name = xmlXPathParseLiteral(ctxt); |
| 8686 | CHECK_ERROR 0; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8687 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8688 | SKIP_BLANKS; |
| 8689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8690 | } |
| 8691 | if (CUR != ')') { |
| 8692 | if (name != NULL) |
| 8693 | xmlFree(name); |
| 8694 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
| 8695 | } |
| 8696 | NEXT; |
| 8697 | return(name); |
| 8698 | } |
| 8699 | *test = NODE_TEST_NAME; |
| 8700 | if ((!blanks) && (CUR == ':')) { |
| 8701 | NEXT; |
| 8702 | |
| 8703 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8704 | * Since currently the parser context don't have a |
| 8705 | * namespace list associated: |
| 8706 | * The namespace name for this prefix can be computed |
| 8707 | * only at evaluation time. The compilation is done |
| 8708 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8709 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8710 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8711 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8712 | if (name != NULL) |
| 8713 | xmlFree(name); |
| 8714 | if (*prefix == NULL) { |
| 8715 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8716 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8717 | #else |
| 8718 | *prefix = name; |
| 8719 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8720 | |
| 8721 | if (CUR == '*') { |
| 8722 | /* |
| 8723 | * All elements |
| 8724 | */ |
| 8725 | NEXT; |
| 8726 | *test = NODE_TEST_ALL; |
| 8727 | return(NULL); |
| 8728 | } |
| 8729 | |
| 8730 | name = xmlXPathParseNCName(ctxt); |
| 8731 | if (name == NULL) { |
| 8732 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8733 | } |
| 8734 | } |
| 8735 | return(name); |
| 8736 | } |
| 8737 | |
| 8738 | /** |
| 8739 | * xmlXPathIsAxisName: |
| 8740 | * @name: a preparsed name token |
| 8741 | * |
| 8742 | * [6] AxisName ::= 'ancestor' |
| 8743 | * | 'ancestor-or-self' |
| 8744 | * | 'attribute' |
| 8745 | * | 'child' |
| 8746 | * | 'descendant' |
| 8747 | * | 'descendant-or-self' |
| 8748 | * | 'following' |
| 8749 | * | 'following-sibling' |
| 8750 | * | 'namespace' |
| 8751 | * | 'parent' |
| 8752 | * | 'preceding' |
| 8753 | * | 'preceding-sibling' |
| 8754 | * | 'self' |
| 8755 | * |
| 8756 | * Returns the axis or 0 |
| 8757 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8758 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8759 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8760 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8761 | switch (name[0]) { |
| 8762 | case 'a': |
| 8763 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8764 | ret = AXIS_ANCESTOR; |
| 8765 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8766 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8767 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8768 | ret = AXIS_ATTRIBUTE; |
| 8769 | break; |
| 8770 | case 'c': |
| 8771 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8772 | ret = AXIS_CHILD; |
| 8773 | break; |
| 8774 | case 'd': |
| 8775 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8776 | ret = AXIS_DESCENDANT; |
| 8777 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8778 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8779 | break; |
| 8780 | case 'f': |
| 8781 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8782 | ret = AXIS_FOLLOWING; |
| 8783 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8784 | ret = AXIS_FOLLOWING_SIBLING; |
| 8785 | break; |
| 8786 | case 'n': |
| 8787 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8788 | ret = AXIS_NAMESPACE; |
| 8789 | break; |
| 8790 | case 'p': |
| 8791 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8792 | ret = AXIS_PARENT; |
| 8793 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8794 | ret = AXIS_PRECEDING; |
| 8795 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8796 | ret = AXIS_PRECEDING_SIBLING; |
| 8797 | break; |
| 8798 | case 's': |
| 8799 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8800 | ret = AXIS_SELF; |
| 8801 | break; |
| 8802 | } |
| 8803 | return(ret); |
| 8804 | } |
| 8805 | |
| 8806 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8807 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8808 | * @ctxt: the XPath Parser context |
| 8809 | * |
| 8810 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8811 | * | AbbreviatedStep |
| 8812 | * |
| 8813 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8814 | * |
| 8815 | * [5] AxisSpecifier ::= AxisName '::' |
| 8816 | * | AbbreviatedAxisSpecifier |
| 8817 | * |
| 8818 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8819 | * |
| 8820 | * Modified for XPtr range support as: |
| 8821 | * |
| 8822 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8823 | * | AbbreviatedStep |
| 8824 | * | 'range-to' '(' Expr ')' Predicate* |
| 8825 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8826 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8827 | * A location step of . is short for self::node(). This is |
| 8828 | * particularly useful in conjunction with //. For example, the |
| 8829 | * location path .//para is short for |
| 8830 | * self::node()/descendant-or-self::node()/child::para |
| 8831 | * and so will select all para descendant elements of the context |
| 8832 | * node. |
| 8833 | * Similarly, a location step of .. is short for parent::node(). |
| 8834 | * For example, ../title is short for parent::node()/child::title |
| 8835 | * and so will select the title children of the parent of the context |
| 8836 | * node. |
| 8837 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8838 | static void |
| 8839 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8840 | #ifdef LIBXML_XPTR_ENABLED |
| 8841 | int rangeto = 0; |
| 8842 | int op2 = -1; |
| 8843 | #endif |
| 8844 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8845 | SKIP_BLANKS; |
| 8846 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8847 | SKIP(2); |
| 8848 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8849 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8850 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8851 | } else if (CUR == '.') { |
| 8852 | NEXT; |
| 8853 | SKIP_BLANKS; |
| 8854 | } else { |
| 8855 | xmlChar *name = NULL; |
| 8856 | const xmlChar *prefix = NULL; |
| 8857 | xmlXPathTestVal test; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8858 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8859 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8860 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8861 | |
| 8862 | /* |
| 8863 | * The modification needed for XPointer change to the production |
| 8864 | */ |
| 8865 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8866 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8867 | name = xmlXPathParseNCName(ctxt); |
| 8868 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8869 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8870 | xmlFree(name); |
| 8871 | SKIP_BLANKS; |
| 8872 | if (CUR != '(') { |
| 8873 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8874 | } |
| 8875 | NEXT; |
| 8876 | SKIP_BLANKS; |
| 8877 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8878 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8879 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8880 | CHECK_ERROR; |
| 8881 | |
| 8882 | SKIP_BLANKS; |
| 8883 | if (CUR != ')') { |
| 8884 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8885 | } |
| 8886 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8887 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8888 | goto eval_predicates; |
| 8889 | } |
| 8890 | } |
| 8891 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8892 | if (CUR == '*') { |
| 8893 | axis = AXIS_CHILD; |
| 8894 | } else { |
| 8895 | if (name == NULL) |
| 8896 | name = xmlXPathParseNCName(ctxt); |
| 8897 | if (name != NULL) { |
| 8898 | axis = xmlXPathIsAxisName(name); |
| 8899 | if (axis != 0) { |
| 8900 | SKIP_BLANKS; |
| 8901 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8902 | SKIP(2); |
| 8903 | xmlFree(name); |
| 8904 | name = NULL; |
| 8905 | } else { |
| 8906 | /* an element name can conflict with an axis one :-\ */ |
| 8907 | axis = AXIS_CHILD; |
| 8908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8909 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8910 | axis = AXIS_CHILD; |
| 8911 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8912 | } else if (CUR == '@') { |
| 8913 | NEXT; |
| 8914 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8915 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8916 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8917 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8918 | } |
| 8919 | |
| 8920 | CHECK_ERROR; |
| 8921 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8922 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8923 | if (test == 0) |
| 8924 | return; |
| 8925 | |
| 8926 | #ifdef DEBUG_STEP |
| 8927 | xmlGenericError(xmlGenericErrorContext, |
| 8928 | "Basis : computing new set\n"); |
| 8929 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8930 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8931 | #ifdef DEBUG_STEP |
| 8932 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8933 | if (ctxt->value == NULL) |
| 8934 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8935 | else if (ctxt->value->nodesetval == NULL) |
| 8936 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8937 | else |
| 8938 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8939 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8940 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8941 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8942 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8943 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8944 | op1 = ctxt->comp->last; |
| 8945 | ctxt->comp->last = -1; |
| 8946 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8947 | SKIP_BLANKS; |
| 8948 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8949 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8950 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8951 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8952 | #ifdef LIBXML_XPTR_ENABLED |
| 8953 | if (rangeto) { |
| 8954 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8955 | } else |
| 8956 | #endif |
| 8957 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8958 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8959 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8960 | } |
| 8961 | #ifdef DEBUG_STEP |
| 8962 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8963 | if (ctxt->value == NULL) |
| 8964 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8965 | else if (ctxt->value->nodesetval == NULL) |
| 8966 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8967 | else |
| 8968 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8969 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8970 | #endif |
| 8971 | } |
| 8972 | |
| 8973 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8974 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8975 | * @ctxt: the XPath Parser context |
| 8976 | * |
| 8977 | * [3] RelativeLocationPath ::= Step |
| 8978 | * | RelativeLocationPath '/' Step |
| 8979 | * | AbbreviatedRelativeLocationPath |
| 8980 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8981 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8982 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8983 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8984 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8985 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8986 | (xmlXPathParserContextPtr ctxt) { |
| 8987 | SKIP_BLANKS; |
| 8988 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8989 | SKIP(2); |
| 8990 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8991 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8992 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8993 | } else if (CUR == '/') { |
| 8994 | NEXT; |
| 8995 | SKIP_BLANKS; |
| 8996 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8997 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8998 | SKIP_BLANKS; |
| 8999 | while (CUR == '/') { |
| 9000 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9001 | SKIP(2); |
| 9002 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9003 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9004 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9005 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9006 | } else if (CUR == '/') { |
| 9007 | NEXT; |
| 9008 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9009 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9010 | } |
| 9011 | SKIP_BLANKS; |
| 9012 | } |
| 9013 | } |
| 9014 | |
| 9015 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9016 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9017 | * @ctxt: the XPath Parser context |
| 9018 | * |
| 9019 | * [1] LocationPath ::= RelativeLocationPath |
| 9020 | * | AbsoluteLocationPath |
| 9021 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 9022 | * | AbbreviatedAbsoluteLocationPath |
| 9023 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 9024 | * '//' RelativeLocationPath |
| 9025 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9026 | * Compile a location path |
| 9027 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9028 | * // is short for /descendant-or-self::node()/. For example, |
| 9029 | * //para is short for /descendant-or-self::node()/child::para and |
| 9030 | * so will select any para element in the document (even a para element |
| 9031 | * that is a document element will be selected by //para since the |
| 9032 | * document element node is a child of the root node); div//para is |
| 9033 | * short for div/descendant-or-self::node()/child::para and so will |
| 9034 | * select all para descendants of div children. |
| 9035 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9036 | static void |
| 9037 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9038 | SKIP_BLANKS; |
| 9039 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9040 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9041 | } else { |
| 9042 | while (CUR == '/') { |
| 9043 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9044 | SKIP(2); |
| 9045 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9046 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 9047 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9048 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9049 | } else if (CUR == '/') { |
| 9050 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9051 | SKIP_BLANKS; |
| 9052 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 9053 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9054 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9055 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9056 | } |
| 9057 | } |
| 9058 | } |
| 9059 | } |
| 9060 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9061 | /************************************************************************ |
| 9062 | * * |
| 9063 | * XPath precompiled expression evaluation * |
| 9064 | * * |
| 9065 | ************************************************************************/ |
| 9066 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9067 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9068 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 9069 | |
| 9070 | /** |
| 9071 | * xmlXPathNodeCollectAndTest: |
| 9072 | * @ctxt: the XPath Parser context |
| 9073 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9074 | * @first: pointer to the first element in document order |
| 9075 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9076 | * |
| 9077 | * This is the function implementing a step: based on the current list |
| 9078 | * 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] | 9079 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9080 | * |
| 9081 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9082 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9083 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 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 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9087 | xmlXPathStepOpPtr op, |
| 9088 | xmlNodePtr * first, xmlNodePtr * last) |
| 9089 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9090 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9091 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9092 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9093 | const xmlChar *prefix = op->value4; |
| 9094 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9095 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9096 | |
| 9097 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9098 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9099 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9100 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9101 | xmlNodeSetPtr ret, list; |
| 9102 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9103 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9104 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9105 | xmlNodePtr cur = NULL; |
| 9106 | xmlXPathObjectPtr obj; |
| 9107 | xmlNodeSetPtr nodelist; |
| 9108 | xmlNodePtr tmp; |
| 9109 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9110 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9111 | obj = valuePop(ctxt); |
| 9112 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9113 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9114 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9115 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9116 | if (URI == NULL) { |
| 9117 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9118 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9119 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9120 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9121 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9122 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9123 | #endif |
| 9124 | switch (axis) { |
| 9125 | case AXIS_ANCESTOR: |
| 9126 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9127 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9128 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9129 | first = NULL; |
| 9130 | next = xmlXPathNextAncestor; |
| 9131 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9132 | case AXIS_ANCESTOR_OR_SELF: |
| 9133 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9134 | xmlGenericError(xmlGenericErrorContext, |
| 9135 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9136 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9137 | first = NULL; |
| 9138 | next = xmlXPathNextAncestorOrSelf; |
| 9139 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9140 | case AXIS_ATTRIBUTE: |
| 9141 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9142 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9143 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9144 | first = NULL; |
| 9145 | last = NULL; |
| 9146 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9147 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9148 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9149 | case AXIS_CHILD: |
| 9150 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9151 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9152 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9153 | last = NULL; |
| 9154 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9155 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9156 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9157 | case AXIS_DESCENDANT: |
| 9158 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9159 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9160 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9161 | last = NULL; |
| 9162 | next = xmlXPathNextDescendant; |
| 9163 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9164 | case AXIS_DESCENDANT_OR_SELF: |
| 9165 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9166 | xmlGenericError(xmlGenericErrorContext, |
| 9167 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9168 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9169 | last = NULL; |
| 9170 | next = xmlXPathNextDescendantOrSelf; |
| 9171 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9172 | case AXIS_FOLLOWING: |
| 9173 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9174 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9175 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9176 | last = NULL; |
| 9177 | next = xmlXPathNextFollowing; |
| 9178 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9179 | case AXIS_FOLLOWING_SIBLING: |
| 9180 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9181 | xmlGenericError(xmlGenericErrorContext, |
| 9182 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9183 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9184 | last = NULL; |
| 9185 | next = xmlXPathNextFollowingSibling; |
| 9186 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9187 | case AXIS_NAMESPACE: |
| 9188 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9189 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9190 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9191 | first = NULL; |
| 9192 | last = NULL; |
| 9193 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9194 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9195 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9196 | case AXIS_PARENT: |
| 9197 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9198 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9199 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9200 | first = NULL; |
| 9201 | next = xmlXPathNextParent; |
| 9202 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9203 | case AXIS_PRECEDING: |
| 9204 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9205 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9206 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9207 | first = NULL; |
| 9208 | next = xmlXPathNextPrecedingInternal; |
| 9209 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9210 | case AXIS_PRECEDING_SIBLING: |
| 9211 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9212 | xmlGenericError(xmlGenericErrorContext, |
| 9213 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9214 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9215 | first = NULL; |
| 9216 | next = xmlXPathNextPrecedingSibling; |
| 9217 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9218 | case AXIS_SELF: |
| 9219 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9220 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9221 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9222 | first = NULL; |
| 9223 | last = NULL; |
| 9224 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9225 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9226 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9227 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9228 | if (next == NULL) { |
| 9229 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9230 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9231 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9232 | |
| 9233 | nodelist = obj->nodesetval; |
| 9234 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9235 | xmlXPathFreeObject(obj); |
| 9236 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9237 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9238 | } |
| 9239 | addNode = xmlXPathNodeSetAddUnique; |
| 9240 | ret = NULL; |
| 9241 | #ifdef DEBUG_STEP |
| 9242 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9243 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9244 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9245 | case NODE_TEST_NONE: |
| 9246 | xmlGenericError(xmlGenericErrorContext, |
| 9247 | " searching for none !!!\n"); |
| 9248 | break; |
| 9249 | case NODE_TEST_TYPE: |
| 9250 | xmlGenericError(xmlGenericErrorContext, |
| 9251 | " searching for type %d\n", type); |
| 9252 | break; |
| 9253 | case NODE_TEST_PI: |
| 9254 | xmlGenericError(xmlGenericErrorContext, |
| 9255 | " searching for PI !!!\n"); |
| 9256 | break; |
| 9257 | case NODE_TEST_ALL: |
| 9258 | xmlGenericError(xmlGenericErrorContext, |
| 9259 | " searching for *\n"); |
| 9260 | break; |
| 9261 | case NODE_TEST_NS: |
| 9262 | xmlGenericError(xmlGenericErrorContext, |
| 9263 | " searching for namespace %s\n", |
| 9264 | prefix); |
| 9265 | break; |
| 9266 | case NODE_TEST_NAME: |
| 9267 | xmlGenericError(xmlGenericErrorContext, |
| 9268 | " searching for name %s\n", name); |
| 9269 | if (prefix != NULL) |
| 9270 | xmlGenericError(xmlGenericErrorContext, |
| 9271 | " with namespace %s\n", prefix); |
| 9272 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9273 | } |
| 9274 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9275 | #endif |
| 9276 | /* |
| 9277 | * 2.3 Node Tests |
| 9278 | * - For the attribute axis, the principal node type is attribute. |
| 9279 | * - For the namespace axis, the principal node type is namespace. |
| 9280 | * - For other axes, the principal node type is element. |
| 9281 | * |
| 9282 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9283 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9284 | * select all element children of the context node |
| 9285 | */ |
| 9286 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9287 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9288 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9289 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9290 | cur = NULL; |
| 9291 | list = xmlXPathNodeSetCreate(NULL); |
| 9292 | do { |
| 9293 | cur = next(ctxt, cur); |
| 9294 | if (cur == NULL) |
| 9295 | break; |
| 9296 | if ((first != NULL) && (*first == cur)) |
| 9297 | break; |
| 9298 | if (((t % 256) == 0) && |
| 9299 | (first != NULL) && (*first != NULL) && |
| 9300 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9301 | break; |
| 9302 | if ((last != NULL) && (*last == cur)) |
| 9303 | break; |
| 9304 | if (((t % 256) == 0) && |
| 9305 | (last != NULL) && (*last != NULL) && |
| 9306 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9307 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9308 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9309 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9310 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9311 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9312 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9313 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9314 | ctxt->context->node = tmp; |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9315 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9316 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9317 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9318 | if ((cur->type == type) || |
| 9319 | ((type == NODE_TYPE_NODE) && |
| 9320 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9321 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9322 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9323 | (cur->type == XML_NAMESPACE_DECL) || |
| 9324 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9325 | (cur->type == XML_PI_NODE) || |
| 9326 | (cur->type == XML_COMMENT_NODE) || |
| 9327 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9328 | (cur->type == XML_TEXT_NODE))) || |
| 9329 | ((type == NODE_TYPE_TEXT) && |
| 9330 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9331 | #ifdef DEBUG_STEP |
| 9332 | n++; |
| 9333 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9334 | addNode(list, cur); |
| 9335 | } |
| 9336 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9337 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9338 | if (cur->type == XML_PI_NODE) { |
| 9339 | if ((name != NULL) && |
| 9340 | (!xmlStrEqual(name, cur->name))) |
| 9341 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9342 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9343 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9344 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9345 | addNode(list, cur); |
| 9346 | } |
| 9347 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9348 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9349 | if (axis == AXIS_ATTRIBUTE) { |
| 9350 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9351 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9352 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9353 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9354 | addNode(list, cur); |
| 9355 | } |
| 9356 | } else if (axis == AXIS_NAMESPACE) { |
| 9357 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9358 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9359 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9360 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9361 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9362 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9363 | } |
| 9364 | } else { |
| 9365 | if (cur->type == XML_ELEMENT_NODE) { |
| 9366 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9367 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9368 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9369 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9370 | addNode(list, cur); |
| 9371 | } else if ((cur->ns != NULL) && |
| 9372 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9373 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9374 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9375 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9376 | addNode(list, cur); |
| 9377 | } |
| 9378 | } |
| 9379 | } |
| 9380 | break; |
| 9381 | case NODE_TEST_NS:{ |
| 9382 | TODO; |
| 9383 | break; |
| 9384 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9385 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9386 | switch (cur->type) { |
| 9387 | case XML_ELEMENT_NODE: |
| 9388 | if (xmlStrEqual(name, cur->name)) { |
| 9389 | if (prefix == NULL) { |
| 9390 | if (cur->ns == NULL) { |
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 | } else { |
| 9397 | if ((cur->ns != NULL) && |
| 9398 | (xmlStrEqual(URI, |
| 9399 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9400 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9401 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9402 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9403 | addNode(list, cur); |
| 9404 | } |
| 9405 | } |
| 9406 | } |
| 9407 | break; |
| 9408 | case XML_ATTRIBUTE_NODE:{ |
| 9409 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9410 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9411 | if (xmlStrEqual(name, attr->name)) { |
| 9412 | if (prefix == NULL) { |
| 9413 | if ((attr->ns == NULL) || |
| 9414 | (attr->ns->prefix == NULL)) { |
| 9415 | #ifdef DEBUG_STEP |
| 9416 | n++; |
| 9417 | #endif |
| 9418 | addNode(list, |
| 9419 | (xmlNodePtr) attr); |
| 9420 | } |
| 9421 | } else { |
| 9422 | if ((attr->ns != NULL) && |
| 9423 | (xmlStrEqual(URI, |
| 9424 | attr->ns-> |
| 9425 | href))) { |
| 9426 | #ifdef DEBUG_STEP |
| 9427 | n++; |
| 9428 | #endif |
| 9429 | addNode(list, |
| 9430 | (xmlNodePtr) attr); |
| 9431 | } |
| 9432 | } |
| 9433 | } |
| 9434 | break; |
| 9435 | } |
| 9436 | case XML_NAMESPACE_DECL: |
| 9437 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9438 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9439 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9440 | if ((ns->prefix != NULL) && (name != NULL) |
| 9441 | && (xmlStrEqual(ns->prefix, name))) { |
| 9442 | #ifdef DEBUG_STEP |
| 9443 | n++; |
| 9444 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9445 | xmlXPathNodeSetAddNs(list, |
| 9446 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9447 | } |
| 9448 | } |
| 9449 | break; |
| 9450 | default: |
| 9451 | break; |
| 9452 | } |
| 9453 | break; |
| 9454 | break; |
| 9455 | } |
| 9456 | } while (cur != NULL); |
| 9457 | |
| 9458 | /* |
| 9459 | * If there is some predicate filtering do it now |
| 9460 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9461 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9462 | xmlXPathObjectPtr obj2; |
| 9463 | |
| 9464 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9465 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9466 | CHECK_TYPE0(XPATH_NODESET); |
| 9467 | obj2 = valuePop(ctxt); |
| 9468 | list = obj2->nodesetval; |
| 9469 | obj2->nodesetval = NULL; |
| 9470 | xmlXPathFreeObject(obj2); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9471 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 9472 | xmlXPathFreeObject(obj); |
| 9473 | xmlXPathFreeNodeSet(list); |
| 9474 | return(0); |
| 9475 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9476 | } |
| 9477 | if (ret == NULL) { |
| 9478 | ret = list; |
| 9479 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9480 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9481 | xmlXPathFreeNodeSet(list); |
| 9482 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9483 | } |
| 9484 | ctxt->context->node = tmp; |
| 9485 | #ifdef DEBUG_STEP |
| 9486 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9487 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9488 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9489 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9490 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9491 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9492 | ctxt->value->boolval = 1; |
| 9493 | ctxt->value->user = obj->user; |
| 9494 | obj->user = NULL; |
| 9495 | obj->boolval = 0; |
| 9496 | } |
| 9497 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9498 | return(t); |
| 9499 | } |
| 9500 | |
| 9501 | /** |
| 9502 | * xmlXPathNodeCollectAndTestNth: |
| 9503 | * @ctxt: the XPath Parser context |
| 9504 | * @op: the XPath precompiled step operation |
| 9505 | * @indx: the index to collect |
| 9506 | * @first: pointer to the first element in document order |
| 9507 | * @last: pointer to the last element in document order |
| 9508 | * |
| 9509 | * This is the function implementing a step: based on the current list |
| 9510 | * 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] | 9511 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9512 | * |
| 9513 | * Pushes the new NodeSet resulting from the search. |
| 9514 | * Returns the number of node traversed |
| 9515 | */ |
| 9516 | static int |
| 9517 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9518 | xmlXPathStepOpPtr op, int indx, |
| 9519 | xmlNodePtr * first, xmlNodePtr * last) |
| 9520 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9521 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9522 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9523 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9524 | const xmlChar *prefix = op->value4; |
| 9525 | const xmlChar *name = op->value5; |
| 9526 | const xmlChar *URI = NULL; |
| 9527 | int n = 0, t = 0; |
| 9528 | |
| 9529 | int i; |
| 9530 | xmlNodeSetPtr list; |
| 9531 | xmlXPathTraversalFunction next = NULL; |
| 9532 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9533 | xmlNodePtr cur = NULL; |
| 9534 | xmlXPathObjectPtr obj; |
| 9535 | xmlNodeSetPtr nodelist; |
| 9536 | xmlNodePtr tmp; |
| 9537 | |
| 9538 | CHECK_TYPE0(XPATH_NODESET); |
| 9539 | obj = valuePop(ctxt); |
| 9540 | addNode = xmlXPathNodeSetAdd; |
| 9541 | if (prefix != NULL) { |
| 9542 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9543 | if (URI == NULL) { |
| 9544 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9545 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9546 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9547 | } |
| 9548 | #ifdef DEBUG_STEP_NTH |
| 9549 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9550 | if (first != NULL) { |
| 9551 | if (*first != NULL) |
| 9552 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9553 | (*first)->name); |
| 9554 | else |
| 9555 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9556 | } |
| 9557 | if (last != NULL) { |
| 9558 | if (*last != NULL) |
| 9559 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9560 | (*last)->name); |
| 9561 | else |
| 9562 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9563 | } |
| 9564 | #endif |
| 9565 | switch (axis) { |
| 9566 | case AXIS_ANCESTOR: |
| 9567 | #ifdef DEBUG_STEP_NTH |
| 9568 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9569 | #endif |
| 9570 | first = NULL; |
| 9571 | next = xmlXPathNextAncestor; |
| 9572 | break; |
| 9573 | case AXIS_ANCESTOR_OR_SELF: |
| 9574 | #ifdef DEBUG_STEP_NTH |
| 9575 | xmlGenericError(xmlGenericErrorContext, |
| 9576 | "axis 'ancestors-or-self' "); |
| 9577 | #endif |
| 9578 | first = NULL; |
| 9579 | next = xmlXPathNextAncestorOrSelf; |
| 9580 | break; |
| 9581 | case AXIS_ATTRIBUTE: |
| 9582 | #ifdef DEBUG_STEP_NTH |
| 9583 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 9584 | #endif |
| 9585 | first = NULL; |
| 9586 | last = NULL; |
| 9587 | next = xmlXPathNextAttribute; |
| 9588 | break; |
| 9589 | case AXIS_CHILD: |
| 9590 | #ifdef DEBUG_STEP_NTH |
| 9591 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9592 | #endif |
| 9593 | last = NULL; |
| 9594 | next = xmlXPathNextChild; |
| 9595 | break; |
| 9596 | case AXIS_DESCENDANT: |
| 9597 | #ifdef DEBUG_STEP_NTH |
| 9598 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9599 | #endif |
| 9600 | last = NULL; |
| 9601 | next = xmlXPathNextDescendant; |
| 9602 | break; |
| 9603 | case AXIS_DESCENDANT_OR_SELF: |
| 9604 | #ifdef DEBUG_STEP_NTH |
| 9605 | xmlGenericError(xmlGenericErrorContext, |
| 9606 | "axis 'descendant-or-self' "); |
| 9607 | #endif |
| 9608 | last = NULL; |
| 9609 | next = xmlXPathNextDescendantOrSelf; |
| 9610 | break; |
| 9611 | case AXIS_FOLLOWING: |
| 9612 | #ifdef DEBUG_STEP_NTH |
| 9613 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9614 | #endif |
| 9615 | last = NULL; |
| 9616 | next = xmlXPathNextFollowing; |
| 9617 | break; |
| 9618 | case AXIS_FOLLOWING_SIBLING: |
| 9619 | #ifdef DEBUG_STEP_NTH |
| 9620 | xmlGenericError(xmlGenericErrorContext, |
| 9621 | "axis 'following-siblings' "); |
| 9622 | #endif |
| 9623 | last = NULL; |
| 9624 | next = xmlXPathNextFollowingSibling; |
| 9625 | break; |
| 9626 | case AXIS_NAMESPACE: |
| 9627 | #ifdef DEBUG_STEP_NTH |
| 9628 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9629 | #endif |
| 9630 | last = NULL; |
| 9631 | first = NULL; |
| 9632 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9633 | break; |
| 9634 | case AXIS_PARENT: |
| 9635 | #ifdef DEBUG_STEP_NTH |
| 9636 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9637 | #endif |
| 9638 | first = NULL; |
| 9639 | next = xmlXPathNextParent; |
| 9640 | break; |
| 9641 | case AXIS_PRECEDING: |
| 9642 | #ifdef DEBUG_STEP_NTH |
| 9643 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9644 | #endif |
| 9645 | first = NULL; |
| 9646 | next = xmlXPathNextPrecedingInternal; |
| 9647 | break; |
| 9648 | case AXIS_PRECEDING_SIBLING: |
| 9649 | #ifdef DEBUG_STEP_NTH |
| 9650 | xmlGenericError(xmlGenericErrorContext, |
| 9651 | "axis 'preceding-sibling' "); |
| 9652 | #endif |
| 9653 | first = NULL; |
| 9654 | next = xmlXPathNextPrecedingSibling; |
| 9655 | break; |
| 9656 | case AXIS_SELF: |
| 9657 | #ifdef DEBUG_STEP_NTH |
| 9658 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9659 | #endif |
| 9660 | first = NULL; |
| 9661 | last = NULL; |
| 9662 | next = xmlXPathNextSelf; |
| 9663 | break; |
| 9664 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9665 | if (next == NULL) { |
| 9666 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9667 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9668 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9669 | |
| 9670 | nodelist = obj->nodesetval; |
| 9671 | if (nodelist == NULL) { |
| 9672 | xmlXPathFreeObject(obj); |
| 9673 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9674 | return(0); |
| 9675 | } |
| 9676 | addNode = xmlXPathNodeSetAddUnique; |
| 9677 | #ifdef DEBUG_STEP_NTH |
| 9678 | xmlGenericError(xmlGenericErrorContext, |
| 9679 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9680 | switch (test) { |
| 9681 | case NODE_TEST_NONE: |
| 9682 | xmlGenericError(xmlGenericErrorContext, |
| 9683 | " searching for none !!!\n"); |
| 9684 | break; |
| 9685 | case NODE_TEST_TYPE: |
| 9686 | xmlGenericError(xmlGenericErrorContext, |
| 9687 | " searching for type %d\n", type); |
| 9688 | break; |
| 9689 | case NODE_TEST_PI: |
| 9690 | xmlGenericError(xmlGenericErrorContext, |
| 9691 | " searching for PI !!!\n"); |
| 9692 | break; |
| 9693 | case NODE_TEST_ALL: |
| 9694 | xmlGenericError(xmlGenericErrorContext, |
| 9695 | " searching for *\n"); |
| 9696 | break; |
| 9697 | case NODE_TEST_NS: |
| 9698 | xmlGenericError(xmlGenericErrorContext, |
| 9699 | " searching for namespace %s\n", |
| 9700 | prefix); |
| 9701 | break; |
| 9702 | case NODE_TEST_NAME: |
| 9703 | xmlGenericError(xmlGenericErrorContext, |
| 9704 | " searching for name %s\n", name); |
| 9705 | if (prefix != NULL) |
| 9706 | xmlGenericError(xmlGenericErrorContext, |
| 9707 | " with namespace %s\n", prefix); |
| 9708 | break; |
| 9709 | } |
| 9710 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9711 | #endif |
| 9712 | /* |
| 9713 | * 2.3 Node Tests |
| 9714 | * - For the attribute axis, the principal node type is attribute. |
| 9715 | * - For the namespace axis, the principal node type is namespace. |
| 9716 | * - For other axes, the principal node type is element. |
| 9717 | * |
| 9718 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9719 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9720 | * select all element children of the context node |
| 9721 | */ |
| 9722 | tmp = ctxt->context->node; |
| 9723 | list = xmlXPathNodeSetCreate(NULL); |
| 9724 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9725 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9726 | |
| 9727 | cur = NULL; |
| 9728 | n = 0; |
| 9729 | do { |
| 9730 | cur = next(ctxt, cur); |
| 9731 | if (cur == NULL) |
| 9732 | break; |
| 9733 | if ((first != NULL) && (*first == cur)) |
| 9734 | break; |
| 9735 | if (((t % 256) == 0) && |
| 9736 | (first != NULL) && (*first != NULL) && |
| 9737 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9738 | break; |
| 9739 | if ((last != NULL) && (*last == cur)) |
| 9740 | break; |
| 9741 | if (((t % 256) == 0) && |
| 9742 | (last != NULL) && (*last != NULL) && |
| 9743 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9744 | break; |
| 9745 | t++; |
| 9746 | switch (test) { |
| 9747 | case NODE_TEST_NONE: |
| 9748 | ctxt->context->node = tmp; |
| 9749 | STRANGE return(0); |
| 9750 | case NODE_TEST_TYPE: |
| 9751 | if ((cur->type == type) || |
| 9752 | ((type == NODE_TYPE_NODE) && |
| 9753 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9754 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9755 | (cur->type == XML_ELEMENT_NODE) || |
| 9756 | (cur->type == XML_PI_NODE) || |
| 9757 | (cur->type == XML_COMMENT_NODE) || |
| 9758 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 9759 | (cur->type == XML_TEXT_NODE))) || |
| 9760 | ((type == NODE_TYPE_TEXT) && |
| 9761 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9762 | n++; |
| 9763 | if (n == indx) |
| 9764 | addNode(list, cur); |
| 9765 | } |
| 9766 | break; |
| 9767 | case NODE_TEST_PI: |
| 9768 | if (cur->type == XML_PI_NODE) { |
| 9769 | if ((name != NULL) && |
| 9770 | (!xmlStrEqual(name, cur->name))) |
| 9771 | break; |
| 9772 | n++; |
| 9773 | if (n == indx) |
| 9774 | addNode(list, cur); |
| 9775 | } |
| 9776 | break; |
| 9777 | case NODE_TEST_ALL: |
| 9778 | if (axis == AXIS_ATTRIBUTE) { |
| 9779 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9780 | n++; |
| 9781 | if (n == indx) |
| 9782 | addNode(list, cur); |
| 9783 | } |
| 9784 | } else if (axis == AXIS_NAMESPACE) { |
| 9785 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9786 | n++; |
| 9787 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9788 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9789 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9790 | } |
| 9791 | } else { |
| 9792 | if (cur->type == XML_ELEMENT_NODE) { |
| 9793 | if (prefix == NULL) { |
| 9794 | n++; |
| 9795 | if (n == indx) |
| 9796 | addNode(list, cur); |
| 9797 | } else if ((cur->ns != NULL) && |
| 9798 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9799 | n++; |
| 9800 | if (n == indx) |
| 9801 | addNode(list, cur); |
| 9802 | } |
| 9803 | } |
| 9804 | } |
| 9805 | break; |
| 9806 | case NODE_TEST_NS:{ |
| 9807 | TODO; |
| 9808 | break; |
| 9809 | } |
| 9810 | case NODE_TEST_NAME: |
| 9811 | switch (cur->type) { |
| 9812 | case XML_ELEMENT_NODE: |
| 9813 | if (xmlStrEqual(name, cur->name)) { |
| 9814 | if (prefix == NULL) { |
| 9815 | if (cur->ns == NULL) { |
| 9816 | n++; |
| 9817 | if (n == indx) |
| 9818 | addNode(list, cur); |
| 9819 | } |
| 9820 | } else { |
| 9821 | if ((cur->ns != NULL) && |
| 9822 | (xmlStrEqual(URI, |
| 9823 | cur->ns->href))) { |
| 9824 | n++; |
| 9825 | if (n == indx) |
| 9826 | addNode(list, cur); |
| 9827 | } |
| 9828 | } |
| 9829 | } |
| 9830 | break; |
| 9831 | case XML_ATTRIBUTE_NODE:{ |
| 9832 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9833 | |
| 9834 | if (xmlStrEqual(name, attr->name)) { |
| 9835 | if (prefix == NULL) { |
| 9836 | if ((attr->ns == NULL) || |
| 9837 | (attr->ns->prefix == NULL)) { |
| 9838 | n++; |
| 9839 | if (n == indx) |
| 9840 | addNode(list, cur); |
| 9841 | } |
| 9842 | } else { |
| 9843 | if ((attr->ns != NULL) && |
| 9844 | (xmlStrEqual(URI, |
| 9845 | attr->ns-> |
| 9846 | href))) { |
| 9847 | n++; |
| 9848 | if (n == indx) |
| 9849 | addNode(list, cur); |
| 9850 | } |
| 9851 | } |
| 9852 | } |
| 9853 | break; |
| 9854 | } |
| 9855 | case XML_NAMESPACE_DECL: |
| 9856 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9857 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9858 | |
| 9859 | if ((ns->prefix != NULL) && (name != NULL) |
| 9860 | && (xmlStrEqual(ns->prefix, name))) { |
| 9861 | n++; |
| 9862 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9863 | xmlXPathNodeSetAddNs(list, |
| 9864 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9865 | } |
| 9866 | } |
| 9867 | break; |
| 9868 | default: |
| 9869 | break; |
| 9870 | } |
| 9871 | break; |
| 9872 | break; |
| 9873 | } |
| 9874 | } while (n < indx); |
| 9875 | } |
| 9876 | ctxt->context->node = tmp; |
| 9877 | #ifdef DEBUG_STEP_NTH |
| 9878 | xmlGenericError(xmlGenericErrorContext, |
| 9879 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9880 | t, list->nodeNr); |
| 9881 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9882 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9883 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9884 | ctxt->value->boolval = 1; |
| 9885 | ctxt->value->user = obj->user; |
| 9886 | obj->user = NULL; |
| 9887 | obj->boolval = 0; |
| 9888 | } |
| 9889 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9890 | return(t); |
| 9891 | } |
| 9892 | |
| 9893 | /** |
| 9894 | * xmlXPathCompOpEvalFirst: |
| 9895 | * @ctxt: the XPath parser context with the compiled expression |
| 9896 | * @op: an XPath compiled operation |
| 9897 | * @first: the first elem found so far |
| 9898 | * |
| 9899 | * Evaluate the Precompiled XPath operation searching only the first |
| 9900 | * element in document order |
| 9901 | * |
| 9902 | * Returns the number of examined objects. |
| 9903 | */ |
| 9904 | static int |
| 9905 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9906 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9907 | { |
| 9908 | int total = 0, cur; |
| 9909 | xmlXPathCompExprPtr comp; |
| 9910 | xmlXPathObjectPtr arg1, arg2; |
| 9911 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9912 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9913 | comp = ctxt->comp; |
| 9914 | switch (op->op) { |
| 9915 | case XPATH_OP_END: |
| 9916 | return (0); |
| 9917 | case XPATH_OP_UNION: |
| 9918 | total = |
| 9919 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9920 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9921 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9922 | if ((ctxt->value != NULL) |
| 9923 | && (ctxt->value->type == XPATH_NODESET) |
| 9924 | && (ctxt->value->nodesetval != NULL) |
| 9925 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9926 | /* |
| 9927 | * limit tree traversing to first node in the result |
| 9928 | */ |
| 9929 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9930 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9931 | } |
| 9932 | cur = |
| 9933 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9934 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9935 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9936 | CHECK_TYPE0(XPATH_NODESET); |
| 9937 | arg2 = valuePop(ctxt); |
| 9938 | |
| 9939 | CHECK_TYPE0(XPATH_NODESET); |
| 9940 | arg1 = valuePop(ctxt); |
| 9941 | |
| 9942 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9943 | arg2->nodesetval); |
| 9944 | valuePush(ctxt, arg1); |
| 9945 | xmlXPathFreeObject(arg2); |
| 9946 | /* optimizer */ |
| 9947 | if (total > cur) |
| 9948 | xmlXPathCompSwap(op); |
| 9949 | return (total + cur); |
| 9950 | case XPATH_OP_ROOT: |
| 9951 | xmlXPathRoot(ctxt); |
| 9952 | return (0); |
| 9953 | case XPATH_OP_NODE: |
| 9954 | if (op->ch1 != -1) |
| 9955 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9956 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9957 | if (op->ch2 != -1) |
| 9958 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9959 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9960 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9961 | return (total); |
| 9962 | case XPATH_OP_RESET: |
| 9963 | if (op->ch1 != -1) |
| 9964 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9965 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9966 | if (op->ch2 != -1) |
| 9967 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9968 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9969 | ctxt->context->node = NULL; |
| 9970 | return (total); |
| 9971 | case XPATH_OP_COLLECT:{ |
| 9972 | if (op->ch1 == -1) |
| 9973 | return (total); |
| 9974 | |
| 9975 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9976 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9977 | |
| 9978 | /* |
| 9979 | * Optimization for [n] selection where n is a number |
| 9980 | */ |
| 9981 | if ((op->ch2 != -1) && |
| 9982 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9983 | (comp->steps[op->ch2].ch1 == -1) && |
| 9984 | (comp->steps[op->ch2].ch2 != -1) && |
| 9985 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9986 | XPATH_OP_VALUE)) { |
| 9987 | xmlXPathObjectPtr val; |
| 9988 | |
| 9989 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9990 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9991 | int indx = (int) val->floatval; |
| 9992 | |
| 9993 | if (val->floatval == (float) indx) { |
| 9994 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 9995 | first, NULL); |
| 9996 | return (total); |
| 9997 | } |
| 9998 | } |
| 9999 | } |
| 10000 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 10001 | return (total); |
| 10002 | } |
| 10003 | case XPATH_OP_VALUE: |
| 10004 | valuePush(ctxt, |
| 10005 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10006 | return (0); |
| 10007 | case XPATH_OP_SORT: |
| 10008 | if (op->ch1 != -1) |
| 10009 | total += |
| 10010 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 10011 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10012 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10013 | if ((ctxt->value != NULL) |
| 10014 | && (ctxt->value->type == XPATH_NODESET) |
| 10015 | && (ctxt->value->nodesetval != NULL)) |
| 10016 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10017 | return (total); |
| 10018 | default: |
| 10019 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10020 | } |
| 10021 | } |
| 10022 | |
| 10023 | /** |
| 10024 | * xmlXPathCompOpEvalLast: |
| 10025 | * @ctxt: the XPath parser context with the compiled expression |
| 10026 | * @op: an XPath compiled operation |
| 10027 | * @last: the last elem found so far |
| 10028 | * |
| 10029 | * Evaluate the Precompiled XPath operation searching only the last |
| 10030 | * element in document order |
| 10031 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10032 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10033 | */ |
| 10034 | static int |
| 10035 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 10036 | xmlNodePtr * last) |
| 10037 | { |
| 10038 | int total = 0, cur; |
| 10039 | xmlXPathCompExprPtr comp; |
| 10040 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10041 | xmlNodePtr bak; |
| 10042 | xmlDocPtr bakd; |
| 10043 | int pp; |
| 10044 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10045 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10046 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10047 | comp = ctxt->comp; |
| 10048 | switch (op->op) { |
| 10049 | case XPATH_OP_END: |
| 10050 | return (0); |
| 10051 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10052 | bakd = ctxt->context->doc; |
| 10053 | bak = ctxt->context->node; |
| 10054 | pp = ctxt->context->proximityPosition; |
| 10055 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10056 | total = |
| 10057 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10058 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10059 | if ((ctxt->value != NULL) |
| 10060 | && (ctxt->value->type == XPATH_NODESET) |
| 10061 | && (ctxt->value->nodesetval != NULL) |
| 10062 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10063 | /* |
| 10064 | * limit tree traversing to first node in the result |
| 10065 | */ |
| 10066 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10067 | *last = |
| 10068 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 10069 | nodesetval->nodeNr - |
| 10070 | 1]; |
| 10071 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10072 | ctxt->context->doc = bakd; |
| 10073 | ctxt->context->node = bak; |
| 10074 | ctxt->context->proximityPosition = pp; |
| 10075 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10076 | cur = |
| 10077 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10078 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10079 | if ((ctxt->value != NULL) |
| 10080 | && (ctxt->value->type == XPATH_NODESET) |
| 10081 | && (ctxt->value->nodesetval != NULL) |
| 10082 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10083 | } |
| 10084 | CHECK_TYPE0(XPATH_NODESET); |
| 10085 | arg2 = valuePop(ctxt); |
| 10086 | |
| 10087 | CHECK_TYPE0(XPATH_NODESET); |
| 10088 | arg1 = valuePop(ctxt); |
| 10089 | |
| 10090 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10091 | arg2->nodesetval); |
| 10092 | valuePush(ctxt, arg1); |
| 10093 | xmlXPathFreeObject(arg2); |
| 10094 | /* optimizer */ |
| 10095 | if (total > cur) |
| 10096 | xmlXPathCompSwap(op); |
| 10097 | return (total + cur); |
| 10098 | case XPATH_OP_ROOT: |
| 10099 | xmlXPathRoot(ctxt); |
| 10100 | return (0); |
| 10101 | case XPATH_OP_NODE: |
| 10102 | if (op->ch1 != -1) |
| 10103 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10104 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10105 | if (op->ch2 != -1) |
| 10106 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10107 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10108 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10109 | return (total); |
| 10110 | case XPATH_OP_RESET: |
| 10111 | if (op->ch1 != -1) |
| 10112 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10113 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10114 | if (op->ch2 != -1) |
| 10115 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10116 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10117 | ctxt->context->node = NULL; |
| 10118 | return (total); |
| 10119 | case XPATH_OP_COLLECT:{ |
| 10120 | if (op->ch1 == -1) |
| 10121 | return (0); |
| 10122 | |
| 10123 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10124 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10125 | |
| 10126 | /* |
| 10127 | * Optimization for [n] selection where n is a number |
| 10128 | */ |
| 10129 | if ((op->ch2 != -1) && |
| 10130 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10131 | (comp->steps[op->ch2].ch1 == -1) && |
| 10132 | (comp->steps[op->ch2].ch2 != -1) && |
| 10133 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10134 | XPATH_OP_VALUE)) { |
| 10135 | xmlXPathObjectPtr val; |
| 10136 | |
| 10137 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10138 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10139 | int indx = (int) val->floatval; |
| 10140 | |
| 10141 | if (val->floatval == (float) indx) { |
| 10142 | total += |
| 10143 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10144 | indx, NULL, |
| 10145 | last); |
| 10146 | return (total); |
| 10147 | } |
| 10148 | } |
| 10149 | } |
| 10150 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 10151 | return (total); |
| 10152 | } |
| 10153 | case XPATH_OP_VALUE: |
| 10154 | valuePush(ctxt, |
| 10155 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10156 | return (0); |
| 10157 | case XPATH_OP_SORT: |
| 10158 | if (op->ch1 != -1) |
| 10159 | total += |
| 10160 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 10161 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10162 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10163 | if ((ctxt->value != NULL) |
| 10164 | && (ctxt->value->type == XPATH_NODESET) |
| 10165 | && (ctxt->value->nodesetval != NULL)) |
| 10166 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10167 | return (total); |
| 10168 | default: |
| 10169 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10170 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10171 | } |
| 10172 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10173 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10174 | * xmlXPathCompOpEval: |
| 10175 | * @ctxt: the XPath parser context with the compiled expression |
| 10176 | * @op: an XPath compiled operation |
| 10177 | * |
| 10178 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10179 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10180 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10181 | static int |
| 10182 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 10183 | { |
| 10184 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10185 | int equal, ret; |
| 10186 | xmlXPathCompExprPtr comp; |
| 10187 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10188 | xmlNodePtr bak; |
| 10189 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10190 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10191 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10192 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10193 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10194 | comp = ctxt->comp; |
| 10195 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10196 | case XPATH_OP_END: |
| 10197 | return (0); |
| 10198 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10199 | bakd = ctxt->context->doc; |
| 10200 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10201 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10202 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10203 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10204 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10205 | xmlXPathBooleanFunction(ctxt, 1); |
| 10206 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10207 | return (total); |
| 10208 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10209 | ctxt->context->doc = bakd; |
| 10210 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10211 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10212 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10213 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10214 | if (ctxt->error) { |
| 10215 | xmlXPathFreeObject(arg2); |
| 10216 | return(0); |
| 10217 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10218 | xmlXPathBooleanFunction(ctxt, 1); |
| 10219 | arg1 = valuePop(ctxt); |
| 10220 | arg1->boolval &= arg2->boolval; |
| 10221 | valuePush(ctxt, arg1); |
| 10222 | xmlXPathFreeObject(arg2); |
| 10223 | return (total); |
| 10224 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10225 | bakd = ctxt->context->doc; |
| 10226 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10227 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10228 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10229 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10230 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10231 | xmlXPathBooleanFunction(ctxt, 1); |
| 10232 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10233 | return (total); |
| 10234 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10235 | ctxt->context->doc = bakd; |
| 10236 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10237 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10238 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10239 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10240 | if (ctxt->error) { |
| 10241 | xmlXPathFreeObject(arg2); |
| 10242 | return(0); |
| 10243 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10244 | xmlXPathBooleanFunction(ctxt, 1); |
| 10245 | arg1 = valuePop(ctxt); |
| 10246 | arg1->boolval |= arg2->boolval; |
| 10247 | valuePush(ctxt, arg1); |
| 10248 | xmlXPathFreeObject(arg2); |
| 10249 | return (total); |
| 10250 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10251 | bakd = ctxt->context->doc; |
| 10252 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10253 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10254 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10255 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10256 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10257 | ctxt->context->doc = bakd; |
| 10258 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10259 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10260 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10261 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10262 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10263 | if (op->value) |
| 10264 | equal = xmlXPathEqualValues(ctxt); |
| 10265 | else |
| 10266 | equal = xmlXPathNotEqualValues(ctxt); |
| 10267 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10268 | return (total); |
| 10269 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10270 | bakd = ctxt->context->doc; |
| 10271 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10272 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10273 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10274 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10275 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10276 | ctxt->context->doc = bakd; |
| 10277 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10278 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10279 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10280 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10281 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10282 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 10283 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 10284 | return (total); |
| 10285 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10286 | bakd = ctxt->context->doc; |
| 10287 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10288 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10289 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10290 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10291 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10292 | if (op->ch2 != -1) { |
| 10293 | ctxt->context->doc = bakd; |
| 10294 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10295 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10296 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10297 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10298 | } |
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 | if (op->value == 0) |
| 10301 | xmlXPathSubValues(ctxt); |
| 10302 | else if (op->value == 1) |
| 10303 | xmlXPathAddValues(ctxt); |
| 10304 | else if (op->value == 2) |
| 10305 | xmlXPathValueFlipSign(ctxt); |
| 10306 | else if (op->value == 3) { |
| 10307 | CAST_TO_NUMBER; |
| 10308 | CHECK_TYPE0(XPATH_NUMBER); |
| 10309 | } |
| 10310 | return (total); |
| 10311 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10312 | bakd = ctxt->context->doc; |
| 10313 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10314 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10315 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10316 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10317 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10318 | ctxt->context->doc = bakd; |
| 10319 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10320 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10321 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10322 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10323 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10324 | if (op->value == 0) |
| 10325 | xmlXPathMultValues(ctxt); |
| 10326 | else if (op->value == 1) |
| 10327 | xmlXPathDivValues(ctxt); |
| 10328 | else if (op->value == 2) |
| 10329 | xmlXPathModValues(ctxt); |
| 10330 | return (total); |
| 10331 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10332 | bakd = ctxt->context->doc; |
| 10333 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10334 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10335 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10336 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10337 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10338 | ctxt->context->doc = bakd; |
| 10339 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10340 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10341 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10342 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10343 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10344 | CHECK_TYPE0(XPATH_NODESET); |
| 10345 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10346 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10347 | CHECK_TYPE0(XPATH_NODESET); |
| 10348 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10349 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10350 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10351 | arg2->nodesetval); |
| 10352 | valuePush(ctxt, arg1); |
| 10353 | xmlXPathFreeObject(arg2); |
| 10354 | return (total); |
| 10355 | case XPATH_OP_ROOT: |
| 10356 | xmlXPathRoot(ctxt); |
| 10357 | return (total); |
| 10358 | case XPATH_OP_NODE: |
| 10359 | if (op->ch1 != -1) |
| 10360 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
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 | if (op->ch2 != -1) |
| 10363 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10364 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10365 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10366 | return (total); |
| 10367 | case XPATH_OP_RESET: |
| 10368 | if (op->ch1 != -1) |
| 10369 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10370 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10371 | if (op->ch2 != -1) |
| 10372 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10373 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10374 | ctxt->context->node = NULL; |
| 10375 | return (total); |
| 10376 | case XPATH_OP_COLLECT:{ |
| 10377 | if (op->ch1 == -1) |
| 10378 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10379 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10380 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10381 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10382 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10383 | /* |
| 10384 | * Optimization for [n] selection where n is a number |
| 10385 | */ |
| 10386 | if ((op->ch2 != -1) && |
| 10387 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10388 | (comp->steps[op->ch2].ch1 == -1) && |
| 10389 | (comp->steps[op->ch2].ch2 != -1) && |
| 10390 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10391 | XPATH_OP_VALUE)) { |
| 10392 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 10393 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10394 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10395 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10396 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10397 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10398 | if (val->floatval == (float) indx) { |
| 10399 | total += |
| 10400 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10401 | indx, NULL, |
| 10402 | NULL); |
| 10403 | return (total); |
| 10404 | } |
| 10405 | } |
| 10406 | } |
| 10407 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 10408 | return (total); |
| 10409 | } |
| 10410 | case XPATH_OP_VALUE: |
| 10411 | valuePush(ctxt, |
| 10412 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10413 | return (total); |
| 10414 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10415 | xmlXPathObjectPtr val; |
| 10416 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10417 | if (op->ch1 != -1) |
| 10418 | total += |
| 10419 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10420 | if (op->value5 == NULL) { |
| 10421 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 10422 | if (val == NULL) { |
| 10423 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10424 | return(0); |
| 10425 | } |
| 10426 | valuePush(ctxt, val); |
| 10427 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10428 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10429 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10430 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10431 | if (URI == NULL) { |
| 10432 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10433 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10434 | op->value4, op->value5); |
| 10435 | return (total); |
| 10436 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10437 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 10438 | op->value4, URI); |
| 10439 | if (val == NULL) { |
| 10440 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10441 | return(0); |
| 10442 | } |
| 10443 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10444 | } |
| 10445 | return (total); |
| 10446 | } |
| 10447 | case XPATH_OP_FUNCTION:{ |
| 10448 | xmlXPathFunction func; |
| 10449 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10450 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10451 | |
| 10452 | if (op->ch1 != -1) |
| 10453 | total += |
| 10454 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10455 | if (ctxt->valueNr < op->value) { |
| 10456 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10457 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10458 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10459 | return (total); |
| 10460 | } |
| 10461 | for (i = 0; i < op->value; i++) |
| 10462 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 10463 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10464 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10465 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10466 | return (total); |
| 10467 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10468 | if (op->cache != NULL) |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 10469 | XML_CAST_FPTR(func) = op->cache; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10470 | else { |
| 10471 | const xmlChar *URI = NULL; |
| 10472 | |
| 10473 | if (op->value5 == NULL) |
| 10474 | func = |
| 10475 | xmlXPathFunctionLookup(ctxt->context, |
| 10476 | op->value4); |
| 10477 | else { |
| 10478 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10479 | if (URI == NULL) { |
| 10480 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10481 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10482 | op->value4, op->value5); |
| 10483 | return (total); |
| 10484 | } |
| 10485 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 10486 | op->value4, URI); |
| 10487 | } |
| 10488 | if (func == NULL) { |
| 10489 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10490 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10491 | op->value4); |
| 10492 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10493 | } |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 10494 | op->cache = XML_CAST_FPTR(func); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10495 | op->cacheURI = (void *) URI; |
| 10496 | } |
| 10497 | oldFunc = ctxt->context->function; |
| 10498 | oldFuncURI = ctxt->context->functionURI; |
| 10499 | ctxt->context->function = op->value4; |
| 10500 | ctxt->context->functionURI = op->cacheURI; |
| 10501 | func(ctxt, op->value); |
| 10502 | ctxt->context->function = oldFunc; |
| 10503 | ctxt->context->functionURI = oldFuncURI; |
| 10504 | return (total); |
| 10505 | } |
| 10506 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10507 | bakd = ctxt->context->doc; |
| 10508 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10509 | pp = ctxt->context->proximityPosition; |
| 10510 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10511 | if (op->ch1 != -1) |
| 10512 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10513 | ctxt->context->contextSize = cs; |
| 10514 | ctxt->context->proximityPosition = pp; |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10515 | ctxt->context->node = bak; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10516 | ctxt->context->doc = bakd; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10517 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10518 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10519 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10520 | ctxt->context->doc = bakd; |
| 10521 | ctxt->context->node = bak; |
| 10522 | CHECK_ERROR0; |
| 10523 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10524 | return (total); |
| 10525 | case XPATH_OP_PREDICATE: |
| 10526 | case XPATH_OP_FILTER:{ |
| 10527 | xmlXPathObjectPtr res; |
| 10528 | xmlXPathObjectPtr obj, tmp; |
| 10529 | xmlNodeSetPtr newset = NULL; |
| 10530 | xmlNodeSetPtr oldset; |
| 10531 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10532 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10533 | int i; |
| 10534 | |
| 10535 | /* |
| 10536 | * Optimization for ()[1] selection i.e. the first elem |
| 10537 | */ |
| 10538 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10539 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10540 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 10541 | xmlXPathObjectPtr val; |
| 10542 | |
| 10543 | val = comp->steps[op->ch2].value4; |
| 10544 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 10545 | (val->floatval == 1.0)) { |
| 10546 | xmlNodePtr first = NULL; |
| 10547 | |
| 10548 | total += |
| 10549 | xmlXPathCompOpEvalFirst(ctxt, |
| 10550 | &comp->steps[op->ch1], |
| 10551 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10552 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10553 | /* |
| 10554 | * The nodeset should be in document order, |
| 10555 | * Keep only the first value |
| 10556 | */ |
| 10557 | if ((ctxt->value != NULL) && |
| 10558 | (ctxt->value->type == XPATH_NODESET) && |
| 10559 | (ctxt->value->nodesetval != NULL) && |
| 10560 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 10561 | ctxt->value->nodesetval->nodeNr = 1; |
| 10562 | return (total); |
| 10563 | } |
| 10564 | } |
| 10565 | /* |
| 10566 | * Optimization for ()[last()] selection i.e. the last elem |
| 10567 | */ |
| 10568 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10569 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10570 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10571 | int f = comp->steps[op->ch2].ch1; |
| 10572 | |
| 10573 | if ((f != -1) && |
| 10574 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10575 | (comp->steps[f].value5 == NULL) && |
| 10576 | (comp->steps[f].value == 0) && |
| 10577 | (comp->steps[f].value4 != NULL) && |
| 10578 | (xmlStrEqual |
| 10579 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10580 | xmlNodePtr last = NULL; |
| 10581 | |
| 10582 | total += |
| 10583 | xmlXPathCompOpEvalLast(ctxt, |
| 10584 | &comp->steps[op->ch1], |
| 10585 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10586 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10587 | /* |
| 10588 | * The nodeset should be in document order, |
| 10589 | * Keep only the last value |
| 10590 | */ |
| 10591 | if ((ctxt->value != NULL) && |
| 10592 | (ctxt->value->type == XPATH_NODESET) && |
| 10593 | (ctxt->value->nodesetval != NULL) && |
| 10594 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10595 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10596 | ctxt->value->nodesetval->nodeTab[0] = |
| 10597 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10598 | value-> |
| 10599 | nodesetval-> |
| 10600 | nodeNr - |
| 10601 | 1]; |
| 10602 | ctxt->value->nodesetval->nodeNr = 1; |
| 10603 | } |
| 10604 | return (total); |
| 10605 | } |
| 10606 | } |
| 10607 | |
| 10608 | if (op->ch1 != -1) |
| 10609 | total += |
| 10610 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10611 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10612 | if (op->ch2 == -1) |
| 10613 | return (total); |
| 10614 | if (ctxt->value == NULL) |
| 10615 | return (total); |
| 10616 | |
| 10617 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10618 | |
| 10619 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10620 | /* |
| 10621 | * Hum are we filtering the result of an XPointer expression |
| 10622 | */ |
| 10623 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10624 | xmlLocationSetPtr newlocset = NULL; |
| 10625 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10626 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10627 | /* |
| 10628 | * Extract the old locset, and then evaluate the result of the |
| 10629 | * expression for all the element in the locset. use it to grow |
| 10630 | * up a new locset. |
| 10631 | */ |
| 10632 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10633 | obj = valuePop(ctxt); |
| 10634 | oldlocset = obj->user; |
| 10635 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10636 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10637 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10638 | ctxt->context->contextSize = 0; |
| 10639 | ctxt->context->proximityPosition = 0; |
| 10640 | if (op->ch2 != -1) |
| 10641 | total += |
| 10642 | xmlXPathCompOpEval(ctxt, |
| 10643 | &comp->steps[op->ch2]); |
| 10644 | res = valuePop(ctxt); |
| 10645 | if (res != NULL) |
| 10646 | xmlXPathFreeObject(res); |
| 10647 | valuePush(ctxt, obj); |
| 10648 | CHECK_ERROR0; |
| 10649 | return (total); |
| 10650 | } |
| 10651 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10652 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10653 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10654 | /* |
| 10655 | * Run the evaluation with a node list made of a |
| 10656 | * single item in the nodelocset. |
| 10657 | */ |
| 10658 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10659 | ctxt->context->contextSize = oldlocset->locNr; |
| 10660 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10661 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10662 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10663 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10664 | if (op->ch2 != -1) |
| 10665 | total += |
| 10666 | xmlXPathCompOpEval(ctxt, |
| 10667 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10668 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10669 | xmlXPathFreeObject(obj); |
| 10670 | return(0); |
| 10671 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10672 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10673 | /* |
| 10674 | * The result of the evaluation need to be tested to |
| 10675 | * decided whether the filter succeeded or not |
| 10676 | */ |
| 10677 | res = valuePop(ctxt); |
| 10678 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10679 | xmlXPtrLocationSetAdd(newlocset, |
| 10680 | xmlXPathObjectCopy |
| 10681 | (oldlocset->locTab[i])); |
| 10682 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10683 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10684 | /* |
| 10685 | * Cleanup |
| 10686 | */ |
| 10687 | if (res != NULL) |
| 10688 | xmlXPathFreeObject(res); |
| 10689 | if (ctxt->value == tmp) { |
| 10690 | res = valuePop(ctxt); |
| 10691 | xmlXPathFreeObject(res); |
| 10692 | } |
| 10693 | |
| 10694 | ctxt->context->node = NULL; |
| 10695 | } |
| 10696 | |
| 10697 | /* |
| 10698 | * The result is used as the new evaluation locset. |
| 10699 | */ |
| 10700 | xmlXPathFreeObject(obj); |
| 10701 | ctxt->context->node = NULL; |
| 10702 | ctxt->context->contextSize = -1; |
| 10703 | ctxt->context->proximityPosition = -1; |
| 10704 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10705 | ctxt->context->node = oldnode; |
| 10706 | return (total); |
| 10707 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10708 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10709 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10710 | /* |
| 10711 | * Extract the old set, and then evaluate the result of the |
| 10712 | * expression for all the element in the set. use it to grow |
| 10713 | * up a new set. |
| 10714 | */ |
| 10715 | CHECK_TYPE0(XPATH_NODESET); |
| 10716 | obj = valuePop(ctxt); |
| 10717 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10718 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10719 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10720 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10721 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10722 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10723 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10724 | ctxt->context->contextSize = 0; |
| 10725 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10726 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10727 | if (op->ch2 != -1) |
| 10728 | total += |
| 10729 | xmlXPathCompOpEval(ctxt, |
| 10730 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10731 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10732 | res = valuePop(ctxt); |
| 10733 | if (res != NULL) |
| 10734 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10735 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10736 | valuePush(ctxt, obj); |
| 10737 | ctxt->context->node = oldnode; |
| 10738 | CHECK_ERROR0; |
| 10739 | } else { |
| 10740 | /* |
| 10741 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10742 | * Also set the xpath document in case things like |
| 10743 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10744 | */ |
| 10745 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10746 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10747 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10748 | /* |
| 10749 | * Run the evaluation with a node list made of |
| 10750 | * a single item in the nodeset. |
| 10751 | */ |
| 10752 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10753 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 10754 | (oldset->nodeTab[i]->doc != NULL)) |
| 10755 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10756 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10757 | valuePush(ctxt, tmp); |
| 10758 | ctxt->context->contextSize = oldset->nodeNr; |
| 10759 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10760 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10761 | if (op->ch2 != -1) |
| 10762 | total += |
| 10763 | xmlXPathCompOpEval(ctxt, |
| 10764 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10765 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10766 | xmlXPathFreeNodeSet(newset); |
| 10767 | xmlXPathFreeObject(obj); |
| 10768 | return(0); |
| 10769 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10770 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10771 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10772 | * The result of the evaluation needs to be tested to |
| 10773 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10774 | */ |
| 10775 | res = valuePop(ctxt); |
| 10776 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10777 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10778 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10779 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10780 | /* |
| 10781 | * Cleanup |
| 10782 | */ |
| 10783 | if (res != NULL) |
| 10784 | xmlXPathFreeObject(res); |
| 10785 | if (ctxt->value == tmp) { |
| 10786 | res = valuePop(ctxt); |
| 10787 | xmlXPathFreeObject(res); |
| 10788 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10789 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10790 | ctxt->context->node = NULL; |
| 10791 | } |
| 10792 | |
| 10793 | /* |
| 10794 | * The result is used as the new evaluation set. |
| 10795 | */ |
| 10796 | xmlXPathFreeObject(obj); |
| 10797 | ctxt->context->node = NULL; |
| 10798 | ctxt->context->contextSize = -1; |
| 10799 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10800 | /* may want to move this past the '}' later */ |
| 10801 | ctxt->context->doc = oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10802 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10803 | } |
| 10804 | ctxt->context->node = oldnode; |
| 10805 | return (total); |
| 10806 | } |
| 10807 | case XPATH_OP_SORT: |
| 10808 | if (op->ch1 != -1) |
| 10809 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10810 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10811 | if ((ctxt->value != NULL) && |
| 10812 | (ctxt->value->type == XPATH_NODESET) && |
| 10813 | (ctxt->value->nodesetval != NULL)) |
| 10814 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10815 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10816 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10817 | case XPATH_OP_RANGETO:{ |
| 10818 | xmlXPathObjectPtr range; |
| 10819 | xmlXPathObjectPtr res, obj; |
| 10820 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10821 | xmlLocationSetPtr newlocset = NULL; |
| 10822 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10823 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10824 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10825 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10826 | if (op->ch1 != -1) |
| 10827 | total += |
| 10828 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10829 | if (op->ch2 == -1) |
| 10830 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10831 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10832 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10833 | /* |
| 10834 | * Extract the old locset, and then evaluate the result of the |
| 10835 | * expression for all the element in the locset. use it to grow |
| 10836 | * up a new locset. |
| 10837 | */ |
| 10838 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10839 | obj = valuePop(ctxt); |
| 10840 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10841 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10842 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10843 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10844 | ctxt->context->contextSize = 0; |
| 10845 | ctxt->context->proximityPosition = 0; |
| 10846 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 10847 | res = valuePop(ctxt); |
| 10848 | if (res != NULL) |
| 10849 | xmlXPathFreeObject(res); |
| 10850 | valuePush(ctxt, obj); |
| 10851 | CHECK_ERROR0; |
| 10852 | return (total); |
| 10853 | } |
| 10854 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10855 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10856 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10857 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10858 | * Run the evaluation with a node list made of a |
| 10859 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10860 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10861 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10862 | ctxt->context->contextSize = oldlocset->locNr; |
| 10863 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10864 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10865 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10866 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10867 | if (op->ch2 != -1) |
| 10868 | total += |
| 10869 | xmlXPathCompOpEval(ctxt, |
| 10870 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10871 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10872 | xmlXPathFreeObject(obj); |
| 10873 | return(0); |
| 10874 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10875 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10876 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10877 | if (res->type == XPATH_LOCATIONSET) { |
| 10878 | xmlLocationSetPtr rloc = |
| 10879 | (xmlLocationSetPtr)res->user; |
| 10880 | for (j=0; j<rloc->locNr; j++) { |
| 10881 | range = xmlXPtrNewRange( |
| 10882 | oldlocset->locTab[i]->user, |
| 10883 | oldlocset->locTab[i]->index, |
| 10884 | rloc->locTab[j]->user2, |
| 10885 | rloc->locTab[j]->index2); |
| 10886 | if (range != NULL) { |
| 10887 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10888 | } |
| 10889 | } |
| 10890 | } else { |
| 10891 | range = xmlXPtrNewRangeNodeObject( |
| 10892 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 10893 | if (range != NULL) { |
| 10894 | xmlXPtrLocationSetAdd(newlocset,range); |
| 10895 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10896 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10897 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10898 | /* |
| 10899 | * Cleanup |
| 10900 | */ |
| 10901 | if (res != NULL) |
| 10902 | xmlXPathFreeObject(res); |
| 10903 | if (ctxt->value == tmp) { |
| 10904 | res = valuePop(ctxt); |
| 10905 | xmlXPathFreeObject(res); |
| 10906 | } |
| 10907 | |
| 10908 | ctxt->context->node = NULL; |
| 10909 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10910 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10911 | CHECK_TYPE0(XPATH_NODESET); |
| 10912 | obj = valuePop(ctxt); |
| 10913 | oldset = obj->nodesetval; |
| 10914 | ctxt->context->node = NULL; |
| 10915 | |
| 10916 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10917 | |
| 10918 | if (oldset != NULL) { |
| 10919 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10920 | /* |
| 10921 | * Run the evaluation with a node list made of a single item |
| 10922 | * in the nodeset. |
| 10923 | */ |
| 10924 | ctxt->context->node = oldset->nodeTab[i]; |
| 10925 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10926 | valuePush(ctxt, tmp); |
| 10927 | |
| 10928 | if (op->ch2 != -1) |
| 10929 | total += |
| 10930 | xmlXPathCompOpEval(ctxt, |
| 10931 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10932 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10933 | xmlXPathFreeObject(obj); |
| 10934 | return(0); |
| 10935 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10936 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10937 | res = valuePop(ctxt); |
| 10938 | range = |
| 10939 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10940 | res); |
| 10941 | if (range != NULL) { |
| 10942 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10943 | } |
| 10944 | |
| 10945 | /* |
| 10946 | * Cleanup |
| 10947 | */ |
| 10948 | if (res != NULL) |
| 10949 | xmlXPathFreeObject(res); |
| 10950 | if (ctxt->value == tmp) { |
| 10951 | res = valuePop(ctxt); |
| 10952 | xmlXPathFreeObject(res); |
| 10953 | } |
| 10954 | |
| 10955 | ctxt->context->node = NULL; |
| 10956 | } |
| 10957 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
| 10960 | /* |
| 10961 | * The result is used as the new evaluation set. |
| 10962 | */ |
| 10963 | xmlXPathFreeObject(obj); |
| 10964 | ctxt->context->node = NULL; |
| 10965 | ctxt->context->contextSize = -1; |
| 10966 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10967 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10968 | return (total); |
| 10969 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10970 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10971 | } |
| 10972 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10973 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10974 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10975 | } |
| 10976 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 10977 | #ifdef XPATH_STREAMING |
| 10978 | /** |
| 10979 | * xmlXPathRunStreamEval: |
| 10980 | * @ctxt: the XPath parser context with the compiled expression |
| 10981 | * |
| 10982 | * Evaluate the Precompiled Streamable XPath expression in the given context. |
| 10983 | */ |
| 10984 | static xmlXPathObjectPtr |
| 10985 | xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp) { |
| 10986 | int max_depth; |
| 10987 | int from_root; |
| 10988 | int ret, depth; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 10989 | xmlNodePtr cur = NULL, limit = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 10990 | xmlXPathObjectPtr retval; |
| 10991 | xmlStreamCtxtPtr patstream; |
| 10992 | |
| 10993 | int nb_nodes = 0; |
| 10994 | |
| 10995 | if ((ctxt == NULL) || (comp == NULL)) |
| 10996 | return(NULL); |
| 10997 | max_depth = xmlPatternMaxDepth(comp); |
| 10998 | if (max_depth == -1) |
| 10999 | return(NULL); |
| 11000 | if (max_depth == -2) |
| 11001 | max_depth = 10000; |
| 11002 | from_root = xmlPatternFromRoot(comp); |
| 11003 | if (from_root < 0) |
| 11004 | return(NULL); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11005 | #if 0 |
| 11006 | printf("stream eval: depth %d from root %d\n", max_depth, from_root); |
| 11007 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11008 | |
| 11009 | retval = xmlXPathNewNodeSet(NULL); |
| 11010 | if (retval == NULL) |
| 11011 | return(NULL); |
| 11012 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11013 | if ((from_root) && (max_depth == 0)) { |
| 11014 | xmlXPathNodeSetAddUnique(retval->nodesetval, (xmlNodePtr) ctxt->doc); |
| 11015 | return(retval); |
| 11016 | } else if (max_depth == 0) { |
| 11017 | xmlXPathNodeSetAddUnique(retval->nodesetval, ctxt->node); |
| 11018 | return(retval); |
| 11019 | } |
| 11020 | if (from_root) { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 11021 | cur = (xmlNodePtr)ctxt->doc; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11022 | } else if (ctxt->node != NULL) { |
| 11023 | switch (ctxt->node->type) { |
| 11024 | case XML_ELEMENT_NODE: |
| 11025 | case XML_DOCUMENT_NODE: |
| 11026 | case XML_DOCUMENT_FRAG_NODE: |
| 11027 | case XML_HTML_DOCUMENT_NODE: |
| 11028 | #ifdef LIBXML_DOCB_ENABLED |
| 11029 | case XML_DOCB_DOCUMENT_NODE: |
| 11030 | #endif |
| 11031 | cur = ctxt->node; |
| 11032 | break; |
| 11033 | case XML_ATTRIBUTE_NODE: |
| 11034 | case XML_TEXT_NODE: |
| 11035 | case XML_CDATA_SECTION_NODE: |
| 11036 | case XML_ENTITY_REF_NODE: |
| 11037 | case XML_ENTITY_NODE: |
| 11038 | case XML_PI_NODE: |
| 11039 | case XML_COMMENT_NODE: |
| 11040 | case XML_NOTATION_NODE: |
| 11041 | case XML_DTD_NODE: |
| 11042 | case XML_DOCUMENT_TYPE_NODE: |
| 11043 | case XML_ELEMENT_DECL: |
| 11044 | case XML_ATTRIBUTE_DECL: |
| 11045 | case XML_ENTITY_DECL: |
| 11046 | case XML_NAMESPACE_DECL: |
| 11047 | case XML_XINCLUDE_START: |
| 11048 | case XML_XINCLUDE_END: |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11049 | break; |
| 11050 | } |
| 11051 | limit = cur; |
| 11052 | } |
| 11053 | if (cur == NULL) |
| 11054 | return(retval); |
| 11055 | |
| 11056 | patstream = xmlPatternGetStreamCtxt(comp); |
| 11057 | if (patstream == NULL) { |
| 11058 | return(retval); |
| 11059 | } |
| 11060 | |
| 11061 | if (from_root) { |
| 11062 | ret = xmlStreamPush(patstream, NULL, NULL); |
| 11063 | if (ret < 0) { |
| 11064 | } else if (ret == 1) { |
| 11065 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11066 | } |
| 11067 | } |
| 11068 | |
| 11069 | depth = 0; |
| 11070 | goto scan_children; |
| 11071 | do { |
| 11072 | next_node: |
| 11073 | nb_nodes++; |
| 11074 | if (cur->type == XML_ELEMENT_NODE) { |
| 11075 | ret = xmlStreamPush(patstream, cur->name, |
| 11076 | (cur->ns ? cur->ns->href : NULL)); |
| 11077 | if (ret < 0) { |
| 11078 | } else if (ret == 1) { |
| 11079 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11080 | } |
| 11081 | if ((cur->children == NULL) || (depth >= max_depth)) { |
| 11082 | ret = xmlStreamPop(patstream); |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame^] | 11083 | while (cur->next != NULL) { |
| 11084 | cur = cur->next; |
| 11085 | if ((cur->type != XML_ENTITY_DECL) && |
| 11086 | (cur->type != XML_DTD_NODE)) |
| 11087 | goto next_node; |
| 11088 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11089 | } |
| 11090 | } |
| 11091 | |
| 11092 | scan_children: |
| 11093 | if ((cur->children != NULL) && (depth < max_depth)) { |
| 11094 | /* |
| 11095 | * Do not descend on entities declarations |
| 11096 | */ |
| 11097 | if (cur->children->type != XML_ENTITY_DECL) { |
| 11098 | cur = cur->children; |
| 11099 | depth++; |
| 11100 | /* |
| 11101 | * Skip DTDs |
| 11102 | */ |
| 11103 | if (cur->type != XML_DTD_NODE) |
| 11104 | continue; |
| 11105 | } |
| 11106 | } |
| 11107 | |
| 11108 | if (cur == limit) |
| 11109 | break; |
| 11110 | |
| 11111 | while (cur->next != NULL) { |
| 11112 | cur = cur->next; |
| 11113 | if ((cur->type != XML_ENTITY_DECL) && |
| 11114 | (cur->type != XML_DTD_NODE)) |
| 11115 | goto next_node; |
| 11116 | } |
| 11117 | |
| 11118 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11119 | cur = cur->parent; |
| 11120 | depth--; |
| 11121 | if ((cur == NULL) || (cur == limit)) |
| 11122 | goto done; |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame^] | 11123 | if (cur->type == XML_ELEMENT_NODE) |
| 11124 | ret = xmlStreamPop(patstream); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11125 | if (cur->next != NULL) { |
| 11126 | cur = cur->next; |
| 11127 | break; |
| 11128 | } |
| 11129 | } while (cur != NULL); |
| 11130 | |
| 11131 | } while ((cur != NULL) && (depth >= 0)); |
| 11132 | done: |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11133 | #if 0 |
| 11134 | printf("stream eval: checked %d nodes selected %d\n", |
| 11135 | nb_nodes, retval->nodesetval->nodeNr); |
| 11136 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11137 | xmlFreeStreamCtxt(patstream); |
| 11138 | return(retval); |
| 11139 | } |
| 11140 | #endif /* XPATH_STREAMING */ |
| 11141 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11142 | /** |
| 11143 | * xmlXPathRunEval: |
| 11144 | * @ctxt: the XPath parser context with the compiled expression |
| 11145 | * |
| 11146 | * Evaluate the Precompiled XPath expression in the given context. |
| 11147 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11148 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11149 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 11150 | xmlXPathCompExprPtr comp; |
| 11151 | |
| 11152 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 11153 | return; |
| 11154 | |
| 11155 | if (ctxt->valueTab == NULL) { |
| 11156 | /* Allocate the value stack */ |
| 11157 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 11158 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 11159 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 11160 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11161 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11162 | } |
| 11163 | ctxt->valueNr = 0; |
| 11164 | ctxt->valueMax = 10; |
| 11165 | ctxt->value = NULL; |
| 11166 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11167 | #ifdef XPATH_STREAMING |
| 11168 | if (ctxt->comp->stream) { |
| 11169 | xmlXPathObjectPtr ret; |
| 11170 | ret = xmlXPathRunStreamEval(ctxt->context, ctxt->comp->stream); |
| 11171 | if (ret != NULL) { |
| 11172 | valuePush(ctxt, ret); |
| 11173 | return; |
| 11174 | } |
| 11175 | } |
| 11176 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11177 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 11178 | if(comp->last < 0) { |
| 11179 | xmlGenericError(xmlGenericErrorContext, |
| 11180 | "xmlXPathRunEval: last is less than zero\n"); |
| 11181 | return; |
| 11182 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11183 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 11184 | } |
| 11185 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11186 | /************************************************************************ |
| 11187 | * * |
| 11188 | * Public interfaces * |
| 11189 | * * |
| 11190 | ************************************************************************/ |
| 11191 | |
| 11192 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11193 | * xmlXPathEvalPredicate: |
| 11194 | * @ctxt: the XPath context |
| 11195 | * @res: the Predicate Expression evaluation result |
| 11196 | * |
| 11197 | * Evaluate a predicate result for the current node. |
| 11198 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 11199 | * the result to a boolean. If the result is a number, the result will |
| 11200 | * be converted to true if the number is equal to the position of the |
| 11201 | * context node in the context node list (as returned by the position |
| 11202 | * function) and will be converted to false otherwise; if the result |
| 11203 | * is not a number, then the result will be converted as if by a call |
| 11204 | * to the boolean function. |
| 11205 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11206 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11207 | */ |
| 11208 | int |
| 11209 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 11210 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11211 | switch (res->type) { |
| 11212 | case XPATH_BOOLEAN: |
| 11213 | return(res->boolval); |
| 11214 | case XPATH_NUMBER: |
| 11215 | return(res->floatval == ctxt->proximityPosition); |
| 11216 | case XPATH_NODESET: |
| 11217 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11218 | if (res->nodesetval == NULL) |
| 11219 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11220 | return(res->nodesetval->nodeNr != 0); |
| 11221 | case XPATH_STRING: |
| 11222 | return((res->stringval != NULL) && |
| 11223 | (xmlStrlen(res->stringval) != 0)); |
| 11224 | default: |
| 11225 | STRANGE |
| 11226 | } |
| 11227 | return(0); |
| 11228 | } |
| 11229 | |
| 11230 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11231 | * xmlXPathEvaluatePredicateResult: |
| 11232 | * @ctxt: the XPath Parser context |
| 11233 | * @res: the Predicate Expression evaluation result |
| 11234 | * |
| 11235 | * Evaluate a predicate result for the current node. |
| 11236 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 11237 | * the result to a boolean. If the result is a number, the result will |
| 11238 | * be converted to true if the number is equal to the position of the |
| 11239 | * context node in the context node list (as returned by the position |
| 11240 | * function) and will be converted to false otherwise; if the result |
| 11241 | * is not a number, then the result will be converted as if by a call |
| 11242 | * to the boolean function. |
| 11243 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11244 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11245 | */ |
| 11246 | int |
| 11247 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 11248 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 11249 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11250 | switch (res->type) { |
| 11251 | case XPATH_BOOLEAN: |
| 11252 | return(res->boolval); |
| 11253 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 11254 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 11255 | return((res->floatval == ctxt->context->proximityPosition) && |
| 11256 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11257 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11258 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11259 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11260 | case XPATH_NODESET: |
| 11261 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 11262 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 11263 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11264 | return(res->nodesetval->nodeNr != 0); |
| 11265 | case XPATH_STRING: |
| 11266 | return((res->stringval != NULL) && |
| 11267 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11268 | #ifdef LIBXML_XPTR_ENABLED |
| 11269 | case XPATH_LOCATIONSET:{ |
| 11270 | xmlLocationSetPtr ptr = res->user; |
| 11271 | if (ptr == NULL) |
| 11272 | return(0); |
| 11273 | return (ptr->locNr != 0); |
| 11274 | } |
| 11275 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11276 | default: |
| 11277 | STRANGE |
| 11278 | } |
| 11279 | return(0); |
| 11280 | } |
| 11281 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11282 | #ifdef XPATH_STREAMING |
| 11283 | /** |
| 11284 | * xmlXPathTryStreamCompile: |
| 11285 | * @ctxt: an XPath context |
| 11286 | * @str: the XPath expression |
| 11287 | * |
| 11288 | * Try to compile the XPath expression as a streamable subset. |
| 11289 | * |
| 11290 | * Returns the compiled expression or NULL if failed to compile. |
| 11291 | */ |
| 11292 | static xmlXPathCompExprPtr |
| 11293 | xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 11294 | /* |
| 11295 | * Optimization: use streaming patterns when the XPath expression can |
| 11296 | * be compiled to a stream lookup |
| 11297 | */ |
| 11298 | xmlPatternPtr stream; |
| 11299 | xmlXPathCompExprPtr comp; |
| 11300 | xmlDictPtr dict = NULL; |
| 11301 | const xmlChar **namespaces = NULL; |
| 11302 | xmlNsPtr ns; |
| 11303 | int i, j; |
| 11304 | |
| 11305 | if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) && |
| 11306 | (!xmlStrchr(str, '@'))) { |
| 11307 | if (ctxt != NULL) { |
| 11308 | dict = ctxt->dict; |
| 11309 | if (ctxt->nsNr > 0) { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 11310 | namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*)); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11311 | if (namespaces == NULL) { |
| 11312 | xmlXPathErrMemory(ctxt, "allocating namespaces array\n"); |
| 11313 | return(NULL); |
| 11314 | } |
| 11315 | for (i = 0, j = 0; (j < ctxt->nsNr); j++) { |
| 11316 | ns = ctxt->namespaces[j]; |
| 11317 | namespaces[i++] = ns->href; |
| 11318 | namespaces[i++] = ns->prefix; |
| 11319 | } |
| 11320 | namespaces[i++] = NULL; |
| 11321 | namespaces[i++] = NULL; |
| 11322 | } |
| 11323 | } |
| 11324 | |
| 11325 | stream = xmlPatterncompile(str, dict, 0, &namespaces[0]); |
| 11326 | if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) { |
| 11327 | comp = xmlXPathNewCompExpr(); |
| 11328 | if (comp == NULL) { |
| 11329 | xmlXPathErrMemory(ctxt, "allocating streamable expression\n"); |
| 11330 | return(NULL); |
| 11331 | } |
| 11332 | comp->stream = stream; |
| 11333 | comp->dict = dict; |
| 11334 | if (comp->dict) |
| 11335 | xmlDictReference(comp->dict); |
| 11336 | return(comp); |
| 11337 | } |
| 11338 | xmlFreePattern(stream); |
| 11339 | } |
| 11340 | return(NULL); |
| 11341 | } |
| 11342 | #endif /* XPATH_STREAMING */ |
| 11343 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11344 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11345 | * xmlXPathCtxtCompile: |
| 11346 | * @ctxt: an XPath context |
| 11347 | * @str: the XPath expression |
| 11348 | * |
| 11349 | * Compile an XPath expression |
| 11350 | * |
| 11351 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 11352 | * the caller has to free the object. |
| 11353 | */ |
| 11354 | xmlXPathCompExprPtr |
| 11355 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 11356 | xmlXPathParserContextPtr pctxt; |
| 11357 | xmlXPathCompExprPtr comp; |
| 11358 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11359 | #ifdef XPATH_STREAMING |
| 11360 | comp = xmlXPathTryStreamCompile(ctxt, str); |
| 11361 | if (comp != NULL) |
| 11362 | return(comp); |
| 11363 | #endif |
| 11364 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11365 | xmlXPathInit(); |
| 11366 | |
| 11367 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11368 | xmlXPathCompileExpr(pctxt); |
| 11369 | |
| 11370 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 11371 | { |
| 11372 | xmlXPathFreeParserContext(pctxt); |
| 11373 | return (0); |
| 11374 | } |
| 11375 | |
| 11376 | if (*pctxt->cur != 0) { |
| 11377 | /* |
| 11378 | * aleksey: in some cases this line prints *second* error message |
| 11379 | * (see bug #78858) and probably this should be fixed. |
| 11380 | * However, we are not sure that all error messages are printed |
| 11381 | * out in other places. It's not critical so we leave it as-is for now |
| 11382 | */ |
| 11383 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11384 | comp = NULL; |
| 11385 | } else { |
| 11386 | comp = pctxt->comp; |
| 11387 | pctxt->comp = NULL; |
| 11388 | } |
| 11389 | xmlXPathFreeParserContext(pctxt); |
| 11390 | if (comp != NULL) { |
| 11391 | comp->expr = xmlStrdup(str); |
| 11392 | #ifdef DEBUG_EVAL_COUNTS |
| 11393 | comp->string = xmlStrdup(str); |
| 11394 | comp->nb = 0; |
| 11395 | #endif |
| 11396 | } |
| 11397 | return(comp); |
| 11398 | } |
| 11399 | |
| 11400 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11401 | * xmlXPathCompile: |
| 11402 | * @str: the XPath expression |
| 11403 | * |
| 11404 | * Compile an XPath expression |
| 11405 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 11406 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11407 | * the caller has to free the object. |
| 11408 | */ |
| 11409 | xmlXPathCompExprPtr |
| 11410 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11411 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11412 | } |
| 11413 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11414 | /** |
| 11415 | * xmlXPathCompiledEval: |
| 11416 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11417 | * @ctx: the XPath context |
| 11418 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11419 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11420 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11421 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11422 | * the caller has to free the object. |
| 11423 | */ |
| 11424 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11425 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11426 | xmlXPathParserContextPtr ctxt; |
| 11427 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11428 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11429 | #ifndef LIBXML_THREAD_ENABLED |
| 11430 | static int reentance = 0; |
| 11431 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11432 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11433 | CHECK_CTXT(ctx) |
| 11434 | |
| 11435 | if (comp == NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11436 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11437 | xmlXPathInit(); |
| 11438 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11439 | #ifndef LIBXML_THREAD_ENABLED |
| 11440 | reentance++; |
| 11441 | if (reentance > 1) |
| 11442 | xmlXPathDisableOptimizer = 1; |
| 11443 | #endif |
| 11444 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11445 | #ifdef DEBUG_EVAL_COUNTS |
| 11446 | comp->nb++; |
| 11447 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 11448 | fprintf(stderr, "100 x %s\n", comp->string); |
| 11449 | comp->nb = 0; |
| 11450 | } |
| 11451 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11452 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 11453 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11454 | |
| 11455 | if (ctxt->value == NULL) { |
| 11456 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11457 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11458 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11459 | } else { |
| 11460 | res = valuePop(ctxt); |
| 11461 | } |
| 11462 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11463 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11464 | do { |
| 11465 | tmp = valuePop(ctxt); |
| 11466 | if (tmp != NULL) { |
| 11467 | if (tmp != init) |
| 11468 | stack++; |
| 11469 | xmlXPathFreeObject(tmp); |
| 11470 | } |
| 11471 | } while (tmp != NULL); |
| 11472 | if ((stack != 0) && (res != NULL)) { |
| 11473 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11474 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11475 | stack); |
| 11476 | } |
| 11477 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11478 | xmlXPathFreeObject(res); |
| 11479 | res = NULL; |
| 11480 | } |
| 11481 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11482 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11483 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11484 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11485 | #ifndef LIBXML_THREAD_ENABLED |
| 11486 | reentance--; |
| 11487 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11488 | return(res); |
| 11489 | } |
| 11490 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11491 | /** |
| 11492 | * xmlXPathEvalExpr: |
| 11493 | * @ctxt: the XPath Parser context |
| 11494 | * |
| 11495 | * Parse and evaluate an XPath expression in the given context, |
| 11496 | * then push the result on the context stack |
| 11497 | */ |
| 11498 | void |
| 11499 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11500 | #ifdef XPATH_STREAMING |
| 11501 | xmlXPathCompExprPtr comp; |
| 11502 | #endif |
| 11503 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 11504 | if (ctxt == NULL) return; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11505 | |
| 11506 | #ifdef XPATH_STREAMING |
| 11507 | comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base); |
| 11508 | if (comp != NULL) { |
| 11509 | if (ctxt->comp != NULL) |
| 11510 | xmlXPathFreeCompExpr(ctxt->comp); |
| 11511 | ctxt->comp = comp; |
| 11512 | if (ctxt->cur != NULL) |
| 11513 | while (*ctxt->cur != 0) ctxt->cur++; |
| 11514 | } else |
| 11515 | #endif |
| 11516 | { |
| 11517 | xmlXPathCompileExpr(ctxt); |
| 11518 | } |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 11519 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11520 | xmlXPathRunEval(ctxt); |
| 11521 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11522 | |
| 11523 | /** |
| 11524 | * xmlXPathEval: |
| 11525 | * @str: the XPath expression |
| 11526 | * @ctx: the XPath context |
| 11527 | * |
| 11528 | * Evaluate the XPath Location Path in the given context. |
| 11529 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11530 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11531 | * the caller has to free the object. |
| 11532 | */ |
| 11533 | xmlXPathObjectPtr |
| 11534 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 11535 | xmlXPathParserContextPtr ctxt; |
| 11536 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11537 | int stack = 0; |
| 11538 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11539 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11540 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11541 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11542 | |
| 11543 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 11544 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11545 | |
| 11546 | if (ctxt->value == NULL) { |
| 11547 | xmlGenericError(xmlGenericErrorContext, |
| 11548 | "xmlXPathEval: evaluation failed\n"); |
| 11549 | res = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11550 | } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL) |
| 11551 | #ifdef XPATH_STREAMING |
| 11552 | && (ctxt->comp->stream == NULL) |
| 11553 | #endif |
| 11554 | ) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11555 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11556 | res = NULL; |
| 11557 | } else { |
| 11558 | res = valuePop(ctxt); |
| 11559 | } |
| 11560 | |
| 11561 | do { |
| 11562 | tmp = valuePop(ctxt); |
| 11563 | if (tmp != NULL) { |
| 11564 | if (tmp != init) |
| 11565 | stack++; |
| 11566 | xmlXPathFreeObject(tmp); |
| 11567 | } |
| 11568 | } while (tmp != NULL); |
| 11569 | if ((stack != 0) && (res != NULL)) { |
| 11570 | xmlGenericError(xmlGenericErrorContext, |
| 11571 | "xmlXPathEval: %d object left on the stack\n", |
| 11572 | stack); |
| 11573 | } |
| 11574 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11575 | xmlXPathFreeObject(res); |
| 11576 | res = NULL; |
| 11577 | } |
| 11578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11579 | xmlXPathFreeParserContext(ctxt); |
| 11580 | return(res); |
| 11581 | } |
| 11582 | |
| 11583 | /** |
| 11584 | * xmlXPathEvalExpression: |
| 11585 | * @str: the XPath expression |
| 11586 | * @ctxt: the XPath context |
| 11587 | * |
| 11588 | * Evaluate the XPath expression in the given context. |
| 11589 | * |
| 11590 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 11591 | * the caller has to free the object. |
| 11592 | */ |
| 11593 | xmlXPathObjectPtr |
| 11594 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 11595 | xmlXPathParserContextPtr pctxt; |
| 11596 | xmlXPathObjectPtr res, tmp; |
| 11597 | int stack = 0; |
| 11598 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11599 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11600 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11601 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11602 | |
| 11603 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11604 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11605 | |
| 11606 | if (*pctxt->cur != 0) { |
| 11607 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11608 | res = NULL; |
| 11609 | } else { |
| 11610 | res = valuePop(pctxt); |
| 11611 | } |
| 11612 | do { |
| 11613 | tmp = valuePop(pctxt); |
| 11614 | if (tmp != NULL) { |
| 11615 | xmlXPathFreeObject(tmp); |
| 11616 | stack++; |
| 11617 | } |
| 11618 | } while (tmp != NULL); |
| 11619 | if ((stack != 0) && (res != NULL)) { |
| 11620 | xmlGenericError(xmlGenericErrorContext, |
| 11621 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 11622 | stack); |
| 11623 | } |
| 11624 | xmlXPathFreeParserContext(pctxt); |
| 11625 | return(res); |
| 11626 | } |
| 11627 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11628 | /************************************************************************ |
| 11629 | * * |
| 11630 | * Extra functions not pertaining to the XPath spec * |
| 11631 | * * |
| 11632 | ************************************************************************/ |
| 11633 | /** |
| 11634 | * xmlXPathEscapeUriFunction: |
| 11635 | * @ctxt: the XPath Parser context |
| 11636 | * @nargs: the number of arguments |
| 11637 | * |
| 11638 | * Implement the escape-uri() XPath function |
| 11639 | * string escape-uri(string $str, bool $escape-reserved) |
| 11640 | * |
| 11641 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 11642 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 11643 | * or part of a URI. The effect of the function is to replace any special |
| 11644 | * character in the string by an escape sequence of the form %xx%yy..., |
| 11645 | * where xxyy... is the hexadecimal representation of the octets used to |
| 11646 | * represent the character in UTF-8. |
| 11647 | * |
| 11648 | * The set of characters that are escaped depends on the setting of the |
| 11649 | * boolean argument $escape-reserved. |
| 11650 | * |
| 11651 | * If $escape-reserved is true, all characters are escaped other than lower |
| 11652 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 11653 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 11654 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 11655 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 11656 | * A-F). |
| 11657 | * |
| 11658 | * If $escape-reserved is false, the behavior differs in that characters |
| 11659 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 11660 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 11661 | * |
| 11662 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 11663 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 11664 | * compared using string comparison functions, this function must always use |
| 11665 | * the upper-case letters A-F. |
| 11666 | * |
| 11667 | * Generally, $escape-reserved should be set to true when escaping a string |
| 11668 | * that is to form a single part of a URI, and to false when escaping an |
| 11669 | * entire URI or URI reference. |
| 11670 | * |
| 11671 | * In the case of non-ascii characters, the string is encoded according to |
| 11672 | * utf-8 and then converted according to RFC 2396. |
| 11673 | * |
| 11674 | * Examples |
| 11675 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 11676 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 11677 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 11678 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 11679 | * |
| 11680 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 11681 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11682 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 11683 | xmlXPathObjectPtr str; |
| 11684 | int escape_reserved; |
| 11685 | xmlBufferPtr target; |
| 11686 | xmlChar *cptr; |
| 11687 | xmlChar escape[4]; |
| 11688 | |
| 11689 | CHECK_ARITY(2); |
| 11690 | |
| 11691 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 11692 | |
| 11693 | CAST_TO_STRING; |
| 11694 | str = valuePop(ctxt); |
| 11695 | |
| 11696 | target = xmlBufferCreate(); |
| 11697 | |
| 11698 | escape[0] = '%'; |
| 11699 | escape[3] = 0; |
| 11700 | |
| 11701 | if (target) { |
| 11702 | for (cptr = str->stringval; *cptr; cptr++) { |
| 11703 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 11704 | (*cptr >= 'a' && *cptr <= 'z') || |
| 11705 | (*cptr >= '0' && *cptr <= '9') || |
| 11706 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 11707 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 11708 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 11709 | (*cptr == '%' && |
| 11710 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 11711 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 11712 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 11713 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 11714 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 11715 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 11716 | (!escape_reserved && |
| 11717 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 11718 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 11719 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 11720 | *cptr == ','))) { |
| 11721 | xmlBufferAdd(target, cptr, 1); |
| 11722 | } else { |
| 11723 | if ((*cptr >> 4) < 10) |
| 11724 | escape[1] = '0' + (*cptr >> 4); |
| 11725 | else |
| 11726 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 11727 | if ((*cptr & 0xF) < 10) |
| 11728 | escape[2] = '0' + (*cptr & 0xF); |
| 11729 | else |
| 11730 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 11731 | |
| 11732 | xmlBufferAdd(target, &escape[0], 3); |
| 11733 | } |
| 11734 | } |
| 11735 | } |
| 11736 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 11737 | xmlBufferFree(target); |
| 11738 | xmlXPathFreeObject(str); |
| 11739 | } |
| 11740 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11741 | /** |
| 11742 | * xmlXPathRegisterAllFunctions: |
| 11743 | * @ctxt: the XPath context |
| 11744 | * |
| 11745 | * Registers all default XPath functions in this context |
| 11746 | */ |
| 11747 | void |
| 11748 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 11749 | { |
| 11750 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 11751 | xmlXPathBooleanFunction); |
| 11752 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 11753 | xmlXPathCeilingFunction); |
| 11754 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 11755 | xmlXPathCountFunction); |
| 11756 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 11757 | xmlXPathConcatFunction); |
| 11758 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 11759 | xmlXPathContainsFunction); |
| 11760 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 11761 | xmlXPathIdFunction); |
| 11762 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 11763 | xmlXPathFalseFunction); |
| 11764 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 11765 | xmlXPathFloorFunction); |
| 11766 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 11767 | xmlXPathLastFunction); |
| 11768 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 11769 | xmlXPathLangFunction); |
| 11770 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 11771 | xmlXPathLocalNameFunction); |
| 11772 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 11773 | xmlXPathNotFunction); |
| 11774 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 11775 | xmlXPathNameFunction); |
| 11776 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 11777 | xmlXPathNamespaceURIFunction); |
| 11778 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 11779 | xmlXPathNormalizeFunction); |
| 11780 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 11781 | xmlXPathNumberFunction); |
| 11782 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 11783 | xmlXPathPositionFunction); |
| 11784 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 11785 | xmlXPathRoundFunction); |
| 11786 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 11787 | xmlXPathStringFunction); |
| 11788 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 11789 | xmlXPathStringLengthFunction); |
| 11790 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 11791 | xmlXPathStartsWithFunction); |
| 11792 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 11793 | xmlXPathSubstringFunction); |
| 11794 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 11795 | xmlXPathSubstringBeforeFunction); |
| 11796 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 11797 | xmlXPathSubstringAfterFunction); |
| 11798 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 11799 | xmlXPathSumFunction); |
| 11800 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 11801 | xmlXPathTrueFunction); |
| 11802 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 11803 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11804 | |
| 11805 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 11806 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 11807 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11808 | } |
| 11809 | |
| 11810 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 11811 | #define bottom_xpath |
| 11812 | #include "elfgcchack.h" |