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 | /* |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 68 | * XP_PATTERN_TO_ANY_NODE_ENABLED: when an XPath expression can be |
| 69 | * evaluated using the streaming mode (pattern.c) then this is used to |
| 70 | * enable resolution to nodes of type text-node, cdata-section-node, |
| 71 | * comment-node and pi-node. The only known scenario where this is |
| 72 | * needed is an expression like "foo//.", "//.", etc.; i.e. an expression |
| 73 | * where the final node to be selected can be of any type. |
| 74 | * Disabling this #define will result in an incorrect evaluation to |
| 75 | * only element-nodes and the document node. |
| 76 | */ |
| 77 | #define XP_PATTERN_TO_ANY_NODE_ENABLED |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 78 | |
| 79 | /* |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 80 | * XP_OPTIMIZED_NON_ELEM_COMPARISON: |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 81 | * If defined, this will use xmlXPathCmpNodesExt() instead of |
| 82 | * xmlXPathCmpNodes(). The new function is optimized comparison of |
| 83 | * non-element nodes; actually it will speed up comparison only if |
| 84 | * xmlXPathOrderDocElems() was called in order to index the elements of |
| 85 | * a tree in document order; Libxslt does such an indexing, thus it will |
| 86 | * benefit from this optimization. |
| 87 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 88 | #define XP_OPTIMIZED_NON_ELEM_COMPARISON |
| 89 | |
| 90 | /* |
| 91 | * XP_OPTIMIZED_FILTER_FIRST: |
| 92 | * If defined, this will optimize expressions like "key('foo', 'val')[b][1]" |
| 93 | * in a way, that it stop evaluation at the first node. |
| 94 | */ |
| 95 | #define XP_OPTIMIZED_FILTER_FIRST |
| 96 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 97 | /* |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 98 | * TODO: |
| 99 | * There are a few spots where some tests are done which depend upon ascii |
| 100 | * data. These should be enhanced for full UTF8 support (see particularly |
| 101 | * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) |
| 102 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 103 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 104 | #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 105 | /************************************************************************ |
| 106 | * * |
| 107 | * Floating point stuff * |
| 108 | * * |
| 109 | ************************************************************************/ |
| 110 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 111 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 112 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 113 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 114 | #include "trionan.c" |
| 115 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 116 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | * The lack of portability of this section of the libc is annoying ! |
| 118 | */ |
| 119 | double xmlXPathNAN = 0; |
| 120 | double xmlXPathPINF = 1; |
| 121 | double xmlXPathNINF = -1; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 122 | static double xmlXPathNZERO = 0; /* not exported from headers */ |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 123 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 124 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 125 | /** |
| 126 | * xmlXPathInit: |
| 127 | * |
| 128 | * Initialize the XPath environment |
| 129 | */ |
| 130 | void |
| 131 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 132 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 133 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 134 | xmlXPathPINF = trio_pinf(); |
| 135 | xmlXPathNINF = trio_ninf(); |
| 136 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 137 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 138 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 139 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 142 | /** |
| 143 | * xmlXPathIsNaN: |
| 144 | * @val: a double value |
| 145 | * |
| 146 | * Provides a portable isnan() function to detect whether a double |
| 147 | * is a NotaNumber. Based on trio code |
| 148 | * http://sourceforge.net/projects/ctrio/ |
| 149 | * |
| 150 | * Returns 1 if the value is a NaN, 0 otherwise |
| 151 | */ |
| 152 | int |
| 153 | xmlXPathIsNaN(double val) { |
| 154 | return(trio_isnan(val)); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * xmlXPathIsInf: |
| 159 | * @val: a double value |
| 160 | * |
| 161 | * Provides a portable isinf() function to detect whether a double |
| 162 | * is a +Infinite or -Infinite. Based on trio code |
| 163 | * http://sourceforge.net/projects/ctrio/ |
| 164 | * |
| 165 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 166 | */ |
| 167 | int |
| 168 | xmlXPathIsInf(double val) { |
| 169 | return(trio_isinf(val)); |
| 170 | } |
| 171 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 172 | #endif /* SCHEMAS or XPATH */ |
| 173 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 174 | /** |
| 175 | * xmlXPathGetSign: |
| 176 | * @val: a double value |
| 177 | * |
| 178 | * Provides a portable function to detect the sign of a double |
| 179 | * Modified from trio code |
| 180 | * http://sourceforge.net/projects/ctrio/ |
| 181 | * |
| 182 | * Returns 1 if the value is Negative, 0 if positive |
| 183 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 184 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 185 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 186 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 190 | /* |
| 191 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 192 | * the test should just be name[0] = ' ' |
| 193 | */ |
| 194 | /* #define DEBUG */ |
| 195 | /* #define DEBUG_STEP */ |
| 196 | /* #define DEBUG_STEP_NTH */ |
| 197 | /* #define DEBUG_EXPR */ |
| 198 | /* #define DEBUG_EVAL_COUNTS */ |
| 199 | |
| 200 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 201 | NULL, |
| 202 | XML_NAMESPACE_DECL, |
| 203 | XML_XML_NAMESPACE, |
| 204 | BAD_CAST "xml", |
| 205 | NULL |
| 206 | }; |
| 207 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 208 | #ifndef LIBXML_THREAD_ENABLED |
| 209 | /* |
| 210 | * Optimizer is disabled only when threaded apps are detected while |
| 211 | * the library ain't compiled for thread safety. |
| 212 | */ |
| 213 | static int xmlXPathDisableOptimizer = 0; |
| 214 | #endif |
| 215 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 216 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 217 | * * |
| 218 | * Error handling routines * |
| 219 | * * |
| 220 | ************************************************************************/ |
| 221 | |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 222 | /** |
| 223 | * XP_ERRORNULL: |
| 224 | * @X: the error code |
| 225 | * |
| 226 | * Macro to raise an XPath error and return NULL. |
| 227 | */ |
| 228 | #define XP_ERRORNULL(X) \ |
| 229 | { xmlXPathErr(ctxt, X); return(NULL); } |
| 230 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 231 | /* |
| 232 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 233 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 234 | static const char *xmlXPathErrorMessages[] = { |
| 235 | "Ok\n", |
| 236 | "Number encoding\n", |
| 237 | "Unfinished literal\n", |
| 238 | "Start of literal\n", |
| 239 | "Expected $ for variable reference\n", |
| 240 | "Undefined variable\n", |
| 241 | "Invalid predicate\n", |
| 242 | "Invalid expression\n", |
| 243 | "Missing closing curly brace\n", |
| 244 | "Unregistered function\n", |
| 245 | "Invalid operand\n", |
| 246 | "Invalid type\n", |
| 247 | "Invalid number of arguments\n", |
| 248 | "Invalid context size\n", |
| 249 | "Invalid context position\n", |
| 250 | "Memory allocation error\n", |
| 251 | "Syntax error\n", |
| 252 | "Resource error\n", |
| 253 | "Sub resource error\n", |
| 254 | "Undefined namespace prefix\n", |
| 255 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 256 | "Char out of XML range\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 257 | "Invalid or incomplete context\n", |
| 258 | "?? Unknown error ??\n" /* Must be last in the list! */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 259 | }; |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 260 | #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \ |
| 261 | sizeof(xmlXPathErrorMessages[0])) - 1) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 262 | /** |
| 263 | * xmlXPathErrMemory: |
| 264 | * @ctxt: an XPath context |
| 265 | * @extra: extra informations |
| 266 | * |
| 267 | * Handle a redefinition of attribute error |
| 268 | */ |
| 269 | static void |
| 270 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 271 | { |
| 272 | if (ctxt != NULL) { |
| 273 | if (extra) { |
| 274 | xmlChar buf[200]; |
| 275 | |
| 276 | xmlStrPrintf(buf, 200, |
| 277 | BAD_CAST "Memory allocation failed : %s\n", |
| 278 | extra); |
| 279 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 280 | } else { |
| 281 | ctxt->lastError.message = (char *) |
| 282 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 283 | } |
| 284 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 285 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 286 | if (ctxt->error != NULL) |
| 287 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 288 | } else { |
| 289 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 290 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 291 | NULL, NULL, XML_FROM_XPATH, |
| 292 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 293 | extra, NULL, NULL, 0, 0, |
| 294 | "Memory allocation failed : %s\n", extra); |
| 295 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 296 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 297 | NULL, NULL, XML_FROM_XPATH, |
| 298 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 299 | NULL, NULL, NULL, 0, 0, |
| 300 | "Memory allocation failed\n"); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 305 | * xmlXPathPErrMemory: |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 306 | * @ctxt: an XPath parser context |
| 307 | * @extra: extra informations |
| 308 | * |
| 309 | * Handle a redefinition of attribute error |
| 310 | */ |
| 311 | static void |
| 312 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 313 | { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 314 | if (ctxt == NULL) |
| 315 | xmlXPathErrMemory(NULL, extra); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 316 | else { |
| 317 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 318 | xmlXPathErrMemory(ctxt->context, extra); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 319 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * xmlXPathErr: |
| 324 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 325 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 326 | * |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 327 | * Handle an XPath error |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 328 | */ |
| 329 | void |
| 330 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 331 | { |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 332 | if ((error < 0) || (error > MAXERRNO)) |
| 333 | error = MAXERRNO; |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 334 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 335 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 336 | NULL, NULL, XML_FROM_XPATH, |
| 337 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 338 | XML_ERR_ERROR, NULL, 0, |
| 339 | NULL, NULL, NULL, 0, 0, |
| 340 | xmlXPathErrorMessages[error]); |
| 341 | return; |
| 342 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 343 | ctxt->error = error; |
| 344 | if (ctxt->context == NULL) { |
| 345 | __xmlRaiseError(NULL, NULL, NULL, |
| 346 | NULL, NULL, XML_FROM_XPATH, |
| 347 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 348 | XML_ERR_ERROR, NULL, 0, |
| 349 | (const char *) ctxt->base, NULL, NULL, |
| 350 | ctxt->cur - ctxt->base, 0, |
| 351 | xmlXPathErrorMessages[error]); |
| 352 | return; |
| 353 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 354 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 355 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 356 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 357 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 358 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 359 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 360 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 361 | if (ctxt->context->error != NULL) { |
| 362 | ctxt->context->error(ctxt->context->userData, |
| 363 | &ctxt->context->lastError); |
| 364 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 365 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 366 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 367 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 368 | XML_ERR_ERROR, NULL, 0, |
| 369 | (const char *) ctxt->base, NULL, NULL, |
| 370 | ctxt->cur - ctxt->base, 0, |
| 371 | xmlXPathErrorMessages[error]); |
| 372 | } |
| 373 | |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * xmlXPatherror: |
| 378 | * @ctxt: the XPath Parser context |
| 379 | * @file: the file name |
| 380 | * @line: the line number |
| 381 | * @no: the error number |
| 382 | * |
| 383 | * Formats an error message. |
| 384 | */ |
| 385 | void |
| 386 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 387 | int line ATTRIBUTE_UNUSED, int no) { |
| 388 | xmlXPathErr(ctxt, no); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 393 | * * |
| 394 | * Parser Types * |
| 395 | * * |
| 396 | ************************************************************************/ |
| 397 | |
| 398 | /* |
| 399 | * Types are private: |
| 400 | */ |
| 401 | |
| 402 | typedef enum { |
| 403 | XPATH_OP_END=0, |
| 404 | XPATH_OP_AND, |
| 405 | XPATH_OP_OR, |
| 406 | XPATH_OP_EQUAL, |
| 407 | XPATH_OP_CMP, |
| 408 | XPATH_OP_PLUS, |
| 409 | XPATH_OP_MULT, |
| 410 | XPATH_OP_UNION, |
| 411 | XPATH_OP_ROOT, |
| 412 | XPATH_OP_NODE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 413 | XPATH_OP_RESET, /* 10 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 414 | XPATH_OP_COLLECT, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 415 | XPATH_OP_VALUE, /* 12 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 416 | XPATH_OP_VARIABLE, |
| 417 | XPATH_OP_FUNCTION, |
| 418 | XPATH_OP_ARG, |
| 419 | XPATH_OP_PREDICATE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 420 | XPATH_OP_FILTER, /* 17 */ |
| 421 | XPATH_OP_SORT /* 18 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 422 | #ifdef LIBXML_XPTR_ENABLED |
| 423 | ,XPATH_OP_RANGETO |
| 424 | #endif |
| 425 | } xmlXPathOp; |
| 426 | |
| 427 | typedef enum { |
| 428 | AXIS_ANCESTOR = 1, |
| 429 | AXIS_ANCESTOR_OR_SELF, |
| 430 | AXIS_ATTRIBUTE, |
| 431 | AXIS_CHILD, |
| 432 | AXIS_DESCENDANT, |
| 433 | AXIS_DESCENDANT_OR_SELF, |
| 434 | AXIS_FOLLOWING, |
| 435 | AXIS_FOLLOWING_SIBLING, |
| 436 | AXIS_NAMESPACE, |
| 437 | AXIS_PARENT, |
| 438 | AXIS_PRECEDING, |
| 439 | AXIS_PRECEDING_SIBLING, |
| 440 | AXIS_SELF |
| 441 | } xmlXPathAxisVal; |
| 442 | |
| 443 | typedef enum { |
| 444 | NODE_TEST_NONE = 0, |
| 445 | NODE_TEST_TYPE = 1, |
| 446 | NODE_TEST_PI = 2, |
| 447 | NODE_TEST_ALL = 3, |
| 448 | NODE_TEST_NS = 4, |
| 449 | NODE_TEST_NAME = 5 |
| 450 | } xmlXPathTestVal; |
| 451 | |
| 452 | typedef enum { |
| 453 | NODE_TYPE_NODE = 0, |
| 454 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 455 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 456 | NODE_TYPE_PI = XML_PI_NODE |
| 457 | } xmlXPathTypeVal; |
| 458 | |
| 459 | |
| 460 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 461 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 462 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 463 | xmlXPathOp op; /* The identifier of the operation */ |
| 464 | int ch1; /* First child */ |
| 465 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 466 | int value; |
| 467 | int value2; |
| 468 | int value3; |
| 469 | void *value4; |
| 470 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 471 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 472 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 476 | int nbStep; /* Number of steps in this expression */ |
| 477 | int maxStep; /* Maximum number of steps allocated */ |
| 478 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 479 | int last; /* index of last step in expression */ |
| 480 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 481 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 482 | #ifdef DEBUG_EVAL_COUNTS |
| 483 | int nb; |
| 484 | xmlChar *string; |
| 485 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 486 | #ifdef XPATH_STREAMING |
| 487 | xmlPatternPtr stream; |
| 488 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | /************************************************************************ |
| 492 | * * |
| 493 | * Parser Type functions * |
| 494 | * * |
| 495 | ************************************************************************/ |
| 496 | |
| 497 | /** |
| 498 | * xmlXPathNewCompExpr: |
| 499 | * |
| 500 | * Create a new Xpath component |
| 501 | * |
| 502 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 503 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 504 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 505 | xmlXPathNewCompExpr(void) { |
| 506 | xmlXPathCompExprPtr cur; |
| 507 | |
| 508 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 509 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 510 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 511 | return(NULL); |
| 512 | } |
| 513 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 514 | cur->maxStep = 10; |
| 515 | cur->nbStep = 0; |
| 516 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 517 | sizeof(xmlXPathStepOp)); |
| 518 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 519 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 520 | xmlFree(cur); |
| 521 | return(NULL); |
| 522 | } |
| 523 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 524 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 525 | #ifdef DEBUG_EVAL_COUNTS |
| 526 | cur->nb = 0; |
| 527 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 528 | return(cur); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * xmlXPathFreeCompExpr: |
| 533 | * @comp: an XPATH comp |
| 534 | * |
| 535 | * Free up the memory allocated by @comp |
| 536 | */ |
| 537 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 538 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 539 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 540 | xmlXPathStepOpPtr op; |
| 541 | int i; |
| 542 | |
| 543 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 544 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 545 | if (comp->dict == NULL) { |
| 546 | for (i = 0; i < comp->nbStep; i++) { |
| 547 | op = &comp->steps[i]; |
| 548 | if (op->value4 != NULL) { |
| 549 | if (op->op == XPATH_OP_VALUE) |
| 550 | xmlXPathFreeObject(op->value4); |
| 551 | else |
| 552 | xmlFree(op->value4); |
| 553 | } |
| 554 | if (op->value5 != NULL) |
| 555 | xmlFree(op->value5); |
| 556 | } |
| 557 | } else { |
| 558 | for (i = 0; i < comp->nbStep; i++) { |
| 559 | op = &comp->steps[i]; |
| 560 | if (op->value4 != NULL) { |
| 561 | if (op->op == XPATH_OP_VALUE) |
| 562 | xmlXPathFreeObject(op->value4); |
| 563 | } |
| 564 | } |
| 565 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 566 | } |
| 567 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 568 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 569 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 570 | #ifdef DEBUG_EVAL_COUNTS |
| 571 | if (comp->string != NULL) { |
| 572 | xmlFree(comp->string); |
| 573 | } |
| 574 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 575 | #ifdef XPATH_STREAMING |
| 576 | if (comp->stream != NULL) { |
| 577 | xmlFreePatternList(comp->stream); |
| 578 | } |
| 579 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 580 | if (comp->expr != NULL) { |
| 581 | xmlFree(comp->expr); |
| 582 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 583 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 584 | xmlFree(comp); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * xmlXPathCompExprAdd: |
| 589 | * @comp: the compiled expression |
| 590 | * @ch1: first child index |
| 591 | * @ch2: second child index |
| 592 | * @op: an op |
| 593 | * @value: the first int value |
| 594 | * @value2: the second int value |
| 595 | * @value3: the third int value |
| 596 | * @value4: the first string value |
| 597 | * @value5: the second string value |
| 598 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 599 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 600 | * |
| 601 | * Returns -1 in case of failure, the index otherwise |
| 602 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 603 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 604 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 605 | xmlXPathOp op, int value, |
| 606 | int value2, int value3, void *value4, void *value5) { |
| 607 | if (comp->nbStep >= comp->maxStep) { |
| 608 | xmlXPathStepOp *real; |
| 609 | |
| 610 | comp->maxStep *= 2; |
| 611 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 612 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 613 | if (real == NULL) { |
| 614 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 615 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 616 | return(-1); |
| 617 | } |
| 618 | comp->steps = real; |
| 619 | } |
| 620 | comp->last = comp->nbStep; |
| 621 | comp->steps[comp->nbStep].ch1 = ch1; |
| 622 | comp->steps[comp->nbStep].ch2 = ch2; |
| 623 | comp->steps[comp->nbStep].op = op; |
| 624 | comp->steps[comp->nbStep].value = value; |
| 625 | comp->steps[comp->nbStep].value2 = value2; |
| 626 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 627 | if ((comp->dict != NULL) && |
| 628 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 629 | (op == XPATH_OP_COLLECT))) { |
| 630 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 631 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 632 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 633 | xmlFree(value4); |
| 634 | } else |
| 635 | comp->steps[comp->nbStep].value4 = NULL; |
| 636 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 637 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 638 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 639 | xmlFree(value5); |
| 640 | } else |
| 641 | comp->steps[comp->nbStep].value5 = NULL; |
| 642 | } else { |
| 643 | comp->steps[comp->nbStep].value4 = value4; |
| 644 | comp->steps[comp->nbStep].value5 = value5; |
| 645 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 646 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 647 | return(comp->nbStep++); |
| 648 | } |
| 649 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 650 | /** |
| 651 | * xmlXPathCompSwap: |
| 652 | * @comp: the compiled expression |
| 653 | * @op: operation index |
| 654 | * |
| 655 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 656 | */ |
| 657 | static void |
| 658 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 659 | int tmp; |
| 660 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 661 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 662 | /* |
| 663 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 664 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 665 | * application |
| 666 | */ |
| 667 | if (xmlXPathDisableOptimizer) |
| 668 | return; |
| 669 | #endif |
| 670 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 671 | tmp = op->ch1; |
| 672 | op->ch1 = op->ch2; |
| 673 | op->ch2 = tmp; |
| 674 | } |
| 675 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 676 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 677 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 678 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 679 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 680 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 681 | (op), (val), (val2), (val3), (val4), (val5)) |
| 682 | |
| 683 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 684 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 685 | |
| 686 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 687 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 688 | |
| 689 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 690 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 691 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 692 | |
| 693 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 694 | * * |
| 695 | * Debugging related functions * |
| 696 | * * |
| 697 | ************************************************************************/ |
| 698 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | #define STRANGE \ |
| 700 | xmlGenericError(xmlGenericErrorContext, \ |
| 701 | "Internal error at %s:%d\n", \ |
| 702 | __FILE__, __LINE__); |
| 703 | |
| 704 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 705 | static void |
| 706 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 707 | int i; |
| 708 | char shift[100]; |
| 709 | |
| 710 | for (i = 0;((i < depth) && (i < 25));i++) |
| 711 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 712 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 713 | if (cur == NULL) { |
| 714 | fprintf(output, shift); |
| 715 | fprintf(output, "Node is NULL !\n"); |
| 716 | return; |
| 717 | |
| 718 | } |
| 719 | |
| 720 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 721 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 722 | fprintf(output, shift); |
| 723 | fprintf(output, " /\n"); |
| 724 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 725 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 726 | else |
| 727 | xmlDebugDumpOneNode(output, cur, depth); |
| 728 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 729 | static void |
| 730 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 731 | xmlNodePtr tmp; |
| 732 | int i; |
| 733 | char shift[100]; |
| 734 | |
| 735 | for (i = 0;((i < depth) && (i < 25));i++) |
| 736 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 737 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 738 | if (cur == NULL) { |
| 739 | fprintf(output, shift); |
| 740 | fprintf(output, "Node is NULL !\n"); |
| 741 | return; |
| 742 | |
| 743 | } |
| 744 | |
| 745 | while (cur != NULL) { |
| 746 | tmp = cur; |
| 747 | cur = cur->next; |
| 748 | xmlDebugDumpOneNode(output, tmp, depth); |
| 749 | } |
| 750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 751 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 752 | static void |
| 753 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 754 | int i; |
| 755 | char shift[100]; |
| 756 | |
| 757 | for (i = 0;((i < depth) && (i < 25));i++) |
| 758 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 759 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 760 | |
| 761 | if (cur == NULL) { |
| 762 | fprintf(output, shift); |
| 763 | fprintf(output, "NodeSet is NULL !\n"); |
| 764 | return; |
| 765 | |
| 766 | } |
| 767 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 768 | if (cur != NULL) { |
| 769 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 770 | for (i = 0;i < cur->nodeNr;i++) { |
| 771 | fprintf(output, shift); |
| 772 | fprintf(output, "%d", i + 1); |
| 773 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 774 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 778 | static void |
| 779 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 780 | int i; |
| 781 | char shift[100]; |
| 782 | |
| 783 | for (i = 0;((i < depth) && (i < 25));i++) |
| 784 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 785 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 786 | |
| 787 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 788 | fprintf(output, shift); |
| 789 | fprintf(output, "Value Tree is NULL !\n"); |
| 790 | return; |
| 791 | |
| 792 | } |
| 793 | |
| 794 | fprintf(output, shift); |
| 795 | fprintf(output, "%d", i + 1); |
| 796 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 797 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 798 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 799 | static void |
| 800 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 801 | int i; |
| 802 | char shift[100]; |
| 803 | |
| 804 | for (i = 0;((i < depth) && (i < 25));i++) |
| 805 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 806 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 807 | |
| 808 | if (cur == NULL) { |
| 809 | fprintf(output, shift); |
| 810 | fprintf(output, "LocationSet is NULL !\n"); |
| 811 | return; |
| 812 | |
| 813 | } |
| 814 | |
| 815 | for (i = 0;i < cur->locNr;i++) { |
| 816 | fprintf(output, shift); |
| 817 | fprintf(output, "%d : ", i + 1); |
| 818 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 819 | } |
| 820 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 821 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 822 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 823 | /** |
| 824 | * xmlXPathDebugDumpObject: |
| 825 | * @output: the FILE * to dump the output |
| 826 | * @cur: the object to inspect |
| 827 | * @depth: indentation level |
| 828 | * |
| 829 | * Dump the content of the object for debugging purposes |
| 830 | */ |
| 831 | void |
| 832 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 833 | int i; |
| 834 | char shift[100]; |
| 835 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 836 | if (output == NULL) return; |
| 837 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 838 | for (i = 0;((i < depth) && (i < 25));i++) |
| 839 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 840 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 841 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 842 | |
| 843 | fprintf(output, shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 844 | |
| 845 | if (cur == NULL) { |
| 846 | fprintf(output, "Object is empty (NULL)\n"); |
| 847 | return; |
| 848 | } |
| 849 | switch(cur->type) { |
| 850 | case XPATH_UNDEFINED: |
| 851 | fprintf(output, "Object is uninitialized\n"); |
| 852 | break; |
| 853 | case XPATH_NODESET: |
| 854 | fprintf(output, "Object is a Node Set :\n"); |
| 855 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 856 | break; |
| 857 | case XPATH_XSLT_TREE: |
| 858 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 859 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 860 | break; |
| 861 | case XPATH_BOOLEAN: |
| 862 | fprintf(output, "Object is a Boolean : "); |
| 863 | if (cur->boolval) fprintf(output, "true\n"); |
| 864 | else fprintf(output, "false\n"); |
| 865 | break; |
| 866 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 867 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 868 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 869 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 870 | break; |
| 871 | case -1: |
| 872 | fprintf(output, "Object is a number : -Infinity\n"); |
| 873 | break; |
| 874 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 875 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 876 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 877 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 878 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 879 | } else { |
| 880 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 881 | } |
| 882 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 883 | break; |
| 884 | case XPATH_STRING: |
| 885 | fprintf(output, "Object is a string : "); |
| 886 | xmlDebugDumpString(output, cur->stringval); |
| 887 | fprintf(output, "\n"); |
| 888 | break; |
| 889 | case XPATH_POINT: |
| 890 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 891 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 892 | fprintf(output, "\n"); |
| 893 | break; |
| 894 | case XPATH_RANGE: |
| 895 | if ((cur->user2 == NULL) || |
| 896 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 897 | fprintf(output, "Object is a collapsed range :\n"); |
| 898 | fprintf(output, shift); |
| 899 | if (cur->index >= 0) |
| 900 | fprintf(output, "index %d in ", cur->index); |
| 901 | fprintf(output, "node\n"); |
| 902 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 903 | depth + 1); |
| 904 | } else { |
| 905 | fprintf(output, "Object is a range :\n"); |
| 906 | fprintf(output, shift); |
| 907 | fprintf(output, "From "); |
| 908 | if (cur->index >= 0) |
| 909 | fprintf(output, "index %d in ", cur->index); |
| 910 | fprintf(output, "node\n"); |
| 911 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 912 | depth + 1); |
| 913 | fprintf(output, shift); |
| 914 | fprintf(output, "To "); |
| 915 | if (cur->index2 >= 0) |
| 916 | fprintf(output, "index %d in ", cur->index2); |
| 917 | fprintf(output, "node\n"); |
| 918 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 919 | depth + 1); |
| 920 | fprintf(output, "\n"); |
| 921 | } |
| 922 | break; |
| 923 | case XPATH_LOCATIONSET: |
| 924 | #if defined(LIBXML_XPTR_ENABLED) |
| 925 | fprintf(output, "Object is a Location Set:\n"); |
| 926 | xmlXPathDebugDumpLocationSet(output, |
| 927 | (xmlLocationSetPtr) cur->user, depth); |
| 928 | #endif |
| 929 | break; |
| 930 | case XPATH_USERS: |
| 931 | fprintf(output, "Object is user defined\n"); |
| 932 | break; |
| 933 | } |
| 934 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 935 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 936 | static void |
| 937 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 938 | xmlXPathStepOpPtr op, int depth) { |
| 939 | int i; |
| 940 | char shift[100]; |
| 941 | |
| 942 | for (i = 0;((i < depth) && (i < 25));i++) |
| 943 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 944 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 945 | |
| 946 | fprintf(output, shift); |
| 947 | if (op == NULL) { |
| 948 | fprintf(output, "Step is NULL\n"); |
| 949 | return; |
| 950 | } |
| 951 | switch (op->op) { |
| 952 | case XPATH_OP_END: |
| 953 | fprintf(output, "END"); break; |
| 954 | case XPATH_OP_AND: |
| 955 | fprintf(output, "AND"); break; |
| 956 | case XPATH_OP_OR: |
| 957 | fprintf(output, "OR"); break; |
| 958 | case XPATH_OP_EQUAL: |
| 959 | if (op->value) |
| 960 | fprintf(output, "EQUAL ="); |
| 961 | else |
| 962 | fprintf(output, "EQUAL !="); |
| 963 | break; |
| 964 | case XPATH_OP_CMP: |
| 965 | if (op->value) |
| 966 | fprintf(output, "CMP <"); |
| 967 | else |
| 968 | fprintf(output, "CMP >"); |
| 969 | if (!op->value2) |
| 970 | fprintf(output, "="); |
| 971 | break; |
| 972 | case XPATH_OP_PLUS: |
| 973 | if (op->value == 0) |
| 974 | fprintf(output, "PLUS -"); |
| 975 | else if (op->value == 1) |
| 976 | fprintf(output, "PLUS +"); |
| 977 | else if (op->value == 2) |
| 978 | fprintf(output, "PLUS unary -"); |
| 979 | else if (op->value == 3) |
| 980 | fprintf(output, "PLUS unary - -"); |
| 981 | break; |
| 982 | case XPATH_OP_MULT: |
| 983 | if (op->value == 0) |
| 984 | fprintf(output, "MULT *"); |
| 985 | else if (op->value == 1) |
| 986 | fprintf(output, "MULT div"); |
| 987 | else |
| 988 | fprintf(output, "MULT mod"); |
| 989 | break; |
| 990 | case XPATH_OP_UNION: |
| 991 | fprintf(output, "UNION"); break; |
| 992 | case XPATH_OP_ROOT: |
| 993 | fprintf(output, "ROOT"); break; |
| 994 | case XPATH_OP_NODE: |
| 995 | fprintf(output, "NODE"); break; |
| 996 | case XPATH_OP_RESET: |
| 997 | fprintf(output, "RESET"); break; |
| 998 | case XPATH_OP_SORT: |
| 999 | fprintf(output, "SORT"); break; |
| 1000 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1001 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 1002 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 1003 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1004 | const xmlChar *prefix = op->value4; |
| 1005 | const xmlChar *name = op->value5; |
| 1006 | |
| 1007 | fprintf(output, "COLLECT "); |
| 1008 | switch (axis) { |
| 1009 | case AXIS_ANCESTOR: |
| 1010 | fprintf(output, " 'ancestors' "); break; |
| 1011 | case AXIS_ANCESTOR_OR_SELF: |
| 1012 | fprintf(output, " 'ancestors-or-self' "); break; |
| 1013 | case AXIS_ATTRIBUTE: |
| 1014 | fprintf(output, " 'attributes' "); break; |
| 1015 | case AXIS_CHILD: |
| 1016 | fprintf(output, " 'child' "); break; |
| 1017 | case AXIS_DESCENDANT: |
| 1018 | fprintf(output, " 'descendant' "); break; |
| 1019 | case AXIS_DESCENDANT_OR_SELF: |
| 1020 | fprintf(output, " 'descendant-or-self' "); break; |
| 1021 | case AXIS_FOLLOWING: |
| 1022 | fprintf(output, " 'following' "); break; |
| 1023 | case AXIS_FOLLOWING_SIBLING: |
| 1024 | fprintf(output, " 'following-siblings' "); break; |
| 1025 | case AXIS_NAMESPACE: |
| 1026 | fprintf(output, " 'namespace' "); break; |
| 1027 | case AXIS_PARENT: |
| 1028 | fprintf(output, " 'parent' "); break; |
| 1029 | case AXIS_PRECEDING: |
| 1030 | fprintf(output, " 'preceding' "); break; |
| 1031 | case AXIS_PRECEDING_SIBLING: |
| 1032 | fprintf(output, " 'preceding-sibling' "); break; |
| 1033 | case AXIS_SELF: |
| 1034 | fprintf(output, " 'self' "); break; |
| 1035 | } |
| 1036 | switch (test) { |
| 1037 | case NODE_TEST_NONE: |
| 1038 | fprintf(output, "'none' "); break; |
| 1039 | case NODE_TEST_TYPE: |
| 1040 | fprintf(output, "'type' "); break; |
| 1041 | case NODE_TEST_PI: |
| 1042 | fprintf(output, "'PI' "); break; |
| 1043 | case NODE_TEST_ALL: |
| 1044 | fprintf(output, "'all' "); break; |
| 1045 | case NODE_TEST_NS: |
| 1046 | fprintf(output, "'namespace' "); break; |
| 1047 | case NODE_TEST_NAME: |
| 1048 | fprintf(output, "'name' "); break; |
| 1049 | } |
| 1050 | switch (type) { |
| 1051 | case NODE_TYPE_NODE: |
| 1052 | fprintf(output, "'node' "); break; |
| 1053 | case NODE_TYPE_COMMENT: |
| 1054 | fprintf(output, "'comment' "); break; |
| 1055 | case NODE_TYPE_TEXT: |
| 1056 | fprintf(output, "'text' "); break; |
| 1057 | case NODE_TYPE_PI: |
| 1058 | fprintf(output, "'PI' "); break; |
| 1059 | } |
| 1060 | if (prefix != NULL) |
| 1061 | fprintf(output, "%s:", prefix); |
| 1062 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1063 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | |
| 1066 | } |
| 1067 | case XPATH_OP_VALUE: { |
| 1068 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1069 | |
| 1070 | fprintf(output, "ELEM "); |
| 1071 | xmlXPathDebugDumpObject(output, object, 0); |
| 1072 | goto finish; |
| 1073 | } |
| 1074 | case XPATH_OP_VARIABLE: { |
| 1075 | const xmlChar *prefix = op->value5; |
| 1076 | const xmlChar *name = op->value4; |
| 1077 | |
| 1078 | if (prefix != NULL) |
| 1079 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1080 | else |
| 1081 | fprintf(output, "VARIABLE %s", name); |
| 1082 | break; |
| 1083 | } |
| 1084 | case XPATH_OP_FUNCTION: { |
| 1085 | int nbargs = op->value; |
| 1086 | const xmlChar *prefix = op->value5; |
| 1087 | const xmlChar *name = op->value4; |
| 1088 | |
| 1089 | if (prefix != NULL) |
| 1090 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1091 | prefix, name, nbargs); |
| 1092 | else |
| 1093 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1094 | break; |
| 1095 | } |
| 1096 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1097 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1098 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1099 | #ifdef LIBXML_XPTR_ENABLED |
| 1100 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1101 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1102 | default: |
| 1103 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1104 | } |
| 1105 | fprintf(output, "\n"); |
| 1106 | finish: |
| 1107 | if (op->ch1 >= 0) |
| 1108 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1109 | if (op->ch2 >= 0) |
| 1110 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1111 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1112 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1113 | /** |
| 1114 | * xmlXPathDebugDumpCompExpr: |
| 1115 | * @output: the FILE * for the output |
| 1116 | * @comp: the precompiled XPath expression |
| 1117 | * @depth: the indentation level. |
| 1118 | * |
| 1119 | * Dumps the tree of the compiled XPath expression. |
| 1120 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1121 | void |
| 1122 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1123 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1124 | int i; |
| 1125 | char shift[100]; |
| 1126 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1127 | if ((output == NULL) || (comp == NULL)) return; |
| 1128 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1129 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1130 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1131 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1132 | |
| 1133 | fprintf(output, shift); |
| 1134 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1135 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1136 | comp->nbStep); |
| 1137 | i = comp->last; |
| 1138 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1139 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1140 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1141 | |
| 1142 | /************************************************************************ |
| 1143 | * * |
| 1144 | * Parser stacks related functions and macros * |
| 1145 | * * |
| 1146 | ************************************************************************/ |
| 1147 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1148 | /** |
| 1149 | * valuePop: |
| 1150 | * @ctxt: an XPath evaluation context |
| 1151 | * |
| 1152 | * Pops the top XPath object from the value stack |
| 1153 | * |
| 1154 | * Returns the XPath object just removed |
| 1155 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1156 | xmlXPathObjectPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1157 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1158 | { |
| 1159 | xmlXPathObjectPtr ret; |
| 1160 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1161 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1162 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1163 | ctxt->valueNr--; |
| 1164 | if (ctxt->valueNr > 0) |
| 1165 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1166 | else |
| 1167 | ctxt->value = NULL; |
| 1168 | ret = ctxt->valueTab[ctxt->valueNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1169 | ctxt->valueTab[ctxt->valueNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1170 | return (ret); |
| 1171 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1172 | /** |
| 1173 | * valuePush: |
| 1174 | * @ctxt: an XPath evaluation context |
| 1175 | * @value: the XPath object |
| 1176 | * |
| 1177 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1178 | * |
| 1179 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1180 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1181 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1182 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1183 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1184 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1185 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1186 | xmlXPathObjectPtr *tmp; |
| 1187 | |
| 1188 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1189 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1190 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1191 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1192 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1193 | return (0); |
| 1194 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1195 | ctxt->valueMax *= 2; |
| 1196 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1197 | } |
| 1198 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1199 | ctxt->value = value; |
| 1200 | return (ctxt->valueNr++); |
| 1201 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1202 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1203 | /** |
| 1204 | * xmlXPathPopBoolean: |
| 1205 | * @ctxt: an XPath parser context |
| 1206 | * |
| 1207 | * Pops a boolean from the stack, handling conversion if needed. |
| 1208 | * Check error with #xmlXPathCheckError. |
| 1209 | * |
| 1210 | * Returns the boolean |
| 1211 | */ |
| 1212 | int |
| 1213 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1214 | xmlXPathObjectPtr obj; |
| 1215 | int ret; |
| 1216 | |
| 1217 | obj = valuePop(ctxt); |
| 1218 | if (obj == NULL) { |
| 1219 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1220 | return(0); |
| 1221 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1222 | if (obj->type != XPATH_BOOLEAN) |
| 1223 | ret = xmlXPathCastToBoolean(obj); |
| 1224 | else |
| 1225 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1226 | xmlXPathFreeObject(obj); |
| 1227 | return(ret); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * xmlXPathPopNumber: |
| 1232 | * @ctxt: an XPath parser context |
| 1233 | * |
| 1234 | * Pops a number from the stack, handling conversion if needed. |
| 1235 | * Check error with #xmlXPathCheckError. |
| 1236 | * |
| 1237 | * Returns the number |
| 1238 | */ |
| 1239 | double |
| 1240 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1241 | xmlXPathObjectPtr obj; |
| 1242 | double ret; |
| 1243 | |
| 1244 | obj = valuePop(ctxt); |
| 1245 | if (obj == NULL) { |
| 1246 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1247 | return(0); |
| 1248 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1249 | if (obj->type != XPATH_NUMBER) |
| 1250 | ret = xmlXPathCastToNumber(obj); |
| 1251 | else |
| 1252 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1253 | xmlXPathFreeObject(obj); |
| 1254 | return(ret); |
| 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * xmlXPathPopString: |
| 1259 | * @ctxt: an XPath parser context |
| 1260 | * |
| 1261 | * Pops a string from the stack, handling conversion if needed. |
| 1262 | * Check error with #xmlXPathCheckError. |
| 1263 | * |
| 1264 | * Returns the string |
| 1265 | */ |
| 1266 | xmlChar * |
| 1267 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1268 | xmlXPathObjectPtr obj; |
| 1269 | xmlChar * ret; |
| 1270 | |
| 1271 | obj = valuePop(ctxt); |
| 1272 | if (obj == NULL) { |
| 1273 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1274 | return(NULL); |
| 1275 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1276 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1277 | /* TODO: needs refactoring somewhere else */ |
| 1278 | if (obj->stringval == ret) |
| 1279 | obj->stringval = NULL; |
| 1280 | xmlXPathFreeObject(obj); |
| 1281 | return(ret); |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * xmlXPathPopNodeSet: |
| 1286 | * @ctxt: an XPath parser context |
| 1287 | * |
| 1288 | * Pops a node-set from the stack, handling conversion if needed. |
| 1289 | * Check error with #xmlXPathCheckError. |
| 1290 | * |
| 1291 | * Returns the node-set |
| 1292 | */ |
| 1293 | xmlNodeSetPtr |
| 1294 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1295 | xmlXPathObjectPtr obj; |
| 1296 | xmlNodeSetPtr ret; |
| 1297 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 1298 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1299 | if (ctxt->value == NULL) { |
| 1300 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1301 | return(NULL); |
| 1302 | } |
| 1303 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1304 | xmlXPathSetTypeError(ctxt); |
| 1305 | return(NULL); |
| 1306 | } |
| 1307 | obj = valuePop(ctxt); |
| 1308 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1309 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1310 | /* to fix memory leak of not clearing obj->user */ |
| 1311 | if (obj->boolval && obj->user != NULL) |
| 1312 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1313 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1314 | xmlXPathFreeNodeSetList(obj); |
| 1315 | return(ret); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * xmlXPathPopExternal: |
| 1320 | * @ctxt: an XPath parser context |
| 1321 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1322 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1323 | * Check error with #xmlXPathCheckError. |
| 1324 | * |
| 1325 | * Returns the object |
| 1326 | */ |
| 1327 | void * |
| 1328 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1329 | xmlXPathObjectPtr obj; |
| 1330 | void * ret; |
| 1331 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1332 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1333 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1334 | return(NULL); |
| 1335 | } |
| 1336 | if (ctxt->value->type != XPATH_USERS) { |
| 1337 | xmlXPathSetTypeError(ctxt); |
| 1338 | return(NULL); |
| 1339 | } |
| 1340 | obj = valuePop(ctxt); |
| 1341 | ret = obj->user; |
| 1342 | xmlXPathFreeObject(obj); |
| 1343 | return(ret); |
| 1344 | } |
| 1345 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1346 | /* |
| 1347 | * Macros for accessing the content. Those should be used only by the parser, |
| 1348 | * and not exported. |
| 1349 | * |
| 1350 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1351 | * |
| 1352 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1353 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1354 | * in ISO-Latin or UTF-8. |
| 1355 | * This should be used internally by the parser |
| 1356 | * only to compare to ASCII values otherwise it would break when |
| 1357 | * running with UTF-8 encoding. |
| 1358 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1359 | * to compare on ASCII based substring. |
| 1360 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1361 | * strings within the parser. |
| 1362 | * CURRENT Returns the current char value, with the full decoding of |
| 1363 | * UTF-8 if we are using this mode. It returns an int. |
| 1364 | * NEXT Skip to the next character, this does the proper decoding |
| 1365 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1366 | * It returns the pointer to the current xmlChar. |
| 1367 | */ |
| 1368 | |
| 1369 | #define CUR (*ctxt->cur) |
| 1370 | #define SKIP(val) ctxt->cur += (val) |
| 1371 | #define NXT(val) ctxt->cur[(val)] |
| 1372 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1373 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1374 | |
| 1375 | #define COPY_BUF(l,b,i,v) \ |
| 1376 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1377 | else i += xmlCopyChar(l,&b[i],v) |
| 1378 | |
| 1379 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1380 | |
| 1381 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1382 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1383 | |
| 1384 | #define CURRENT (*ctxt->cur) |
| 1385 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1386 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1387 | |
| 1388 | #ifndef DBL_DIG |
| 1389 | #define DBL_DIG 16 |
| 1390 | #endif |
| 1391 | #ifndef DBL_EPSILON |
| 1392 | #define DBL_EPSILON 1E-9 |
| 1393 | #endif |
| 1394 | |
| 1395 | #define UPPER_DOUBLE 1E9 |
| 1396 | #define LOWER_DOUBLE 1E-5 |
| 1397 | |
| 1398 | #define INTEGER_DIGITS DBL_DIG |
| 1399 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1400 | #define EXPONENT_DIGITS (3 + 2) |
| 1401 | |
| 1402 | /** |
| 1403 | * xmlXPathFormatNumber: |
| 1404 | * @number: number to format |
| 1405 | * @buffer: output buffer |
| 1406 | * @buffersize: size of output buffer |
| 1407 | * |
| 1408 | * Convert the number into a string representation. |
| 1409 | */ |
| 1410 | static void |
| 1411 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1412 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1413 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1414 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1415 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1416 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1417 | break; |
| 1418 | case -1: |
| 1419 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1420 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1421 | break; |
| 1422 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1423 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1424 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1425 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1426 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1427 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1428 | } else if (number == ((int) number)) { |
| 1429 | char work[30]; |
| 1430 | char *ptr, *cur; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 1431 | int value = (int) number; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1432 | |
| 1433 | ptr = &buffer[0]; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1434 | if (value == 0) { |
| 1435 | *ptr++ = '0'; |
| 1436 | } else { |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 1437 | snprintf(work, 29, "%d", value); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1438 | cur = &work[0]; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 1439 | while ((*cur) && (ptr - buffer < buffersize)) { |
| 1440 | *ptr++ = *cur++; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | if (ptr - buffer < buffersize) { |
| 1444 | *ptr = 0; |
| 1445 | } else if (buffersize > 0) { |
| 1446 | ptr--; |
| 1447 | *ptr = 0; |
| 1448 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1449 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1450 | /* 3 is sign, decimal point, and terminating zero */ |
| 1451 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1452 | int integer_place, fraction_place; |
| 1453 | char *ptr; |
| 1454 | char *after_fraction; |
| 1455 | double absolute_value; |
| 1456 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1457 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1458 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1459 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1460 | /* |
| 1461 | * First choose format - scientific or regular floating point. |
| 1462 | * In either case, result is in work, and after_fraction points |
| 1463 | * just past the fractional part. |
| 1464 | */ |
| 1465 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1466 | (absolute_value < LOWER_DOUBLE)) && |
| 1467 | (absolute_value != 0.0) ) { |
| 1468 | /* Use scientific notation */ |
| 1469 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1470 | fraction_place = DBL_DIG - 1; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 1471 | size = snprintf(work, sizeof(work),"%*.*e", |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1472 | integer_place, fraction_place, number); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 1473 | while ((size > 0) && (work[size] != 'e')) size--; |
| 1474 | after_fraction = work + size; |
| 1475 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1476 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1477 | else { |
| 1478 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1479 | if (absolute_value > 0.0) |
| 1480 | integer_place = 1 + (int)log10(absolute_value); |
| 1481 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1482 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1483 | fraction_place = (integer_place > 0) |
| 1484 | ? DBL_DIG - integer_place |
| 1485 | : DBL_DIG; |
| 1486 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1487 | fraction_place, number); |
| 1488 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1491 | /* Remove fractional trailing zeroes */ |
| 1492 | ptr = after_fraction; |
| 1493 | while (*(--ptr) == '0') |
| 1494 | ; |
| 1495 | if (*ptr != '.') |
| 1496 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1497 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* Finally copy result back to caller */ |
| 1500 | size = strlen(work) + 1; |
| 1501 | if (size > buffersize) { |
| 1502 | work[buffersize - 1] = 0; |
| 1503 | size = buffersize; |
| 1504 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1505 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1506 | } |
| 1507 | break; |
| 1508 | } |
| 1509 | } |
| 1510 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1511 | |
| 1512 | /************************************************************************ |
| 1513 | * * |
| 1514 | * Routines to handle NodeSets * |
| 1515 | * * |
| 1516 | ************************************************************************/ |
| 1517 | |
| 1518 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1519 | * xmlXPathOrderDocElems: |
| 1520 | * @doc: an input document |
| 1521 | * |
| 1522 | * Call this routine to speed up XPath computation on static documents. |
| 1523 | * This stamps all the element nodes with the document order |
| 1524 | * 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] | 1525 | * field, the value stored is actually - the node number (starting at -1) |
| 1526 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1527 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1528 | * 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] | 1529 | * of error. |
| 1530 | */ |
| 1531 | long |
| 1532 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1533 | long count = 0; |
| 1534 | xmlNodePtr cur; |
| 1535 | |
| 1536 | if (doc == NULL) |
| 1537 | return(-1); |
| 1538 | cur = doc->children; |
| 1539 | while (cur != NULL) { |
| 1540 | if (cur->type == XML_ELEMENT_NODE) { |
| 1541 | cur->content = (void *) (-(++count)); |
| 1542 | if (cur->children != NULL) { |
| 1543 | cur = cur->children; |
| 1544 | continue; |
| 1545 | } |
| 1546 | } |
| 1547 | if (cur->next != NULL) { |
| 1548 | cur = cur->next; |
| 1549 | continue; |
| 1550 | } |
| 1551 | do { |
| 1552 | cur = cur->parent; |
| 1553 | if (cur == NULL) |
| 1554 | break; |
| 1555 | if (cur == (xmlNodePtr) doc) { |
| 1556 | cur = NULL; |
| 1557 | break; |
| 1558 | } |
| 1559 | if (cur->next != NULL) { |
| 1560 | cur = cur->next; |
| 1561 | break; |
| 1562 | } |
| 1563 | } while (cur != NULL); |
| 1564 | } |
| 1565 | return(count); |
| 1566 | } |
| 1567 | |
| 1568 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | * xmlXPathCmpNodes: |
| 1570 | * @node1: the first node |
| 1571 | * @node2: the second node |
| 1572 | * |
| 1573 | * Compare two nodes w.r.t document order |
| 1574 | * |
| 1575 | * 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] | 1576 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1577 | */ |
| 1578 | int |
| 1579 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1580 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1581 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1582 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | xmlNodePtr cur, root; |
| 1584 | |
| 1585 | if ((node1 == NULL) || (node2 == NULL)) |
| 1586 | return(-2); |
| 1587 | /* |
| 1588 | * a couple of optimizations which will avoid computations in most cases |
| 1589 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1590 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1591 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1592 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1593 | node1 = node1->parent; |
| 1594 | } |
| 1595 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1596 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1597 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1598 | node2 = node2->parent; |
| 1599 | } |
| 1600 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1601 | if (attr1 == attr2) { |
| 1602 | /* not required, but we keep attributes in order */ |
| 1603 | if (attr1 != 0) { |
| 1604 | cur = attrNode2->prev; |
| 1605 | while (cur != NULL) { |
| 1606 | if (cur == attrNode1) |
| 1607 | return (1); |
| 1608 | cur = cur->prev; |
| 1609 | } |
| 1610 | return (-1); |
| 1611 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1612 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1613 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1614 | if (attr2 == 1) |
| 1615 | return(1); |
| 1616 | return(-1); |
| 1617 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1618 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1619 | (node2->type == XML_NAMESPACE_DECL)) |
| 1620 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1621 | if (node1 == node2->prev) |
| 1622 | return(1); |
| 1623 | if (node1 == node2->next) |
| 1624 | return(-1); |
| 1625 | |
| 1626 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1627 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1628 | */ |
| 1629 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1630 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1631 | (0 > (long) node1->content) && |
| 1632 | (0 > (long) node2->content) && |
| 1633 | (node1->doc == node2->doc)) { |
| 1634 | long l1, l2; |
| 1635 | |
| 1636 | l1 = -((long) node1->content); |
| 1637 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1638 | if (l1 < l2) |
| 1639 | return(1); |
| 1640 | if (l1 > l2) |
| 1641 | return(-1); |
| 1642 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1643 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1644 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1645 | * compute depth to root |
| 1646 | */ |
| 1647 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1648 | if (cur == node1) |
| 1649 | return(1); |
| 1650 | depth2++; |
| 1651 | } |
| 1652 | root = cur; |
| 1653 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1654 | if (cur == node2) |
| 1655 | return(-1); |
| 1656 | depth1++; |
| 1657 | } |
| 1658 | /* |
| 1659 | * Distinct document (or distinct entities :-( ) case. |
| 1660 | */ |
| 1661 | if (root != cur) { |
| 1662 | return(-2); |
| 1663 | } |
| 1664 | /* |
| 1665 | * get the nearest common ancestor. |
| 1666 | */ |
| 1667 | while (depth1 > depth2) { |
| 1668 | depth1--; |
| 1669 | node1 = node1->parent; |
| 1670 | } |
| 1671 | while (depth2 > depth1) { |
| 1672 | depth2--; |
| 1673 | node2 = node2->parent; |
| 1674 | } |
| 1675 | while (node1->parent != node2->parent) { |
| 1676 | node1 = node1->parent; |
| 1677 | node2 = node2->parent; |
| 1678 | /* should not happen but just in case ... */ |
| 1679 | if ((node1 == NULL) || (node2 == NULL)) |
| 1680 | return(-2); |
| 1681 | } |
| 1682 | /* |
| 1683 | * Find who's first. |
| 1684 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1685 | if (node1 == node2->prev) |
| 1686 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1687 | if (node1 == node2->next) |
| 1688 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1689 | /* |
| 1690 | * Speedup using document order if availble. |
| 1691 | */ |
| 1692 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1693 | (node2->type == XML_ELEMENT_NODE) && |
| 1694 | (0 > (long) node1->content) && |
| 1695 | (0 > (long) node2->content) && |
| 1696 | (node1->doc == node2->doc)) { |
| 1697 | long l1, l2; |
| 1698 | |
| 1699 | l1 = -((long) node1->content); |
| 1700 | l2 = -((long) node2->content); |
| 1701 | if (l1 < l2) |
| 1702 | return(1); |
| 1703 | if (l1 > l2) |
| 1704 | return(-1); |
| 1705 | } |
| 1706 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1707 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1708 | if (cur == node2) |
| 1709 | return(1); |
| 1710 | return(-1); /* assume there is no sibling list corruption */ |
| 1711 | } |
| 1712 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 1713 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1714 | /** |
| 1715 | * xmlXPathCmpNodesExt: |
| 1716 | * @node1: the first node |
| 1717 | * @node2: the second node |
| 1718 | * |
| 1719 | * Compare two nodes w.r.t document order. |
| 1720 | * This one is optimized for handling of non-element nodes. |
| 1721 | * |
| 1722 | * Returns -2 in case of error 1 if first point < second point, 0 if |
| 1723 | * it's the same node, -1 otherwise |
| 1724 | */ |
| 1725 | static int |
| 1726 | xmlXPathCmpNodesExt(xmlNodePtr node1, xmlNodePtr node2) { |
| 1727 | int depth1, depth2; |
| 1728 | int misc = 0, precedence1 = 0, precedence2 = 0; |
| 1729 | xmlNodePtr miscNode1 = NULL, miscNode2 = NULL; |
| 1730 | xmlNodePtr cur, root; |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1731 | long l1, l2; |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1732 | |
| 1733 | if ((node1 == NULL) || (node2 == NULL)) |
| 1734 | return(-2); |
| 1735 | |
| 1736 | if (node1 == node2) |
| 1737 | return(0); |
| 1738 | |
| 1739 | /* |
| 1740 | * a couple of optimizations which will avoid computations in most cases |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1741 | */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1742 | switch (node1->type) { |
| 1743 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1744 | if (node2->type == XML_ELEMENT_NODE) { |
| 1745 | if ((0 > (long) node1->content) && /* TODO: Would a != 0 suffice here? */ |
| 1746 | (0 > (long) node2->content) && |
| 1747 | (node1->doc == node2->doc)) |
| 1748 | { |
| 1749 | l1 = -((long) node1->content); |
| 1750 | l2 = -((long) node2->content); |
| 1751 | if (l1 < l2) |
| 1752 | return(1); |
| 1753 | if (l1 > l2) |
| 1754 | return(-1); |
| 1755 | } else |
| 1756 | goto turtle_comparison; |
| 1757 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | case XML_ATTRIBUTE_NODE: |
| 1760 | precedence1 = 1; /* element is owner */ |
| 1761 | miscNode1 = node1; |
| 1762 | node1 = node1->parent; |
| 1763 | misc = 1; |
| 1764 | break; |
| 1765 | case XML_TEXT_NODE: |
| 1766 | case XML_CDATA_SECTION_NODE: |
| 1767 | case XML_COMMENT_NODE: |
| 1768 | case XML_PI_NODE: { |
| 1769 | miscNode1 = node1; |
| 1770 | /* |
| 1771 | * Find nearest element node. |
| 1772 | */ |
| 1773 | if (node1->prev != NULL) { |
| 1774 | do { |
| 1775 | node1 = node1->prev; |
| 1776 | if (node1->type == XML_ELEMENT_NODE) { |
| 1777 | precedence1 = 3; /* element in prev-sibl axis */ |
| 1778 | break; |
| 1779 | } |
| 1780 | if (node1->prev == NULL) { |
| 1781 | precedence1 = 2; /* element is parent */ |
| 1782 | /* |
| 1783 | * URGENT TODO: Are there any cases, where the |
| 1784 | * parent of such a node is not an element node? |
| 1785 | */ |
| 1786 | node1 = node1->parent; |
| 1787 | break; |
| 1788 | } |
| 1789 | } while (1); |
| 1790 | } else { |
| 1791 | precedence1 = 2; /* element is parent */ |
| 1792 | node1 = node1->parent; |
| 1793 | } |
| 1794 | if ((node1 == NULL) || (node1->type != XML_ELEMENT_NODE)) { |
| 1795 | /* |
| 1796 | * Fallback for whatever case. |
| 1797 | */ |
| 1798 | node1 = miscNode1; |
| 1799 | precedence1 = 0; |
| 1800 | } else |
| 1801 | misc = 1; |
| 1802 | } |
| 1803 | break; |
| 1804 | case XML_NAMESPACE_DECL: |
| 1805 | /* |
| 1806 | * TODO: why do we return 1 for namespace nodes? |
| 1807 | */ |
| 1808 | return(1); |
| 1809 | default: |
| 1810 | break; |
| 1811 | } |
| 1812 | switch (node2->type) { |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1813 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1814 | break; |
| 1815 | case XML_ATTRIBUTE_NODE: |
| 1816 | precedence2 = 1; /* element is owner */ |
| 1817 | miscNode2 = node2; |
| 1818 | node2 = node2->parent; |
| 1819 | misc = 1; |
| 1820 | break; |
| 1821 | case XML_TEXT_NODE: |
| 1822 | case XML_CDATA_SECTION_NODE: |
| 1823 | case XML_COMMENT_NODE: |
| 1824 | case XML_PI_NODE: { |
| 1825 | miscNode2 = node2; |
| 1826 | if (node2->prev != NULL) { |
| 1827 | do { |
| 1828 | node2 = node2->prev; |
| 1829 | if (node2->type == XML_ELEMENT_NODE) { |
| 1830 | precedence2 = 3; /* element in prev-sibl axis */ |
| 1831 | break; |
| 1832 | } |
| 1833 | if (node2->prev == NULL) { |
| 1834 | precedence2 = 2; /* element is parent */ |
| 1835 | node2 = node2->parent; |
| 1836 | break; |
| 1837 | } |
| 1838 | } while (1); |
| 1839 | } else { |
| 1840 | precedence2 = 2; /* element is parent */ |
| 1841 | node2 = node2->parent; |
| 1842 | } |
| 1843 | if ((node2 == NULL) || (node2->type != XML_ELEMENT_NODE) || |
| 1844 | (0 <= (long) node1->content)) |
| 1845 | { |
| 1846 | node2 = miscNode2; |
| 1847 | precedence2 = 0; |
| 1848 | } else |
| 1849 | misc = 1; |
| 1850 | } |
| 1851 | break; |
| 1852 | case XML_NAMESPACE_DECL: |
| 1853 | return(1); |
| 1854 | default: |
| 1855 | break; |
| 1856 | } |
| 1857 | if (misc) { |
| 1858 | if (node1 == node2) { |
| 1859 | if (precedence1 == precedence2) { |
| 1860 | /* |
| 1861 | * The ugly case; but normally there aren't many |
| 1862 | * adjacent non-element nodes around. |
| 1863 | */ |
| 1864 | cur = miscNode2->prev; |
| 1865 | while (cur != NULL) { |
| 1866 | if (cur == miscNode1) |
| 1867 | return(1); |
| 1868 | if (cur->type == XML_ELEMENT_NODE) |
| 1869 | return(-1); |
| 1870 | cur = cur->prev; |
| 1871 | } |
| 1872 | return (-1); |
| 1873 | } else { |
| 1874 | /* |
| 1875 | * Evaluate based on higher precedence wrt to the element. |
| 1876 | * TODO: This assumes attributes are sorted before content. |
| 1877 | * Is this 100% correct? |
| 1878 | */ |
| 1879 | if (precedence1 < precedence2) |
| 1880 | return(1); |
| 1881 | else |
| 1882 | return(-1); |
| 1883 | } |
| 1884 | } |
| 1885 | /* |
| 1886 | * Special case: One of the helper-elements is contained by the other. |
| 1887 | * <foo> |
| 1888 | * <node2> |
| 1889 | * <node1>Text-1(precedence1 == 2)</node1> |
| 1890 | * </node2> |
| 1891 | * Text-6(precedence2 == 3) |
| 1892 | * </foo> |
| 1893 | */ |
| 1894 | if ((precedence2 == 3) && (precedence1 > 1)) { |
| 1895 | cur = node1->parent; |
| 1896 | while (cur) { |
| 1897 | if (cur == node2) |
| 1898 | return(1); |
| 1899 | cur = cur->parent; |
| 1900 | } |
| 1901 | } |
| 1902 | if ((precedence1 == 3) && (precedence2 > 1)) { |
| 1903 | cur = node2->parent; |
| 1904 | while (cur) { |
| 1905 | if (cur == node1) |
| 1906 | return(-1); |
| 1907 | cur = cur->parent; |
| 1908 | } |
| 1909 | } |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1910 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * Speedup using document order if availble. |
| 1914 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1915 | if ((node1->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1916 | (node2->type == XML_ELEMENT_NODE) && |
| 1917 | (0 > (long) node1->content) && |
| 1918 | (0 > (long) node2->content) && |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1919 | (node1->doc == node2->doc)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1920 | |
| 1921 | l1 = -((long) node1->content); |
| 1922 | l2 = -((long) node2->content); |
| 1923 | if (l1 < l2) |
| 1924 | return(1); |
| 1925 | if (l1 > l2) |
| 1926 | return(-1); |
| 1927 | } |
| 1928 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1929 | turtle_comparison: |
| 1930 | |
| 1931 | if (node1 == node2->prev) |
| 1932 | return(1); |
| 1933 | if (node1 == node2->next) |
| 1934 | return(-1); |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1935 | /* |
| 1936 | * compute depth to root |
| 1937 | */ |
| 1938 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1939 | if (cur == node1) |
| 1940 | return(1); |
| 1941 | depth2++; |
| 1942 | } |
| 1943 | root = cur; |
| 1944 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1945 | if (cur == node2) |
| 1946 | return(-1); |
| 1947 | depth1++; |
| 1948 | } |
| 1949 | /* |
| 1950 | * Distinct document (or distinct entities :-( ) case. |
| 1951 | */ |
| 1952 | if (root != cur) { |
| 1953 | return(-2); |
| 1954 | } |
| 1955 | /* |
| 1956 | * get the nearest common ancestor. |
| 1957 | */ |
| 1958 | while (depth1 > depth2) { |
| 1959 | depth1--; |
| 1960 | node1 = node1->parent; |
| 1961 | } |
| 1962 | while (depth2 > depth1) { |
| 1963 | depth2--; |
| 1964 | node2 = node2->parent; |
| 1965 | } |
| 1966 | while (node1->parent != node2->parent) { |
| 1967 | node1 = node1->parent; |
| 1968 | node2 = node2->parent; |
| 1969 | /* should not happen but just in case ... */ |
| 1970 | if ((node1 == NULL) || (node2 == NULL)) |
| 1971 | return(-2); |
| 1972 | } |
| 1973 | /* |
| 1974 | * Find who's first. |
| 1975 | */ |
| 1976 | if (node1 == node2->prev) |
| 1977 | return(1); |
| 1978 | if (node1 == node2->next) |
| 1979 | return(-1); |
| 1980 | /* |
| 1981 | * Speedup using document order if availble. |
| 1982 | */ |
| 1983 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1984 | (node2->type == XML_ELEMENT_NODE) && |
| 1985 | (0 > (long) node1->content) && |
| 1986 | (0 > (long) node2->content) && |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 1987 | (node1->doc == node2->doc)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 1988 | |
| 1989 | l1 = -((long) node1->content); |
| 1990 | l2 = -((long) node2->content); |
| 1991 | if (l1 < l2) |
| 1992 | return(1); |
| 1993 | if (l1 > l2) |
| 1994 | return(-1); |
| 1995 | } |
| 1996 | |
| 1997 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1998 | if (cur == node2) |
| 1999 | return(1); |
| 2000 | return(-1); /* assume there is no sibling list corruption */ |
| 2001 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2002 | #endif /* XP_OPTIMIZED_NON_ELEM_COMPARISON */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 2003 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2004 | /** |
| 2005 | * xmlXPathNodeSetSort: |
| 2006 | * @set: the node set |
| 2007 | * |
| 2008 | * Sort the node set in document order |
| 2009 | */ |
| 2010 | void |
| 2011 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2012 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2013 | xmlNodePtr tmp; |
| 2014 | |
| 2015 | if (set == NULL) |
| 2016 | return; |
| 2017 | |
| 2018 | /* Use Shell's sort to sort the node-set */ |
| 2019 | len = set->nodeNr; |
| 2020 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 2021 | for (i = incr; i < len; i++) { |
| 2022 | j = i - incr; |
| 2023 | while (j >= 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2024 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 2025 | if (xmlXPathCmpNodesExt(set->nodeTab[j], |
| 2026 | set->nodeTab[j + incr]) == -1) |
| 2027 | #else |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2028 | if (xmlXPathCmpNodes(set->nodeTab[j], |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 2029 | set->nodeTab[j + incr]) == -1) |
| 2030 | #endif |
| 2031 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2032 | tmp = set->nodeTab[j]; |
| 2033 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 2034 | set->nodeTab[j + incr] = tmp; |
| 2035 | j -= incr; |
| 2036 | } else |
| 2037 | break; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | #define XML_NODESET_DEFAULT 10 |
| 2044 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2045 | * xmlXPathNodeSetDupNs: |
| 2046 | * @node: the parent node of the namespace XPath node |
| 2047 | * @ns: the libxml namespace declaration node. |
| 2048 | * |
| 2049 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 2050 | * the namespace nodes are duplicated and the next pointer is set to the |
| 2051 | * parent node in the XPath semantic. |
| 2052 | * |
| 2053 | * Returns the newly created object. |
| 2054 | */ |
| 2055 | static xmlNodePtr |
| 2056 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 2057 | xmlNsPtr cur; |
| 2058 | |
| 2059 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 2060 | return(NULL); |
| 2061 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 2062 | return((xmlNodePtr) ns); |
| 2063 | |
| 2064 | /* |
| 2065 | * Allocate a new Namespace and fill the fields. |
| 2066 | */ |
| 2067 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 2068 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2069 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2070 | return(NULL); |
| 2071 | } |
| 2072 | memset(cur, 0, sizeof(xmlNs)); |
| 2073 | cur->type = XML_NAMESPACE_DECL; |
| 2074 | if (ns->href != NULL) |
| 2075 | cur->href = xmlStrdup(ns->href); |
| 2076 | if (ns->prefix != NULL) |
| 2077 | cur->prefix = xmlStrdup(ns->prefix); |
| 2078 | cur->next = (xmlNsPtr) node; |
| 2079 | return((xmlNodePtr) cur); |
| 2080 | } |
| 2081 | |
| 2082 | /** |
| 2083 | * xmlXPathNodeSetFreeNs: |
| 2084 | * @ns: the XPath namespace node found in a nodeset. |
| 2085 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2086 | * 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] | 2087 | * 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] | 2088 | * 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] | 2089 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 2090 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2091 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 2092 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 2093 | return; |
| 2094 | |
| 2095 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 2096 | if (ns->href != NULL) |
| 2097 | xmlFree((xmlChar *)ns->href); |
| 2098 | if (ns->prefix != NULL) |
| 2099 | xmlFree((xmlChar *)ns->prefix); |
| 2100 | xmlFree(ns); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | * xmlXPathNodeSetCreate: |
| 2106 | * @val: an initial xmlNodePtr, or NULL |
| 2107 | * |
| 2108 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 2109 | * |
| 2110 | * Returns the newly created object. |
| 2111 | */ |
| 2112 | xmlNodeSetPtr |
| 2113 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 2114 | xmlNodeSetPtr ret; |
| 2115 | |
| 2116 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 2117 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2118 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2119 | return(NULL); |
| 2120 | } |
| 2121 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 2122 | if (val != NULL) { |
| 2123 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2124 | sizeof(xmlNodePtr)); |
| 2125 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2126 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 2127 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2128 | return(NULL); |
| 2129 | } |
| 2130 | memset(ret->nodeTab, 0 , |
| 2131 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2132 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2133 | if (val->type == XML_NAMESPACE_DECL) { |
| 2134 | xmlNsPtr ns = (xmlNsPtr) val; |
| 2135 | |
| 2136 | ret->nodeTab[ret->nodeNr++] = |
| 2137 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2138 | } else |
| 2139 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | } |
| 2141 | return(ret); |
| 2142 | } |
| 2143 | |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame^] | 2144 | #if 0 /* enable if needed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2145 | /** |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2146 | * xmlXPathNodeSetCreateSize: |
| 2147 | * @val: an initial xmlNodePtr, or NULL |
| 2148 | * @size: the initial size of the node-sets |
| 2149 | * |
| 2150 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 2151 | * |
| 2152 | * Returns the newly created object. |
| 2153 | */ |
| 2154 | static xmlNodeSetPtr |
| 2155 | xmlXPathNodeSetCreateSize(int size) |
| 2156 | { |
| 2157 | xmlNodeSetPtr ret; |
| 2158 | |
| 2159 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 2160 | if (ret == NULL) { |
| 2161 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 2162 | return(NULL); |
| 2163 | } |
| 2164 | memset(ret, 0, (size_t) sizeof(xmlNodeSet)); |
| 2165 | if (size > 0) { |
| 2166 | if (size < XML_NODESET_DEFAULT) |
| 2167 | size = XML_NODESET_DEFAULT; |
| 2168 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(size * sizeof(xmlNodePtr)); |
| 2169 | if (ret->nodeTab == NULL) { |
| 2170 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 2171 | xmlFree(ret); |
| 2172 | return(NULL); |
| 2173 | } |
| 2174 | memset(ret->nodeTab, 0, size * (size_t) sizeof(xmlNodePtr)); |
| 2175 | ret->nodeMax = size; |
| 2176 | } |
| 2177 | return(ret); |
| 2178 | } |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame^] | 2179 | #endif |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2180 | |
| 2181 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2182 | * xmlXPathNodeSetContains: |
| 2183 | * @cur: the node-set |
| 2184 | * @val: the node |
| 2185 | * |
| 2186 | * checks whether @cur contains @val |
| 2187 | * |
| 2188 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 2189 | */ |
| 2190 | int |
| 2191 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2192 | int i; |
| 2193 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2194 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2195 | if (val->type == XML_NAMESPACE_DECL) { |
| 2196 | for (i = 0; i < cur->nodeNr; i++) { |
| 2197 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2198 | xmlNsPtr ns1, ns2; |
| 2199 | |
| 2200 | ns1 = (xmlNsPtr) val; |
| 2201 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 2202 | if (ns1 == ns2) |
| 2203 | return(1); |
| 2204 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 2205 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 2206 | return(1); |
| 2207 | } |
| 2208 | } |
| 2209 | } else { |
| 2210 | for (i = 0; i < cur->nodeNr; i++) { |
| 2211 | if (cur->nodeTab[i] == val) |
| 2212 | return(1); |
| 2213 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2214 | } |
| 2215 | return(0); |
| 2216 | } |
| 2217 | |
| 2218 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2219 | * xmlXPathNodeSetAddNs: |
| 2220 | * @cur: the initial node set |
| 2221 | * @node: the hosting node |
| 2222 | * @ns: a the namespace node |
| 2223 | * |
| 2224 | * add a new namespace node to an existing NodeSet |
| 2225 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 2226 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2227 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 2228 | int i; |
| 2229 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2230 | |
| 2231 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 2232 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2233 | (node->type != XML_ELEMENT_NODE)) |
| 2234 | return; |
| 2235 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2236 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2237 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2238 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2239 | */ |
| 2240 | for (i = 0;i < cur->nodeNr;i++) { |
| 2241 | if ((cur->nodeTab[i] != NULL) && |
| 2242 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 2243 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2244 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 2245 | return; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * grow the nodeTab if needed |
| 2250 | */ |
| 2251 | if (cur->nodeMax == 0) { |
| 2252 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2253 | sizeof(xmlNodePtr)); |
| 2254 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2255 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2256 | return; |
| 2257 | } |
| 2258 | memset(cur->nodeTab, 0 , |
| 2259 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2260 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 2261 | } else if (cur->nodeNr == cur->nodeMax) { |
| 2262 | xmlNodePtr *temp; |
| 2263 | |
| 2264 | cur->nodeMax *= 2; |
| 2265 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 2266 | sizeof(xmlNodePtr)); |
| 2267 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2268 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2269 | return; |
| 2270 | } |
| 2271 | cur->nodeTab = temp; |
| 2272 | } |
| 2273 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 2274 | } |
| 2275 | |
| 2276 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2277 | * xmlXPathNodeSetAdd: |
| 2278 | * @cur: the initial node set |
| 2279 | * @val: a new xmlNodePtr |
| 2280 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2281 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2282 | */ |
| 2283 | void |
| 2284 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2285 | int i; |
| 2286 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2287 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 2289 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 2290 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 2291 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 2292 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 2293 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2294 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2295 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2296 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2297 | */ |
| 2298 | for (i = 0;i < cur->nodeNr;i++) |
| 2299 | if (cur->nodeTab[i] == val) return; |
| 2300 | |
| 2301 | /* |
| 2302 | * grow the nodeTab if needed |
| 2303 | */ |
| 2304 | if (cur->nodeMax == 0) { |
| 2305 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2306 | sizeof(xmlNodePtr)); |
| 2307 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2308 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | } |
| 2311 | memset(cur->nodeTab, 0 , |
| 2312 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2313 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 2314 | } else if (cur->nodeNr == cur->nodeMax) { |
| 2315 | xmlNodePtr *temp; |
| 2316 | |
| 2317 | cur->nodeMax *= 2; |
| 2318 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 2319 | sizeof(xmlNodePtr)); |
| 2320 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2321 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2322 | return; |
| 2323 | } |
| 2324 | cur->nodeTab = temp; |
| 2325 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2326 | if (val->type == XML_NAMESPACE_DECL) { |
| 2327 | xmlNsPtr ns = (xmlNsPtr) val; |
| 2328 | |
| 2329 | cur->nodeTab[cur->nodeNr++] = |
| 2330 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2331 | } else |
| 2332 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /** |
| 2336 | * xmlXPathNodeSetAddUnique: |
| 2337 | * @cur: the initial node set |
| 2338 | * @val: a new xmlNodePtr |
| 2339 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2340 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | * when we are sure the node is not already in the set. |
| 2342 | */ |
| 2343 | void |
| 2344 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2345 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2346 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 2347 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 2348 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 2349 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 2350 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 2351 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2352 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2353 | /* |
| 2354 | * grow the nodeTab if needed |
| 2355 | */ |
| 2356 | if (cur->nodeMax == 0) { |
| 2357 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2358 | sizeof(xmlNodePtr)); |
| 2359 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2360 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
| 2363 | memset(cur->nodeTab, 0 , |
| 2364 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2365 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 2366 | } else if (cur->nodeNr == cur->nodeMax) { |
| 2367 | xmlNodePtr *temp; |
| 2368 | |
| 2369 | cur->nodeMax *= 2; |
| 2370 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 2371 | sizeof(xmlNodePtr)); |
| 2372 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2373 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | cur->nodeTab = temp; |
| 2377 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2378 | if (val->type == XML_NAMESPACE_DECL) { |
| 2379 | xmlNsPtr ns = (xmlNsPtr) val; |
| 2380 | |
| 2381 | cur->nodeTab[cur->nodeNr++] = |
| 2382 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2383 | } else |
| 2384 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * xmlXPathNodeSetMerge: |
| 2389 | * @val1: the first NodeSet or NULL |
| 2390 | * @val2: the second NodeSet |
| 2391 | * |
| 2392 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2393 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2394 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2395 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2396 | */ |
| 2397 | xmlNodeSetPtr |
| 2398 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2399 | int i, j, initNr, skip; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2400 | xmlNodePtr n1, n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (val2 == NULL) return(val1); |
| 2403 | if (val1 == NULL) { |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame^] | 2404 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2405 | #if 0 |
| 2406 | /* |
| 2407 | * TODO: The optimization won't work in every case, since |
| 2408 | * those nasty namespace nodes need to be added with |
| 2409 | * xmlXPathNodeSetDupNs() to the set; thus a pure |
| 2410 | * memcpy is not possible. |
| 2411 | */ |
| 2412 | /* |
| 2413 | * Optimization: Create an equally sized node-set |
| 2414 | * and memcpy the content. |
| 2415 | */ |
| 2416 | val1 = xmlXPathNodeSetCreateSize(val2->nodeNr); |
| 2417 | if (val1 == NULL) |
| 2418 | return(NULL); |
| 2419 | if (val2->nodeNr != 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2420 | if (val2->nodeNr == 1) |
| 2421 | *(val1->nodeTab) = *(val2->nodeTab); |
| 2422 | else { |
| 2423 | memcpy(val1->nodeTab, val2->nodeTab, |
| 2424 | val2->nodeNr * sizeof(xmlNodePtr)); |
| 2425 | } |
| 2426 | val1->nodeNr = val2->nodeNr; |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame^] | 2427 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2428 | return(val1); |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame^] | 2429 | #endif /* if 0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2432 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2433 | initNr = val1->nodeNr; |
| 2434 | |
| 2435 | for (i = 0;i < val2->nodeNr;i++) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2436 | n2 = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2437 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2438 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2439 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2440 | skip = 0; |
| 2441 | for (j = 0; j < initNr; j++) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2442 | n1 = val1->nodeTab[j]; |
| 2443 | if (n1 == n2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2444 | skip = 1; |
| 2445 | break; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2446 | } else if ((n1->type == XML_NAMESPACE_DECL) && |
| 2447 | (n2->type == XML_NAMESPACE_DECL)) { |
| 2448 | if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) && |
| 2449 | (xmlStrEqual(((xmlNsPtr) n1)->prefix, |
| 2450 | ((xmlNsPtr) n2)->prefix))) |
| 2451 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2452 | skip = 1; |
| 2453 | break; |
| 2454 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | if (skip) |
| 2458 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2459 | |
| 2460 | /* |
| 2461 | * grow the nodeTab if needed |
| 2462 | */ |
| 2463 | if (val1->nodeMax == 0) { |
| 2464 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2465 | sizeof(xmlNodePtr)); |
| 2466 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2467 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2468 | return(NULL); |
| 2469 | } |
| 2470 | memset(val1->nodeTab, 0 , |
| 2471 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2472 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2473 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2474 | xmlNodePtr *temp; |
| 2475 | |
| 2476 | val1->nodeMax *= 2; |
| 2477 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2478 | sizeof(xmlNodePtr)); |
| 2479 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2480 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2481 | return(NULL); |
| 2482 | } |
| 2483 | val1->nodeTab = temp; |
| 2484 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2485 | if (n2->type == XML_NAMESPACE_DECL) { |
| 2486 | xmlNsPtr ns = (xmlNsPtr) n2; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2487 | |
| 2488 | val1->nodeTab[val1->nodeNr++] = |
| 2489 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2490 | } else |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 2491 | val1->nodeTab[val1->nodeNr++] = n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | return(val1); |
| 2495 | } |
| 2496 | |
| 2497 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2498 | * xmlXPathNodeSetMergeUnique: |
| 2499 | * @val1: the first NodeSet or NULL |
| 2500 | * @val2: the second NodeSet |
| 2501 | * |
| 2502 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2503 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2504 | * |
| 2505 | * Returns @val1 once extended or NULL in case of error. |
| 2506 | */ |
| 2507 | static xmlNodeSetPtr |
| 2508 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2509 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2510 | |
| 2511 | if (val2 == NULL) return(val1); |
| 2512 | if (val1 == NULL) { |
| 2513 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2514 | } |
| 2515 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2516 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2517 | |
| 2518 | for (i = 0;i < val2->nodeNr;i++) { |
| 2519 | /* |
| 2520 | * grow the nodeTab if needed |
| 2521 | */ |
| 2522 | if (val1->nodeMax == 0) { |
| 2523 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2524 | sizeof(xmlNodePtr)); |
| 2525 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2526 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2527 | return(NULL); |
| 2528 | } |
| 2529 | memset(val1->nodeTab, 0 , |
| 2530 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2531 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2532 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2533 | xmlNodePtr *temp; |
| 2534 | |
| 2535 | val1->nodeMax *= 2; |
| 2536 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2537 | sizeof(xmlNodePtr)); |
| 2538 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2539 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2540 | return(NULL); |
| 2541 | } |
| 2542 | val1->nodeTab = temp; |
| 2543 | } |
| 2544 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2545 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2546 | |
| 2547 | val1->nodeTab[val1->nodeNr++] = |
| 2548 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2549 | } else |
| 2550 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2551 | } |
| 2552 | |
| 2553 | return(val1); |
| 2554 | } |
| 2555 | |
| 2556 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2557 | * xmlXPathNodeSetDel: |
| 2558 | * @cur: the initial node set |
| 2559 | * @val: an xmlNodePtr |
| 2560 | * |
| 2561 | * Removes an xmlNodePtr from an existing NodeSet |
| 2562 | */ |
| 2563 | void |
| 2564 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2565 | int i; |
| 2566 | |
| 2567 | if (cur == NULL) return; |
| 2568 | if (val == NULL) return; |
| 2569 | |
| 2570 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2571 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2572 | */ |
| 2573 | for (i = 0;i < cur->nodeNr;i++) |
| 2574 | if (cur->nodeTab[i] == val) break; |
| 2575 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2576 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2577 | #ifdef DEBUG |
| 2578 | xmlGenericError(xmlGenericErrorContext, |
| 2579 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2580 | val->name); |
| 2581 | #endif |
| 2582 | return; |
| 2583 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2584 | if ((cur->nodeTab[i] != NULL) && |
| 2585 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2586 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2587 | cur->nodeNr--; |
| 2588 | for (;i < cur->nodeNr;i++) |
| 2589 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2590 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2591 | } |
| 2592 | |
| 2593 | /** |
| 2594 | * xmlXPathNodeSetRemove: |
| 2595 | * @cur: the initial node set |
| 2596 | * @val: the index to remove |
| 2597 | * |
| 2598 | * Removes an entry from an existing NodeSet list. |
| 2599 | */ |
| 2600 | void |
| 2601 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2602 | if (cur == NULL) return; |
| 2603 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2604 | if ((cur->nodeTab[val] != NULL) && |
| 2605 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2606 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2607 | cur->nodeNr--; |
| 2608 | for (;val < cur->nodeNr;val++) |
| 2609 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2610 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2611 | } |
| 2612 | |
| 2613 | /** |
| 2614 | * xmlXPathFreeNodeSet: |
| 2615 | * @obj: the xmlNodeSetPtr to free |
| 2616 | * |
| 2617 | * Free the NodeSet compound (not the actual nodes !). |
| 2618 | */ |
| 2619 | void |
| 2620 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2621 | if (obj == NULL) return; |
| 2622 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2623 | int i; |
| 2624 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2625 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2626 | for (i = 0;i < obj->nodeNr;i++) |
| 2627 | if ((obj->nodeTab[i] != NULL) && |
| 2628 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2629 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2630 | xmlFree(obj->nodeTab); |
| 2631 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | xmlFree(obj); |
| 2633 | } |
| 2634 | |
| 2635 | /** |
| 2636 | * xmlXPathFreeValueTree: |
| 2637 | * @obj: the xmlNodeSetPtr to free |
| 2638 | * |
| 2639 | * Free the NodeSet compound and the actual tree, this is different |
| 2640 | * from xmlXPathFreeNodeSet() |
| 2641 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2642 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2644 | int i; |
| 2645 | |
| 2646 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2649 | for (i = 0;i < obj->nodeNr;i++) { |
| 2650 | if (obj->nodeTab[i] != NULL) { |
| 2651 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2652 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2653 | } else { |
| 2654 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2655 | } |
| 2656 | } |
| 2657 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2658 | xmlFree(obj->nodeTab); |
| 2659 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2660 | xmlFree(obj); |
| 2661 | } |
| 2662 | |
| 2663 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2664 | /** |
| 2665 | * xmlGenericErrorContextNodeSet: |
| 2666 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2667 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | * |
| 2669 | * Quick display of a NodeSet |
| 2670 | */ |
| 2671 | void |
| 2672 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2673 | int i; |
| 2674 | |
| 2675 | if (output == NULL) output = xmlGenericErrorContext; |
| 2676 | if (obj == NULL) { |
| 2677 | fprintf(output, "NodeSet == NULL !\n"); |
| 2678 | return; |
| 2679 | } |
| 2680 | if (obj->nodeNr == 0) { |
| 2681 | fprintf(output, "NodeSet is empty\n"); |
| 2682 | return; |
| 2683 | } |
| 2684 | if (obj->nodeTab == NULL) { |
| 2685 | fprintf(output, " nodeTab == NULL !\n"); |
| 2686 | return; |
| 2687 | } |
| 2688 | for (i = 0; i < obj->nodeNr; i++) { |
| 2689 | if (obj->nodeTab[i] == NULL) { |
| 2690 | fprintf(output, " NULL !\n"); |
| 2691 | return; |
| 2692 | } |
| 2693 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2694 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2695 | fprintf(output, " /"); |
| 2696 | else if (obj->nodeTab[i]->name == NULL) |
| 2697 | fprintf(output, " noname!"); |
| 2698 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2699 | } |
| 2700 | fprintf(output, "\n"); |
| 2701 | } |
| 2702 | #endif |
| 2703 | |
| 2704 | /** |
| 2705 | * xmlXPathNewNodeSet: |
| 2706 | * @val: the NodePtr value |
| 2707 | * |
| 2708 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2709 | * it with the single Node @val |
| 2710 | * |
| 2711 | * Returns the newly created object. |
| 2712 | */ |
| 2713 | xmlXPathObjectPtr |
| 2714 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2715 | xmlXPathObjectPtr ret; |
| 2716 | |
| 2717 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2718 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2719 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2720 | return(NULL); |
| 2721 | } |
| 2722 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2723 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2724 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2725 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2726 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2727 | return(ret); |
| 2728 | } |
| 2729 | |
| 2730 | /** |
| 2731 | * xmlXPathNewValueTree: |
| 2732 | * @val: the NodePtr value |
| 2733 | * |
| 2734 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2735 | * it with the tree root @val |
| 2736 | * |
| 2737 | * Returns the newly created object. |
| 2738 | */ |
| 2739 | xmlXPathObjectPtr |
| 2740 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2741 | xmlXPathObjectPtr ret; |
| 2742 | |
| 2743 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2744 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2745 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2746 | return(NULL); |
| 2747 | } |
| 2748 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2749 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2750 | ret->boolval = 1; |
| 2751 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2753 | return(ret); |
| 2754 | } |
| 2755 | |
| 2756 | /** |
| 2757 | * xmlXPathNewNodeSetList: |
| 2758 | * @val: an existing NodeSet |
| 2759 | * |
| 2760 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2761 | * it with the Nodeset @val |
| 2762 | * |
| 2763 | * Returns the newly created object. |
| 2764 | */ |
| 2765 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2766 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2767 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2768 | xmlXPathObjectPtr ret; |
| 2769 | int i; |
| 2770 | |
| 2771 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2772 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2773 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2774 | ret = xmlXPathNewNodeSet(NULL); |
| 2775 | else { |
| 2776 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2777 | for (i = 1; i < val->nodeNr; ++i) |
| 2778 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2779 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2780 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2781 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * xmlXPathWrapNodeSet: |
| 2786 | * @val: the NodePtr value |
| 2787 | * |
| 2788 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2789 | * |
| 2790 | * Returns the newly created object. |
| 2791 | */ |
| 2792 | xmlXPathObjectPtr |
| 2793 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2794 | xmlXPathObjectPtr ret; |
| 2795 | |
| 2796 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2797 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2798 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2799 | return(NULL); |
| 2800 | } |
| 2801 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2802 | ret->type = XPATH_NODESET; |
| 2803 | ret->nodesetval = val; |
| 2804 | return(ret); |
| 2805 | } |
| 2806 | |
| 2807 | /** |
| 2808 | * xmlXPathFreeNodeSetList: |
| 2809 | * @obj: an existing NodeSetList object |
| 2810 | * |
| 2811 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2812 | * the list contrary to xmlXPathFreeObject(). |
| 2813 | */ |
| 2814 | void |
| 2815 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2816 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | xmlFree(obj); |
| 2818 | } |
| 2819 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2820 | /** |
| 2821 | * xmlXPathDifference: |
| 2822 | * @nodes1: a node-set |
| 2823 | * @nodes2: a node-set |
| 2824 | * |
| 2825 | * Implements the EXSLT - Sets difference() function: |
| 2826 | * node-set set:difference (node-set, node-set) |
| 2827 | * |
| 2828 | * Returns the difference between the two node sets, or nodes1 if |
| 2829 | * nodes2 is empty |
| 2830 | */ |
| 2831 | xmlNodeSetPtr |
| 2832 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2833 | xmlNodeSetPtr ret; |
| 2834 | int i, l1; |
| 2835 | xmlNodePtr cur; |
| 2836 | |
| 2837 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2838 | return(nodes1); |
| 2839 | |
| 2840 | ret = xmlXPathNodeSetCreate(NULL); |
| 2841 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2842 | return(ret); |
| 2843 | |
| 2844 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2845 | |
| 2846 | for (i = 0; i < l1; i++) { |
| 2847 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2848 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2849 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2850 | } |
| 2851 | return(ret); |
| 2852 | } |
| 2853 | |
| 2854 | /** |
| 2855 | * xmlXPathIntersection: |
| 2856 | * @nodes1: a node-set |
| 2857 | * @nodes2: a node-set |
| 2858 | * |
| 2859 | * Implements the EXSLT - Sets intersection() function: |
| 2860 | * node-set set:intersection (node-set, node-set) |
| 2861 | * |
| 2862 | * Returns a node set comprising the nodes that are within both the |
| 2863 | * node sets passed as arguments |
| 2864 | */ |
| 2865 | xmlNodeSetPtr |
| 2866 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2867 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2868 | int i, l1; |
| 2869 | xmlNodePtr cur; |
| 2870 | |
| 2871 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2872 | return(ret); |
| 2873 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2874 | return(ret); |
| 2875 | |
| 2876 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2877 | |
| 2878 | for (i = 0; i < l1; i++) { |
| 2879 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2880 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2881 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2882 | } |
| 2883 | return(ret); |
| 2884 | } |
| 2885 | |
| 2886 | /** |
| 2887 | * xmlXPathDistinctSorted: |
| 2888 | * @nodes: a node-set, sorted by document order |
| 2889 | * |
| 2890 | * Implements the EXSLT - Sets distinct() function: |
| 2891 | * node-set set:distinct (node-set) |
| 2892 | * |
| 2893 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2894 | * it is empty |
| 2895 | */ |
| 2896 | xmlNodeSetPtr |
| 2897 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2898 | xmlNodeSetPtr ret; |
| 2899 | xmlHashTablePtr hash; |
| 2900 | int i, l; |
| 2901 | xmlChar * strval; |
| 2902 | xmlNodePtr cur; |
| 2903 | |
| 2904 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2905 | return(nodes); |
| 2906 | |
| 2907 | ret = xmlXPathNodeSetCreate(NULL); |
| 2908 | l = xmlXPathNodeSetGetLength(nodes); |
| 2909 | hash = xmlHashCreate (l); |
| 2910 | for (i = 0; i < l; i++) { |
| 2911 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2912 | strval = xmlXPathCastNodeToString(cur); |
| 2913 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2914 | xmlHashAddEntry(hash, strval, strval); |
| 2915 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2916 | } else { |
| 2917 | xmlFree(strval); |
| 2918 | } |
| 2919 | } |
| 2920 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2921 | return(ret); |
| 2922 | } |
| 2923 | |
| 2924 | /** |
| 2925 | * xmlXPathDistinct: |
| 2926 | * @nodes: a node-set |
| 2927 | * |
| 2928 | * Implements the EXSLT - Sets distinct() function: |
| 2929 | * node-set set:distinct (node-set) |
| 2930 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2931 | * is called with the sorted node-set |
| 2932 | * |
| 2933 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2934 | * it is empty |
| 2935 | */ |
| 2936 | xmlNodeSetPtr |
| 2937 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2938 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2939 | return(nodes); |
| 2940 | |
| 2941 | xmlXPathNodeSetSort(nodes); |
| 2942 | return(xmlXPathDistinctSorted(nodes)); |
| 2943 | } |
| 2944 | |
| 2945 | /** |
| 2946 | * xmlXPathHasSameNodes: |
| 2947 | * @nodes1: a node-set |
| 2948 | * @nodes2: a node-set |
| 2949 | * |
| 2950 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2951 | * boolean set:has-same-node(node-set, node-set) |
| 2952 | * |
| 2953 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2954 | * otherwise |
| 2955 | */ |
| 2956 | int |
| 2957 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2958 | int i, l; |
| 2959 | xmlNodePtr cur; |
| 2960 | |
| 2961 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2962 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2963 | return(0); |
| 2964 | |
| 2965 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2966 | for (i = 0; i < l; i++) { |
| 2967 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2968 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2969 | return(1); |
| 2970 | } |
| 2971 | return(0); |
| 2972 | } |
| 2973 | |
| 2974 | /** |
| 2975 | * xmlXPathNodeLeadingSorted: |
| 2976 | * @nodes: a node-set, sorted by document order |
| 2977 | * @node: a node |
| 2978 | * |
| 2979 | * Implements the EXSLT - Sets leading() function: |
| 2980 | * node-set set:leading (node-set, node-set) |
| 2981 | * |
| 2982 | * Returns the nodes in @nodes that precede @node in document order, |
| 2983 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2984 | * doesn't contain @node |
| 2985 | */ |
| 2986 | xmlNodeSetPtr |
| 2987 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2988 | int i, l; |
| 2989 | xmlNodePtr cur; |
| 2990 | xmlNodeSetPtr ret; |
| 2991 | |
| 2992 | if (node == NULL) |
| 2993 | return(nodes); |
| 2994 | |
| 2995 | ret = xmlXPathNodeSetCreate(NULL); |
| 2996 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2997 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2998 | return(ret); |
| 2999 | |
| 3000 | l = xmlXPathNodeSetGetLength(nodes); |
| 3001 | for (i = 0; i < l; i++) { |
| 3002 | cur = xmlXPathNodeSetItem(nodes, i); |
| 3003 | if (cur == node) |
| 3004 | break; |
| 3005 | xmlXPathNodeSetAddUnique(ret, cur); |
| 3006 | } |
| 3007 | return(ret); |
| 3008 | } |
| 3009 | |
| 3010 | /** |
| 3011 | * xmlXPathNodeLeading: |
| 3012 | * @nodes: a node-set |
| 3013 | * @node: a node |
| 3014 | * |
| 3015 | * Implements the EXSLT - Sets leading() function: |
| 3016 | * node-set set:leading (node-set, node-set) |
| 3017 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 3018 | * is called. |
| 3019 | * |
| 3020 | * Returns the nodes in @nodes that precede @node in document order, |
| 3021 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 3022 | * doesn't contain @node |
| 3023 | */ |
| 3024 | xmlNodeSetPtr |
| 3025 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 3026 | xmlXPathNodeSetSort(nodes); |
| 3027 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 3028 | } |
| 3029 | |
| 3030 | /** |
| 3031 | * xmlXPathLeadingSorted: |
| 3032 | * @nodes1: a node-set, sorted by document order |
| 3033 | * @nodes2: a node-set, sorted by document order |
| 3034 | * |
| 3035 | * Implements the EXSLT - Sets leading() function: |
| 3036 | * node-set set:leading (node-set, node-set) |
| 3037 | * |
| 3038 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 3039 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 3040 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 3041 | */ |
| 3042 | xmlNodeSetPtr |
| 3043 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 3044 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 3045 | return(nodes1); |
| 3046 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 3047 | xmlXPathNodeSetItem(nodes2, 1))); |
| 3048 | } |
| 3049 | |
| 3050 | /** |
| 3051 | * xmlXPathLeading: |
| 3052 | * @nodes1: a node-set |
| 3053 | * @nodes2: a node-set |
| 3054 | * |
| 3055 | * Implements the EXSLT - Sets leading() function: |
| 3056 | * node-set set:leading (node-set, node-set) |
| 3057 | * @nodes1 and @nodes2 are sorted by document order, then |
| 3058 | * #exslSetsLeadingSorted is called. |
| 3059 | * |
| 3060 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 3061 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 3062 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 3063 | */ |
| 3064 | xmlNodeSetPtr |
| 3065 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 3066 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 3067 | return(nodes1); |
| 3068 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 3069 | return(xmlXPathNodeSetCreate(NULL)); |
| 3070 | xmlXPathNodeSetSort(nodes1); |
| 3071 | xmlXPathNodeSetSort(nodes2); |
| 3072 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 3073 | xmlXPathNodeSetItem(nodes2, 1))); |
| 3074 | } |
| 3075 | |
| 3076 | /** |
| 3077 | * xmlXPathNodeTrailingSorted: |
| 3078 | * @nodes: a node-set, sorted by document order |
| 3079 | * @node: a node |
| 3080 | * |
| 3081 | * Implements the EXSLT - Sets trailing() function: |
| 3082 | * node-set set:trailing (node-set, node-set) |
| 3083 | * |
| 3084 | * Returns the nodes in @nodes that follow @node in document order, |
| 3085 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 3086 | * doesn't contain @node |
| 3087 | */ |
| 3088 | xmlNodeSetPtr |
| 3089 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 3090 | int i, l; |
| 3091 | xmlNodePtr cur; |
| 3092 | xmlNodeSetPtr ret; |
| 3093 | |
| 3094 | if (node == NULL) |
| 3095 | return(nodes); |
| 3096 | |
| 3097 | ret = xmlXPathNodeSetCreate(NULL); |
| 3098 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 3099 | (!xmlXPathNodeSetContains(nodes, node))) |
| 3100 | return(ret); |
| 3101 | |
| 3102 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 3103 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3104 | cur = xmlXPathNodeSetItem(nodes, i); |
| 3105 | if (cur == node) |
| 3106 | break; |
| 3107 | xmlXPathNodeSetAddUnique(ret, cur); |
| 3108 | } |
| 3109 | return(ret); |
| 3110 | } |
| 3111 | |
| 3112 | /** |
| 3113 | * xmlXPathNodeTrailing: |
| 3114 | * @nodes: a node-set |
| 3115 | * @node: a node |
| 3116 | * |
| 3117 | * Implements the EXSLT - Sets trailing() function: |
| 3118 | * node-set set:trailing (node-set, node-set) |
| 3119 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 3120 | * is called. |
| 3121 | * |
| 3122 | * Returns the nodes in @nodes that follow @node in document order, |
| 3123 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 3124 | * doesn't contain @node |
| 3125 | */ |
| 3126 | xmlNodeSetPtr |
| 3127 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 3128 | xmlXPathNodeSetSort(nodes); |
| 3129 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 3130 | } |
| 3131 | |
| 3132 | /** |
| 3133 | * xmlXPathTrailingSorted: |
| 3134 | * @nodes1: a node-set, sorted by document order |
| 3135 | * @nodes2: a node-set, sorted by document order |
| 3136 | * |
| 3137 | * Implements the EXSLT - Sets trailing() function: |
| 3138 | * node-set set:trailing (node-set, node-set) |
| 3139 | * |
| 3140 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 3141 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 3142 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 3143 | */ |
| 3144 | xmlNodeSetPtr |
| 3145 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 3146 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 3147 | return(nodes1); |
| 3148 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 3149 | xmlXPathNodeSetItem(nodes2, 0))); |
| 3150 | } |
| 3151 | |
| 3152 | /** |
| 3153 | * xmlXPathTrailing: |
| 3154 | * @nodes1: a node-set |
| 3155 | * @nodes2: a node-set |
| 3156 | * |
| 3157 | * Implements the EXSLT - Sets trailing() function: |
| 3158 | * node-set set:trailing (node-set, node-set) |
| 3159 | * @nodes1 and @nodes2 are sorted by document order, then |
| 3160 | * #xmlXPathTrailingSorted is called. |
| 3161 | * |
| 3162 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 3163 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 3164 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 3165 | */ |
| 3166 | xmlNodeSetPtr |
| 3167 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 3168 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 3169 | return(nodes1); |
| 3170 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 3171 | return(xmlXPathNodeSetCreate(NULL)); |
| 3172 | xmlXPathNodeSetSort(nodes1); |
| 3173 | xmlXPathNodeSetSort(nodes2); |
| 3174 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 3175 | xmlXPathNodeSetItem(nodes2, 0))); |
| 3176 | } |
| 3177 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3178 | /************************************************************************ |
| 3179 | * * |
| 3180 | * Routines to handle extra functions * |
| 3181 | * * |
| 3182 | ************************************************************************/ |
| 3183 | |
| 3184 | /** |
| 3185 | * xmlXPathRegisterFunc: |
| 3186 | * @ctxt: the XPath context |
| 3187 | * @name: the function name |
| 3188 | * @f: the function implementation or NULL |
| 3189 | * |
| 3190 | * Register a new function. If @f is NULL it unregisters the function |
| 3191 | * |
| 3192 | * Returns 0 in case of success, -1 in case of error |
| 3193 | */ |
| 3194 | int |
| 3195 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3196 | xmlXPathFunction f) { |
| 3197 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 3198 | } |
| 3199 | |
| 3200 | /** |
| 3201 | * xmlXPathRegisterFuncNS: |
| 3202 | * @ctxt: the XPath context |
| 3203 | * @name: the function name |
| 3204 | * @ns_uri: the function namespace URI |
| 3205 | * @f: the function implementation or NULL |
| 3206 | * |
| 3207 | * Register a new function. If @f is NULL it unregisters the function |
| 3208 | * |
| 3209 | * Returns 0 in case of success, -1 in case of error |
| 3210 | */ |
| 3211 | int |
| 3212 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3213 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 3214 | if (ctxt == NULL) |
| 3215 | return(-1); |
| 3216 | if (name == NULL) |
| 3217 | return(-1); |
| 3218 | |
| 3219 | if (ctxt->funcHash == NULL) |
| 3220 | ctxt->funcHash = xmlHashCreate(0); |
| 3221 | if (ctxt->funcHash == NULL) |
| 3222 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3223 | if (f == NULL) |
| 3224 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 3225 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3229 | * xmlXPathRegisterFuncLookup: |
| 3230 | * @ctxt: the XPath context |
| 3231 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3232 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3233 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3234 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3235 | */ |
| 3236 | void |
| 3237 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 3238 | xmlXPathFuncLookupFunc f, |
| 3239 | void *funcCtxt) { |
| 3240 | if (ctxt == NULL) |
| 3241 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3242 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3243 | ctxt->funcLookupData = funcCtxt; |
| 3244 | } |
| 3245 | |
| 3246 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3247 | * xmlXPathFunctionLookup: |
| 3248 | * @ctxt: the XPath context |
| 3249 | * @name: the function name |
| 3250 | * |
| 3251 | * Search in the Function array of the context for the given |
| 3252 | * function. |
| 3253 | * |
| 3254 | * Returns the xmlXPathFunction or NULL if not found |
| 3255 | */ |
| 3256 | xmlXPathFunction |
| 3257 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3258 | if (ctxt == NULL) |
| 3259 | return (NULL); |
| 3260 | |
| 3261 | if (ctxt->funcLookupFunc != NULL) { |
| 3262 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 3263 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3264 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3265 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 3266 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3267 | if (ret != NULL) |
| 3268 | return(ret); |
| 3269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3270 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 3271 | } |
| 3272 | |
| 3273 | /** |
| 3274 | * xmlXPathFunctionLookupNS: |
| 3275 | * @ctxt: the XPath context |
| 3276 | * @name: the function name |
| 3277 | * @ns_uri: the function namespace URI |
| 3278 | * |
| 3279 | * Search in the Function array of the context for the given |
| 3280 | * function. |
| 3281 | * |
| 3282 | * Returns the xmlXPathFunction or NULL if not found |
| 3283 | */ |
| 3284 | xmlXPathFunction |
| 3285 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3286 | const xmlChar *ns_uri) { |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 3287 | xmlXPathFunction ret; |
| 3288 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3289 | if (ctxt == NULL) |
| 3290 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3291 | if (name == NULL) |
| 3292 | return(NULL); |
| 3293 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3294 | if (ctxt->funcLookupFunc != NULL) { |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 3295 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3296 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3297 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 3298 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 3299 | if (ret != NULL) |
| 3300 | return(ret); |
| 3301 | } |
| 3302 | |
| 3303 | if (ctxt->funcHash == NULL) |
| 3304 | return(NULL); |
| 3305 | |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 3306 | XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri); |
| 3307 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | /** |
| 3311 | * xmlXPathRegisteredFuncsCleanup: |
| 3312 | * @ctxt: the XPath context |
| 3313 | * |
| 3314 | * Cleanup the XPath context data associated to registered functions |
| 3315 | */ |
| 3316 | void |
| 3317 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 3318 | if (ctxt == NULL) |
| 3319 | return; |
| 3320 | |
| 3321 | xmlHashFree(ctxt->funcHash, NULL); |
| 3322 | ctxt->funcHash = NULL; |
| 3323 | } |
| 3324 | |
| 3325 | /************************************************************************ |
| 3326 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3327 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3328 | * * |
| 3329 | ************************************************************************/ |
| 3330 | |
| 3331 | /** |
| 3332 | * xmlXPathRegisterVariable: |
| 3333 | * @ctxt: the XPath context |
| 3334 | * @name: the variable name |
| 3335 | * @value: the variable value or NULL |
| 3336 | * |
| 3337 | * Register a new variable value. If @value is NULL it unregisters |
| 3338 | * the variable |
| 3339 | * |
| 3340 | * Returns 0 in case of success, -1 in case of error |
| 3341 | */ |
| 3342 | int |
| 3343 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3344 | xmlXPathObjectPtr value) { |
| 3345 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 3346 | } |
| 3347 | |
| 3348 | /** |
| 3349 | * xmlXPathRegisterVariableNS: |
| 3350 | * @ctxt: the XPath context |
| 3351 | * @name: the variable name |
| 3352 | * @ns_uri: the variable namespace URI |
| 3353 | * @value: the variable value or NULL |
| 3354 | * |
| 3355 | * Register a new variable value. If @value is NULL it unregisters |
| 3356 | * the variable |
| 3357 | * |
| 3358 | * Returns 0 in case of success, -1 in case of error |
| 3359 | */ |
| 3360 | int |
| 3361 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3362 | const xmlChar *ns_uri, |
| 3363 | xmlXPathObjectPtr value) { |
| 3364 | if (ctxt == NULL) |
| 3365 | return(-1); |
| 3366 | if (name == NULL) |
| 3367 | return(-1); |
| 3368 | |
| 3369 | if (ctxt->varHash == NULL) |
| 3370 | ctxt->varHash = xmlHashCreate(0); |
| 3371 | if (ctxt->varHash == NULL) |
| 3372 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3373 | if (value == NULL) |
| 3374 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 3375 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3376 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 3377 | (void *) value, |
| 3378 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * xmlXPathRegisterVariableLookup: |
| 3383 | * @ctxt: the XPath context |
| 3384 | * @f: the lookup function |
| 3385 | * @data: the lookup data |
| 3386 | * |
| 3387 | * register an external mechanism to do variable lookup |
| 3388 | */ |
| 3389 | void |
| 3390 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 3391 | xmlXPathVariableLookupFunc f, void *data) { |
| 3392 | if (ctxt == NULL) |
| 3393 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3394 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3395 | ctxt->varLookupData = data; |
| 3396 | } |
| 3397 | |
| 3398 | /** |
| 3399 | * xmlXPathVariableLookup: |
| 3400 | * @ctxt: the XPath context |
| 3401 | * @name: the variable name |
| 3402 | * |
| 3403 | * Search in the Variable array of the context for the given |
| 3404 | * variable value. |
| 3405 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3406 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3407 | */ |
| 3408 | xmlXPathObjectPtr |
| 3409 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 3410 | if (ctxt == NULL) |
| 3411 | return(NULL); |
| 3412 | |
| 3413 | if (ctxt->varLookupFunc != NULL) { |
| 3414 | xmlXPathObjectPtr ret; |
| 3415 | |
| 3416 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3417 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 3418 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3419 | } |
| 3420 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 3421 | } |
| 3422 | |
| 3423 | /** |
| 3424 | * xmlXPathVariableLookupNS: |
| 3425 | * @ctxt: the XPath context |
| 3426 | * @name: the variable name |
| 3427 | * @ns_uri: the variable namespace URI |
| 3428 | * |
| 3429 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3430 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3431 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3432 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3433 | */ |
| 3434 | xmlXPathObjectPtr |
| 3435 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3436 | const xmlChar *ns_uri) { |
| 3437 | if (ctxt == NULL) |
| 3438 | return(NULL); |
| 3439 | |
| 3440 | if (ctxt->varLookupFunc != NULL) { |
| 3441 | xmlXPathObjectPtr ret; |
| 3442 | |
| 3443 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3444 | (ctxt->varLookupData, name, ns_uri); |
| 3445 | if (ret != NULL) return(ret); |
| 3446 | } |
| 3447 | |
| 3448 | if (ctxt->varHash == NULL) |
| 3449 | return(NULL); |
| 3450 | if (name == NULL) |
| 3451 | return(NULL); |
| 3452 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 3453 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 3454 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | /** |
| 3458 | * xmlXPathRegisteredVariablesCleanup: |
| 3459 | * @ctxt: the XPath context |
| 3460 | * |
| 3461 | * Cleanup the XPath context data associated to registered variables |
| 3462 | */ |
| 3463 | void |
| 3464 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 3465 | if (ctxt == NULL) |
| 3466 | return; |
| 3467 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3468 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3469 | ctxt->varHash = NULL; |
| 3470 | } |
| 3471 | |
| 3472 | /** |
| 3473 | * xmlXPathRegisterNs: |
| 3474 | * @ctxt: the XPath context |
| 3475 | * @prefix: the namespace prefix |
| 3476 | * @ns_uri: the namespace name |
| 3477 | * |
| 3478 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 3479 | * the namespace |
| 3480 | * |
| 3481 | * Returns 0 in case of success, -1 in case of error |
| 3482 | */ |
| 3483 | int |
| 3484 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3485 | const xmlChar *ns_uri) { |
| 3486 | if (ctxt == NULL) |
| 3487 | return(-1); |
| 3488 | if (prefix == NULL) |
| 3489 | return(-1); |
| 3490 | |
| 3491 | if (ctxt->nsHash == NULL) |
| 3492 | ctxt->nsHash = xmlHashCreate(10); |
| 3493 | if (ctxt->nsHash == NULL) |
| 3494 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3495 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3496 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3497 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3498 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3499 | (xmlHashDeallocator)xmlFree)); |
| 3500 | } |
| 3501 | |
| 3502 | /** |
| 3503 | * xmlXPathNsLookup: |
| 3504 | * @ctxt: the XPath context |
| 3505 | * @prefix: the namespace prefix value |
| 3506 | * |
| 3507 | * Search in the namespace declaration array of the context for the given |
| 3508 | * namespace name associated to the given prefix |
| 3509 | * |
| 3510 | * Returns the value or NULL if not found |
| 3511 | */ |
| 3512 | const xmlChar * |
| 3513 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3514 | if (ctxt == NULL) |
| 3515 | return(NULL); |
| 3516 | if (prefix == NULL) |
| 3517 | return(NULL); |
| 3518 | |
| 3519 | #ifdef XML_XML_NAMESPACE |
| 3520 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3521 | return(XML_XML_NAMESPACE); |
| 3522 | #endif |
| 3523 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3524 | if (ctxt->namespaces != NULL) { |
| 3525 | int i; |
| 3526 | |
| 3527 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3528 | if ((ctxt->namespaces[i] != NULL) && |
| 3529 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3530 | return(ctxt->namespaces[i]->href); |
| 3531 | } |
| 3532 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3533 | |
| 3534 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3535 | } |
| 3536 | |
| 3537 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3538 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3539 | * @ctxt: the XPath context |
| 3540 | * |
| 3541 | * Cleanup the XPath context data associated to registered variables |
| 3542 | */ |
| 3543 | void |
| 3544 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3545 | if (ctxt == NULL) |
| 3546 | return; |
| 3547 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3548 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3549 | ctxt->nsHash = NULL; |
| 3550 | } |
| 3551 | |
| 3552 | /************************************************************************ |
| 3553 | * * |
| 3554 | * Routines to handle Values * |
| 3555 | * * |
| 3556 | ************************************************************************/ |
| 3557 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3558 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | |
| 3560 | /** |
| 3561 | * xmlXPathNewFloat: |
| 3562 | * @val: the double value |
| 3563 | * |
| 3564 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3565 | * |
| 3566 | * Returns the newly created object. |
| 3567 | */ |
| 3568 | xmlXPathObjectPtr |
| 3569 | xmlXPathNewFloat(double val) { |
| 3570 | xmlXPathObjectPtr ret; |
| 3571 | |
| 3572 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3573 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3574 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3575 | return(NULL); |
| 3576 | } |
| 3577 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3578 | ret->type = XPATH_NUMBER; |
| 3579 | ret->floatval = val; |
| 3580 | return(ret); |
| 3581 | } |
| 3582 | |
| 3583 | /** |
| 3584 | * xmlXPathNewBoolean: |
| 3585 | * @val: the boolean value |
| 3586 | * |
| 3587 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3588 | * |
| 3589 | * Returns the newly created object. |
| 3590 | */ |
| 3591 | xmlXPathObjectPtr |
| 3592 | xmlXPathNewBoolean(int val) { |
| 3593 | xmlXPathObjectPtr ret; |
| 3594 | |
| 3595 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3596 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3597 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3598 | return(NULL); |
| 3599 | } |
| 3600 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3601 | ret->type = XPATH_BOOLEAN; |
| 3602 | ret->boolval = (val != 0); |
| 3603 | return(ret); |
| 3604 | } |
| 3605 | |
| 3606 | /** |
| 3607 | * xmlXPathNewString: |
| 3608 | * @val: the xmlChar * value |
| 3609 | * |
| 3610 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3611 | * |
| 3612 | * Returns the newly created object. |
| 3613 | */ |
| 3614 | xmlXPathObjectPtr |
| 3615 | xmlXPathNewString(const xmlChar *val) { |
| 3616 | xmlXPathObjectPtr ret; |
| 3617 | |
| 3618 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3619 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3620 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3621 | return(NULL); |
| 3622 | } |
| 3623 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3624 | ret->type = XPATH_STRING; |
| 3625 | if (val != NULL) |
| 3626 | ret->stringval = xmlStrdup(val); |
| 3627 | else |
| 3628 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3629 | return(ret); |
| 3630 | } |
| 3631 | |
| 3632 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3633 | * xmlXPathWrapString: |
| 3634 | * @val: the xmlChar * value |
| 3635 | * |
| 3636 | * Wraps the @val string into an XPath object. |
| 3637 | * |
| 3638 | * Returns the newly created object. |
| 3639 | */ |
| 3640 | xmlXPathObjectPtr |
| 3641 | xmlXPathWrapString (xmlChar *val) { |
| 3642 | xmlXPathObjectPtr ret; |
| 3643 | |
| 3644 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3645 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3646 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3647 | return(NULL); |
| 3648 | } |
| 3649 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3650 | ret->type = XPATH_STRING; |
| 3651 | ret->stringval = val; |
| 3652 | return(ret); |
| 3653 | } |
| 3654 | |
| 3655 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3656 | * xmlXPathNewCString: |
| 3657 | * @val: the char * value |
| 3658 | * |
| 3659 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3660 | * |
| 3661 | * Returns the newly created object. |
| 3662 | */ |
| 3663 | xmlXPathObjectPtr |
| 3664 | xmlXPathNewCString(const char *val) { |
| 3665 | xmlXPathObjectPtr ret; |
| 3666 | |
| 3667 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3668 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3669 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3670 | return(NULL); |
| 3671 | } |
| 3672 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3673 | ret->type = XPATH_STRING; |
| 3674 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3675 | return(ret); |
| 3676 | } |
| 3677 | |
| 3678 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3679 | * xmlXPathWrapCString: |
| 3680 | * @val: the char * value |
| 3681 | * |
| 3682 | * Wraps a string into an XPath object. |
| 3683 | * |
| 3684 | * Returns the newly created object. |
| 3685 | */ |
| 3686 | xmlXPathObjectPtr |
| 3687 | xmlXPathWrapCString (char * val) { |
| 3688 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3689 | } |
| 3690 | |
| 3691 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3692 | * xmlXPathWrapExternal: |
| 3693 | * @val: the user data |
| 3694 | * |
| 3695 | * Wraps the @val data into an XPath object. |
| 3696 | * |
| 3697 | * Returns the newly created object. |
| 3698 | */ |
| 3699 | xmlXPathObjectPtr |
| 3700 | xmlXPathWrapExternal (void *val) { |
| 3701 | xmlXPathObjectPtr ret; |
| 3702 | |
| 3703 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3704 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3705 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3706 | return(NULL); |
| 3707 | } |
| 3708 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3709 | ret->type = XPATH_USERS; |
| 3710 | ret->user = val; |
| 3711 | return(ret); |
| 3712 | } |
| 3713 | |
| 3714 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3715 | * xmlXPathObjectCopy: |
| 3716 | * @val: the original object |
| 3717 | * |
| 3718 | * allocate a new copy of a given object |
| 3719 | * |
| 3720 | * Returns the newly created object. |
| 3721 | */ |
| 3722 | xmlXPathObjectPtr |
| 3723 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3724 | xmlXPathObjectPtr ret; |
| 3725 | |
| 3726 | if (val == NULL) |
| 3727 | return(NULL); |
| 3728 | |
| 3729 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3730 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3731 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3732 | return(NULL); |
| 3733 | } |
| 3734 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3735 | switch (val->type) { |
| 3736 | case XPATH_BOOLEAN: |
| 3737 | case XPATH_NUMBER: |
| 3738 | case XPATH_POINT: |
| 3739 | case XPATH_RANGE: |
| 3740 | break; |
| 3741 | case XPATH_STRING: |
| 3742 | ret->stringval = xmlStrdup(val->stringval); |
| 3743 | break; |
| 3744 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3745 | #if 0 |
| 3746 | /* |
| 3747 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 3748 | this previous handling is no longer correct, and can cause some serious |
| 3749 | problems (ref. bug 145547) |
| 3750 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3751 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3752 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3753 | xmlNodePtr cur, tmp; |
| 3754 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3755 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3756 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3757 | top = xmlNewDoc(NULL); |
| 3758 | top->name = (char *) |
| 3759 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3760 | ret->user = top; |
| 3761 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3762 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3763 | cur = val->nodesetval->nodeTab[0]->children; |
| 3764 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3765 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3766 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3767 | cur = cur->next; |
| 3768 | } |
| 3769 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3770 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3771 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3772 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3773 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3774 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3775 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3776 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3777 | case XPATH_NODESET: |
| 3778 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3779 | /* Do not deallocate the copied tree value */ |
| 3780 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3781 | break; |
| 3782 | case XPATH_LOCATIONSET: |
| 3783 | #ifdef LIBXML_XPTR_ENABLED |
| 3784 | { |
| 3785 | xmlLocationSetPtr loc = val->user; |
| 3786 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3787 | break; |
| 3788 | } |
| 3789 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3790 | case XPATH_USERS: |
| 3791 | ret->user = val->user; |
| 3792 | break; |
| 3793 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3794 | xmlGenericError(xmlGenericErrorContext, |
| 3795 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3796 | val->type); |
| 3797 | break; |
| 3798 | } |
| 3799 | return(ret); |
| 3800 | } |
| 3801 | |
| 3802 | /** |
| 3803 | * xmlXPathFreeObject: |
| 3804 | * @obj: the object to free |
| 3805 | * |
| 3806 | * Free up an xmlXPathObjectPtr object. |
| 3807 | */ |
| 3808 | void |
| 3809 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3810 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3811 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3812 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3813 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3814 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3815 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3816 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3817 | } else |
| 3818 | #endif |
| 3819 | if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3820 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3821 | } else { |
| 3822 | if (obj->nodesetval != NULL) |
| 3823 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3824 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | #ifdef LIBXML_XPTR_ENABLED |
| 3826 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3827 | if (obj->user != NULL) |
| 3828 | xmlXPtrFreeLocationSet(obj->user); |
| 3829 | #endif |
| 3830 | } else if (obj->type == XPATH_STRING) { |
| 3831 | if (obj->stringval != NULL) |
| 3832 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3835 | xmlFree(obj); |
| 3836 | } |
| 3837 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3838 | |
| 3839 | /************************************************************************ |
| 3840 | * * |
| 3841 | * Type Casting Routines * |
| 3842 | * * |
| 3843 | ************************************************************************/ |
| 3844 | |
| 3845 | /** |
| 3846 | * xmlXPathCastBooleanToString: |
| 3847 | * @val: a boolean |
| 3848 | * |
| 3849 | * Converts a boolean to its string value. |
| 3850 | * |
| 3851 | * Returns a newly allocated string. |
| 3852 | */ |
| 3853 | xmlChar * |
| 3854 | xmlXPathCastBooleanToString (int val) { |
| 3855 | xmlChar *ret; |
| 3856 | if (val) |
| 3857 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3858 | else |
| 3859 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3860 | return(ret); |
| 3861 | } |
| 3862 | |
| 3863 | /** |
| 3864 | * xmlXPathCastNumberToString: |
| 3865 | * @val: a number |
| 3866 | * |
| 3867 | * Converts a number to its string value. |
| 3868 | * |
| 3869 | * Returns a newly allocated string. |
| 3870 | */ |
| 3871 | xmlChar * |
| 3872 | xmlXPathCastNumberToString (double val) { |
| 3873 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3874 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3875 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3876 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3877 | break; |
| 3878 | case -1: |
| 3879 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3880 | break; |
| 3881 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3882 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3883 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3884 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3885 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3886 | } else { |
| 3887 | /* could be improved */ |
| 3888 | char buf[100]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3889 | xmlXPathFormatNumber(val, buf, 99); |
| 3890 | buf[99] = 0; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3891 | ret = xmlStrdup((const xmlChar *) buf); |
| 3892 | } |
| 3893 | } |
| 3894 | return(ret); |
| 3895 | } |
| 3896 | |
| 3897 | /** |
| 3898 | * xmlXPathCastNodeToString: |
| 3899 | * @node: a node |
| 3900 | * |
| 3901 | * Converts a node to its string value. |
| 3902 | * |
| 3903 | * Returns a newly allocated string. |
| 3904 | */ |
| 3905 | xmlChar * |
| 3906 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3907 | return(xmlNodeGetContent(node)); |
| 3908 | } |
| 3909 | |
| 3910 | /** |
| 3911 | * xmlXPathCastNodeSetToString: |
| 3912 | * @ns: a node-set |
| 3913 | * |
| 3914 | * Converts a node-set to its string value. |
| 3915 | * |
| 3916 | * Returns a newly allocated string. |
| 3917 | */ |
| 3918 | xmlChar * |
| 3919 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3920 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3921 | return(xmlStrdup((const xmlChar *) "")); |
| 3922 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3923 | if (ns->nodeNr > 1) |
| 3924 | xmlXPathNodeSetSort(ns); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3925 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3926 | } |
| 3927 | |
| 3928 | /** |
| 3929 | * xmlXPathCastToString: |
| 3930 | * @val: an XPath object |
| 3931 | * |
| 3932 | * Converts an existing object to its string() equivalent |
| 3933 | * |
| 3934 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3935 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3936 | * string object). |
| 3937 | */ |
| 3938 | xmlChar * |
| 3939 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3940 | xmlChar *ret = NULL; |
| 3941 | |
| 3942 | if (val == NULL) |
| 3943 | return(xmlStrdup((const xmlChar *) "")); |
| 3944 | switch (val->type) { |
| 3945 | case XPATH_UNDEFINED: |
| 3946 | #ifdef DEBUG_EXPR |
| 3947 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3948 | #endif |
| 3949 | ret = xmlStrdup((const xmlChar *) ""); |
| 3950 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3951 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3952 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3953 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3954 | break; |
| 3955 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3956 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3957 | case XPATH_BOOLEAN: |
| 3958 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3959 | break; |
| 3960 | case XPATH_NUMBER: { |
| 3961 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3962 | break; |
| 3963 | } |
| 3964 | case XPATH_USERS: |
| 3965 | case XPATH_POINT: |
| 3966 | case XPATH_RANGE: |
| 3967 | case XPATH_LOCATIONSET: |
| 3968 | TODO |
| 3969 | ret = xmlStrdup((const xmlChar *) ""); |
| 3970 | break; |
| 3971 | } |
| 3972 | return(ret); |
| 3973 | } |
| 3974 | |
| 3975 | /** |
| 3976 | * xmlXPathConvertString: |
| 3977 | * @val: an XPath object |
| 3978 | * |
| 3979 | * Converts an existing object to its string() equivalent |
| 3980 | * |
| 3981 | * Returns the new object, the old one is freed (or the operation |
| 3982 | * is done directly on @val) |
| 3983 | */ |
| 3984 | xmlXPathObjectPtr |
| 3985 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3986 | xmlChar *res = NULL; |
| 3987 | |
| 3988 | if (val == NULL) |
| 3989 | return(xmlXPathNewCString("")); |
| 3990 | |
| 3991 | switch (val->type) { |
| 3992 | case XPATH_UNDEFINED: |
| 3993 | #ifdef DEBUG_EXPR |
| 3994 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3995 | #endif |
| 3996 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3997 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3998 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3999 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 4000 | break; |
| 4001 | case XPATH_STRING: |
| 4002 | return(val); |
| 4003 | case XPATH_BOOLEAN: |
| 4004 | res = xmlXPathCastBooleanToString(val->boolval); |
| 4005 | break; |
| 4006 | case XPATH_NUMBER: |
| 4007 | res = xmlXPathCastNumberToString(val->floatval); |
| 4008 | break; |
| 4009 | case XPATH_USERS: |
| 4010 | case XPATH_POINT: |
| 4011 | case XPATH_RANGE: |
| 4012 | case XPATH_LOCATIONSET: |
| 4013 | TODO; |
| 4014 | break; |
| 4015 | } |
| 4016 | xmlXPathFreeObject(val); |
| 4017 | if (res == NULL) |
| 4018 | return(xmlXPathNewCString("")); |
| 4019 | return(xmlXPathWrapString(res)); |
| 4020 | } |
| 4021 | |
| 4022 | /** |
| 4023 | * xmlXPathCastBooleanToNumber: |
| 4024 | * @val: a boolean |
| 4025 | * |
| 4026 | * Converts a boolean to its number value |
| 4027 | * |
| 4028 | * Returns the number value |
| 4029 | */ |
| 4030 | double |
| 4031 | xmlXPathCastBooleanToNumber(int val) { |
| 4032 | if (val) |
| 4033 | return(1.0); |
| 4034 | return(0.0); |
| 4035 | } |
| 4036 | |
| 4037 | /** |
| 4038 | * xmlXPathCastStringToNumber: |
| 4039 | * @val: a string |
| 4040 | * |
| 4041 | * Converts a string to its number value |
| 4042 | * |
| 4043 | * Returns the number value |
| 4044 | */ |
| 4045 | double |
| 4046 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 4047 | return(xmlXPathStringEvalNumber(val)); |
| 4048 | } |
| 4049 | |
| 4050 | /** |
| 4051 | * xmlXPathCastNodeToNumber: |
| 4052 | * @node: a node |
| 4053 | * |
| 4054 | * Converts a node to its number value |
| 4055 | * |
| 4056 | * Returns the number value |
| 4057 | */ |
| 4058 | double |
| 4059 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 4060 | xmlChar *strval; |
| 4061 | double ret; |
| 4062 | |
| 4063 | if (node == NULL) |
| 4064 | return(xmlXPathNAN); |
| 4065 | strval = xmlXPathCastNodeToString(node); |
| 4066 | if (strval == NULL) |
| 4067 | return(xmlXPathNAN); |
| 4068 | ret = xmlXPathCastStringToNumber(strval); |
| 4069 | xmlFree(strval); |
| 4070 | |
| 4071 | return(ret); |
| 4072 | } |
| 4073 | |
| 4074 | /** |
| 4075 | * xmlXPathCastNodeSetToNumber: |
| 4076 | * @ns: a node-set |
| 4077 | * |
| 4078 | * Converts a node-set to its number value |
| 4079 | * |
| 4080 | * Returns the number value |
| 4081 | */ |
| 4082 | double |
| 4083 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 4084 | xmlChar *str; |
| 4085 | double ret; |
| 4086 | |
| 4087 | if (ns == NULL) |
| 4088 | return(xmlXPathNAN); |
| 4089 | str = xmlXPathCastNodeSetToString(ns); |
| 4090 | ret = xmlXPathCastStringToNumber(str); |
| 4091 | xmlFree(str); |
| 4092 | return(ret); |
| 4093 | } |
| 4094 | |
| 4095 | /** |
| 4096 | * xmlXPathCastToNumber: |
| 4097 | * @val: an XPath object |
| 4098 | * |
| 4099 | * Converts an XPath object to its number value |
| 4100 | * |
| 4101 | * Returns the number value |
| 4102 | */ |
| 4103 | double |
| 4104 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 4105 | double ret = 0.0; |
| 4106 | |
| 4107 | if (val == NULL) |
| 4108 | return(xmlXPathNAN); |
| 4109 | switch (val->type) { |
| 4110 | case XPATH_UNDEFINED: |
| 4111 | #ifdef DEGUB_EXPR |
| 4112 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 4113 | #endif |
| 4114 | ret = xmlXPathNAN; |
| 4115 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4116 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4117 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4118 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 4119 | break; |
| 4120 | case XPATH_STRING: |
| 4121 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 4122 | break; |
| 4123 | case XPATH_NUMBER: |
| 4124 | ret = val->floatval; |
| 4125 | break; |
| 4126 | case XPATH_BOOLEAN: |
| 4127 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 4128 | break; |
| 4129 | case XPATH_USERS: |
| 4130 | case XPATH_POINT: |
| 4131 | case XPATH_RANGE: |
| 4132 | case XPATH_LOCATIONSET: |
| 4133 | TODO; |
| 4134 | ret = xmlXPathNAN; |
| 4135 | break; |
| 4136 | } |
| 4137 | return(ret); |
| 4138 | } |
| 4139 | |
| 4140 | /** |
| 4141 | * xmlXPathConvertNumber: |
| 4142 | * @val: an XPath object |
| 4143 | * |
| 4144 | * Converts an existing object to its number() equivalent |
| 4145 | * |
| 4146 | * Returns the new object, the old one is freed (or the operation |
| 4147 | * is done directly on @val) |
| 4148 | */ |
| 4149 | xmlXPathObjectPtr |
| 4150 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 4151 | xmlXPathObjectPtr ret; |
| 4152 | |
| 4153 | if (val == NULL) |
| 4154 | return(xmlXPathNewFloat(0.0)); |
| 4155 | if (val->type == XPATH_NUMBER) |
| 4156 | return(val); |
| 4157 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 4158 | xmlXPathFreeObject(val); |
| 4159 | return(ret); |
| 4160 | } |
| 4161 | |
| 4162 | /** |
| 4163 | * xmlXPathCastNumberToBoolean: |
| 4164 | * @val: a number |
| 4165 | * |
| 4166 | * Converts a number to its boolean value |
| 4167 | * |
| 4168 | * Returns the boolean value |
| 4169 | */ |
| 4170 | int |
| 4171 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4172 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4173 | return(0); |
| 4174 | return(1); |
| 4175 | } |
| 4176 | |
| 4177 | /** |
| 4178 | * xmlXPathCastStringToBoolean: |
| 4179 | * @val: a string |
| 4180 | * |
| 4181 | * Converts a string to its boolean value |
| 4182 | * |
| 4183 | * Returns the boolean value |
| 4184 | */ |
| 4185 | int |
| 4186 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 4187 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 4188 | return(0); |
| 4189 | return(1); |
| 4190 | } |
| 4191 | |
| 4192 | /** |
| 4193 | * xmlXPathCastNodeSetToBoolean: |
| 4194 | * @ns: a node-set |
| 4195 | * |
| 4196 | * Converts a node-set to its boolean value |
| 4197 | * |
| 4198 | * Returns the boolean value |
| 4199 | */ |
| 4200 | int |
| 4201 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 4202 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 4203 | return(0); |
| 4204 | return(1); |
| 4205 | } |
| 4206 | |
| 4207 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 4208 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4209 | * @val: an XPath object |
| 4210 | * |
| 4211 | * Converts an XPath object to its boolean value |
| 4212 | * |
| 4213 | * Returns the boolean value |
| 4214 | */ |
| 4215 | int |
| 4216 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 4217 | int ret = 0; |
| 4218 | |
| 4219 | if (val == NULL) |
| 4220 | return(0); |
| 4221 | switch (val->type) { |
| 4222 | case XPATH_UNDEFINED: |
| 4223 | #ifdef DEBUG_EXPR |
| 4224 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 4225 | #endif |
| 4226 | ret = 0; |
| 4227 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4228 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4229 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4230 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 4231 | break; |
| 4232 | case XPATH_STRING: |
| 4233 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 4234 | break; |
| 4235 | case XPATH_NUMBER: |
| 4236 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 4237 | break; |
| 4238 | case XPATH_BOOLEAN: |
| 4239 | ret = val->boolval; |
| 4240 | break; |
| 4241 | case XPATH_USERS: |
| 4242 | case XPATH_POINT: |
| 4243 | case XPATH_RANGE: |
| 4244 | case XPATH_LOCATIONSET: |
| 4245 | TODO; |
| 4246 | ret = 0; |
| 4247 | break; |
| 4248 | } |
| 4249 | return(ret); |
| 4250 | } |
| 4251 | |
| 4252 | |
| 4253 | /** |
| 4254 | * xmlXPathConvertBoolean: |
| 4255 | * @val: an XPath object |
| 4256 | * |
| 4257 | * Converts an existing object to its boolean() equivalent |
| 4258 | * |
| 4259 | * Returns the new object, the old one is freed (or the operation |
| 4260 | * is done directly on @val) |
| 4261 | */ |
| 4262 | xmlXPathObjectPtr |
| 4263 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 4264 | xmlXPathObjectPtr ret; |
| 4265 | |
| 4266 | if (val == NULL) |
| 4267 | return(xmlXPathNewBoolean(0)); |
| 4268 | if (val->type == XPATH_BOOLEAN) |
| 4269 | return(val); |
| 4270 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 4271 | xmlXPathFreeObject(val); |
| 4272 | return(ret); |
| 4273 | } |
| 4274 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | /************************************************************************ |
| 4276 | * * |
| 4277 | * Routines to handle XPath contexts * |
| 4278 | * * |
| 4279 | ************************************************************************/ |
| 4280 | |
| 4281 | /** |
| 4282 | * xmlXPathNewContext: |
| 4283 | * @doc: the XML document |
| 4284 | * |
| 4285 | * Create a new xmlXPathContext |
| 4286 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 4287 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4288 | */ |
| 4289 | xmlXPathContextPtr |
| 4290 | xmlXPathNewContext(xmlDocPtr doc) { |
| 4291 | xmlXPathContextPtr ret; |
| 4292 | |
| 4293 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 4294 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4295 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4296 | return(NULL); |
| 4297 | } |
| 4298 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 4299 | ret->doc = doc; |
| 4300 | ret->node = NULL; |
| 4301 | |
| 4302 | ret->varHash = NULL; |
| 4303 | |
| 4304 | ret->nb_types = 0; |
| 4305 | ret->max_types = 0; |
| 4306 | ret->types = NULL; |
| 4307 | |
| 4308 | ret->funcHash = xmlHashCreate(0); |
| 4309 | |
| 4310 | ret->nb_axis = 0; |
| 4311 | ret->max_axis = 0; |
| 4312 | ret->axis = NULL; |
| 4313 | |
| 4314 | ret->nsHash = NULL; |
| 4315 | ret->user = NULL; |
| 4316 | |
| 4317 | ret->contextSize = -1; |
| 4318 | ret->proximityPosition = -1; |
| 4319 | |
| 4320 | xmlXPathRegisterAllFunctions(ret); |
| 4321 | |
| 4322 | return(ret); |
| 4323 | } |
| 4324 | |
| 4325 | /** |
| 4326 | * xmlXPathFreeContext: |
| 4327 | * @ctxt: the context to free |
| 4328 | * |
| 4329 | * Free up an xmlXPathContext |
| 4330 | */ |
| 4331 | void |
| 4332 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 4333 | if (ctxt == NULL) return; |
| 4334 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4335 | xmlXPathRegisteredNsCleanup(ctxt); |
| 4336 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 4337 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 4338 | xmlResetError(&ctxt->lastError); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | xmlFree(ctxt); |
| 4340 | } |
| 4341 | |
| 4342 | /************************************************************************ |
| 4343 | * * |
| 4344 | * Routines to handle XPath parser contexts * |
| 4345 | * * |
| 4346 | ************************************************************************/ |
| 4347 | |
| 4348 | #define CHECK_CTXT(ctxt) \ |
| 4349 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 4350 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 4351 | NULL, NULL, XML_FROM_XPATH, \ |
| 4352 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 4353 | __FILE__, __LINE__, \ |
| 4354 | NULL, NULL, NULL, 0, 0, \ |
| 4355 | "NULL context pointer\n"); \ |
| 4356 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4357 | } \ |
| 4358 | |
| 4359 | |
| 4360 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 4361 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
| 4362 | (ctxt->doc->children == NULL)) { \ |
| 4363 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4364 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 4365 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4366 | |
| 4367 | |
| 4368 | /** |
| 4369 | * xmlXPathNewParserContext: |
| 4370 | * @str: the XPath expression |
| 4371 | * @ctxt: the XPath context |
| 4372 | * |
| 4373 | * Create a new xmlXPathParserContext |
| 4374 | * |
| 4375 | * Returns the xmlXPathParserContext just allocated. |
| 4376 | */ |
| 4377 | xmlXPathParserContextPtr |
| 4378 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 4379 | xmlXPathParserContextPtr ret; |
| 4380 | |
| 4381 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 4382 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4383 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4384 | return(NULL); |
| 4385 | } |
| 4386 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4387 | ret->cur = ret->base = str; |
| 4388 | ret->context = ctxt; |
| 4389 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4390 | ret->comp = xmlXPathNewCompExpr(); |
| 4391 | if (ret->comp == NULL) { |
| 4392 | xmlFree(ret->valueTab); |
| 4393 | xmlFree(ret); |
| 4394 | return(NULL); |
| 4395 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 4396 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 4397 | ret->comp->dict = ctxt->dict; |
| 4398 | xmlDictReference(ret->comp->dict); |
| 4399 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4400 | |
| 4401 | return(ret); |
| 4402 | } |
| 4403 | |
| 4404 | /** |
| 4405 | * xmlXPathCompParserContext: |
| 4406 | * @comp: the XPath compiled expression |
| 4407 | * @ctxt: the XPath context |
| 4408 | * |
| 4409 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 4410 | * |
| 4411 | * Returns the xmlXPathParserContext just allocated. |
| 4412 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4413 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4414 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 4415 | xmlXPathParserContextPtr ret; |
| 4416 | |
| 4417 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 4418 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4419 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4420 | return(NULL); |
| 4421 | } |
| 4422 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4423 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4424 | /* Allocate the value stack */ |
| 4425 | ret->valueTab = (xmlXPathObjectPtr *) |
| 4426 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4427 | if (ret->valueTab == NULL) { |
| 4428 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4429 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4430 | return(NULL); |
| 4431 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4432 | ret->valueNr = 0; |
| 4433 | ret->valueMax = 10; |
| 4434 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4435 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 4436 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4437 | ret->comp = comp; |
| 4438 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4439 | return(ret); |
| 4440 | } |
| 4441 | |
| 4442 | /** |
| 4443 | * xmlXPathFreeParserContext: |
| 4444 | * @ctxt: the context to free |
| 4445 | * |
| 4446 | * Free up an xmlXPathParserContext |
| 4447 | */ |
| 4448 | void |
| 4449 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 4450 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4451 | xmlFree(ctxt->valueTab); |
| 4452 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4453 | if (ctxt->comp != NULL) { |
| 4454 | #ifdef XPATH_STREAMING |
| 4455 | if (ctxt->comp->stream != NULL) { |
| 4456 | xmlFreePatternList(ctxt->comp->stream); |
| 4457 | ctxt->comp->stream = NULL; |
| 4458 | } |
| 4459 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4460 | xmlXPathFreeCompExpr(ctxt->comp); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 4461 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4462 | xmlFree(ctxt); |
| 4463 | } |
| 4464 | |
| 4465 | /************************************************************************ |
| 4466 | * * |
| 4467 | * The implicit core function library * |
| 4468 | * * |
| 4469 | ************************************************************************/ |
| 4470 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4471 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4472 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4473 | * @node: a node pointer |
| 4474 | * |
| 4475 | * Function computing the beginning of the string value of the node, |
| 4476 | * used to speed up comparisons |
| 4477 | * |
| 4478 | * Returns an int usable as a hash |
| 4479 | */ |
| 4480 | static unsigned int |
| 4481 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 4482 | int len = 2; |
| 4483 | const xmlChar * string = NULL; |
| 4484 | xmlNodePtr tmp = NULL; |
| 4485 | unsigned int ret = 0; |
| 4486 | |
| 4487 | if (node == NULL) |
| 4488 | return(0); |
| 4489 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4490 | if (node->type == XML_DOCUMENT_NODE) { |
| 4491 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 4492 | if (tmp == NULL) |
| 4493 | node = node->children; |
| 4494 | else |
| 4495 | node = tmp; |
| 4496 | |
| 4497 | if (node == NULL) |
| 4498 | return(0); |
| 4499 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4500 | |
| 4501 | switch (node->type) { |
| 4502 | case XML_COMMENT_NODE: |
| 4503 | case XML_PI_NODE: |
| 4504 | case XML_CDATA_SECTION_NODE: |
| 4505 | case XML_TEXT_NODE: |
| 4506 | string = node->content; |
| 4507 | if (string == NULL) |
| 4508 | return(0); |
| 4509 | if (string[0] == 0) |
| 4510 | return(0); |
| 4511 | return(((unsigned int) string[0]) + |
| 4512 | (((unsigned int) string[1]) << 8)); |
| 4513 | case XML_NAMESPACE_DECL: |
| 4514 | string = ((xmlNsPtr)node)->href; |
| 4515 | if (string == NULL) |
| 4516 | return(0); |
| 4517 | if (string[0] == 0) |
| 4518 | return(0); |
| 4519 | return(((unsigned int) string[0]) + |
| 4520 | (((unsigned int) string[1]) << 8)); |
| 4521 | case XML_ATTRIBUTE_NODE: |
| 4522 | tmp = ((xmlAttrPtr) node)->children; |
| 4523 | break; |
| 4524 | case XML_ELEMENT_NODE: |
| 4525 | tmp = node->children; |
| 4526 | break; |
| 4527 | default: |
| 4528 | return(0); |
| 4529 | } |
| 4530 | while (tmp != NULL) { |
| 4531 | switch (tmp->type) { |
| 4532 | case XML_COMMENT_NODE: |
| 4533 | case XML_PI_NODE: |
| 4534 | case XML_CDATA_SECTION_NODE: |
| 4535 | case XML_TEXT_NODE: |
| 4536 | string = tmp->content; |
| 4537 | break; |
| 4538 | case XML_NAMESPACE_DECL: |
| 4539 | string = ((xmlNsPtr)tmp)->href; |
| 4540 | break; |
| 4541 | default: |
| 4542 | break; |
| 4543 | } |
| 4544 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4545 | if (len == 1) { |
| 4546 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4547 | } |
| 4548 | if (string[1] == 0) { |
| 4549 | len = 1; |
| 4550 | ret = (unsigned int) string[0]; |
| 4551 | } else { |
| 4552 | return(((unsigned int) string[0]) + |
| 4553 | (((unsigned int) string[1]) << 8)); |
| 4554 | } |
| 4555 | } |
| 4556 | /* |
| 4557 | * Skip to next node |
| 4558 | */ |
| 4559 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4560 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4561 | tmp = tmp->children; |
| 4562 | continue; |
| 4563 | } |
| 4564 | } |
| 4565 | if (tmp == node) |
| 4566 | break; |
| 4567 | |
| 4568 | if (tmp->next != NULL) { |
| 4569 | tmp = tmp->next; |
| 4570 | continue; |
| 4571 | } |
| 4572 | |
| 4573 | do { |
| 4574 | tmp = tmp->parent; |
| 4575 | if (tmp == NULL) |
| 4576 | break; |
| 4577 | if (tmp == node) { |
| 4578 | tmp = NULL; |
| 4579 | break; |
| 4580 | } |
| 4581 | if (tmp->next != NULL) { |
| 4582 | tmp = tmp->next; |
| 4583 | break; |
| 4584 | } |
| 4585 | } while (tmp != NULL); |
| 4586 | } |
| 4587 | return(ret); |
| 4588 | } |
| 4589 | |
| 4590 | /** |
| 4591 | * xmlXPathStringHash: |
| 4592 | * @string: a string |
| 4593 | * |
| 4594 | * Function computing the beginning of the string value of the node, |
| 4595 | * used to speed up comparisons |
| 4596 | * |
| 4597 | * Returns an int usable as a hash |
| 4598 | */ |
| 4599 | static unsigned int |
| 4600 | xmlXPathStringHash(const xmlChar * string) { |
| 4601 | if (string == NULL) |
| 4602 | return((unsigned int) 0); |
| 4603 | if (string[0] == 0) |
| 4604 | return(0); |
| 4605 | return(((unsigned int) string[0]) + |
| 4606 | (((unsigned int) string[1]) << 8)); |
| 4607 | } |
| 4608 | |
| 4609 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4610 | * xmlXPathCompareNodeSetFloat: |
| 4611 | * @ctxt: the XPath Parser context |
| 4612 | * @inf: less than (1) or greater than (0) |
| 4613 | * @strict: is the comparison strict |
| 4614 | * @arg: the node set |
| 4615 | * @f: the value |
| 4616 | * |
| 4617 | * Implement the compare operation between a nodeset and a number |
| 4618 | * @ns < @val (1, 1, ... |
| 4619 | * @ns <= @val (1, 0, ... |
| 4620 | * @ns > @val (0, 1, ... |
| 4621 | * @ns >= @val (0, 0, ... |
| 4622 | * |
| 4623 | * If one object to be compared is a node-set and the other is a number, |
| 4624 | * then the comparison will be true if and only if there is a node in the |
| 4625 | * node-set such that the result of performing the comparison on the number |
| 4626 | * to be compared and on the result of converting the string-value of that |
| 4627 | * node to a number using the number function is true. |
| 4628 | * |
| 4629 | * Returns 0 or 1 depending on the results of the test. |
| 4630 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4631 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4632 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4633 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4634 | int i, ret = 0; |
| 4635 | xmlNodeSetPtr ns; |
| 4636 | xmlChar *str2; |
| 4637 | |
| 4638 | if ((f == NULL) || (arg == NULL) || |
| 4639 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4640 | xmlXPathFreeObject(arg); |
| 4641 | xmlXPathFreeObject(f); |
| 4642 | return(0); |
| 4643 | } |
| 4644 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4645 | if (ns != NULL) { |
| 4646 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4647 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4648 | if (str2 != NULL) { |
| 4649 | valuePush(ctxt, |
| 4650 | xmlXPathNewString(str2)); |
| 4651 | xmlFree(str2); |
| 4652 | xmlXPathNumberFunction(ctxt, 1); |
| 4653 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4654 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4655 | if (ret) |
| 4656 | break; |
| 4657 | } |
| 4658 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4659 | } |
| 4660 | xmlXPathFreeObject(arg); |
| 4661 | xmlXPathFreeObject(f); |
| 4662 | return(ret); |
| 4663 | } |
| 4664 | |
| 4665 | /** |
| 4666 | * xmlXPathCompareNodeSetString: |
| 4667 | * @ctxt: the XPath Parser context |
| 4668 | * @inf: less than (1) or greater than (0) |
| 4669 | * @strict: is the comparison strict |
| 4670 | * @arg: the node set |
| 4671 | * @s: the value |
| 4672 | * |
| 4673 | * Implement the compare operation between a nodeset and a string |
| 4674 | * @ns < @val (1, 1, ... |
| 4675 | * @ns <= @val (1, 0, ... |
| 4676 | * @ns > @val (0, 1, ... |
| 4677 | * @ns >= @val (0, 0, ... |
| 4678 | * |
| 4679 | * If one object to be compared is a node-set and the other is a string, |
| 4680 | * then the comparison will be true if and only if there is a node in |
| 4681 | * the node-set such that the result of performing the comparison on the |
| 4682 | * string-value of the node and the other string is true. |
| 4683 | * |
| 4684 | * Returns 0 or 1 depending on the results of the test. |
| 4685 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4686 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4687 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4688 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4689 | int i, ret = 0; |
| 4690 | xmlNodeSetPtr ns; |
| 4691 | xmlChar *str2; |
| 4692 | |
| 4693 | if ((s == NULL) || (arg == NULL) || |
| 4694 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4695 | xmlXPathFreeObject(arg); |
| 4696 | xmlXPathFreeObject(s); |
| 4697 | return(0); |
| 4698 | } |
| 4699 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4700 | if (ns != NULL) { |
| 4701 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4702 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4703 | if (str2 != NULL) { |
| 4704 | valuePush(ctxt, |
| 4705 | xmlXPathNewString(str2)); |
| 4706 | xmlFree(str2); |
| 4707 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4708 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4709 | if (ret) |
| 4710 | break; |
| 4711 | } |
| 4712 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4713 | } |
| 4714 | xmlXPathFreeObject(arg); |
| 4715 | xmlXPathFreeObject(s); |
| 4716 | return(ret); |
| 4717 | } |
| 4718 | |
| 4719 | /** |
| 4720 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4721 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4722 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4723 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | * @arg2: the second node set object |
| 4725 | * |
| 4726 | * Implement the compare operation on nodesets: |
| 4727 | * |
| 4728 | * If both objects to be compared are node-sets, then the comparison |
| 4729 | * will be true if and only if there is a node in the first node-set |
| 4730 | * and a node in the second node-set such that the result of performing |
| 4731 | * the comparison on the string-values of the two nodes is true. |
| 4732 | * .... |
| 4733 | * When neither object to be compared is a node-set and the operator |
| 4734 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4735 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4736 | * .... |
| 4737 | * The number function converts its argument to a number as follows: |
| 4738 | * - a string that consists of optional whitespace followed by an |
| 4739 | * optional minus sign followed by a Number followed by whitespace |
| 4740 | * is converted to the IEEE 754 number that is nearest (according |
| 4741 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4742 | * represented by the string; any other string is converted to NaN |
| 4743 | * |
| 4744 | * Conclusion all nodes need to be converted first to their string value |
| 4745 | * and then the comparison must be done when possible |
| 4746 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4747 | static int |
| 4748 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4749 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4750 | int i, j, init = 0; |
| 4751 | double val1; |
| 4752 | double *values2; |
| 4753 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4754 | xmlNodeSetPtr ns1; |
| 4755 | xmlNodeSetPtr ns2; |
| 4756 | |
| 4757 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4758 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4759 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4760 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4761 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4763 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4764 | xmlXPathFreeObject(arg1); |
| 4765 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4766 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4767 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4768 | |
| 4769 | ns1 = arg1->nodesetval; |
| 4770 | ns2 = arg2->nodesetval; |
| 4771 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4772 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4773 | xmlXPathFreeObject(arg1); |
| 4774 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4775 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4776 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4777 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4778 | xmlXPathFreeObject(arg1); |
| 4779 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4780 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4781 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4782 | |
| 4783 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4784 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4785 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4786 | xmlXPathFreeObject(arg1); |
| 4787 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | return(0); |
| 4789 | } |
| 4790 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4791 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4792 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4793 | continue; |
| 4794 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4795 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4796 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4797 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4798 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4799 | continue; |
| 4800 | if (inf && strict) |
| 4801 | ret = (val1 < values2[j]); |
| 4802 | else if (inf && !strict) |
| 4803 | ret = (val1 <= values2[j]); |
| 4804 | else if (!inf && strict) |
| 4805 | ret = (val1 > values2[j]); |
| 4806 | else if (!inf && !strict) |
| 4807 | ret = (val1 >= values2[j]); |
| 4808 | if (ret) |
| 4809 | break; |
| 4810 | } |
| 4811 | if (ret) |
| 4812 | break; |
| 4813 | init = 1; |
| 4814 | } |
| 4815 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4816 | xmlXPathFreeObject(arg1); |
| 4817 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4818 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | /** |
| 4822 | * xmlXPathCompareNodeSetValue: |
| 4823 | * @ctxt: the XPath Parser context |
| 4824 | * @inf: less than (1) or greater than (0) |
| 4825 | * @strict: is the comparison strict |
| 4826 | * @arg: the node set |
| 4827 | * @val: the value |
| 4828 | * |
| 4829 | * Implement the compare operation between a nodeset and a value |
| 4830 | * @ns < @val (1, 1, ... |
| 4831 | * @ns <= @val (1, 0, ... |
| 4832 | * @ns > @val (0, 1, ... |
| 4833 | * @ns >= @val (0, 0, ... |
| 4834 | * |
| 4835 | * If one object to be compared is a node-set and the other is a boolean, |
| 4836 | * then the comparison will be true if and only if the result of performing |
| 4837 | * the comparison on the boolean and on the result of converting |
| 4838 | * the node-set to a boolean using the boolean function is true. |
| 4839 | * |
| 4840 | * Returns 0 or 1 depending on the results of the test. |
| 4841 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4842 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4843 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4844 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4845 | if ((val == NULL) || (arg == NULL) || |
| 4846 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4847 | return(0); |
| 4848 | |
| 4849 | switch(val->type) { |
| 4850 | case XPATH_NUMBER: |
| 4851 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4852 | case XPATH_NODESET: |
| 4853 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4854 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4855 | case XPATH_STRING: |
| 4856 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4857 | case XPATH_BOOLEAN: |
| 4858 | valuePush(ctxt, arg); |
| 4859 | xmlXPathBooleanFunction(ctxt, 1); |
| 4860 | valuePush(ctxt, val); |
| 4861 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4862 | default: |
| 4863 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4864 | } |
| 4865 | return(0); |
| 4866 | } |
| 4867 | |
| 4868 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4869 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4870 | * @arg: the nodeset object argument |
| 4871 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4872 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4873 | * |
| 4874 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4875 | * If one object to be compared is a node-set and the other is a string, |
| 4876 | * then the comparison will be true if and only if there is a node in |
| 4877 | * the node-set such that the result of performing the comparison on the |
| 4878 | * string-value of the node and the other string is true. |
| 4879 | * |
| 4880 | * Returns 0 or 1 depending on the results of the test. |
| 4881 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4882 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4883 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4884 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4885 | int i; |
| 4886 | xmlNodeSetPtr ns; |
| 4887 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4888 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4889 | |
| 4890 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4891 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4892 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4893 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4894 | /* |
| 4895 | * A NULL nodeset compared with a string is always false |
| 4896 | * (since there is no node equal, and no node not equal) |
| 4897 | */ |
| 4898 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4899 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4900 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4901 | for (i = 0; i < ns->nodeNr; i++) { |
| 4902 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4903 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4904 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4905 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4906 | if (neq) |
| 4907 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4908 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4909 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4910 | if (neq) |
| 4911 | continue; |
| 4912 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4913 | } else if (neq) { |
| 4914 | if (str2 != NULL) |
| 4915 | xmlFree(str2); |
| 4916 | return (1); |
| 4917 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4918 | if (str2 != NULL) |
| 4919 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4920 | } else if (neq) |
| 4921 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4922 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4923 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4927 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4928 | * @arg: the nodeset object argument |
| 4929 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4930 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4931 | * |
| 4932 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4933 | * If one object to be compared is a node-set and the other is a number, |
| 4934 | * then the comparison will be true if and only if there is a node in |
| 4935 | * the node-set such that the result of performing the comparison on the |
| 4936 | * number to be compared and on the result of converting the string-value |
| 4937 | * of that node to a number using the number function is true. |
| 4938 | * |
| 4939 | * Returns 0 or 1 depending on the results of the test. |
| 4940 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4941 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4942 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4943 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4944 | int i, ret=0; |
| 4945 | xmlNodeSetPtr ns; |
| 4946 | xmlChar *str2; |
| 4947 | xmlXPathObjectPtr val; |
| 4948 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4949 | |
| 4950 | if ((arg == NULL) || |
| 4951 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4952 | return(0); |
| 4953 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4954 | ns = arg->nodesetval; |
| 4955 | if (ns != NULL) { |
| 4956 | for (i=0;i<ns->nodeNr;i++) { |
| 4957 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4958 | if (str2 != NULL) { |
| 4959 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4960 | xmlFree(str2); |
| 4961 | xmlXPathNumberFunction(ctxt, 1); |
| 4962 | val = valuePop(ctxt); |
| 4963 | v = val->floatval; |
| 4964 | xmlXPathFreeObject(val); |
| 4965 | if (!xmlXPathIsNaN(v)) { |
| 4966 | if ((!neq) && (v==f)) { |
| 4967 | ret = 1; |
| 4968 | break; |
| 4969 | } else if ((neq) && (v!=f)) { |
| 4970 | ret = 1; |
| 4971 | break; |
| 4972 | } |
William M. Brack | 32f0f71 | 2005-07-14 07:00:33 +0000 | [diff] [blame] | 4973 | } else { /* NaN is unequal to any value */ |
| 4974 | if (neq) |
| 4975 | ret = 1; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4976 | } |
| 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | |
| 4985 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4986 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4987 | * @arg1: first nodeset object argument |
| 4988 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4989 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4990 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4991 | * Implement the equal / not equal operation on XPath nodesets: |
| 4992 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4993 | * If both objects to be compared are node-sets, then the comparison |
| 4994 | * will be true if and only if there is a node in the first node-set and |
| 4995 | * a node in the second node-set such that the result of performing the |
| 4996 | * comparison on the string-values of the two nodes is true. |
| 4997 | * |
| 4998 | * (needless to say, this is a costly operation) |
| 4999 | * |
| 5000 | * Returns 0 or 1 depending on the results of the test. |
| 5001 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5002 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5003 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5004 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5005 | unsigned int *hashs1; |
| 5006 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5007 | xmlChar **values1; |
| 5008 | xmlChar **values2; |
| 5009 | int ret = 0; |
| 5010 | xmlNodeSetPtr ns1; |
| 5011 | xmlNodeSetPtr ns2; |
| 5012 | |
| 5013 | if ((arg1 == NULL) || |
| 5014 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 5015 | return(0); |
| 5016 | if ((arg2 == NULL) || |
| 5017 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 5018 | return(0); |
| 5019 | |
| 5020 | ns1 = arg1->nodesetval; |
| 5021 | ns2 = arg2->nodesetval; |
| 5022 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5023 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5024 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 5025 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5026 | return(0); |
| 5027 | |
| 5028 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5029 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5030 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5031 | if (neq == 0) |
| 5032 | for (i = 0;i < ns1->nodeNr;i++) |
| 5033 | for (j = 0;j < ns2->nodeNr;j++) |
| 5034 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 5035 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5036 | |
| 5037 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5038 | if (values1 == NULL) { |
| 5039 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5040 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5041 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5042 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 5043 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5044 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5045 | xmlFree(values1); |
| 5046 | return(0); |
| 5047 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5048 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 5049 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 5050 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5051 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5052 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | xmlFree(values1); |
| 5054 | return(0); |
| 5055 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5056 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 5057 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5058 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5059 | xmlFree(hashs1); |
| 5060 | xmlFree(values1); |
| 5061 | xmlFree(values2); |
| 5062 | return(0); |
| 5063 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5064 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 5065 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5066 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5067 | for (j = 0;j < ns2->nodeNr;j++) { |
| 5068 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5069 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5070 | if (hashs1[i] != hashs2[j]) { |
| 5071 | if (neq) { |
| 5072 | ret = 1; |
| 5073 | break; |
| 5074 | } |
| 5075 | } |
| 5076 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5077 | if (values1[i] == NULL) |
| 5078 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 5079 | if (values2[j] == NULL) |
| 5080 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5081 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5082 | if (ret) |
| 5083 | break; |
| 5084 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5085 | } |
| 5086 | if (ret) |
| 5087 | break; |
| 5088 | } |
| 5089 | for (i = 0;i < ns1->nodeNr;i++) |
| 5090 | if (values1[i] != NULL) |
| 5091 | xmlFree(values1[i]); |
| 5092 | for (j = 0;j < ns2->nodeNr;j++) |
| 5093 | if (values2[j] != NULL) |
| 5094 | xmlFree(values2[j]); |
| 5095 | xmlFree(values1); |
| 5096 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5097 | xmlFree(hashs1); |
| 5098 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5099 | return(ret); |
| 5100 | } |
| 5101 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5102 | static int |
| 5103 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 5104 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5106 | /* |
| 5107 | *At this point we are assured neither arg1 nor arg2 |
| 5108 | *is a nodeset, so we can just pick the appropriate routine. |
| 5109 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5110 | switch (arg1->type) { |
| 5111 | case XPATH_UNDEFINED: |
| 5112 | #ifdef DEBUG_EXPR |
| 5113 | xmlGenericError(xmlGenericErrorContext, |
| 5114 | "Equal: undefined\n"); |
| 5115 | #endif |
| 5116 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5117 | case XPATH_BOOLEAN: |
| 5118 | switch (arg2->type) { |
| 5119 | case XPATH_UNDEFINED: |
| 5120 | #ifdef DEBUG_EXPR |
| 5121 | xmlGenericError(xmlGenericErrorContext, |
| 5122 | "Equal: undefined\n"); |
| 5123 | #endif |
| 5124 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5125 | case XPATH_BOOLEAN: |
| 5126 | #ifdef DEBUG_EXPR |
| 5127 | xmlGenericError(xmlGenericErrorContext, |
| 5128 | "Equal: %d boolean %d \n", |
| 5129 | arg1->boolval, arg2->boolval); |
| 5130 | #endif |
| 5131 | ret = (arg1->boolval == arg2->boolval); |
| 5132 | break; |
| 5133 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5134 | ret = (arg1->boolval == |
| 5135 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5136 | break; |
| 5137 | case XPATH_STRING: |
| 5138 | if ((arg2->stringval == NULL) || |
| 5139 | (arg2->stringval[0] == 0)) ret = 0; |
| 5140 | else |
| 5141 | ret = 1; |
| 5142 | ret = (arg1->boolval == ret); |
| 5143 | break; |
| 5144 | case XPATH_USERS: |
| 5145 | case XPATH_POINT: |
| 5146 | case XPATH_RANGE: |
| 5147 | case XPATH_LOCATIONSET: |
| 5148 | TODO |
| 5149 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5150 | case XPATH_NODESET: |
| 5151 | case XPATH_XSLT_TREE: |
| 5152 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5153 | } |
| 5154 | break; |
| 5155 | case XPATH_NUMBER: |
| 5156 | switch (arg2->type) { |
| 5157 | case XPATH_UNDEFINED: |
| 5158 | #ifdef DEBUG_EXPR |
| 5159 | xmlGenericError(xmlGenericErrorContext, |
| 5160 | "Equal: undefined\n"); |
| 5161 | #endif |
| 5162 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5163 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5164 | ret = (arg2->boolval== |
| 5165 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5166 | break; |
| 5167 | case XPATH_STRING: |
| 5168 | valuePush(ctxt, arg2); |
| 5169 | xmlXPathNumberFunction(ctxt, 1); |
| 5170 | arg2 = valuePop(ctxt); |
| 5171 | /* no break on purpose */ |
| 5172 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5173 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5174 | if (xmlXPathIsNaN(arg1->floatval) || |
| 5175 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5176 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5177 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 5178 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 5179 | ret = 1; |
| 5180 | else |
| 5181 | ret = 0; |
| 5182 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 5183 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 5184 | ret = 1; |
| 5185 | else |
| 5186 | ret = 0; |
| 5187 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 5188 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 5189 | ret = 1; |
| 5190 | else |
| 5191 | ret = 0; |
| 5192 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 5193 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 5194 | ret = 1; |
| 5195 | else |
| 5196 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5197 | } else { |
| 5198 | ret = (arg1->floatval == arg2->floatval); |
| 5199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | break; |
| 5201 | case XPATH_USERS: |
| 5202 | case XPATH_POINT: |
| 5203 | case XPATH_RANGE: |
| 5204 | case XPATH_LOCATIONSET: |
| 5205 | TODO |
| 5206 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5207 | case XPATH_NODESET: |
| 5208 | case XPATH_XSLT_TREE: |
| 5209 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5210 | } |
| 5211 | break; |
| 5212 | case XPATH_STRING: |
| 5213 | switch (arg2->type) { |
| 5214 | case XPATH_UNDEFINED: |
| 5215 | #ifdef DEBUG_EXPR |
| 5216 | xmlGenericError(xmlGenericErrorContext, |
| 5217 | "Equal: undefined\n"); |
| 5218 | #endif |
| 5219 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5220 | case XPATH_BOOLEAN: |
| 5221 | if ((arg1->stringval == NULL) || |
| 5222 | (arg1->stringval[0] == 0)) ret = 0; |
| 5223 | else |
| 5224 | ret = 1; |
| 5225 | ret = (arg2->boolval == ret); |
| 5226 | break; |
| 5227 | case XPATH_STRING: |
| 5228 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 5229 | break; |
| 5230 | case XPATH_NUMBER: |
| 5231 | valuePush(ctxt, arg1); |
| 5232 | xmlXPathNumberFunction(ctxt, 1); |
| 5233 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5234 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5235 | if (xmlXPathIsNaN(arg1->floatval) || |
| 5236 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5237 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5238 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 5239 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 5240 | ret = 1; |
| 5241 | else |
| 5242 | ret = 0; |
| 5243 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 5244 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 5245 | ret = 1; |
| 5246 | else |
| 5247 | ret = 0; |
| 5248 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 5249 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 5250 | ret = 1; |
| 5251 | else |
| 5252 | ret = 0; |
| 5253 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 5254 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 5255 | ret = 1; |
| 5256 | else |
| 5257 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5258 | } else { |
| 5259 | ret = (arg1->floatval == arg2->floatval); |
| 5260 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | break; |
| 5262 | case XPATH_USERS: |
| 5263 | case XPATH_POINT: |
| 5264 | case XPATH_RANGE: |
| 5265 | case XPATH_LOCATIONSET: |
| 5266 | TODO |
| 5267 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5268 | case XPATH_NODESET: |
| 5269 | case XPATH_XSLT_TREE: |
| 5270 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5271 | } |
| 5272 | break; |
| 5273 | case XPATH_USERS: |
| 5274 | case XPATH_POINT: |
| 5275 | case XPATH_RANGE: |
| 5276 | case XPATH_LOCATIONSET: |
| 5277 | TODO |
| 5278 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5279 | case XPATH_NODESET: |
| 5280 | case XPATH_XSLT_TREE: |
| 5281 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5282 | } |
| 5283 | xmlXPathFreeObject(arg1); |
| 5284 | xmlXPathFreeObject(arg2); |
| 5285 | return(ret); |
| 5286 | } |
| 5287 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5288 | /** |
| 5289 | * xmlXPathEqualValues: |
| 5290 | * @ctxt: the XPath Parser context |
| 5291 | * |
| 5292 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 5293 | * |
| 5294 | * Returns 0 or 1 depending on the results of the test. |
| 5295 | */ |
| 5296 | int |
| 5297 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 5298 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 5299 | int ret = 0; |
| 5300 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5301 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5302 | arg2 = valuePop(ctxt); |
| 5303 | arg1 = valuePop(ctxt); |
| 5304 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5305 | if (arg1 != NULL) |
| 5306 | xmlXPathFreeObject(arg1); |
| 5307 | else |
| 5308 | xmlXPathFreeObject(arg2); |
| 5309 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5310 | } |
| 5311 | |
| 5312 | if (arg1 == arg2) { |
| 5313 | #ifdef DEBUG_EXPR |
| 5314 | xmlGenericError(xmlGenericErrorContext, |
| 5315 | "Equal: by pointer\n"); |
| 5316 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 5317 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5318 | return(1); |
| 5319 | } |
| 5320 | |
| 5321 | /* |
| 5322 | *If either argument is a nodeset, it's a 'special case' |
| 5323 | */ |
| 5324 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5325 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5326 | /* |
| 5327 | *Hack it to assure arg1 is the nodeset |
| 5328 | */ |
| 5329 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 5330 | argtmp = arg2; |
| 5331 | arg2 = arg1; |
| 5332 | arg1 = argtmp; |
| 5333 | } |
| 5334 | switch (arg2->type) { |
| 5335 | case XPATH_UNDEFINED: |
| 5336 | #ifdef DEBUG_EXPR |
| 5337 | xmlGenericError(xmlGenericErrorContext, |
| 5338 | "Equal: undefined\n"); |
| 5339 | #endif |
| 5340 | break; |
| 5341 | case XPATH_NODESET: |
| 5342 | case XPATH_XSLT_TREE: |
| 5343 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 5344 | break; |
| 5345 | case XPATH_BOOLEAN: |
| 5346 | if ((arg1->nodesetval == NULL) || |
| 5347 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 5348 | else |
| 5349 | ret = 1; |
| 5350 | ret = (ret == arg2->boolval); |
| 5351 | break; |
| 5352 | case XPATH_NUMBER: |
| 5353 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 5354 | break; |
| 5355 | case XPATH_STRING: |
| 5356 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 5357 | break; |
| 5358 | case XPATH_USERS: |
| 5359 | case XPATH_POINT: |
| 5360 | case XPATH_RANGE: |
| 5361 | case XPATH_LOCATIONSET: |
| 5362 | TODO |
| 5363 | break; |
| 5364 | } |
| 5365 | xmlXPathFreeObject(arg1); |
| 5366 | xmlXPathFreeObject(arg2); |
| 5367 | return(ret); |
| 5368 | } |
| 5369 | |
| 5370 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5371 | } |
| 5372 | |
| 5373 | /** |
| 5374 | * xmlXPathNotEqualValues: |
| 5375 | * @ctxt: the XPath Parser context |
| 5376 | * |
| 5377 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 5378 | * |
| 5379 | * Returns 0 or 1 depending on the results of the test. |
| 5380 | */ |
| 5381 | int |
| 5382 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 5383 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 5384 | int ret = 0; |
| 5385 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5386 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5387 | arg2 = valuePop(ctxt); |
| 5388 | arg1 = valuePop(ctxt); |
| 5389 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5390 | if (arg1 != NULL) |
| 5391 | xmlXPathFreeObject(arg1); |
| 5392 | else |
| 5393 | xmlXPathFreeObject(arg2); |
| 5394 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5395 | } |
| 5396 | |
| 5397 | if (arg1 == arg2) { |
| 5398 | #ifdef DEBUG_EXPR |
| 5399 | xmlGenericError(xmlGenericErrorContext, |
| 5400 | "NotEqual: by pointer\n"); |
| 5401 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 5402 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5403 | return(0); |
| 5404 | } |
| 5405 | |
| 5406 | /* |
| 5407 | *If either argument is a nodeset, it's a 'special case' |
| 5408 | */ |
| 5409 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5410 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5411 | /* |
| 5412 | *Hack it to assure arg1 is the nodeset |
| 5413 | */ |
| 5414 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 5415 | argtmp = arg2; |
| 5416 | arg2 = arg1; |
| 5417 | arg1 = argtmp; |
| 5418 | } |
| 5419 | switch (arg2->type) { |
| 5420 | case XPATH_UNDEFINED: |
| 5421 | #ifdef DEBUG_EXPR |
| 5422 | xmlGenericError(xmlGenericErrorContext, |
| 5423 | "NotEqual: undefined\n"); |
| 5424 | #endif |
| 5425 | break; |
| 5426 | case XPATH_NODESET: |
| 5427 | case XPATH_XSLT_TREE: |
| 5428 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 5429 | break; |
| 5430 | case XPATH_BOOLEAN: |
| 5431 | if ((arg1->nodesetval == NULL) || |
| 5432 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 5433 | else |
| 5434 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5435 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5436 | break; |
| 5437 | case XPATH_NUMBER: |
| 5438 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 5439 | break; |
| 5440 | case XPATH_STRING: |
| 5441 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 5442 | break; |
| 5443 | case XPATH_USERS: |
| 5444 | case XPATH_POINT: |
| 5445 | case XPATH_RANGE: |
| 5446 | case XPATH_LOCATIONSET: |
| 5447 | TODO |
| 5448 | break; |
| 5449 | } |
| 5450 | xmlXPathFreeObject(arg1); |
| 5451 | xmlXPathFreeObject(arg2); |
| 5452 | return(ret); |
| 5453 | } |
| 5454 | |
| 5455 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5456 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5457 | |
| 5458 | /** |
| 5459 | * xmlXPathCompareValues: |
| 5460 | * @ctxt: the XPath Parser context |
| 5461 | * @inf: less than (1) or greater than (0) |
| 5462 | * @strict: is the comparison strict |
| 5463 | * |
| 5464 | * Implement the compare operation on XPath objects: |
| 5465 | * @arg1 < @arg2 (1, 1, ... |
| 5466 | * @arg1 <= @arg2 (1, 0, ... |
| 5467 | * @arg1 > @arg2 (0, 1, ... |
| 5468 | * @arg1 >= @arg2 (0, 0, ... |
| 5469 | * |
| 5470 | * When neither object to be compared is a node-set and the operator is |
| 5471 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 5472 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 5473 | * comparison will be true if and only if the first number is less than the |
| 5474 | * second number. The <= comparison will be true if and only if the first |
| 5475 | * number is less than or equal to the second number. The > comparison |
| 5476 | * will be true if and only if the first number is greater than the second |
| 5477 | * number. The >= comparison will be true if and only if the first number |
| 5478 | * is greater than or equal to the second number. |
| 5479 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5480 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5481 | */ |
| 5482 | int |
| 5483 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5484 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5485 | xmlXPathObjectPtr arg1, arg2; |
| 5486 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5487 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5488 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5489 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5490 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5491 | if (arg1 != NULL) |
| 5492 | xmlXPathFreeObject(arg1); |
| 5493 | else |
| 5494 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5495 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5496 | } |
| 5497 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5498 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5499 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 5500 | /* |
| 5501 | * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments |
| 5502 | * are not freed from within this routine; they will be freed from the |
| 5503 | * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue |
| 5504 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5505 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 5506 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5507 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5509 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5510 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 5511 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5512 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5513 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 5514 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5515 | } |
| 5516 | } |
| 5517 | return(ret); |
| 5518 | } |
| 5519 | |
| 5520 | if (arg1->type != XPATH_NUMBER) { |
| 5521 | valuePush(ctxt, arg1); |
| 5522 | xmlXPathNumberFunction(ctxt, 1); |
| 5523 | arg1 = valuePop(ctxt); |
| 5524 | } |
| 5525 | if (arg1->type != XPATH_NUMBER) { |
| 5526 | xmlXPathFreeObject(arg1); |
| 5527 | xmlXPathFreeObject(arg2); |
| 5528 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5529 | } |
| 5530 | if (arg2->type != XPATH_NUMBER) { |
| 5531 | valuePush(ctxt, arg2); |
| 5532 | xmlXPathNumberFunction(ctxt, 1); |
| 5533 | arg2 = valuePop(ctxt); |
| 5534 | } |
| 5535 | if (arg2->type != XPATH_NUMBER) { |
| 5536 | xmlXPathFreeObject(arg1); |
| 5537 | xmlXPathFreeObject(arg2); |
| 5538 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5539 | } |
| 5540 | /* |
| 5541 | * Add tests for infinity and nan |
| 5542 | * => feedback on 3.4 for Inf and NaN |
| 5543 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5544 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5545 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5546 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5547 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5548 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5549 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5550 | if (inf && strict) { |
| 5551 | if ((arg1i == -1 && arg2i != -1) || |
| 5552 | (arg2i == 1 && arg1i != 1)) { |
| 5553 | ret = 1; |
| 5554 | } else if (arg1i == 0 && arg2i == 0) { |
| 5555 | ret = (arg1->floatval < arg2->floatval); |
| 5556 | } else { |
| 5557 | ret = 0; |
| 5558 | } |
| 5559 | } |
| 5560 | else if (inf && !strict) { |
| 5561 | if (arg1i == -1 || arg2i == 1) { |
| 5562 | ret = 1; |
| 5563 | } else if (arg1i == 0 && arg2i == 0) { |
| 5564 | ret = (arg1->floatval <= arg2->floatval); |
| 5565 | } else { |
| 5566 | ret = 0; |
| 5567 | } |
| 5568 | } |
| 5569 | else if (!inf && strict) { |
| 5570 | if ((arg1i == 1 && arg2i != 1) || |
| 5571 | (arg2i == -1 && arg1i != -1)) { |
| 5572 | ret = 1; |
| 5573 | } else if (arg1i == 0 && arg2i == 0) { |
| 5574 | ret = (arg1->floatval > arg2->floatval); |
| 5575 | } else { |
| 5576 | ret = 0; |
| 5577 | } |
| 5578 | } |
| 5579 | else if (!inf && !strict) { |
| 5580 | if (arg1i == 1 || arg2i == -1) { |
| 5581 | ret = 1; |
| 5582 | } else if (arg1i == 0 && arg2i == 0) { |
| 5583 | ret = (arg1->floatval >= arg2->floatval); |
| 5584 | } else { |
| 5585 | ret = 0; |
| 5586 | } |
| 5587 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5588 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5589 | xmlXPathFreeObject(arg1); |
| 5590 | xmlXPathFreeObject(arg2); |
| 5591 | return(ret); |
| 5592 | } |
| 5593 | |
| 5594 | /** |
| 5595 | * xmlXPathValueFlipSign: |
| 5596 | * @ctxt: the XPath Parser context |
| 5597 | * |
| 5598 | * Implement the unary - operation on an XPath object |
| 5599 | * The numeric operators convert their operands to numbers as if |
| 5600 | * by calling the number function. |
| 5601 | */ |
| 5602 | void |
| 5603 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5604 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5605 | CAST_TO_NUMBER; |
| 5606 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5607 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5608 | ctxt->value->floatval=xmlXPathNAN; |
| 5609 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5610 | ctxt->value->floatval=xmlXPathNINF; |
| 5611 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5612 | ctxt->value->floatval=xmlXPathPINF; |
| 5613 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5614 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5615 | ctxt->value->floatval = xmlXPathNZERO; |
| 5616 | else |
| 5617 | ctxt->value->floatval = 0; |
| 5618 | } |
| 5619 | else |
| 5620 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | /** |
| 5624 | * xmlXPathAddValues: |
| 5625 | * @ctxt: the XPath Parser context |
| 5626 | * |
| 5627 | * Implement the add operation on XPath objects: |
| 5628 | * The numeric operators convert their operands to numbers as if |
| 5629 | * by calling the number function. |
| 5630 | */ |
| 5631 | void |
| 5632 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5633 | xmlXPathObjectPtr arg; |
| 5634 | double val; |
| 5635 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5636 | arg = valuePop(ctxt); |
| 5637 | if (arg == NULL) |
| 5638 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5639 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5640 | xmlXPathFreeObject(arg); |
| 5641 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5642 | CAST_TO_NUMBER; |
| 5643 | CHECK_TYPE(XPATH_NUMBER); |
| 5644 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | /** |
| 5648 | * xmlXPathSubValues: |
| 5649 | * @ctxt: the XPath Parser context |
| 5650 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5651 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5652 | * The numeric operators convert their operands to numbers as if |
| 5653 | * by calling the number function. |
| 5654 | */ |
| 5655 | void |
| 5656 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5657 | xmlXPathObjectPtr arg; |
| 5658 | double val; |
| 5659 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5660 | arg = valuePop(ctxt); |
| 5661 | if (arg == NULL) |
| 5662 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5663 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5664 | xmlXPathFreeObject(arg); |
| 5665 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5666 | CAST_TO_NUMBER; |
| 5667 | CHECK_TYPE(XPATH_NUMBER); |
| 5668 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
| 5671 | /** |
| 5672 | * xmlXPathMultValues: |
| 5673 | * @ctxt: the XPath Parser context |
| 5674 | * |
| 5675 | * Implement the multiply operation on XPath objects: |
| 5676 | * The numeric operators convert their operands to numbers as if |
| 5677 | * by calling the number function. |
| 5678 | */ |
| 5679 | void |
| 5680 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5681 | xmlXPathObjectPtr arg; |
| 5682 | double val; |
| 5683 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5684 | arg = valuePop(ctxt); |
| 5685 | if (arg == NULL) |
| 5686 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5687 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5688 | xmlXPathFreeObject(arg); |
| 5689 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5690 | CAST_TO_NUMBER; |
| 5691 | CHECK_TYPE(XPATH_NUMBER); |
| 5692 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /** |
| 5696 | * xmlXPathDivValues: |
| 5697 | * @ctxt: the XPath Parser context |
| 5698 | * |
| 5699 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5700 | * The numeric operators convert their operands to numbers as if |
| 5701 | * by calling the number function. |
| 5702 | */ |
| 5703 | void |
| 5704 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5705 | xmlXPathObjectPtr arg; |
| 5706 | double val; |
| 5707 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5708 | arg = valuePop(ctxt); |
| 5709 | if (arg == NULL) |
| 5710 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5711 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | xmlXPathFreeObject(arg); |
| 5713 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5714 | CAST_TO_NUMBER; |
| 5715 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5716 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5717 | ctxt->value->floatval = xmlXPathNAN; |
| 5718 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5719 | if (ctxt->value->floatval == 0) |
| 5720 | ctxt->value->floatval = xmlXPathNAN; |
| 5721 | else if (ctxt->value->floatval > 0) |
| 5722 | ctxt->value->floatval = xmlXPathNINF; |
| 5723 | else if (ctxt->value->floatval < 0) |
| 5724 | ctxt->value->floatval = xmlXPathPINF; |
| 5725 | } |
| 5726 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5727 | if (ctxt->value->floatval == 0) |
| 5728 | ctxt->value->floatval = xmlXPathNAN; |
| 5729 | else if (ctxt->value->floatval > 0) |
| 5730 | ctxt->value->floatval = xmlXPathPINF; |
| 5731 | else if (ctxt->value->floatval < 0) |
| 5732 | ctxt->value->floatval = xmlXPathNINF; |
| 5733 | } else |
| 5734 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | /** |
| 5738 | * xmlXPathModValues: |
| 5739 | * @ctxt: the XPath Parser context |
| 5740 | * |
| 5741 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5742 | * The numeric operators convert their operands to numbers as if |
| 5743 | * by calling the number function. |
| 5744 | */ |
| 5745 | void |
| 5746 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5747 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5748 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5749 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5750 | arg = valuePop(ctxt); |
| 5751 | if (arg == NULL) |
| 5752 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5753 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | xmlXPathFreeObject(arg); |
| 5755 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5756 | CAST_TO_NUMBER; |
| 5757 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5758 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5759 | if (arg2 == 0) |
| 5760 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5761 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5762 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
| 5766 | /************************************************************************ |
| 5767 | * * |
| 5768 | * The traversal functions * |
| 5769 | * * |
| 5770 | ************************************************************************/ |
| 5771 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5772 | /* |
| 5773 | * A traversal function enumerates nodes along an axis. |
| 5774 | * Initially it must be called with NULL, and it indicates |
| 5775 | * termination on the axis by returning NULL. |
| 5776 | */ |
| 5777 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5778 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5779 | |
| 5780 | /** |
| 5781 | * xmlXPathNextSelf: |
| 5782 | * @ctxt: the XPath Parser context |
| 5783 | * @cur: the current node in the traversal |
| 5784 | * |
| 5785 | * Traversal function for the "self" direction |
| 5786 | * The self axis contains just the context node itself |
| 5787 | * |
| 5788 | * Returns the next element following that axis |
| 5789 | */ |
| 5790 | xmlNodePtr |
| 5791 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5792 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5793 | if (cur == NULL) |
| 5794 | return(ctxt->context->node); |
| 5795 | return(NULL); |
| 5796 | } |
| 5797 | |
| 5798 | /** |
| 5799 | * xmlXPathNextChild: |
| 5800 | * @ctxt: the XPath Parser context |
| 5801 | * @cur: the current node in the traversal |
| 5802 | * |
| 5803 | * Traversal function for the "child" direction |
| 5804 | * The child axis contains the children of the context node in document order. |
| 5805 | * |
| 5806 | * Returns the next element following that axis |
| 5807 | */ |
| 5808 | xmlNodePtr |
| 5809 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5810 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5811 | if (cur == NULL) { |
| 5812 | if (ctxt->context->node == NULL) return(NULL); |
| 5813 | switch (ctxt->context->node->type) { |
| 5814 | case XML_ELEMENT_NODE: |
| 5815 | case XML_TEXT_NODE: |
| 5816 | case XML_CDATA_SECTION_NODE: |
| 5817 | case XML_ENTITY_REF_NODE: |
| 5818 | case XML_ENTITY_NODE: |
| 5819 | case XML_PI_NODE: |
| 5820 | case XML_COMMENT_NODE: |
| 5821 | case XML_NOTATION_NODE: |
| 5822 | case XML_DTD_NODE: |
| 5823 | return(ctxt->context->node->children); |
| 5824 | case XML_DOCUMENT_NODE: |
| 5825 | case XML_DOCUMENT_TYPE_NODE: |
| 5826 | case XML_DOCUMENT_FRAG_NODE: |
| 5827 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5828 | #ifdef LIBXML_DOCB_ENABLED |
| 5829 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5830 | #endif |
| 5831 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5832 | case XML_ELEMENT_DECL: |
| 5833 | case XML_ATTRIBUTE_DECL: |
| 5834 | case XML_ENTITY_DECL: |
| 5835 | case XML_ATTRIBUTE_NODE: |
| 5836 | case XML_NAMESPACE_DECL: |
| 5837 | case XML_XINCLUDE_START: |
| 5838 | case XML_XINCLUDE_END: |
| 5839 | return(NULL); |
| 5840 | } |
| 5841 | return(NULL); |
| 5842 | } |
| 5843 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5844 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5845 | return(NULL); |
| 5846 | return(cur->next); |
| 5847 | } |
| 5848 | |
| 5849 | /** |
| 5850 | * xmlXPathNextDescendant: |
| 5851 | * @ctxt: the XPath Parser context |
| 5852 | * @cur: the current node in the traversal |
| 5853 | * |
| 5854 | * Traversal function for the "descendant" direction |
| 5855 | * the descendant axis contains the descendants of the context node in document |
| 5856 | * order; a descendant is a child or a child of a child and so on. |
| 5857 | * |
| 5858 | * Returns the next element following that axis |
| 5859 | */ |
| 5860 | xmlNodePtr |
| 5861 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5862 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | if (cur == NULL) { |
| 5864 | if (ctxt->context->node == NULL) |
| 5865 | return(NULL); |
| 5866 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5867 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5868 | return(NULL); |
| 5869 | |
| 5870 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5871 | return(ctxt->context->doc->children); |
| 5872 | return(ctxt->context->node->children); |
| 5873 | } |
| 5874 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5875 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5876 | /* |
| 5877 | * Do not descend on entities declarations |
| 5878 | */ |
| 5879 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5880 | cur = cur->children; |
| 5881 | /* |
| 5882 | * Skip DTDs |
| 5883 | */ |
| 5884 | if (cur->type != XML_DTD_NODE) |
| 5885 | return(cur); |
| 5886 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | if (cur == ctxt->context->node) return(NULL); |
| 5890 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5891 | while (cur->next != NULL) { |
| 5892 | cur = cur->next; |
| 5893 | if ((cur->type != XML_ENTITY_DECL) && |
| 5894 | (cur->type != XML_DTD_NODE)) |
| 5895 | return(cur); |
| 5896 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5897 | |
| 5898 | do { |
| 5899 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5900 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5901 | if (cur == ctxt->context->node) return(NULL); |
| 5902 | if (cur->next != NULL) { |
| 5903 | cur = cur->next; |
| 5904 | return(cur); |
| 5905 | } |
| 5906 | } while (cur != NULL); |
| 5907 | return(cur); |
| 5908 | } |
| 5909 | |
| 5910 | /** |
| 5911 | * xmlXPathNextDescendantOrSelf: |
| 5912 | * @ctxt: the XPath Parser context |
| 5913 | * @cur: the current node in the traversal |
| 5914 | * |
| 5915 | * Traversal function for the "descendant-or-self" direction |
| 5916 | * the descendant-or-self axis contains the context node and the descendants |
| 5917 | * of the context node in document order; thus the context node is the first |
| 5918 | * node on the axis, and the first child of the context node is the second node |
| 5919 | * on the axis |
| 5920 | * |
| 5921 | * Returns the next element following that axis |
| 5922 | */ |
| 5923 | xmlNodePtr |
| 5924 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5925 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5926 | if (cur == NULL) { |
| 5927 | if (ctxt->context->node == NULL) |
| 5928 | return(NULL); |
| 5929 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5930 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5931 | return(NULL); |
| 5932 | return(ctxt->context->node); |
| 5933 | } |
| 5934 | |
| 5935 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5936 | } |
| 5937 | |
| 5938 | /** |
| 5939 | * xmlXPathNextParent: |
| 5940 | * @ctxt: the XPath Parser context |
| 5941 | * @cur: the current node in the traversal |
| 5942 | * |
| 5943 | * Traversal function for the "parent" direction |
| 5944 | * The parent axis contains the parent of the context node, if there is one. |
| 5945 | * |
| 5946 | * Returns the next element following that axis |
| 5947 | */ |
| 5948 | xmlNodePtr |
| 5949 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5950 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | /* |
| 5952 | * the parent of an attribute or namespace node is the element |
| 5953 | * to which the attribute or namespace node is attached |
| 5954 | * Namespace handling !!! |
| 5955 | */ |
| 5956 | if (cur == NULL) { |
| 5957 | if (ctxt->context->node == NULL) return(NULL); |
| 5958 | switch (ctxt->context->node->type) { |
| 5959 | case XML_ELEMENT_NODE: |
| 5960 | case XML_TEXT_NODE: |
| 5961 | case XML_CDATA_SECTION_NODE: |
| 5962 | case XML_ENTITY_REF_NODE: |
| 5963 | case XML_ENTITY_NODE: |
| 5964 | case XML_PI_NODE: |
| 5965 | case XML_COMMENT_NODE: |
| 5966 | case XML_NOTATION_NODE: |
| 5967 | case XML_DTD_NODE: |
| 5968 | case XML_ELEMENT_DECL: |
| 5969 | case XML_ATTRIBUTE_DECL: |
| 5970 | case XML_XINCLUDE_START: |
| 5971 | case XML_XINCLUDE_END: |
| 5972 | case XML_ENTITY_DECL: |
| 5973 | if (ctxt->context->node->parent == NULL) |
| 5974 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5975 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5976 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5977 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5978 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5979 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5980 | return(ctxt->context->node->parent); |
| 5981 | case XML_ATTRIBUTE_NODE: { |
| 5982 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5983 | |
| 5984 | return(att->parent); |
| 5985 | } |
| 5986 | case XML_DOCUMENT_NODE: |
| 5987 | case XML_DOCUMENT_TYPE_NODE: |
| 5988 | case XML_DOCUMENT_FRAG_NODE: |
| 5989 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5990 | #ifdef LIBXML_DOCB_ENABLED |
| 5991 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5992 | #endif |
| 5993 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5994 | case XML_NAMESPACE_DECL: { |
| 5995 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5996 | |
| 5997 | if ((ns->next != NULL) && |
| 5998 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5999 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6000 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 6001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6002 | } |
| 6003 | } |
| 6004 | return(NULL); |
| 6005 | } |
| 6006 | |
| 6007 | /** |
| 6008 | * xmlXPathNextAncestor: |
| 6009 | * @ctxt: the XPath Parser context |
| 6010 | * @cur: the current node in the traversal |
| 6011 | * |
| 6012 | * Traversal function for the "ancestor" direction |
| 6013 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 6014 | * of the context node consist of the parent of context node and the parent's |
| 6015 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 6016 | * parent is the first node on the axis, and the parent's parent is the second |
| 6017 | * node on the axis |
| 6018 | * |
| 6019 | * Returns the next element following that axis |
| 6020 | */ |
| 6021 | xmlNodePtr |
| 6022 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6023 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6024 | /* |
| 6025 | * the parent of an attribute or namespace node is the element |
| 6026 | * to which the attribute or namespace node is attached |
| 6027 | * !!!!!!!!!!!!! |
| 6028 | */ |
| 6029 | if (cur == NULL) { |
| 6030 | if (ctxt->context->node == NULL) return(NULL); |
| 6031 | switch (ctxt->context->node->type) { |
| 6032 | case XML_ELEMENT_NODE: |
| 6033 | case XML_TEXT_NODE: |
| 6034 | case XML_CDATA_SECTION_NODE: |
| 6035 | case XML_ENTITY_REF_NODE: |
| 6036 | case XML_ENTITY_NODE: |
| 6037 | case XML_PI_NODE: |
| 6038 | case XML_COMMENT_NODE: |
| 6039 | case XML_DTD_NODE: |
| 6040 | case XML_ELEMENT_DECL: |
| 6041 | case XML_ATTRIBUTE_DECL: |
| 6042 | case XML_ENTITY_DECL: |
| 6043 | case XML_NOTATION_NODE: |
| 6044 | case XML_XINCLUDE_START: |
| 6045 | case XML_XINCLUDE_END: |
| 6046 | if (ctxt->context->node->parent == NULL) |
| 6047 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 6048 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6049 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 6050 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 6051 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 6052 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6053 | return(ctxt->context->node->parent); |
| 6054 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6055 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6056 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6057 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6058 | } |
| 6059 | case XML_DOCUMENT_NODE: |
| 6060 | case XML_DOCUMENT_TYPE_NODE: |
| 6061 | case XML_DOCUMENT_FRAG_NODE: |
| 6062 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 6063 | #ifdef LIBXML_DOCB_ENABLED |
| 6064 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6065 | #endif |
| 6066 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 6067 | case XML_NAMESPACE_DECL: { |
| 6068 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 6069 | |
| 6070 | if ((ns->next != NULL) && |
| 6071 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 6072 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6073 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6074 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 6075 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6076 | } |
| 6077 | return(NULL); |
| 6078 | } |
| 6079 | if (cur == ctxt->context->doc->children) |
| 6080 | return((xmlNodePtr) ctxt->context->doc); |
| 6081 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 6082 | return(NULL); |
| 6083 | switch (cur->type) { |
| 6084 | case XML_ELEMENT_NODE: |
| 6085 | case XML_TEXT_NODE: |
| 6086 | case XML_CDATA_SECTION_NODE: |
| 6087 | case XML_ENTITY_REF_NODE: |
| 6088 | case XML_ENTITY_NODE: |
| 6089 | case XML_PI_NODE: |
| 6090 | case XML_COMMENT_NODE: |
| 6091 | case XML_NOTATION_NODE: |
| 6092 | case XML_DTD_NODE: |
| 6093 | case XML_ELEMENT_DECL: |
| 6094 | case XML_ATTRIBUTE_DECL: |
| 6095 | case XML_ENTITY_DECL: |
| 6096 | case XML_XINCLUDE_START: |
| 6097 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 6098 | if (cur->parent == NULL) |
| 6099 | return(NULL); |
| 6100 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6101 | ((cur->parent->name[0] == ' ') || |
| 6102 | (xmlStrEqual(cur->parent->name, |
| 6103 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 6104 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6105 | return(cur->parent); |
| 6106 | case XML_ATTRIBUTE_NODE: { |
| 6107 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 6108 | |
| 6109 | return(att->parent); |
| 6110 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 6111 | case XML_NAMESPACE_DECL: { |
| 6112 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 6113 | |
| 6114 | if ((ns->next != NULL) && |
| 6115 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 6116 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6117 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 6118 | return(NULL); |
| 6119 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6120 | case XML_DOCUMENT_NODE: |
| 6121 | case XML_DOCUMENT_TYPE_NODE: |
| 6122 | case XML_DOCUMENT_FRAG_NODE: |
| 6123 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 6124 | #ifdef LIBXML_DOCB_ENABLED |
| 6125 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6126 | #endif |
| 6127 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6128 | } |
| 6129 | return(NULL); |
| 6130 | } |
| 6131 | |
| 6132 | /** |
| 6133 | * xmlXPathNextAncestorOrSelf: |
| 6134 | * @ctxt: the XPath Parser context |
| 6135 | * @cur: the current node in the traversal |
| 6136 | * |
| 6137 | * Traversal function for the "ancestor-or-self" direction |
| 6138 | * he ancestor-or-self axis contains the context node and ancestors of |
| 6139 | * the context node in reverse document order; thus the context node is |
| 6140 | * the first node on the axis, and the context node's parent the second; |
| 6141 | * parent here is defined the same as with the parent axis. |
| 6142 | * |
| 6143 | * Returns the next element following that axis |
| 6144 | */ |
| 6145 | xmlNodePtr |
| 6146 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6147 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6148 | if (cur == NULL) |
| 6149 | return(ctxt->context->node); |
| 6150 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 6151 | } |
| 6152 | |
| 6153 | /** |
| 6154 | * xmlXPathNextFollowingSibling: |
| 6155 | * @ctxt: the XPath Parser context |
| 6156 | * @cur: the current node in the traversal |
| 6157 | * |
| 6158 | * Traversal function for the "following-sibling" direction |
| 6159 | * The following-sibling axis contains the following siblings of the context |
| 6160 | * node in document order. |
| 6161 | * |
| 6162 | * Returns the next element following that axis |
| 6163 | */ |
| 6164 | xmlNodePtr |
| 6165 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6166 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6167 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 6168 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 6169 | return(NULL); |
| 6170 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 6171 | return(NULL); |
| 6172 | if (cur == NULL) |
| 6173 | return(ctxt->context->node->next); |
| 6174 | return(cur->next); |
| 6175 | } |
| 6176 | |
| 6177 | /** |
| 6178 | * xmlXPathNextPrecedingSibling: |
| 6179 | * @ctxt: the XPath Parser context |
| 6180 | * @cur: the current node in the traversal |
| 6181 | * |
| 6182 | * Traversal function for the "preceding-sibling" direction |
| 6183 | * The preceding-sibling axis contains the preceding siblings of the context |
| 6184 | * node in reverse document order; the first preceding sibling is first on the |
| 6185 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 6186 | * |
| 6187 | * Returns the next element following that axis |
| 6188 | */ |
| 6189 | xmlNodePtr |
| 6190 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6191 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6192 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 6193 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 6194 | return(NULL); |
| 6195 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 6196 | return(NULL); |
| 6197 | if (cur == NULL) |
| 6198 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6199 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 6200 | cur = cur->prev; |
| 6201 | if (cur == NULL) |
| 6202 | return(ctxt->context->node->prev); |
| 6203 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6204 | return(cur->prev); |
| 6205 | } |
| 6206 | |
| 6207 | /** |
| 6208 | * xmlXPathNextFollowing: |
| 6209 | * @ctxt: the XPath Parser context |
| 6210 | * @cur: the current node in the traversal |
| 6211 | * |
| 6212 | * Traversal function for the "following" direction |
| 6213 | * The following axis contains all nodes in the same document as the context |
| 6214 | * node that are after the context node in document order, excluding any |
| 6215 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 6216 | * are ordered in document order |
| 6217 | * |
| 6218 | * Returns the next element following that axis |
| 6219 | */ |
| 6220 | xmlNodePtr |
| 6221 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6222 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6223 | if (cur != NULL && cur->children != NULL) |
| 6224 | return cur->children ; |
| 6225 | if (cur == NULL) cur = ctxt->context->node; |
| 6226 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 6227 | if (cur->next != NULL) return(cur->next) ; |
| 6228 | do { |
| 6229 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6230 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 6232 | if (cur->next != NULL) return(cur->next); |
| 6233 | } while (cur != NULL); |
| 6234 | return(cur); |
| 6235 | } |
| 6236 | |
| 6237 | /* |
| 6238 | * xmlXPathIsAncestor: |
| 6239 | * @ancestor: the ancestor node |
| 6240 | * @node: the current node |
| 6241 | * |
| 6242 | * Check that @ancestor is a @node's ancestor |
| 6243 | * |
| 6244 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 6245 | */ |
| 6246 | static int |
| 6247 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 6248 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 6249 | /* nodes need to be in the same document */ |
| 6250 | if (ancestor->doc != node->doc) return(0); |
| 6251 | /* avoid searching if ancestor or node is the root node */ |
| 6252 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 6253 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 6254 | while (node->parent != NULL) { |
| 6255 | if (node->parent == ancestor) |
| 6256 | return(1); |
| 6257 | node = node->parent; |
| 6258 | } |
| 6259 | return(0); |
| 6260 | } |
| 6261 | |
| 6262 | /** |
| 6263 | * xmlXPathNextPreceding: |
| 6264 | * @ctxt: the XPath Parser context |
| 6265 | * @cur: the current node in the traversal |
| 6266 | * |
| 6267 | * Traversal function for the "preceding" direction |
| 6268 | * the preceding axis contains all nodes in the same document as the context |
| 6269 | * node that are before the context node in document order, excluding any |
| 6270 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 6271 | * ordered in reverse document order |
| 6272 | * |
| 6273 | * Returns the next element following that axis |
| 6274 | */ |
| 6275 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6276 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 6277 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6278 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6279 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6280 | cur = ctxt->context->node; |
| 6281 | if (cur == NULL) |
| 6282 | return (NULL); |
| 6283 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 6284 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6285 | do { |
| 6286 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6287 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 6288 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6292 | if (cur == NULL) |
| 6293 | return (NULL); |
| 6294 | if (cur == ctxt->context->doc->children) |
| 6295 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6296 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6297 | return (cur); |
| 6298 | } |
| 6299 | |
| 6300 | /** |
| 6301 | * xmlXPathNextPrecedingInternal: |
| 6302 | * @ctxt: the XPath Parser context |
| 6303 | * @cur: the current node in the traversal |
| 6304 | * |
| 6305 | * Traversal function for the "preceding" direction |
| 6306 | * the preceding axis contains all nodes in the same document as the context |
| 6307 | * node that are before the context node in document order, excluding any |
| 6308 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 6309 | * ordered in reverse document order |
| 6310 | * This is a faster implementation but internal only since it requires a |
| 6311 | * state kept in the parser context: ctxt->ancestor. |
| 6312 | * |
| 6313 | * Returns the next element following that axis |
| 6314 | */ |
| 6315 | static xmlNodePtr |
| 6316 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 6317 | xmlNodePtr cur) |
| 6318 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6319 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6320 | if (cur == NULL) { |
| 6321 | cur = ctxt->context->node; |
| 6322 | if (cur == NULL) |
| 6323 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 6324 | if (cur->type == XML_NAMESPACE_DECL) |
| 6325 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6326 | ctxt->ancestor = cur->parent; |
| 6327 | } |
| 6328 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 6329 | cur = cur->prev; |
| 6330 | while (cur->prev == NULL) { |
| 6331 | cur = cur->parent; |
| 6332 | if (cur == NULL) |
| 6333 | return (NULL); |
| 6334 | if (cur == ctxt->context->doc->children) |
| 6335 | return (NULL); |
| 6336 | if (cur != ctxt->ancestor) |
| 6337 | return (cur); |
| 6338 | ctxt->ancestor = cur->parent; |
| 6339 | } |
| 6340 | cur = cur->prev; |
| 6341 | while (cur->last != NULL) |
| 6342 | cur = cur->last; |
| 6343 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | /** |
| 6347 | * xmlXPathNextNamespace: |
| 6348 | * @ctxt: the XPath Parser context |
| 6349 | * @cur: the current attribute in the traversal |
| 6350 | * |
| 6351 | * Traversal function for the "namespace" direction |
| 6352 | * the namespace axis contains the namespace nodes of the context node; |
| 6353 | * the order of nodes on this axis is implementation-defined; the axis will |
| 6354 | * be empty unless the context node is an element |
| 6355 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 6356 | * We keep the XML namespace node at the end of the list. |
| 6357 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6358 | * Returns the next element following that axis |
| 6359 | */ |
| 6360 | xmlNodePtr |
| 6361 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6362 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6363 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6364 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 6365 | if (ctxt->context->tmpNsList != NULL) |
| 6366 | xmlFree(ctxt->context->tmpNsList); |
| 6367 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6368 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 6369 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6370 | if (ctxt->context->tmpNsList != NULL) { |
| 6371 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 6372 | ctxt->context->tmpNsNr++; |
| 6373 | } |
| 6374 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 6375 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 6376 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6377 | if (ctxt->context->tmpNsNr > 0) { |
| 6378 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 6379 | } else { |
| 6380 | if (ctxt->context->tmpNsList != NULL) |
| 6381 | xmlFree(ctxt->context->tmpNsList); |
| 6382 | ctxt->context->tmpNsList = NULL; |
| 6383 | return(NULL); |
| 6384 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | /** |
| 6388 | * xmlXPathNextAttribute: |
| 6389 | * @ctxt: the XPath Parser context |
| 6390 | * @cur: the current attribute in the traversal |
| 6391 | * |
| 6392 | * Traversal function for the "attribute" direction |
| 6393 | * TODO: support DTD inherited default attributes |
| 6394 | * |
| 6395 | * Returns the next element following that axis |
| 6396 | */ |
| 6397 | xmlNodePtr |
| 6398 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6399 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6400 | if (ctxt->context->node == NULL) |
| 6401 | return(NULL); |
| 6402 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 6403 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6404 | if (cur == NULL) { |
| 6405 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 6406 | return(NULL); |
| 6407 | return((xmlNodePtr)ctxt->context->node->properties); |
| 6408 | } |
| 6409 | return((xmlNodePtr)cur->next); |
| 6410 | } |
| 6411 | |
| 6412 | /************************************************************************ |
| 6413 | * * |
| 6414 | * NodeTest Functions * |
| 6415 | * * |
| 6416 | ************************************************************************/ |
| 6417 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6418 | #define IS_FUNCTION 200 |
| 6419 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6420 | |
| 6421 | /************************************************************************ |
| 6422 | * * |
| 6423 | * Implicit tree core function library * |
| 6424 | * * |
| 6425 | ************************************************************************/ |
| 6426 | |
| 6427 | /** |
| 6428 | * xmlXPathRoot: |
| 6429 | * @ctxt: the XPath Parser context |
| 6430 | * |
| 6431 | * Initialize the context to the root of the document |
| 6432 | */ |
| 6433 | void |
| 6434 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6435 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6436 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 6437 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6438 | } |
| 6439 | |
| 6440 | /************************************************************************ |
| 6441 | * * |
| 6442 | * The explicit core function library * |
| 6443 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 6444 | * * |
| 6445 | ************************************************************************/ |
| 6446 | |
| 6447 | |
| 6448 | /** |
| 6449 | * xmlXPathLastFunction: |
| 6450 | * @ctxt: the XPath Parser context |
| 6451 | * @nargs: the number of arguments |
| 6452 | * |
| 6453 | * Implement the last() XPath function |
| 6454 | * number last() |
| 6455 | * The last function returns the number of nodes in the context node list. |
| 6456 | */ |
| 6457 | void |
| 6458 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6459 | CHECK_ARITY(0); |
| 6460 | if (ctxt->context->contextSize >= 0) { |
| 6461 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 6462 | #ifdef DEBUG_EXPR |
| 6463 | xmlGenericError(xmlGenericErrorContext, |
| 6464 | "last() : %d\n", ctxt->context->contextSize); |
| 6465 | #endif |
| 6466 | } else { |
| 6467 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 6468 | } |
| 6469 | } |
| 6470 | |
| 6471 | /** |
| 6472 | * xmlXPathPositionFunction: |
| 6473 | * @ctxt: the XPath Parser context |
| 6474 | * @nargs: the number of arguments |
| 6475 | * |
| 6476 | * Implement the position() XPath function |
| 6477 | * number position() |
| 6478 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6479 | * 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] | 6480 | * will be equal to last(). |
| 6481 | */ |
| 6482 | void |
| 6483 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6484 | CHECK_ARITY(0); |
| 6485 | if (ctxt->context->proximityPosition >= 0) { |
| 6486 | valuePush(ctxt, |
| 6487 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 6488 | #ifdef DEBUG_EXPR |
| 6489 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 6490 | ctxt->context->proximityPosition); |
| 6491 | #endif |
| 6492 | } else { |
| 6493 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | /** |
| 6498 | * xmlXPathCountFunction: |
| 6499 | * @ctxt: the XPath Parser context |
| 6500 | * @nargs: the number of arguments |
| 6501 | * |
| 6502 | * Implement the count() XPath function |
| 6503 | * number count(node-set) |
| 6504 | */ |
| 6505 | void |
| 6506 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6507 | xmlXPathObjectPtr cur; |
| 6508 | |
| 6509 | CHECK_ARITY(1); |
| 6510 | if ((ctxt->value == NULL) || |
| 6511 | ((ctxt->value->type != XPATH_NODESET) && |
| 6512 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6513 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6514 | cur = valuePop(ctxt); |
| 6515 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6516 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 6517 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6518 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6519 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 6520 | } else { |
| 6521 | if ((cur->nodesetval->nodeNr != 1) || |
| 6522 | (cur->nodesetval->nodeTab == NULL)) { |
| 6523 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 6524 | } else { |
| 6525 | xmlNodePtr tmp; |
| 6526 | int i = 0; |
| 6527 | |
| 6528 | tmp = cur->nodesetval->nodeTab[0]; |
| 6529 | if (tmp != NULL) { |
| 6530 | tmp = tmp->children; |
| 6531 | while (tmp != NULL) { |
| 6532 | tmp = tmp->next; |
| 6533 | i++; |
| 6534 | } |
| 6535 | } |
| 6536 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6537 | } |
| 6538 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | xmlXPathFreeObject(cur); |
| 6540 | } |
| 6541 | |
| 6542 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6543 | * xmlXPathGetElementsByIds: |
| 6544 | * @doc: the document |
| 6545 | * @ids: a whitespace separated list of IDs |
| 6546 | * |
| 6547 | * Selects elements by their unique ID. |
| 6548 | * |
| 6549 | * Returns a node-set of selected elements. |
| 6550 | */ |
| 6551 | static xmlNodeSetPtr |
| 6552 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6553 | xmlNodeSetPtr ret; |
| 6554 | const xmlChar *cur = ids; |
| 6555 | xmlChar *ID; |
| 6556 | xmlAttrPtr attr; |
| 6557 | xmlNodePtr elem = NULL; |
| 6558 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6559 | if (ids == NULL) return(NULL); |
| 6560 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6561 | ret = xmlXPathNodeSetCreate(NULL); |
| 6562 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6563 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6564 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6565 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6566 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6567 | |
| 6568 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6569 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 6570 | /* |
| 6571 | * We used to check the fact that the value passed |
| 6572 | * was an NCName, but this generated much troubles for |
| 6573 | * me and Aleksey Sanin, people blatantly violated that |
| 6574 | * constaint, like Visa3D spec. |
| 6575 | * if (xmlValidateNCName(ID, 1) == 0) |
| 6576 | */ |
| 6577 | attr = xmlGetID(doc, ID); |
| 6578 | if (attr != NULL) { |
| 6579 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6580 | elem = attr->parent; |
| 6581 | else if (attr->type == XML_ELEMENT_NODE) |
| 6582 | elem = (xmlNodePtr) attr; |
| 6583 | else |
| 6584 | elem = NULL; |
| 6585 | if (elem != NULL) |
| 6586 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6587 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6588 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6589 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6590 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6591 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6592 | ids = cur; |
| 6593 | } |
| 6594 | return(ret); |
| 6595 | } |
| 6596 | |
| 6597 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6598 | * xmlXPathIdFunction: |
| 6599 | * @ctxt: the XPath Parser context |
| 6600 | * @nargs: the number of arguments |
| 6601 | * |
| 6602 | * Implement the id() XPath function |
| 6603 | * node-set id(object) |
| 6604 | * The id function selects elements by their unique ID |
| 6605 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6606 | * then the result is the union of the result of applying id to the |
| 6607 | * string value of each of the nodes in the argument node-set. When the |
| 6608 | * argument to id is of any other type, the argument is converted to a |
| 6609 | * string as if by a call to the string function; the string is split |
| 6610 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6611 | * of characters matching the production S); the result is a node-set |
| 6612 | * containing the elements in the same document as the context node that |
| 6613 | * have a unique ID equal to any of the tokens in the list. |
| 6614 | */ |
| 6615 | void |
| 6616 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6617 | xmlChar *tokens; |
| 6618 | xmlNodeSetPtr ret; |
| 6619 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | |
| 6621 | CHECK_ARITY(1); |
| 6622 | obj = valuePop(ctxt); |
| 6623 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6624 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6625 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6626 | int i; |
| 6627 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6628 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6629 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6630 | if (obj->nodesetval != NULL) { |
| 6631 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6632 | tokens = |
| 6633 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6634 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6635 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6636 | xmlXPathFreeNodeSet(ns); |
| 6637 | if (tokens != NULL) |
| 6638 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6639 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
| 6642 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6643 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | return; |
| 6645 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6646 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6647 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6648 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6649 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6650 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6651 | xmlXPathFreeObject(obj); |
| 6652 | return; |
| 6653 | } |
| 6654 | |
| 6655 | /** |
| 6656 | * xmlXPathLocalNameFunction: |
| 6657 | * @ctxt: the XPath Parser context |
| 6658 | * @nargs: the number of arguments |
| 6659 | * |
| 6660 | * Implement the local-name() XPath function |
| 6661 | * string local-name(node-set?) |
| 6662 | * The local-name function returns a string containing the local part |
| 6663 | * of the name of the node in the argument node-set that is first in |
| 6664 | * document order. If the node-set is empty or the first node has no |
| 6665 | * name, an empty string is returned. If the argument is omitted it |
| 6666 | * defaults to the context node. |
| 6667 | */ |
| 6668 | void |
| 6669 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6670 | xmlXPathObjectPtr cur; |
| 6671 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6672 | if (ctxt == NULL) return; |
| 6673 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6674 | if (nargs == 0) { |
| 6675 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6676 | nargs = 1; |
| 6677 | } |
| 6678 | |
| 6679 | CHECK_ARITY(1); |
| 6680 | if ((ctxt->value == NULL) || |
| 6681 | ((ctxt->value->type != XPATH_NODESET) && |
| 6682 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6683 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6684 | cur = valuePop(ctxt); |
| 6685 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6686 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6687 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6688 | } else { |
| 6689 | int i = 0; /* Should be first in document order !!!!! */ |
| 6690 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6691 | case XML_ELEMENT_NODE: |
| 6692 | case XML_ATTRIBUTE_NODE: |
| 6693 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6694 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6695 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6696 | else |
| 6697 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6698 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6699 | break; |
| 6700 | case XML_NAMESPACE_DECL: |
| 6701 | valuePush(ctxt, xmlXPathNewString( |
| 6702 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6703 | break; |
| 6704 | default: |
| 6705 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6706 | } |
| 6707 | } |
| 6708 | xmlXPathFreeObject(cur); |
| 6709 | } |
| 6710 | |
| 6711 | /** |
| 6712 | * xmlXPathNamespaceURIFunction: |
| 6713 | * @ctxt: the XPath Parser context |
| 6714 | * @nargs: the number of arguments |
| 6715 | * |
| 6716 | * Implement the namespace-uri() XPath function |
| 6717 | * string namespace-uri(node-set?) |
| 6718 | * The namespace-uri function returns a string containing the |
| 6719 | * namespace URI of the expanded name of the node in the argument |
| 6720 | * node-set that is first in document order. If the node-set is empty, |
| 6721 | * the first node has no name, or the expanded name has no namespace |
| 6722 | * URI, an empty string is returned. If the argument is omitted it |
| 6723 | * defaults to the context node. |
| 6724 | */ |
| 6725 | void |
| 6726 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6727 | xmlXPathObjectPtr cur; |
| 6728 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6729 | if (ctxt == NULL) return; |
| 6730 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6731 | if (nargs == 0) { |
| 6732 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6733 | nargs = 1; |
| 6734 | } |
| 6735 | CHECK_ARITY(1); |
| 6736 | if ((ctxt->value == NULL) || |
| 6737 | ((ctxt->value->type != XPATH_NODESET) && |
| 6738 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6739 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6740 | cur = valuePop(ctxt); |
| 6741 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6742 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6743 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6744 | } else { |
| 6745 | int i = 0; /* Should be first in document order !!!!! */ |
| 6746 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6747 | case XML_ELEMENT_NODE: |
| 6748 | case XML_ATTRIBUTE_NODE: |
| 6749 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6750 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6751 | else |
| 6752 | valuePush(ctxt, xmlXPathNewString( |
| 6753 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6754 | break; |
| 6755 | default: |
| 6756 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6757 | } |
| 6758 | } |
| 6759 | xmlXPathFreeObject(cur); |
| 6760 | } |
| 6761 | |
| 6762 | /** |
| 6763 | * xmlXPathNameFunction: |
| 6764 | * @ctxt: the XPath Parser context |
| 6765 | * @nargs: the number of arguments |
| 6766 | * |
| 6767 | * Implement the name() XPath function |
| 6768 | * string name(node-set?) |
| 6769 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6770 | * 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] | 6771 | * order. The QName must represent the name with respect to the namespace |
| 6772 | * declarations in effect on the node whose name is being represented. |
| 6773 | * Typically, this will be the form in which the name occurred in the XML |
| 6774 | * source. This need not be the case if there are namespace declarations |
| 6775 | * in effect on the node that associate multiple prefixes with the same |
| 6776 | * namespace. However, an implementation may include information about |
| 6777 | * the original prefix in its representation of nodes; in this case, an |
| 6778 | * implementation can ensure that the returned string is always the same |
| 6779 | * as the QName used in the XML source. If the argument it omitted it |
| 6780 | * defaults to the context node. |
| 6781 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6782 | * returned. |
| 6783 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6784 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6785 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6786 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6787 | xmlXPathObjectPtr cur; |
| 6788 | |
| 6789 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6790 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6791 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
| 6794 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6795 | if ((ctxt->value == NULL) || |
| 6796 | ((ctxt->value->type != XPATH_NODESET) && |
| 6797 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6798 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6799 | cur = valuePop(ctxt); |
| 6800 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6801 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6802 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6803 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6804 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6806 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6807 | case XML_ELEMENT_NODE: |
| 6808 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6809 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6810 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6811 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6812 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6813 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6814 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6815 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6816 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6817 | xmlChar *fullname; |
| 6818 | |
| 6819 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6820 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6821 | NULL, 0); |
| 6822 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6823 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6824 | if (fullname == NULL) { |
| 6825 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6826 | } |
| 6827 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6828 | } |
| 6829 | break; |
| 6830 | default: |
| 6831 | valuePush(ctxt, |
| 6832 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6833 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6834 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6835 | } |
| 6836 | xmlXPathFreeObject(cur); |
| 6837 | } |
| 6838 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6839 | |
| 6840 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6841 | * xmlXPathStringFunction: |
| 6842 | * @ctxt: the XPath Parser context |
| 6843 | * @nargs: the number of arguments |
| 6844 | * |
| 6845 | * Implement the string() XPath function |
| 6846 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6847 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6848 | * - A node-set is converted to a string by returning the value of |
| 6849 | * the node in the node-set that is first in document order. |
| 6850 | * If the node-set is empty, an empty string is returned. |
| 6851 | * - A number is converted to a string as follows |
| 6852 | * + NaN is converted to the string NaN |
| 6853 | * + positive zero is converted to the string 0 |
| 6854 | * + negative zero is converted to the string 0 |
| 6855 | * + positive infinity is converted to the string Infinity |
| 6856 | * + negative infinity is converted to the string -Infinity |
| 6857 | * + if the number is an integer, the number is represented in |
| 6858 | * decimal form as a Number with no decimal point and no leading |
| 6859 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6860 | * + otherwise, the number is represented in decimal form as a |
| 6861 | * Number including a decimal point with at least one digit |
| 6862 | * before the decimal point and at least one digit after the |
| 6863 | * decimal point, preceded by a minus sign (-) if the number |
| 6864 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6865 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6866 | * before the decimal point; beyond the one required digit |
| 6867 | * after the decimal point there must be as many, but only as |
| 6868 | * many, more digits as are needed to uniquely distinguish the |
| 6869 | * number from all other IEEE 754 numeric values. |
| 6870 | * - The boolean false value is converted to the string false. |
| 6871 | * The boolean true value is converted to the string true. |
| 6872 | * |
| 6873 | * If the argument is omitted, it defaults to a node-set with the |
| 6874 | * context node as its only member. |
| 6875 | */ |
| 6876 | void |
| 6877 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6878 | xmlXPathObjectPtr cur; |
| 6879 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6880 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6881 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6882 | valuePush(ctxt, |
| 6883 | xmlXPathWrapString( |
| 6884 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6885 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
| 6888 | CHECK_ARITY(1); |
| 6889 | cur = valuePop(ctxt); |
| 6890 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6891 | cur = xmlXPathConvertString(cur); |
| 6892 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | /** |
| 6896 | * xmlXPathStringLengthFunction: |
| 6897 | * @ctxt: the XPath Parser context |
| 6898 | * @nargs: the number of arguments |
| 6899 | * |
| 6900 | * Implement the string-length() XPath function |
| 6901 | * number string-length(string?) |
| 6902 | * The string-length returns the number of characters in the string |
| 6903 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6904 | * the context node converted to a string, in other words the value |
| 6905 | * of the context node. |
| 6906 | */ |
| 6907 | void |
| 6908 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6909 | xmlXPathObjectPtr cur; |
| 6910 | |
| 6911 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6912 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 6913 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6914 | if (ctxt->context->node == NULL) { |
| 6915 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6916 | } else { |
| 6917 | xmlChar *content; |
| 6918 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6919 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6920 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6921 | xmlFree(content); |
| 6922 | } |
| 6923 | return; |
| 6924 | } |
| 6925 | CHECK_ARITY(1); |
| 6926 | CAST_TO_STRING; |
| 6927 | CHECK_TYPE(XPATH_STRING); |
| 6928 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6929 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6930 | xmlXPathFreeObject(cur); |
| 6931 | } |
| 6932 | |
| 6933 | /** |
| 6934 | * xmlXPathConcatFunction: |
| 6935 | * @ctxt: the XPath Parser context |
| 6936 | * @nargs: the number of arguments |
| 6937 | * |
| 6938 | * Implement the concat() XPath function |
| 6939 | * string concat(string, string, string*) |
| 6940 | * The concat function returns the concatenation of its arguments. |
| 6941 | */ |
| 6942 | void |
| 6943 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6944 | xmlXPathObjectPtr cur, newobj; |
| 6945 | xmlChar *tmp; |
| 6946 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6947 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6948 | if (nargs < 2) { |
| 6949 | CHECK_ARITY(2); |
| 6950 | } |
| 6951 | |
| 6952 | CAST_TO_STRING; |
| 6953 | cur = valuePop(ctxt); |
| 6954 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6955 | xmlXPathFreeObject(cur); |
| 6956 | return; |
| 6957 | } |
| 6958 | nargs--; |
| 6959 | |
| 6960 | while (nargs > 0) { |
| 6961 | CAST_TO_STRING; |
| 6962 | newobj = valuePop(ctxt); |
| 6963 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6964 | xmlXPathFreeObject(newobj); |
| 6965 | xmlXPathFreeObject(cur); |
| 6966 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6967 | } |
| 6968 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6969 | newobj->stringval = cur->stringval; |
| 6970 | cur->stringval = tmp; |
| 6971 | |
| 6972 | xmlXPathFreeObject(newobj); |
| 6973 | nargs--; |
| 6974 | } |
| 6975 | valuePush(ctxt, cur); |
| 6976 | } |
| 6977 | |
| 6978 | /** |
| 6979 | * xmlXPathContainsFunction: |
| 6980 | * @ctxt: the XPath Parser context |
| 6981 | * @nargs: the number of arguments |
| 6982 | * |
| 6983 | * Implement the contains() XPath function |
| 6984 | * boolean contains(string, string) |
| 6985 | * The contains function returns true if the first argument string |
| 6986 | * contains the second argument string, and otherwise returns false. |
| 6987 | */ |
| 6988 | void |
| 6989 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6990 | xmlXPathObjectPtr hay, needle; |
| 6991 | |
| 6992 | CHECK_ARITY(2); |
| 6993 | CAST_TO_STRING; |
| 6994 | CHECK_TYPE(XPATH_STRING); |
| 6995 | needle = valuePop(ctxt); |
| 6996 | CAST_TO_STRING; |
| 6997 | hay = valuePop(ctxt); |
| 6998 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6999 | xmlXPathFreeObject(hay); |
| 7000 | xmlXPathFreeObject(needle); |
| 7001 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7002 | } |
| 7003 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 7004 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7005 | else |
| 7006 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7007 | xmlXPathFreeObject(hay); |
| 7008 | xmlXPathFreeObject(needle); |
| 7009 | } |
| 7010 | |
| 7011 | /** |
| 7012 | * xmlXPathStartsWithFunction: |
| 7013 | * @ctxt: the XPath Parser context |
| 7014 | * @nargs: the number of arguments |
| 7015 | * |
| 7016 | * Implement the starts-with() XPath function |
| 7017 | * boolean starts-with(string, string) |
| 7018 | * The starts-with function returns true if the first argument string |
| 7019 | * starts with the second argument string, and otherwise returns false. |
| 7020 | */ |
| 7021 | void |
| 7022 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7023 | xmlXPathObjectPtr hay, needle; |
| 7024 | int n; |
| 7025 | |
| 7026 | CHECK_ARITY(2); |
| 7027 | CAST_TO_STRING; |
| 7028 | CHECK_TYPE(XPATH_STRING); |
| 7029 | needle = valuePop(ctxt); |
| 7030 | CAST_TO_STRING; |
| 7031 | hay = valuePop(ctxt); |
| 7032 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 7033 | xmlXPathFreeObject(hay); |
| 7034 | xmlXPathFreeObject(needle); |
| 7035 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7036 | } |
| 7037 | n = xmlStrlen(needle->stringval); |
| 7038 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 7039 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7040 | else |
| 7041 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7042 | xmlXPathFreeObject(hay); |
| 7043 | xmlXPathFreeObject(needle); |
| 7044 | } |
| 7045 | |
| 7046 | /** |
| 7047 | * xmlXPathSubstringFunction: |
| 7048 | * @ctxt: the XPath Parser context |
| 7049 | * @nargs: the number of arguments |
| 7050 | * |
| 7051 | * Implement the substring() XPath function |
| 7052 | * string substring(string, number, number?) |
| 7053 | * The substring function returns the substring of the first argument |
| 7054 | * starting at the position specified in the second argument with |
| 7055 | * length specified in the third argument. For example, |
| 7056 | * substring("12345",2,3) returns "234". If the third argument is not |
| 7057 | * specified, it returns the substring starting at the position specified |
| 7058 | * in the second argument and continuing to the end of the string. For |
| 7059 | * example, substring("12345",2) returns "2345". More precisely, each |
| 7060 | * character in the string (see [3.6 Strings]) is considered to have a |
| 7061 | * numeric position: the position of the first character is 1, the position |
| 7062 | * of the second character is 2 and so on. The returned substring contains |
| 7063 | * those characters for which the position of the character is greater than |
| 7064 | * or equal to the second argument and, if the third argument is specified, |
| 7065 | * less than the sum of the second and third arguments; the comparisons |
| 7066 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 7067 | * - substring("12345", 1.5, 2.6) returns "234" |
| 7068 | * - substring("12345", 0, 3) returns "12" |
| 7069 | * - substring("12345", 0 div 0, 3) returns "" |
| 7070 | * - substring("12345", 1, 0 div 0) returns "" |
| 7071 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 7072 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 7073 | */ |
| 7074 | void |
| 7075 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7076 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7077 | double le=0, in; |
| 7078 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7079 | xmlChar *ret; |
| 7080 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7081 | if (nargs < 2) { |
| 7082 | CHECK_ARITY(2); |
| 7083 | } |
| 7084 | if (nargs > 3) { |
| 7085 | CHECK_ARITY(3); |
| 7086 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7087 | /* |
| 7088 | * take care of possible last (position) argument |
| 7089 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7090 | if (nargs == 3) { |
| 7091 | CAST_TO_NUMBER; |
| 7092 | CHECK_TYPE(XPATH_NUMBER); |
| 7093 | len = valuePop(ctxt); |
| 7094 | le = len->floatval; |
| 7095 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7096 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7097 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7098 | CAST_TO_NUMBER; |
| 7099 | CHECK_TYPE(XPATH_NUMBER); |
| 7100 | start = valuePop(ctxt); |
| 7101 | in = start->floatval; |
| 7102 | xmlXPathFreeObject(start); |
| 7103 | CAST_TO_STRING; |
| 7104 | CHECK_TYPE(XPATH_STRING); |
| 7105 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7106 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7107 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7108 | /* |
| 7109 | * If last pos not present, calculate last position |
| 7110 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7111 | if (nargs != 3) { |
| 7112 | le = (double)m; |
| 7113 | if (in < 1.0) |
| 7114 | in = 1.0; |
| 7115 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7116 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7117 | /* Need to check for the special cases where either |
| 7118 | * the index is NaN, the length is NaN, or both |
| 7119 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7120 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7121 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7122 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7123 | * To meet the requirements of the spec, the arguments |
| 7124 | * must be converted to integer format before |
| 7125 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7126 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7127 | * First we go to integer form, rounding up |
| 7128 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7129 | */ |
| 7130 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7131 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7132 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 7133 | if (xmlXPathIsInf(le) == 1) { |
| 7134 | l = m; |
| 7135 | if (i < 1) |
| 7136 | i = 1; |
| 7137 | } |
| 7138 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 7139 | l = 0; |
| 7140 | else { |
| 7141 | l = (int) le; |
| 7142 | if (((double)l)+0.5 <= le) l++; |
| 7143 | } |
| 7144 | |
| 7145 | /* Now we normalize inidices */ |
| 7146 | i -= 1; |
| 7147 | l += i; |
| 7148 | if (i < 0) |
| 7149 | i = 0; |
| 7150 | if (l > m) |
| 7151 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7152 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7153 | /* number of chars to copy */ |
| 7154 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7155 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 7156 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 7157 | } |
| 7158 | else { |
| 7159 | ret = NULL; |
| 7160 | } |
| 7161 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7162 | if (ret == NULL) |
| 7163 | valuePush(ctxt, xmlXPathNewCString("")); |
| 7164 | else { |
| 7165 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 7166 | xmlFree(ret); |
| 7167 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7168 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7169 | xmlXPathFreeObject(str); |
| 7170 | } |
| 7171 | |
| 7172 | /** |
| 7173 | * xmlXPathSubstringBeforeFunction: |
| 7174 | * @ctxt: the XPath Parser context |
| 7175 | * @nargs: the number of arguments |
| 7176 | * |
| 7177 | * Implement the substring-before() XPath function |
| 7178 | * string substring-before(string, string) |
| 7179 | * The substring-before function returns the substring of the first |
| 7180 | * argument string that precedes the first occurrence of the second |
| 7181 | * argument string in the first argument string, or the empty string |
| 7182 | * if the first argument string does not contain the second argument |
| 7183 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 7184 | */ |
| 7185 | void |
| 7186 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7187 | xmlXPathObjectPtr str; |
| 7188 | xmlXPathObjectPtr find; |
| 7189 | xmlBufferPtr target; |
| 7190 | const xmlChar *point; |
| 7191 | int offset; |
| 7192 | |
| 7193 | CHECK_ARITY(2); |
| 7194 | CAST_TO_STRING; |
| 7195 | find = valuePop(ctxt); |
| 7196 | CAST_TO_STRING; |
| 7197 | str = valuePop(ctxt); |
| 7198 | |
| 7199 | target = xmlBufferCreate(); |
| 7200 | if (target) { |
| 7201 | point = xmlStrstr(str->stringval, find->stringval); |
| 7202 | if (point) { |
| 7203 | offset = (int)(point - str->stringval); |
| 7204 | xmlBufferAdd(target, str->stringval, offset); |
| 7205 | } |
| 7206 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7207 | xmlBufferFree(target); |
| 7208 | } |
| 7209 | |
| 7210 | xmlXPathFreeObject(str); |
| 7211 | xmlXPathFreeObject(find); |
| 7212 | } |
| 7213 | |
| 7214 | /** |
| 7215 | * xmlXPathSubstringAfterFunction: |
| 7216 | * @ctxt: the XPath Parser context |
| 7217 | * @nargs: the number of arguments |
| 7218 | * |
| 7219 | * Implement the substring-after() XPath function |
| 7220 | * string substring-after(string, string) |
| 7221 | * The substring-after function returns the substring of the first |
| 7222 | * argument string that follows the first occurrence of the second |
| 7223 | * argument string in the first argument string, or the empty stringi |
| 7224 | * if the first argument string does not contain the second argument |
| 7225 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 7226 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 7227 | */ |
| 7228 | void |
| 7229 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7230 | xmlXPathObjectPtr str; |
| 7231 | xmlXPathObjectPtr find; |
| 7232 | xmlBufferPtr target; |
| 7233 | const xmlChar *point; |
| 7234 | int offset; |
| 7235 | |
| 7236 | CHECK_ARITY(2); |
| 7237 | CAST_TO_STRING; |
| 7238 | find = valuePop(ctxt); |
| 7239 | CAST_TO_STRING; |
| 7240 | str = valuePop(ctxt); |
| 7241 | |
| 7242 | target = xmlBufferCreate(); |
| 7243 | if (target) { |
| 7244 | point = xmlStrstr(str->stringval, find->stringval); |
| 7245 | if (point) { |
| 7246 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 7247 | xmlBufferAdd(target, &str->stringval[offset], |
| 7248 | xmlStrlen(str->stringval) - offset); |
| 7249 | } |
| 7250 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7251 | xmlBufferFree(target); |
| 7252 | } |
| 7253 | |
| 7254 | xmlXPathFreeObject(str); |
| 7255 | xmlXPathFreeObject(find); |
| 7256 | } |
| 7257 | |
| 7258 | /** |
| 7259 | * xmlXPathNormalizeFunction: |
| 7260 | * @ctxt: the XPath Parser context |
| 7261 | * @nargs: the number of arguments |
| 7262 | * |
| 7263 | * Implement the normalize-space() XPath function |
| 7264 | * string normalize-space(string?) |
| 7265 | * The normalize-space function returns the argument string with white |
| 7266 | * space normalized by stripping leading and trailing whitespace |
| 7267 | * and replacing sequences of whitespace characters by a single |
| 7268 | * space. Whitespace characters are the same allowed by the S production |
| 7269 | * in XML. If the argument is omitted, it defaults to the context |
| 7270 | * node converted to a string, in other words the value of the context node. |
| 7271 | */ |
| 7272 | void |
| 7273 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7274 | xmlXPathObjectPtr obj = NULL; |
| 7275 | xmlChar *source = NULL; |
| 7276 | xmlBufferPtr target; |
| 7277 | xmlChar blank; |
| 7278 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7279 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7280 | if (nargs == 0) { |
| 7281 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7282 | valuePush(ctxt, |
| 7283 | xmlXPathWrapString( |
| 7284 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7285 | nargs = 1; |
| 7286 | } |
| 7287 | |
| 7288 | CHECK_ARITY(1); |
| 7289 | CAST_TO_STRING; |
| 7290 | CHECK_TYPE(XPATH_STRING); |
| 7291 | obj = valuePop(ctxt); |
| 7292 | source = obj->stringval; |
| 7293 | |
| 7294 | target = xmlBufferCreate(); |
| 7295 | if (target && source) { |
| 7296 | |
| 7297 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7298 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7299 | source++; |
| 7300 | |
| 7301 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 7302 | blank = 0; |
| 7303 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7304 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7305 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7306 | } else { |
| 7307 | if (blank) { |
| 7308 | xmlBufferAdd(target, &blank, 1); |
| 7309 | blank = 0; |
| 7310 | } |
| 7311 | xmlBufferAdd(target, source, 1); |
| 7312 | } |
| 7313 | source++; |
| 7314 | } |
| 7315 | |
| 7316 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7317 | xmlBufferFree(target); |
| 7318 | } |
| 7319 | xmlXPathFreeObject(obj); |
| 7320 | } |
| 7321 | |
| 7322 | /** |
| 7323 | * xmlXPathTranslateFunction: |
| 7324 | * @ctxt: the XPath Parser context |
| 7325 | * @nargs: the number of arguments |
| 7326 | * |
| 7327 | * Implement the translate() XPath function |
| 7328 | * string translate(string, string, string) |
| 7329 | * The translate function returns the first argument string with |
| 7330 | * occurrences of characters in the second argument string replaced |
| 7331 | * by the character at the corresponding position in the third argument |
| 7332 | * string. For example, translate("bar","abc","ABC") returns the string |
| 7333 | * BAr. If there is a character in the second argument string with no |
| 7334 | * character at a corresponding position in the third argument string |
| 7335 | * (because the second argument string is longer than the third argument |
| 7336 | * string), then occurrences of that character in the first argument |
| 7337 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 7338 | * returns "AAA". If a character occurs more than once in second |
| 7339 | * argument string, then the first occurrence determines the replacement |
| 7340 | * character. If the third argument string is longer than the second |
| 7341 | * argument string, then excess characters are ignored. |
| 7342 | */ |
| 7343 | void |
| 7344 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7345 | xmlXPathObjectPtr str; |
| 7346 | xmlXPathObjectPtr from; |
| 7347 | xmlXPathObjectPtr to; |
| 7348 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7349 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7350 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 7351 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7352 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7353 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7354 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7355 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7356 | CAST_TO_STRING; |
| 7357 | to = valuePop(ctxt); |
| 7358 | CAST_TO_STRING; |
| 7359 | from = valuePop(ctxt); |
| 7360 | CAST_TO_STRING; |
| 7361 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7362 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7363 | target = xmlBufferCreate(); |
| 7364 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 7365 | max = xmlUTF8Strlen(to->stringval); |
| 7366 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 7367 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 7368 | if (offset >= 0) { |
| 7369 | if (offset < max) { |
| 7370 | point = xmlUTF8Strpos(to->stringval, offset); |
| 7371 | if (point) |
| 7372 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 7373 | } |
| 7374 | } else |
| 7375 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 7376 | |
| 7377 | /* Step to next character in input */ |
| 7378 | cptr++; |
| 7379 | if ( ch & 0x80 ) { |
| 7380 | /* if not simple ascii, verify proper format */ |
| 7381 | if ( (ch & 0xc0) != 0xc0 ) { |
| 7382 | xmlGenericError(xmlGenericErrorContext, |
| 7383 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 7384 | break; |
| 7385 | } |
| 7386 | /* then skip over remaining bytes for this char */ |
| 7387 | while ( (ch <<= 1) & 0x80 ) |
| 7388 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 7389 | xmlGenericError(xmlGenericErrorContext, |
| 7390 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 7391 | break; |
| 7392 | } |
| 7393 | if (ch & 0x80) /* must have had error encountered */ |
| 7394 | break; |
| 7395 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7396 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7397 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 7398 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 7399 | xmlBufferFree(target); |
| 7400 | xmlXPathFreeObject(str); |
| 7401 | xmlXPathFreeObject(from); |
| 7402 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | /** |
| 7406 | * xmlXPathBooleanFunction: |
| 7407 | * @ctxt: the XPath Parser context |
| 7408 | * @nargs: the number of arguments |
| 7409 | * |
| 7410 | * Implement the boolean() XPath function |
| 7411 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7412 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7413 | * - a number is true if and only if it is neither positive or |
| 7414 | * negative zero nor NaN |
| 7415 | * - a node-set is true if and only if it is non-empty |
| 7416 | * - a string is true if and only if its length is non-zero |
| 7417 | */ |
| 7418 | void |
| 7419 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7420 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7421 | |
| 7422 | CHECK_ARITY(1); |
| 7423 | cur = valuePop(ctxt); |
| 7424 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7425 | cur = xmlXPathConvertBoolean(cur); |
| 7426 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
| 7429 | /** |
| 7430 | * xmlXPathNotFunction: |
| 7431 | * @ctxt: the XPath Parser context |
| 7432 | * @nargs: the number of arguments |
| 7433 | * |
| 7434 | * Implement the not() XPath function |
| 7435 | * boolean not(boolean) |
| 7436 | * The not function returns true if its argument is false, |
| 7437 | * and false otherwise. |
| 7438 | */ |
| 7439 | void |
| 7440 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7441 | CHECK_ARITY(1); |
| 7442 | CAST_TO_BOOLEAN; |
| 7443 | CHECK_TYPE(XPATH_BOOLEAN); |
| 7444 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 7445 | } |
| 7446 | |
| 7447 | /** |
| 7448 | * xmlXPathTrueFunction: |
| 7449 | * @ctxt: the XPath Parser context |
| 7450 | * @nargs: the number of arguments |
| 7451 | * |
| 7452 | * Implement the true() XPath function |
| 7453 | * boolean true() |
| 7454 | */ |
| 7455 | void |
| 7456 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7457 | CHECK_ARITY(0); |
| 7458 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7459 | } |
| 7460 | |
| 7461 | /** |
| 7462 | * xmlXPathFalseFunction: |
| 7463 | * @ctxt: the XPath Parser context |
| 7464 | * @nargs: the number of arguments |
| 7465 | * |
| 7466 | * Implement the false() XPath function |
| 7467 | * boolean false() |
| 7468 | */ |
| 7469 | void |
| 7470 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7471 | CHECK_ARITY(0); |
| 7472 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7473 | } |
| 7474 | |
| 7475 | /** |
| 7476 | * xmlXPathLangFunction: |
| 7477 | * @ctxt: the XPath Parser context |
| 7478 | * @nargs: the number of arguments |
| 7479 | * |
| 7480 | * Implement the lang() XPath function |
| 7481 | * boolean lang(string) |
| 7482 | * The lang function returns true or false depending on whether the |
| 7483 | * language of the context node as specified by xml:lang attributes |
| 7484 | * is the same as or is a sublanguage of the language specified by |
| 7485 | * the argument string. The language of the context node is determined |
| 7486 | * by the value of the xml:lang attribute on the context node, or, if |
| 7487 | * the context node has no xml:lang attribute, by the value of the |
| 7488 | * xml:lang attribute on the nearest ancestor of the context node that |
| 7489 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 7490 | * returns false. If there is such an attribute, then lang returns |
| 7491 | * true if the attribute value is equal to the argument ignoring case, |
| 7492 | * or if there is some suffix starting with - such that the attribute |
| 7493 | * value is equal to the argument ignoring that suffix of the attribute |
| 7494 | * value and ignoring case. |
| 7495 | */ |
| 7496 | void |
| 7497 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7498 | xmlXPathObjectPtr val = NULL; |
| 7499 | const xmlChar *theLang = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7500 | const xmlChar *lang; |
| 7501 | int ret = 0; |
| 7502 | int i; |
| 7503 | |
| 7504 | CHECK_ARITY(1); |
| 7505 | CAST_TO_STRING; |
| 7506 | CHECK_TYPE(XPATH_STRING); |
| 7507 | val = valuePop(ctxt); |
| 7508 | lang = val->stringval; |
| 7509 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 7510 | if ((theLang != NULL) && (lang != NULL)) { |
| 7511 | for (i = 0;lang[i] != 0;i++) |
| 7512 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 7513 | goto not_equal; |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7514 | if ((theLang[i] == 0) || (theLang[i] == '-')) |
| 7515 | ret = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7516 | } |
| 7517 | not_equal: |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 7518 | if (theLang != NULL) |
| 7519 | xmlFree((void *)theLang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7520 | xmlXPathFreeObject(val); |
| 7521 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 7522 | } |
| 7523 | |
| 7524 | /** |
| 7525 | * xmlXPathNumberFunction: |
| 7526 | * @ctxt: the XPath Parser context |
| 7527 | * @nargs: the number of arguments |
| 7528 | * |
| 7529 | * Implement the number() XPath function |
| 7530 | * number number(object?) |
| 7531 | */ |
| 7532 | void |
| 7533 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7534 | xmlXPathObjectPtr cur; |
| 7535 | double res; |
| 7536 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7537 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7538 | if (nargs == 0) { |
| 7539 | if (ctxt->context->node == NULL) { |
| 7540 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 7541 | } else { |
| 7542 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 7543 | |
| 7544 | res = xmlXPathStringEvalNumber(content); |
| 7545 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 7546 | xmlFree(content); |
| 7547 | } |
| 7548 | return; |
| 7549 | } |
| 7550 | |
| 7551 | CHECK_ARITY(1); |
| 7552 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7553 | cur = xmlXPathConvertNumber(cur); |
| 7554 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7555 | } |
| 7556 | |
| 7557 | /** |
| 7558 | * xmlXPathSumFunction: |
| 7559 | * @ctxt: the XPath Parser context |
| 7560 | * @nargs: the number of arguments |
| 7561 | * |
| 7562 | * Implement the sum() XPath function |
| 7563 | * number sum(node-set) |
| 7564 | * The sum function returns the sum of the values of the nodes in |
| 7565 | * the argument node-set. |
| 7566 | */ |
| 7567 | void |
| 7568 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7569 | xmlXPathObjectPtr cur; |
| 7570 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7571 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7572 | |
| 7573 | CHECK_ARITY(1); |
| 7574 | if ((ctxt->value == NULL) || |
| 7575 | ((ctxt->value->type != XPATH_NODESET) && |
| 7576 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7577 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7578 | cur = valuePop(ctxt); |
| 7579 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7580 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7581 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7582 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7583 | } |
| 7584 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7585 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7586 | xmlXPathFreeObject(cur); |
| 7587 | } |
| 7588 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7589 | /* |
| 7590 | * To assure working code on multiple platforms, we want to only depend |
| 7591 | * upon the characteristic truncation of converting a floating point value |
| 7592 | * to an integer. Unfortunately, because of the different storage sizes |
| 7593 | * of our internal floating point value (double) and integer (int), we |
| 7594 | * can't directly convert (see bug 301162). This macro is a messy |
| 7595 | * 'workaround' |
| 7596 | */ |
| 7597 | #define XTRUNC(f, v) \ |
| 7598 | f = fmod((v), INT_MAX); \ |
| 7599 | f = (v) - (f) + (double)((int)(f)); |
| 7600 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7601 | /** |
| 7602 | * xmlXPathFloorFunction: |
| 7603 | * @ctxt: the XPath Parser context |
| 7604 | * @nargs: the number of arguments |
| 7605 | * |
| 7606 | * Implement the floor() XPath function |
| 7607 | * number floor(number) |
| 7608 | * The floor function returns the largest (closest to positive infinity) |
| 7609 | * number that is not greater than the argument and that is an integer. |
| 7610 | */ |
| 7611 | void |
| 7612 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7613 | double f; |
| 7614 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7615 | CHECK_ARITY(1); |
| 7616 | CAST_TO_NUMBER; |
| 7617 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7618 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7619 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7620 | if (f != ctxt->value->floatval) { |
| 7621 | if (ctxt->value->floatval > 0) |
| 7622 | ctxt->value->floatval = f; |
| 7623 | else |
| 7624 | ctxt->value->floatval = f - 1; |
| 7625 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
| 7628 | /** |
| 7629 | * xmlXPathCeilingFunction: |
| 7630 | * @ctxt: the XPath Parser context |
| 7631 | * @nargs: the number of arguments |
| 7632 | * |
| 7633 | * Implement the ceiling() XPath function |
| 7634 | * number ceiling(number) |
| 7635 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7636 | * number that is not less than the argument and that is an integer. |
| 7637 | */ |
| 7638 | void |
| 7639 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7640 | double f; |
| 7641 | |
| 7642 | CHECK_ARITY(1); |
| 7643 | CAST_TO_NUMBER; |
| 7644 | CHECK_TYPE(XPATH_NUMBER); |
| 7645 | |
| 7646 | #if 0 |
| 7647 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7648 | #else |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7649 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7650 | if (f != ctxt->value->floatval) { |
| 7651 | if (ctxt->value->floatval > 0) |
| 7652 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7653 | else { |
| 7654 | if (ctxt->value->floatval < 0 && f == 0) |
| 7655 | ctxt->value->floatval = xmlXPathNZERO; |
| 7656 | else |
| 7657 | ctxt->value->floatval = f; |
| 7658 | } |
| 7659 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7660 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7661 | #endif |
| 7662 | } |
| 7663 | |
| 7664 | /** |
| 7665 | * xmlXPathRoundFunction: |
| 7666 | * @ctxt: the XPath Parser context |
| 7667 | * @nargs: the number of arguments |
| 7668 | * |
| 7669 | * Implement the round() XPath function |
| 7670 | * number round(number) |
| 7671 | * The round function returns the number that is closest to the |
| 7672 | * argument and that is an integer. If there are two such numbers, |
| 7673 | * then the one that is even is returned. |
| 7674 | */ |
| 7675 | void |
| 7676 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7677 | double f; |
| 7678 | |
| 7679 | CHECK_ARITY(1); |
| 7680 | CAST_TO_NUMBER; |
| 7681 | CHECK_TYPE(XPATH_NUMBER); |
| 7682 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7683 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7684 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7685 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7686 | (ctxt->value->floatval == 0.0)) |
| 7687 | return; |
| 7688 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 7689 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7690 | if (ctxt->value->floatval < 0) { |
| 7691 | if (ctxt->value->floatval < f - 0.5) |
| 7692 | ctxt->value->floatval = f - 1; |
| 7693 | else |
| 7694 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7695 | if (ctxt->value->floatval == 0) |
| 7696 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7697 | } else { |
| 7698 | if (ctxt->value->floatval < f + 0.5) |
| 7699 | ctxt->value->floatval = f; |
| 7700 | else |
| 7701 | ctxt->value->floatval = f + 1; |
| 7702 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7703 | } |
| 7704 | |
| 7705 | /************************************************************************ |
| 7706 | * * |
| 7707 | * The Parser * |
| 7708 | * * |
| 7709 | ************************************************************************/ |
| 7710 | |
| 7711 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7712 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7713 | * implementation. |
| 7714 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 7715 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7716 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7717 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7718 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7719 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7720 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7721 | |
| 7722 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7723 | * xmlXPathCurrentChar: |
| 7724 | * @ctxt: the XPath parser context |
| 7725 | * @cur: pointer to the beginning of the char |
| 7726 | * @len: pointer to the length of the char read |
| 7727 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7728 | * 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] | 7729 | * bytes in the input buffer. |
| 7730 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7731 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7732 | */ |
| 7733 | |
| 7734 | static int |
| 7735 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7736 | unsigned char c; |
| 7737 | unsigned int val; |
| 7738 | const xmlChar *cur; |
| 7739 | |
| 7740 | if (ctxt == NULL) |
| 7741 | return(0); |
| 7742 | cur = ctxt->cur; |
| 7743 | |
| 7744 | /* |
| 7745 | * We are supposed to handle UTF8, check it's valid |
| 7746 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7747 | * |
| 7748 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7749 | * 0000 0000-0000 007F 0xxxxxxx |
| 7750 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7751 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7752 | * |
| 7753 | * Check for the 0x110000 limit too |
| 7754 | */ |
| 7755 | c = *cur; |
| 7756 | if (c & 0x80) { |
| 7757 | if ((cur[1] & 0xc0) != 0x80) |
| 7758 | goto encoding_error; |
| 7759 | if ((c & 0xe0) == 0xe0) { |
| 7760 | |
| 7761 | if ((cur[2] & 0xc0) != 0x80) |
| 7762 | goto encoding_error; |
| 7763 | if ((c & 0xf0) == 0xf0) { |
| 7764 | if (((c & 0xf8) != 0xf0) || |
| 7765 | ((cur[3] & 0xc0) != 0x80)) |
| 7766 | goto encoding_error; |
| 7767 | /* 4-byte code */ |
| 7768 | *len = 4; |
| 7769 | val = (cur[0] & 0x7) << 18; |
| 7770 | val |= (cur[1] & 0x3f) << 12; |
| 7771 | val |= (cur[2] & 0x3f) << 6; |
| 7772 | val |= cur[3] & 0x3f; |
| 7773 | } else { |
| 7774 | /* 3-byte code */ |
| 7775 | *len = 3; |
| 7776 | val = (cur[0] & 0xf) << 12; |
| 7777 | val |= (cur[1] & 0x3f) << 6; |
| 7778 | val |= cur[2] & 0x3f; |
| 7779 | } |
| 7780 | } else { |
| 7781 | /* 2-byte code */ |
| 7782 | *len = 2; |
| 7783 | val = (cur[0] & 0x1f) << 6; |
| 7784 | val |= cur[1] & 0x3f; |
| 7785 | } |
| 7786 | if (!IS_CHAR(val)) { |
| 7787 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7788 | } |
| 7789 | return(val); |
| 7790 | } else { |
| 7791 | /* 1-byte code */ |
| 7792 | *len = 1; |
| 7793 | return((int) *cur); |
| 7794 | } |
| 7795 | encoding_error: |
| 7796 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7797 | * If we detect an UTF8 error that probably means that the |
| 7798 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7799 | * declaration header. Report the error and switch the encoding |
| 7800 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7801 | * encoding !) |
| 7802 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7803 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7804 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7808 | * xmlXPathParseNCName: |
| 7809 | * @ctxt: the XPath Parser context |
| 7810 | * |
| 7811 | * parse an XML namespace non qualified name. |
| 7812 | * |
| 7813 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7814 | * |
| 7815 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7816 | * CombiningChar | Extender |
| 7817 | * |
| 7818 | * Returns the namespace name or NULL |
| 7819 | */ |
| 7820 | |
| 7821 | xmlChar * |
| 7822 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7823 | const xmlChar *in; |
| 7824 | xmlChar *ret; |
| 7825 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7826 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7827 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7828 | /* |
| 7829 | * Accelerator for simple ASCII names |
| 7830 | */ |
| 7831 | in = ctxt->cur; |
| 7832 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7833 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7834 | (*in == '_')) { |
| 7835 | in++; |
| 7836 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7837 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7838 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7839 | (*in == '_') || (*in == '.') || |
| 7840 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7841 | in++; |
| 7842 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7843 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7844 | (*in == '@') || (*in == '*')) { |
| 7845 | count = in - ctxt->cur; |
| 7846 | if (count == 0) |
| 7847 | return(NULL); |
| 7848 | ret = xmlStrndup(ctxt->cur, count); |
| 7849 | ctxt->cur = in; |
| 7850 | return(ret); |
| 7851 | } |
| 7852 | } |
| 7853 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7854 | } |
| 7855 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7856 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7857 | /** |
| 7858 | * xmlXPathParseQName: |
| 7859 | * @ctxt: the XPath Parser context |
| 7860 | * @prefix: a xmlChar ** |
| 7861 | * |
| 7862 | * parse an XML qualified name |
| 7863 | * |
| 7864 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7865 | * |
| 7866 | * [NS 6] Prefix ::= NCName |
| 7867 | * |
| 7868 | * [NS 7] LocalPart ::= NCName |
| 7869 | * |
| 7870 | * Returns the function returns the local part, and prefix is updated |
| 7871 | * to get the Prefix if any. |
| 7872 | */ |
| 7873 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7874 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7875 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7876 | xmlChar *ret = NULL; |
| 7877 | |
| 7878 | *prefix = NULL; |
| 7879 | ret = xmlXPathParseNCName(ctxt); |
| 7880 | if (CUR == ':') { |
| 7881 | *prefix = ret; |
| 7882 | NEXT; |
| 7883 | ret = xmlXPathParseNCName(ctxt); |
| 7884 | } |
| 7885 | return(ret); |
| 7886 | } |
| 7887 | |
| 7888 | /** |
| 7889 | * xmlXPathParseName: |
| 7890 | * @ctxt: the XPath Parser context |
| 7891 | * |
| 7892 | * parse an XML name |
| 7893 | * |
| 7894 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7895 | * CombiningChar | Extender |
| 7896 | * |
| 7897 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7898 | * |
| 7899 | * Returns the namespace name or NULL |
| 7900 | */ |
| 7901 | |
| 7902 | xmlChar * |
| 7903 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7904 | const xmlChar *in; |
| 7905 | xmlChar *ret; |
| 7906 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7907 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7908 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7909 | /* |
| 7910 | * Accelerator for simple ASCII names |
| 7911 | */ |
| 7912 | in = ctxt->cur; |
| 7913 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7914 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7915 | (*in == '_') || (*in == ':')) { |
| 7916 | in++; |
| 7917 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7918 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7919 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7920 | (*in == '_') || (*in == '-') || |
| 7921 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7922 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7923 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7924 | count = in - ctxt->cur; |
| 7925 | ret = xmlStrndup(ctxt->cur, count); |
| 7926 | ctxt->cur = in; |
| 7927 | return(ret); |
| 7928 | } |
| 7929 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7930 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7933 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7934 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7935 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7936 | int len = 0, l; |
| 7937 | int c; |
| 7938 | |
| 7939 | /* |
| 7940 | * Handler for more complex cases |
| 7941 | */ |
| 7942 | c = CUR_CHAR(l); |
| 7943 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7944 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7945 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7946 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7947 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7948 | return(NULL); |
| 7949 | } |
| 7950 | |
| 7951 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7952 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7953 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7954 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7955 | (IS_COMBINING(c)) || |
| 7956 | (IS_EXTENDER(c)))) { |
| 7957 | COPY_BUF(l,buf,len,c); |
| 7958 | NEXTL(l); |
| 7959 | c = CUR_CHAR(l); |
| 7960 | if (len >= XML_MAX_NAMELEN) { |
| 7961 | /* |
| 7962 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7963 | * for the processing speed. |
| 7964 | */ |
| 7965 | xmlChar *buffer; |
| 7966 | int max = len * 2; |
| 7967 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7968 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7969 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7970 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7971 | } |
| 7972 | memcpy(buffer, buf, len); |
| 7973 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7974 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7975 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7976 | (IS_COMBINING(c)) || |
| 7977 | (IS_EXTENDER(c))) { |
| 7978 | if (len + 10 > max) { |
| 7979 | max *= 2; |
| 7980 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7981 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7982 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 7983 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7984 | } |
| 7985 | } |
| 7986 | COPY_BUF(l,buffer,len,c); |
| 7987 | NEXTL(l); |
| 7988 | c = CUR_CHAR(l); |
| 7989 | } |
| 7990 | buffer[len] = 0; |
| 7991 | return(buffer); |
| 7992 | } |
| 7993 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7994 | if (len == 0) |
| 7995 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7996 | return(xmlStrndup(buf, len)); |
| 7997 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7998 | |
| 7999 | #define MAX_FRAC 20 |
| 8000 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 8001 | /* |
| 8002 | * These are used as divisors for the fractional part of a number. |
| 8003 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 8004 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 8005 | */ |
| 8006 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 8007 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 8008 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 8009 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 8010 | 100000000000000.0, |
| 8011 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 8012 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 8013 | }; |
| 8014 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8015 | /** |
| 8016 | * xmlXPathStringEvalNumber: |
| 8017 | * @str: A string to scan |
| 8018 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8019 | * [30a] Float ::= Number ('e' Digits?)? |
| 8020 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8021 | * [30] Number ::= Digits ('.' Digits?)? |
| 8022 | * | '.' Digits |
| 8023 | * [31] Digits ::= [0-9]+ |
| 8024 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8025 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8026 | * In complement of the Number expression, this function also handles |
| 8027 | * negative values : '-' Number. |
| 8028 | * |
| 8029 | * Returns the double value. |
| 8030 | */ |
| 8031 | double |
| 8032 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 8033 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8034 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8035 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8036 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8037 | int exponent = 0; |
| 8038 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8039 | #ifdef __GNUC__ |
| 8040 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8041 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8042 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 8043 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8044 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8045 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 8046 | return(xmlXPathNAN); |
| 8047 | } |
| 8048 | if (*cur == '-') { |
| 8049 | isneg = 1; |
| 8050 | cur++; |
| 8051 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8052 | |
| 8053 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8054 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8055 | * tmp/temp is a workaround against a gcc compiler bug |
| 8056 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8057 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8058 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8059 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8060 | ret = ret * 10; |
| 8061 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8062 | ok = 1; |
| 8063 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8064 | temp = (double) tmp; |
| 8065 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8066 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8067 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8068 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 8069 | while ((*cur >= '0') && (*cur <= '9')) { |
| 8070 | ret = ret * 10 + (*cur - '0'); |
| 8071 | ok = 1; |
| 8072 | cur++; |
| 8073 | } |
| 8074 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8075 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8076 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 8077 | int v, frac = 0; |
| 8078 | double fraction = 0; |
| 8079 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8080 | cur++; |
| 8081 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 8082 | return(xmlXPathNAN); |
| 8083 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 8084 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 8085 | v = (*cur - '0'); |
| 8086 | fraction = fraction * 10 + v; |
| 8087 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8088 | cur++; |
| 8089 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 8090 | fraction /= my_pow10[frac]; |
| 8091 | ret = ret + fraction; |
| 8092 | while ((*cur >= '0') && (*cur <= '9')) |
| 8093 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8094 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8095 | if ((*cur == 'e') || (*cur == 'E')) { |
| 8096 | cur++; |
| 8097 | if (*cur == '-') { |
| 8098 | is_exponent_negative = 1; |
| 8099 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 8100 | } else if (*cur == '+') { |
| 8101 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8102 | } |
| 8103 | while ((*cur >= '0') && (*cur <= '9')) { |
| 8104 | exponent = exponent * 10 + (*cur - '0'); |
| 8105 | cur++; |
| 8106 | } |
| 8107 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8108 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8109 | if (*cur != 0) return(xmlXPathNAN); |
| 8110 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8111 | if (is_exponent_negative) exponent = -exponent; |
| 8112 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8113 | return(ret); |
| 8114 | } |
| 8115 | |
| 8116 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8117 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8118 | * @ctxt: the XPath Parser context |
| 8119 | * |
| 8120 | * [30] Number ::= Digits ('.' Digits?)? |
| 8121 | * | '.' Digits |
| 8122 | * [31] Digits ::= [0-9]+ |
| 8123 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8124 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8125 | * |
| 8126 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8127 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8128 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 8129 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8130 | double ret = 0.0; |
| 8131 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8132 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8133 | int exponent = 0; |
| 8134 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8135 | #ifdef __GNUC__ |
| 8136 | unsigned long tmp = 0; |
| 8137 | double temp; |
| 8138 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8139 | |
| 8140 | CHECK_ERROR; |
| 8141 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 8142 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 8143 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8144 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8145 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8146 | * tmp/temp is a workaround against a gcc compiler bug |
| 8147 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8148 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8149 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8150 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8151 | ret = ret * 10; |
| 8152 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8153 | ok = 1; |
| 8154 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8155 | temp = (double) tmp; |
| 8156 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8157 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 8158 | #else |
| 8159 | ret = 0; |
| 8160 | while ((CUR >= '0') && (CUR <= '9')) { |
| 8161 | ret = ret * 10 + (CUR - '0'); |
| 8162 | ok = 1; |
| 8163 | NEXT; |
| 8164 | } |
| 8165 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8166 | if (CUR == '.') { |
| 8167 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8168 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 8169 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 8170 | } |
| 8171 | while ((CUR >= '0') && (CUR <= '9')) { |
| 8172 | mult /= 10; |
| 8173 | ret = ret + (CUR - '0') * mult; |
| 8174 | NEXT; |
| 8175 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8176 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8177 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8178 | NEXT; |
| 8179 | if (CUR == '-') { |
| 8180 | is_exponent_negative = 1; |
| 8181 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 8182 | } else if (CUR == '+') { |
| 8183 | NEXT; |
| 8184 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8185 | while ((CUR >= '0') && (CUR <= '9')) { |
| 8186 | exponent = exponent * 10 + (CUR - '0'); |
| 8187 | NEXT; |
| 8188 | } |
| 8189 | if (is_exponent_negative) |
| 8190 | exponent = -exponent; |
| 8191 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8192 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8193 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 8194 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8195 | } |
| 8196 | |
| 8197 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8198 | * xmlXPathParseLiteral: |
| 8199 | * @ctxt: the XPath Parser context |
| 8200 | * |
| 8201 | * Parse a Literal |
| 8202 | * |
| 8203 | * [29] Literal ::= '"' [^"]* '"' |
| 8204 | * | "'" [^']* "'" |
| 8205 | * |
| 8206 | * Returns the value found or NULL in case of error |
| 8207 | */ |
| 8208 | static xmlChar * |
| 8209 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 8210 | const xmlChar *q; |
| 8211 | xmlChar *ret = NULL; |
| 8212 | |
| 8213 | if (CUR == '"') { |
| 8214 | NEXT; |
| 8215 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8216 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8217 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8218 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8219 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8220 | } else { |
| 8221 | ret = xmlStrndup(q, CUR_PTR - q); |
| 8222 | NEXT; |
| 8223 | } |
| 8224 | } else if (CUR == '\'') { |
| 8225 | NEXT; |
| 8226 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8227 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8228 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8229 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8230 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8231 | } else { |
| 8232 | ret = xmlStrndup(q, CUR_PTR - q); |
| 8233 | NEXT; |
| 8234 | } |
| 8235 | } else { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 8236 | XP_ERRORNULL(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8237 | } |
| 8238 | return(ret); |
| 8239 | } |
| 8240 | |
| 8241 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8242 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8243 | * @ctxt: the XPath Parser context |
| 8244 | * |
| 8245 | * Parse a Literal and push it on the stack. |
| 8246 | * |
| 8247 | * [29] Literal ::= '"' [^"]* '"' |
| 8248 | * | "'" [^']* "'" |
| 8249 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8250 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8251 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8252 | static void |
| 8253 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8254 | const xmlChar *q; |
| 8255 | xmlChar *ret = NULL; |
| 8256 | |
| 8257 | if (CUR == '"') { |
| 8258 | NEXT; |
| 8259 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8260 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8261 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8262 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8263 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 8264 | } else { |
| 8265 | ret = xmlStrndup(q, CUR_PTR - q); |
| 8266 | NEXT; |
| 8267 | } |
| 8268 | } else if (CUR == '\'') { |
| 8269 | NEXT; |
| 8270 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8271 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8272 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8273 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8274 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 8275 | } else { |
| 8276 | ret = xmlStrndup(q, CUR_PTR - q); |
| 8277 | NEXT; |
| 8278 | } |
| 8279 | } else { |
| 8280 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 8281 | } |
| 8282 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8283 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 8284 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8285 | xmlFree(ret); |
| 8286 | } |
| 8287 | |
| 8288 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8289 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8290 | * @ctxt: the XPath Parser context |
| 8291 | * |
| 8292 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 8293 | * |
| 8294 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8295 | * 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] | 8296 | * of any of the types that are possible for the value of an expression, |
| 8297 | * and may also be of additional types not specified here. |
| 8298 | * |
| 8299 | * Early evaluation is possible since: |
| 8300 | * The variable bindings [...] used to evaluate a subexpression are |
| 8301 | * always the same as those used to evaluate the containing expression. |
| 8302 | * |
| 8303 | * [36] VariableReference ::= '$' QName |
| 8304 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8305 | static void |
| 8306 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8307 | xmlChar *name; |
| 8308 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8309 | |
| 8310 | SKIP_BLANKS; |
| 8311 | if (CUR != '$') { |
| 8312 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 8313 | } |
| 8314 | NEXT; |
| 8315 | name = xmlXPathParseQName(ctxt, &prefix); |
| 8316 | if (name == NULL) { |
| 8317 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 8318 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8319 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8320 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 8321 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8322 | SKIP_BLANKS; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 8323 | if ((ctxt->context != NULL) && (ctxt->context->flags & XML_XPATH_NOVAR)) { |
| 8324 | XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR); |
| 8325 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
| 8328 | /** |
| 8329 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8330 | * @name: a name string |
| 8331 | * |
| 8332 | * Is the name given a NodeType one. |
| 8333 | * |
| 8334 | * [38] NodeType ::= 'comment' |
| 8335 | * | 'text' |
| 8336 | * | 'processing-instruction' |
| 8337 | * | 'node' |
| 8338 | * |
| 8339 | * Returns 1 if true 0 otherwise |
| 8340 | */ |
| 8341 | int |
| 8342 | xmlXPathIsNodeType(const xmlChar *name) { |
| 8343 | if (name == NULL) |
| 8344 | return(0); |
| 8345 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 8346 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8347 | return(1); |
| 8348 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 8349 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 8350 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8351 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 8352 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8353 | return(1); |
| 8354 | return(0); |
| 8355 | } |
| 8356 | |
| 8357 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8358 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | * @ctxt: the XPath Parser context |
| 8360 | * |
| 8361 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 8362 | * [17] Argument ::= Expr |
| 8363 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8364 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8365 | * pushed on the stack |
| 8366 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8367 | static void |
| 8368 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8369 | xmlChar *name; |
| 8370 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8371 | int nbargs = 0; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8372 | int sort = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8373 | |
| 8374 | name = xmlXPathParseQName(ctxt, &prefix); |
| 8375 | if (name == NULL) { |
| 8376 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8377 | } |
| 8378 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8379 | #ifdef DEBUG_EXPR |
| 8380 | if (prefix == NULL) |
| 8381 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 8382 | name); |
| 8383 | else |
| 8384 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 8385 | prefix, name); |
| 8386 | #endif |
| 8387 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8388 | if (CUR != '(') { |
| 8389 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8390 | } |
| 8391 | NEXT; |
| 8392 | SKIP_BLANKS; |
| 8393 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8394 | /* |
| 8395 | * Optimization for count(): we don't need the node-set to be sorted. |
| 8396 | */ |
| 8397 | if ((prefix == NULL) && (name[0] == 'c') && |
| 8398 | xmlStrEqual(name, BAD_CAST "count")) |
| 8399 | { |
| 8400 | sort = 0; |
| 8401 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8402 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 8403 | if (CUR != ')') { |
| 8404 | while (CUR != 0) { |
| 8405 | int op1 = ctxt->comp->last; |
| 8406 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8407 | xmlXPathCompileExpr(ctxt, sort); |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 8408 | CHECK_ERROR; |
| 8409 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 8410 | nbargs++; |
| 8411 | if (CUR == ')') break; |
| 8412 | if (CUR != ',') { |
| 8413 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8414 | } |
| 8415 | NEXT; |
| 8416 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8417 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8418 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8419 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 8420 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8421 | NEXT; |
| 8422 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8423 | } |
| 8424 | |
| 8425 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8426 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8427 | * @ctxt: the XPath Parser context |
| 8428 | * |
| 8429 | * [15] PrimaryExpr ::= VariableReference |
| 8430 | * | '(' Expr ')' |
| 8431 | * | Literal |
| 8432 | * | Number |
| 8433 | * | FunctionCall |
| 8434 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8435 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8436 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8437 | static void |
| 8438 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8439 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8440 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8441 | else if (CUR == '(') { |
| 8442 | NEXT; |
| 8443 | SKIP_BLANKS; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8444 | xmlXPathCompileExpr(ctxt, 1); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 8445 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8446 | if (CUR != ')') { |
| 8447 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8448 | } |
| 8449 | NEXT; |
| 8450 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8451 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8452 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8453 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8454 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8455 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8456 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8457 | } |
| 8458 | SKIP_BLANKS; |
| 8459 | } |
| 8460 | |
| 8461 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8462 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8463 | * @ctxt: the XPath Parser context |
| 8464 | * |
| 8465 | * [20] FilterExpr ::= PrimaryExpr |
| 8466 | * | FilterExpr Predicate |
| 8467 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8468 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | * Square brackets are used to filter expressions in the same way that |
| 8470 | * they are used in location paths. It is an error if the expression to |
| 8471 | * be filtered does not evaluate to a node-set. The context node list |
| 8472 | * used for evaluating the expression in square brackets is the node-set |
| 8473 | * to be filtered listed in document order. |
| 8474 | */ |
| 8475 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8476 | static void |
| 8477 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 8478 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8479 | CHECK_ERROR; |
| 8480 | SKIP_BLANKS; |
| 8481 | |
| 8482 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8483 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | SKIP_BLANKS; |
| 8485 | } |
| 8486 | |
| 8487 | |
| 8488 | } |
| 8489 | |
| 8490 | /** |
| 8491 | * xmlXPathScanName: |
| 8492 | * @ctxt: the XPath Parser context |
| 8493 | * |
| 8494 | * Trickery: parse an XML name but without consuming the input flow |
| 8495 | * Needed to avoid insanity in the parser state. |
| 8496 | * |
| 8497 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 8498 | * CombiningChar | Extender |
| 8499 | * |
| 8500 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 8501 | * |
| 8502 | * [6] Names ::= Name (S Name)* |
| 8503 | * |
| 8504 | * Returns the Name parsed or NULL |
| 8505 | */ |
| 8506 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8507 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8508 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8509 | int len = 0, l; |
| 8510 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8511 | const xmlChar *cur; |
| 8512 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8513 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8514 | cur = ctxt->cur; |
| 8515 | |
| 8516 | c = CUR_CHAR(l); |
| 8517 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 8518 | (!IS_LETTER(c) && (c != '_') && |
| 8519 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8520 | return(NULL); |
| 8521 | } |
| 8522 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8523 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 8524 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 8525 | (c == '.') || (c == '-') || |
| 8526 | (c == '_') || (c == ':') || |
| 8527 | (IS_COMBINING(c)) || |
| 8528 | (IS_EXTENDER(c)))) { |
| 8529 | len += l; |
| 8530 | NEXTL(l); |
| 8531 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8532 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8533 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 8534 | ctxt->cur = cur; |
| 8535 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8536 | } |
| 8537 | |
| 8538 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8539 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8540 | * @ctxt: the XPath Parser context |
| 8541 | * |
| 8542 | * [19] PathExpr ::= LocationPath |
| 8543 | * | FilterExpr |
| 8544 | * | FilterExpr '/' RelativeLocationPath |
| 8545 | * | FilterExpr '//' RelativeLocationPath |
| 8546 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8547 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8548 | * The / operator and // operators combine an arbitrary expression |
| 8549 | * and a relative location path. It is an error if the expression |
| 8550 | * does not evaluate to a node-set. |
| 8551 | * The / operator does composition in the same way as when / is |
| 8552 | * used in a location path. As in location paths, // is short for |
| 8553 | * /descendant-or-self::node()/. |
| 8554 | */ |
| 8555 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8556 | static void |
| 8557 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8558 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 8559 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 8560 | |
| 8561 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8562 | if ((CUR == '$') || (CUR == '(') || |
| 8563 | (IS_ASCII_DIGIT(CUR)) || |
| 8564 | (CUR == '\'') || (CUR == '"') || |
| 8565 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8566 | lc = 0; |
| 8567 | } else if (CUR == '*') { |
| 8568 | /* relative or absolute location path */ |
| 8569 | lc = 1; |
| 8570 | } else if (CUR == '/') { |
| 8571 | /* relative or absolute location path */ |
| 8572 | lc = 1; |
| 8573 | } else if (CUR == '@') { |
| 8574 | /* relative abbreviated attribute location path */ |
| 8575 | lc = 1; |
| 8576 | } else if (CUR == '.') { |
| 8577 | /* relative abbreviated attribute location path */ |
| 8578 | lc = 1; |
| 8579 | } else { |
| 8580 | /* |
| 8581 | * Problem is finding if we have a name here whether it's: |
| 8582 | * - a nodetype |
| 8583 | * - a function call in which case it's followed by '(' |
| 8584 | * - an axis in which case it's followed by ':' |
| 8585 | * - a element name |
| 8586 | * We do an a priori analysis here rather than having to |
| 8587 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8588 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8589 | * read/write/debug. |
| 8590 | */ |
| 8591 | SKIP_BLANKS; |
| 8592 | name = xmlXPathScanName(ctxt); |
| 8593 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8594 | #ifdef DEBUG_STEP |
| 8595 | xmlGenericError(xmlGenericErrorContext, |
| 8596 | "PathExpr: Axis\n"); |
| 8597 | #endif |
| 8598 | lc = 1; |
| 8599 | xmlFree(name); |
| 8600 | } else if (name != NULL) { |
| 8601 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | |
| 8603 | |
| 8604 | while (NXT(len) != 0) { |
| 8605 | if (NXT(len) == '/') { |
| 8606 | /* element name */ |
| 8607 | #ifdef DEBUG_STEP |
| 8608 | xmlGenericError(xmlGenericErrorContext, |
| 8609 | "PathExpr: AbbrRelLocation\n"); |
| 8610 | #endif |
| 8611 | lc = 1; |
| 8612 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8613 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8614 | /* ignore blanks */ |
| 8615 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8616 | } else if (NXT(len) == ':') { |
| 8617 | #ifdef DEBUG_STEP |
| 8618 | xmlGenericError(xmlGenericErrorContext, |
| 8619 | "PathExpr: AbbrRelLocation\n"); |
| 8620 | #endif |
| 8621 | lc = 1; |
| 8622 | break; |
| 8623 | } else if ((NXT(len) == '(')) { |
| 8624 | /* Note Type or Function */ |
| 8625 | if (xmlXPathIsNodeType(name)) { |
| 8626 | #ifdef DEBUG_STEP |
| 8627 | xmlGenericError(xmlGenericErrorContext, |
| 8628 | "PathExpr: Type search\n"); |
| 8629 | #endif |
| 8630 | lc = 1; |
| 8631 | } else { |
| 8632 | #ifdef DEBUG_STEP |
| 8633 | xmlGenericError(xmlGenericErrorContext, |
| 8634 | "PathExpr: function call\n"); |
| 8635 | #endif |
| 8636 | lc = 0; |
| 8637 | } |
| 8638 | break; |
| 8639 | } else if ((NXT(len) == '[')) { |
| 8640 | /* element name */ |
| 8641 | #ifdef DEBUG_STEP |
| 8642 | xmlGenericError(xmlGenericErrorContext, |
| 8643 | "PathExpr: AbbrRelLocation\n"); |
| 8644 | #endif |
| 8645 | lc = 1; |
| 8646 | break; |
| 8647 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8648 | (NXT(len) == '=')) { |
| 8649 | lc = 1; |
| 8650 | break; |
| 8651 | } else { |
| 8652 | lc = 1; |
| 8653 | break; |
| 8654 | } |
| 8655 | len++; |
| 8656 | } |
| 8657 | if (NXT(len) == 0) { |
| 8658 | #ifdef DEBUG_STEP |
| 8659 | xmlGenericError(xmlGenericErrorContext, |
| 8660 | "PathExpr: AbbrRelLocation\n"); |
| 8661 | #endif |
| 8662 | /* element name */ |
| 8663 | lc = 1; |
| 8664 | } |
| 8665 | xmlFree(name); |
| 8666 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8667 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8668 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8669 | } |
| 8670 | } |
| 8671 | |
| 8672 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8673 | if (CUR == '/') { |
| 8674 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8675 | } else { |
| 8676 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8677 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8678 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8679 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8680 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8681 | CHECK_ERROR; |
| 8682 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8683 | SKIP(2); |
| 8684 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8685 | |
| 8686 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8687 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8688 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8689 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8690 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8691 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8692 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8693 | } |
| 8694 | } |
| 8695 | SKIP_BLANKS; |
| 8696 | } |
| 8697 | |
| 8698 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8699 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8700 | * @ctxt: the XPath Parser context |
| 8701 | * |
| 8702 | * [18] UnionExpr ::= PathExpr |
| 8703 | * | UnionExpr '|' PathExpr |
| 8704 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8705 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8706 | */ |
| 8707 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8708 | static void |
| 8709 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8710 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8711 | CHECK_ERROR; |
| 8712 | SKIP_BLANKS; |
| 8713 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8714 | int op1 = ctxt->comp->last; |
| 8715 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8716 | |
| 8717 | NEXT; |
| 8718 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8719 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8720 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8721 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8722 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8723 | SKIP_BLANKS; |
| 8724 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8725 | } |
| 8726 | |
| 8727 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8728 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8729 | * @ctxt: the XPath Parser context |
| 8730 | * |
| 8731 | * [27] UnaryExpr ::= UnionExpr |
| 8732 | * | '-' UnaryExpr |
| 8733 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8734 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8735 | */ |
| 8736 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8737 | static void |
| 8738 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8739 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8740 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8741 | |
| 8742 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8743 | while (CUR == '-') { |
| 8744 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8745 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8746 | NEXT; |
| 8747 | SKIP_BLANKS; |
| 8748 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8749 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8750 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8751 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8752 | if (found) { |
| 8753 | if (minus) |
| 8754 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8755 | else |
| 8756 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8757 | } |
| 8758 | } |
| 8759 | |
| 8760 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8761 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8762 | * @ctxt: the XPath Parser context |
| 8763 | * |
| 8764 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8765 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8766 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8767 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8768 | * [34] MultiplyOperator ::= '*' |
| 8769 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8770 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8771 | */ |
| 8772 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8773 | static void |
| 8774 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8775 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8776 | CHECK_ERROR; |
| 8777 | SKIP_BLANKS; |
| 8778 | while ((CUR == '*') || |
| 8779 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8780 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8781 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8782 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8783 | |
| 8784 | if (CUR == '*') { |
| 8785 | op = 0; |
| 8786 | NEXT; |
| 8787 | } else if (CUR == 'd') { |
| 8788 | op = 1; |
| 8789 | SKIP(3); |
| 8790 | } else if (CUR == 'm') { |
| 8791 | op = 2; |
| 8792 | SKIP(3); |
| 8793 | } |
| 8794 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8795 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8796 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8797 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8798 | SKIP_BLANKS; |
| 8799 | } |
| 8800 | } |
| 8801 | |
| 8802 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8803 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8804 | * @ctxt: the XPath Parser context |
| 8805 | * |
| 8806 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8807 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8808 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8809 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8810 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8811 | */ |
| 8812 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8813 | static void |
| 8814 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8815 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8816 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8817 | CHECK_ERROR; |
| 8818 | SKIP_BLANKS; |
| 8819 | while ((CUR == '+') || (CUR == '-')) { |
| 8820 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8821 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8822 | |
| 8823 | if (CUR == '+') plus = 1; |
| 8824 | else plus = 0; |
| 8825 | NEXT; |
| 8826 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8827 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8828 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8829 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8830 | SKIP_BLANKS; |
| 8831 | } |
| 8832 | } |
| 8833 | |
| 8834 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8835 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8836 | * @ctxt: the XPath Parser context |
| 8837 | * |
| 8838 | * [24] RelationalExpr ::= AdditiveExpr |
| 8839 | * | RelationalExpr '<' AdditiveExpr |
| 8840 | * | RelationalExpr '>' AdditiveExpr |
| 8841 | * | RelationalExpr '<=' AdditiveExpr |
| 8842 | * | RelationalExpr '>=' AdditiveExpr |
| 8843 | * |
| 8844 | * A <= B > C is allowed ? Answer from James, yes with |
| 8845 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8846 | * which is basically what got implemented. |
| 8847 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8848 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8849 | * on the stack |
| 8850 | */ |
| 8851 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8852 | static void |
| 8853 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8854 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8855 | CHECK_ERROR; |
| 8856 | SKIP_BLANKS; |
| 8857 | while ((CUR == '<') || |
| 8858 | (CUR == '>') || |
| 8859 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8860 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8861 | int inf, strict; |
| 8862 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8863 | |
| 8864 | if (CUR == '<') inf = 1; |
| 8865 | else inf = 0; |
| 8866 | if (NXT(1) == '=') strict = 0; |
| 8867 | else strict = 1; |
| 8868 | NEXT; |
| 8869 | if (!strict) NEXT; |
| 8870 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8871 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8872 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8873 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8874 | SKIP_BLANKS; |
| 8875 | } |
| 8876 | } |
| 8877 | |
| 8878 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8879 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8880 | * @ctxt: the XPath Parser context |
| 8881 | * |
| 8882 | * [23] EqualityExpr ::= RelationalExpr |
| 8883 | * | EqualityExpr '=' RelationalExpr |
| 8884 | * | EqualityExpr '!=' RelationalExpr |
| 8885 | * |
| 8886 | * A != B != C is allowed ? Answer from James, yes with |
| 8887 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8888 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8889 | * which is basically what got implemented. |
| 8890 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8891 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8892 | * |
| 8893 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8894 | static void |
| 8895 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8896 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8897 | CHECK_ERROR; |
| 8898 | SKIP_BLANKS; |
| 8899 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8900 | int eq; |
| 8901 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8902 | |
| 8903 | if (CUR == '=') eq = 1; |
| 8904 | else eq = 0; |
| 8905 | NEXT; |
| 8906 | if (!eq) NEXT; |
| 8907 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8908 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8909 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8910 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8911 | SKIP_BLANKS; |
| 8912 | } |
| 8913 | } |
| 8914 | |
| 8915 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8916 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8917 | * @ctxt: the XPath Parser context |
| 8918 | * |
| 8919 | * [22] AndExpr ::= EqualityExpr |
| 8920 | * | AndExpr 'and' EqualityExpr |
| 8921 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8922 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8923 | * |
| 8924 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8925 | static void |
| 8926 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8927 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8928 | CHECK_ERROR; |
| 8929 | SKIP_BLANKS; |
| 8930 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8931 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8932 | SKIP(3); |
| 8933 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8934 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8935 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8936 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8937 | SKIP_BLANKS; |
| 8938 | } |
| 8939 | } |
| 8940 | |
| 8941 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8942 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8943 | * @ctxt: the XPath Parser context |
| 8944 | * |
| 8945 | * [14] Expr ::= OrExpr |
| 8946 | * [21] OrExpr ::= AndExpr |
| 8947 | * | OrExpr 'or' AndExpr |
| 8948 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8949 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8950 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8951 | static void |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8952 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8953 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8954 | CHECK_ERROR; |
| 8955 | SKIP_BLANKS; |
| 8956 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8957 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8958 | SKIP(2); |
| 8959 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8960 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8961 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8962 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8963 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8964 | SKIP_BLANKS; |
| 8965 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8966 | if ((sort) && (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE)) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8967 | /* more ops could be optimized too */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8968 | /* |
| 8969 | * This is the main place to eliminate sorting for |
| 8970 | * operations which don't require a sorted node-set. |
| 8971 | * E.g. count(). |
| 8972 | */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8973 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8974 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8975 | } |
| 8976 | |
| 8977 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8978 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8979 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8980 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8981 | * |
| 8982 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8983 | * [9] PredicateExpr ::= Expr |
| 8984 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8985 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8986 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8987 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8988 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8989 | int op1 = ctxt->comp->last; |
| 8990 | |
| 8991 | SKIP_BLANKS; |
| 8992 | if (CUR != '[') { |
| 8993 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8994 | } |
| 8995 | NEXT; |
| 8996 | SKIP_BLANKS; |
| 8997 | |
| 8998 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 8999 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9000 | CHECK_ERROR; |
| 9001 | |
| 9002 | if (CUR != ']') { |
| 9003 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 9004 | } |
| 9005 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9006 | if (filter) |
| 9007 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 9008 | else |
| 9009 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9010 | |
| 9011 | NEXT; |
| 9012 | SKIP_BLANKS; |
| 9013 | } |
| 9014 | |
| 9015 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9016 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9017 | * @ctxt: the XPath Parser context |
| 9018 | * @test: pointer to a xmlXPathTestVal |
| 9019 | * @type: pointer to a xmlXPathTypeVal |
| 9020 | * @prefix: placeholder for a possible name prefix |
| 9021 | * |
| 9022 | * [7] NodeTest ::= NameTest |
| 9023 | * | NodeType '(' ')' |
| 9024 | * | 'processing-instruction' '(' Literal ')' |
| 9025 | * |
| 9026 | * [37] NameTest ::= '*' |
| 9027 | * | NCName ':' '*' |
| 9028 | * | QName |
| 9029 | * [38] NodeType ::= 'comment' |
| 9030 | * | 'text' |
| 9031 | * | 'processing-instruction' |
| 9032 | * | 'node' |
| 9033 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9034 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9035 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9036 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9037 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 9038 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 9039 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9040 | int blanks; |
| 9041 | |
| 9042 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 9043 | STRANGE; |
| 9044 | return(NULL); |
| 9045 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9046 | *type = (xmlXPathTypeVal) 0; |
| 9047 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9048 | *prefix = NULL; |
| 9049 | SKIP_BLANKS; |
| 9050 | |
| 9051 | if ((name == NULL) && (CUR == '*')) { |
| 9052 | /* |
| 9053 | * All elements |
| 9054 | */ |
| 9055 | NEXT; |
| 9056 | *test = NODE_TEST_ALL; |
| 9057 | return(NULL); |
| 9058 | } |
| 9059 | |
| 9060 | if (name == NULL) |
| 9061 | name = xmlXPathParseNCName(ctxt); |
| 9062 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9063 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9066 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9067 | SKIP_BLANKS; |
| 9068 | if (CUR == '(') { |
| 9069 | NEXT; |
| 9070 | /* |
| 9071 | * NodeType or PI search |
| 9072 | */ |
| 9073 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 9074 | *type = NODE_TYPE_COMMENT; |
| 9075 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 9076 | *type = NODE_TYPE_NODE; |
| 9077 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 9078 | *type = NODE_TYPE_PI; |
| 9079 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 9080 | *type = NODE_TYPE_TEXT; |
| 9081 | else { |
| 9082 | if (name != NULL) |
| 9083 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9084 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9085 | } |
| 9086 | |
| 9087 | *test = NODE_TEST_TYPE; |
| 9088 | |
| 9089 | SKIP_BLANKS; |
| 9090 | if (*type == NODE_TYPE_PI) { |
| 9091 | /* |
| 9092 | * Specific case: search a PI by name. |
| 9093 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9094 | if (name != NULL) |
| 9095 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 9096 | name = NULL; |
| 9097 | if (CUR != ')') { |
| 9098 | name = xmlXPathParseLiteral(ctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9099 | CHECK_ERROR NULL; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 9100 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 9101 | SKIP_BLANKS; |
| 9102 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9103 | } |
| 9104 | if (CUR != ')') { |
| 9105 | if (name != NULL) |
| 9106 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9107 | XP_ERRORNULL(XPATH_UNCLOSED_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9108 | } |
| 9109 | NEXT; |
| 9110 | return(name); |
| 9111 | } |
| 9112 | *test = NODE_TEST_NAME; |
| 9113 | if ((!blanks) && (CUR == ':')) { |
| 9114 | NEXT; |
| 9115 | |
| 9116 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9117 | * Since currently the parser context don't have a |
| 9118 | * namespace list associated: |
| 9119 | * The namespace name for this prefix can be computed |
| 9120 | * only at evaluation time. The compilation is done |
| 9121 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9122 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9123 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9124 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 9125 | if (name != NULL) |
| 9126 | xmlFree(name); |
| 9127 | if (*prefix == NULL) { |
| 9128 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 9129 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9130 | #else |
| 9131 | *prefix = name; |
| 9132 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9133 | |
| 9134 | if (CUR == '*') { |
| 9135 | /* |
| 9136 | * All elements |
| 9137 | */ |
| 9138 | NEXT; |
| 9139 | *test = NODE_TEST_ALL; |
| 9140 | return(NULL); |
| 9141 | } |
| 9142 | |
| 9143 | name = xmlXPathParseNCName(ctxt); |
| 9144 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9145 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9146 | } |
| 9147 | } |
| 9148 | return(name); |
| 9149 | } |
| 9150 | |
| 9151 | /** |
| 9152 | * xmlXPathIsAxisName: |
| 9153 | * @name: a preparsed name token |
| 9154 | * |
| 9155 | * [6] AxisName ::= 'ancestor' |
| 9156 | * | 'ancestor-or-self' |
| 9157 | * | 'attribute' |
| 9158 | * | 'child' |
| 9159 | * | 'descendant' |
| 9160 | * | 'descendant-or-self' |
| 9161 | * | 'following' |
| 9162 | * | 'following-sibling' |
| 9163 | * | 'namespace' |
| 9164 | * | 'parent' |
| 9165 | * | 'preceding' |
| 9166 | * | 'preceding-sibling' |
| 9167 | * | 'self' |
| 9168 | * |
| 9169 | * Returns the axis or 0 |
| 9170 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9171 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9172 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9173 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9174 | switch (name[0]) { |
| 9175 | case 'a': |
| 9176 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 9177 | ret = AXIS_ANCESTOR; |
| 9178 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 9179 | ret = AXIS_ANCESTOR_OR_SELF; |
| 9180 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 9181 | ret = AXIS_ATTRIBUTE; |
| 9182 | break; |
| 9183 | case 'c': |
| 9184 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 9185 | ret = AXIS_CHILD; |
| 9186 | break; |
| 9187 | case 'd': |
| 9188 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 9189 | ret = AXIS_DESCENDANT; |
| 9190 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 9191 | ret = AXIS_DESCENDANT_OR_SELF; |
| 9192 | break; |
| 9193 | case 'f': |
| 9194 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 9195 | ret = AXIS_FOLLOWING; |
| 9196 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 9197 | ret = AXIS_FOLLOWING_SIBLING; |
| 9198 | break; |
| 9199 | case 'n': |
| 9200 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 9201 | ret = AXIS_NAMESPACE; |
| 9202 | break; |
| 9203 | case 'p': |
| 9204 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 9205 | ret = AXIS_PARENT; |
| 9206 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 9207 | ret = AXIS_PRECEDING; |
| 9208 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 9209 | ret = AXIS_PRECEDING_SIBLING; |
| 9210 | break; |
| 9211 | case 's': |
| 9212 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 9213 | ret = AXIS_SELF; |
| 9214 | break; |
| 9215 | } |
| 9216 | return(ret); |
| 9217 | } |
| 9218 | |
| 9219 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9220 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9221 | * @ctxt: the XPath Parser context |
| 9222 | * |
| 9223 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 9224 | * | AbbreviatedStep |
| 9225 | * |
| 9226 | * [12] AbbreviatedStep ::= '.' | '..' |
| 9227 | * |
| 9228 | * [5] AxisSpecifier ::= AxisName '::' |
| 9229 | * | AbbreviatedAxisSpecifier |
| 9230 | * |
| 9231 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 9232 | * |
| 9233 | * Modified for XPtr range support as: |
| 9234 | * |
| 9235 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 9236 | * | AbbreviatedStep |
| 9237 | * | 'range-to' '(' Expr ')' Predicate* |
| 9238 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9239 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9240 | * A location step of . is short for self::node(). This is |
| 9241 | * particularly useful in conjunction with //. For example, the |
| 9242 | * location path .//para is short for |
| 9243 | * self::node()/descendant-or-self::node()/child::para |
| 9244 | * and so will select all para descendant elements of the context |
| 9245 | * node. |
| 9246 | * Similarly, a location step of .. is short for parent::node(). |
| 9247 | * For example, ../title is short for parent::node()/child::title |
| 9248 | * and so will select the title children of the parent of the context |
| 9249 | * node. |
| 9250 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9251 | static void |
| 9252 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9253 | #ifdef LIBXML_XPTR_ENABLED |
| 9254 | int rangeto = 0; |
| 9255 | int op2 = -1; |
| 9256 | #endif |
| 9257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9258 | SKIP_BLANKS; |
| 9259 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 9260 | SKIP(2); |
| 9261 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9262 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 9263 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9264 | } else if (CUR == '.') { |
| 9265 | NEXT; |
| 9266 | SKIP_BLANKS; |
| 9267 | } else { |
| 9268 | xmlChar *name = NULL; |
| 9269 | const xmlChar *prefix = NULL; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 9270 | xmlXPathTestVal test = (xmlXPathTestVal) 0; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9271 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 9272 | xmlXPathTypeVal type = (xmlXPathTypeVal) 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9273 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9274 | |
| 9275 | /* |
| 9276 | * The modification needed for XPointer change to the production |
| 9277 | */ |
| 9278 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9279 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9280 | name = xmlXPathParseNCName(ctxt); |
| 9281 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9282 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9283 | xmlFree(name); |
| 9284 | SKIP_BLANKS; |
| 9285 | if (CUR != '(') { |
| 9286 | XP_ERROR(XPATH_EXPR_ERROR); |
| 9287 | } |
| 9288 | NEXT; |
| 9289 | SKIP_BLANKS; |
| 9290 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 9291 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9292 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9293 | CHECK_ERROR; |
| 9294 | |
| 9295 | SKIP_BLANKS; |
| 9296 | if (CUR != ')') { |
| 9297 | XP_ERROR(XPATH_EXPR_ERROR); |
| 9298 | } |
| 9299 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9300 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9301 | goto eval_predicates; |
| 9302 | } |
| 9303 | } |
| 9304 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9305 | if (CUR == '*') { |
| 9306 | axis = AXIS_CHILD; |
| 9307 | } else { |
| 9308 | if (name == NULL) |
| 9309 | name = xmlXPathParseNCName(ctxt); |
| 9310 | if (name != NULL) { |
| 9311 | axis = xmlXPathIsAxisName(name); |
| 9312 | if (axis != 0) { |
| 9313 | SKIP_BLANKS; |
| 9314 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 9315 | SKIP(2); |
| 9316 | xmlFree(name); |
| 9317 | name = NULL; |
| 9318 | } else { |
| 9319 | /* an element name can conflict with an axis one :-\ */ |
| 9320 | axis = AXIS_CHILD; |
| 9321 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9322 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9323 | axis = AXIS_CHILD; |
| 9324 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9325 | } else if (CUR == '@') { |
| 9326 | NEXT; |
| 9327 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9328 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9329 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9330 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | CHECK_ERROR; |
| 9334 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9335 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9336 | if (test == 0) |
| 9337 | return; |
| 9338 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 9339 | if ((prefix != NULL) && (ctxt->context != NULL) && |
| 9340 | (ctxt->context->flags & XML_XPATH_CHECKNS)) { |
| 9341 | if (xmlXPathNsLookup(ctxt->context, prefix) == NULL) { |
| 9342 | xmlXPathErr(ctxt, XPATH_UNDEF_PREFIX_ERROR); |
| 9343 | } |
| 9344 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9345 | #ifdef DEBUG_STEP |
| 9346 | xmlGenericError(xmlGenericErrorContext, |
| 9347 | "Basis : computing new set\n"); |
| 9348 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9349 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9350 | #ifdef DEBUG_STEP |
| 9351 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9352 | if (ctxt->value == NULL) |
| 9353 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 9354 | else if (ctxt->value->nodesetval == NULL) |
| 9355 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 9356 | else |
| 9357 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9358 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9359 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 9360 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9361 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 9362 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9363 | op1 = ctxt->comp->last; |
| 9364 | ctxt->comp->last = -1; |
| 9365 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9366 | SKIP_BLANKS; |
| 9367 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9368 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9369 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9370 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9371 | #ifdef LIBXML_XPTR_ENABLED |
| 9372 | if (rangeto) { |
| 9373 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 9374 | } else |
| 9375 | #endif |
| 9376 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 9377 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9378 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9379 | } |
| 9380 | #ifdef DEBUG_STEP |
| 9381 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 9382 | if (ctxt->value == NULL) |
| 9383 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 9384 | else if (ctxt->value->nodesetval == NULL) |
| 9385 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 9386 | else |
| 9387 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 9388 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9389 | #endif |
| 9390 | } |
| 9391 | |
| 9392 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9393 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9394 | * @ctxt: the XPath Parser context |
| 9395 | * |
| 9396 | * [3] RelativeLocationPath ::= Step |
| 9397 | * | RelativeLocationPath '/' Step |
| 9398 | * | AbbreviatedRelativeLocationPath |
| 9399 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 9400 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9401 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9402 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9403 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9404 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9405 | (xmlXPathParserContextPtr ctxt) { |
| 9406 | SKIP_BLANKS; |
| 9407 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9408 | SKIP(2); |
| 9409 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9410 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 9411 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9412 | } else if (CUR == '/') { |
| 9413 | NEXT; |
| 9414 | SKIP_BLANKS; |
| 9415 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9416 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9417 | SKIP_BLANKS; |
| 9418 | while (CUR == '/') { |
| 9419 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9420 | SKIP(2); |
| 9421 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9422 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9423 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9424 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9425 | } else if (CUR == '/') { |
| 9426 | NEXT; |
| 9427 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9428 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9429 | } |
| 9430 | SKIP_BLANKS; |
| 9431 | } |
| 9432 | } |
| 9433 | |
| 9434 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9435 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9436 | * @ctxt: the XPath Parser context |
| 9437 | * |
| 9438 | * [1] LocationPath ::= RelativeLocationPath |
| 9439 | * | AbsoluteLocationPath |
| 9440 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 9441 | * | AbbreviatedAbsoluteLocationPath |
| 9442 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 9443 | * '//' RelativeLocationPath |
| 9444 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9445 | * Compile a location path |
| 9446 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9447 | * // is short for /descendant-or-self::node()/. For example, |
| 9448 | * //para is short for /descendant-or-self::node()/child::para and |
| 9449 | * so will select any para element in the document (even a para element |
| 9450 | * that is a document element will be selected by //para since the |
| 9451 | * document element node is a child of the root node); div//para is |
| 9452 | * short for div/descendant-or-self::node()/child::para and so will |
| 9453 | * select all para descendants of div children. |
| 9454 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9455 | static void |
| 9456 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9457 | SKIP_BLANKS; |
| 9458 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9459 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9460 | } else { |
| 9461 | while (CUR == '/') { |
| 9462 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 9463 | SKIP(2); |
| 9464 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9465 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 9466 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9467 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9468 | } else if (CUR == '/') { |
| 9469 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9470 | SKIP_BLANKS; |
| 9471 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 9472 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9473 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9474 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9475 | } |
| 9476 | } |
| 9477 | } |
| 9478 | } |
| 9479 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9480 | /************************************************************************ |
| 9481 | * * |
| 9482 | * XPath precompiled expression evaluation * |
| 9483 | * * |
| 9484 | ************************************************************************/ |
| 9485 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9486 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9487 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 9488 | |
| 9489 | /** |
| 9490 | * xmlXPathNodeCollectAndTest: |
| 9491 | * @ctxt: the XPath Parser context |
| 9492 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9493 | * @first: pointer to the first element in document order |
| 9494 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9495 | * |
| 9496 | * This is the function implementing a step: based on the current list |
| 9497 | * 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] | 9498 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9499 | * |
| 9500 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9501 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9502 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9503 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9504 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9505 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9506 | xmlXPathStepOpPtr op, |
| 9507 | xmlNodePtr * first, xmlNodePtr * last) |
| 9508 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9509 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9510 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9511 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9512 | const xmlChar *prefix = op->value4; |
| 9513 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9514 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9515 | |
| 9516 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9517 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9518 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9519 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9520 | xmlNodeSetPtr ret, list; |
| 9521 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9522 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9523 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9524 | xmlNodePtr cur = NULL; |
| 9525 | xmlXPathObjectPtr obj; |
| 9526 | xmlNodeSetPtr nodelist; |
| 9527 | xmlNodePtr tmp; |
| 9528 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9529 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9530 | obj = valuePop(ctxt); |
| 9531 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9532 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9533 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9534 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9535 | if (URI == NULL) { |
| 9536 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9537 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9538 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9539 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9540 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9541 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9542 | #endif |
| 9543 | switch (axis) { |
| 9544 | case AXIS_ANCESTOR: |
| 9545 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9546 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9547 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9548 | first = NULL; |
| 9549 | next = xmlXPathNextAncestor; |
| 9550 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9551 | case AXIS_ANCESTOR_OR_SELF: |
| 9552 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9553 | xmlGenericError(xmlGenericErrorContext, |
| 9554 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9555 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9556 | first = NULL; |
| 9557 | next = xmlXPathNextAncestorOrSelf; |
| 9558 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9559 | case AXIS_ATTRIBUTE: |
| 9560 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9561 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9562 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9563 | first = NULL; |
| 9564 | last = NULL; |
| 9565 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9566 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9567 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9568 | case AXIS_CHILD: |
| 9569 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9570 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9571 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9572 | last = NULL; |
| 9573 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9574 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9575 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9576 | case AXIS_DESCENDANT: |
| 9577 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9578 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9579 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9580 | last = NULL; |
| 9581 | next = xmlXPathNextDescendant; |
| 9582 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9583 | case AXIS_DESCENDANT_OR_SELF: |
| 9584 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9585 | xmlGenericError(xmlGenericErrorContext, |
| 9586 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9587 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9588 | last = NULL; |
| 9589 | next = xmlXPathNextDescendantOrSelf; |
| 9590 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9591 | case AXIS_FOLLOWING: |
| 9592 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9593 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9594 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9595 | last = NULL; |
| 9596 | next = xmlXPathNextFollowing; |
| 9597 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9598 | case AXIS_FOLLOWING_SIBLING: |
| 9599 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9600 | xmlGenericError(xmlGenericErrorContext, |
| 9601 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9602 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9603 | last = NULL; |
| 9604 | next = xmlXPathNextFollowingSibling; |
| 9605 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9606 | case AXIS_NAMESPACE: |
| 9607 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9608 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9609 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9610 | first = NULL; |
| 9611 | last = NULL; |
| 9612 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9613 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9614 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9615 | case AXIS_PARENT: |
| 9616 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9617 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9618 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9619 | first = NULL; |
| 9620 | next = xmlXPathNextParent; |
| 9621 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9622 | case AXIS_PRECEDING: |
| 9623 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9624 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9625 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9626 | first = NULL; |
| 9627 | next = xmlXPathNextPrecedingInternal; |
| 9628 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9629 | case AXIS_PRECEDING_SIBLING: |
| 9630 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9631 | xmlGenericError(xmlGenericErrorContext, |
| 9632 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9633 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9634 | first = NULL; |
| 9635 | next = xmlXPathNextPrecedingSibling; |
| 9636 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9637 | case AXIS_SELF: |
| 9638 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9639 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9640 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9641 | first = NULL; |
| 9642 | last = NULL; |
| 9643 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9644 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9645 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9646 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9647 | if (next == NULL) { |
| 9648 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9649 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9650 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9651 | |
| 9652 | nodelist = obj->nodesetval; |
| 9653 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9654 | xmlXPathFreeObject(obj); |
| 9655 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9656 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9657 | } |
| 9658 | addNode = xmlXPathNodeSetAddUnique; |
| 9659 | ret = NULL; |
| 9660 | #ifdef DEBUG_STEP |
| 9661 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9662 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9663 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9664 | case NODE_TEST_NONE: |
| 9665 | xmlGenericError(xmlGenericErrorContext, |
| 9666 | " searching for none !!!\n"); |
| 9667 | break; |
| 9668 | case NODE_TEST_TYPE: |
| 9669 | xmlGenericError(xmlGenericErrorContext, |
| 9670 | " searching for type %d\n", type); |
| 9671 | break; |
| 9672 | case NODE_TEST_PI: |
| 9673 | xmlGenericError(xmlGenericErrorContext, |
| 9674 | " searching for PI !!!\n"); |
| 9675 | break; |
| 9676 | case NODE_TEST_ALL: |
| 9677 | xmlGenericError(xmlGenericErrorContext, |
| 9678 | " searching for *\n"); |
| 9679 | break; |
| 9680 | case NODE_TEST_NS: |
| 9681 | xmlGenericError(xmlGenericErrorContext, |
| 9682 | " searching for namespace %s\n", |
| 9683 | prefix); |
| 9684 | break; |
| 9685 | case NODE_TEST_NAME: |
| 9686 | xmlGenericError(xmlGenericErrorContext, |
| 9687 | " searching for name %s\n", name); |
| 9688 | if (prefix != NULL) |
| 9689 | xmlGenericError(xmlGenericErrorContext, |
| 9690 | " with namespace %s\n", prefix); |
| 9691 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9692 | } |
| 9693 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9694 | #endif |
| 9695 | /* |
| 9696 | * 2.3 Node Tests |
| 9697 | * - For the attribute axis, the principal node type is attribute. |
| 9698 | * - For the namespace axis, the principal node type is namespace. |
| 9699 | * - For other axes, the principal node type is element. |
| 9700 | * |
| 9701 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9702 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9703 | * select all element children of the context node |
| 9704 | */ |
| 9705 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9706 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9707 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9708 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9709 | cur = NULL; |
| 9710 | list = xmlXPathNodeSetCreate(NULL); |
| 9711 | do { |
| 9712 | cur = next(ctxt, cur); |
| 9713 | if (cur == NULL) |
| 9714 | break; |
| 9715 | if ((first != NULL) && (*first == cur)) |
| 9716 | break; |
| 9717 | if (((t % 256) == 0) && |
| 9718 | (first != NULL) && (*first != NULL) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 9719 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 9720 | (xmlXPathCmpNodesExt(*first, cur) >= 0)) |
| 9721 | #else |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9722 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 9723 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9724 | break; |
| 9725 | if ((last != NULL) && (*last == cur)) |
| 9726 | break; |
| 9727 | if (((t % 256) == 0) && |
| 9728 | (last != NULL) && (*last != NULL) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 9729 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 9730 | (xmlXPathCmpNodesExt(cur, *last) >= 0)) |
| 9731 | #else |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9732 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 9733 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9734 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9735 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9736 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9737 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9738 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9739 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9740 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9741 | ctxt->context->node = tmp; |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9742 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9743 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9744 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9745 | if ((cur->type == type) || |
| 9746 | ((type == NODE_TYPE_NODE) && |
| 9747 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9748 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9749 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9750 | (cur->type == XML_NAMESPACE_DECL) || |
| 9751 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9752 | (cur->type == XML_PI_NODE) || |
| 9753 | (cur->type == XML_COMMENT_NODE) || |
| 9754 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9755 | (cur->type == XML_TEXT_NODE))) || |
| 9756 | ((type == NODE_TYPE_TEXT) && |
| 9757 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9758 | #ifdef DEBUG_STEP |
| 9759 | n++; |
| 9760 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9761 | addNode(list, cur); |
| 9762 | } |
| 9763 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9764 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9765 | if (cur->type == XML_PI_NODE) { |
| 9766 | if ((name != NULL) && |
| 9767 | (!xmlStrEqual(name, cur->name))) |
| 9768 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9769 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9770 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9771 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9772 | addNode(list, cur); |
| 9773 | } |
| 9774 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9775 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9776 | if (axis == AXIS_ATTRIBUTE) { |
| 9777 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9778 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9779 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9780 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9781 | addNode(list, cur); |
| 9782 | } |
| 9783 | } else if (axis == AXIS_NAMESPACE) { |
| 9784 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9785 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9786 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9787 | #endif |
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) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9794 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9795 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9796 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9797 | addNode(list, cur); |
| 9798 | } else if ((cur->ns != NULL) && |
| 9799 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9800 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9801 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9802 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9803 | addNode(list, cur); |
| 9804 | } |
| 9805 | } |
| 9806 | } |
| 9807 | break; |
| 9808 | case NODE_TEST_NS:{ |
| 9809 | TODO; |
| 9810 | break; |
| 9811 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9812 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9813 | switch (cur->type) { |
| 9814 | case XML_ELEMENT_NODE: |
| 9815 | if (xmlStrEqual(name, cur->name)) { |
| 9816 | if (prefix == NULL) { |
| 9817 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9818 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9819 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9820 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9821 | addNode(list, cur); |
| 9822 | } |
| 9823 | } else { |
| 9824 | if ((cur->ns != NULL) && |
| 9825 | (xmlStrEqual(URI, |
| 9826 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9827 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9828 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9829 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9830 | addNode(list, cur); |
| 9831 | } |
| 9832 | } |
| 9833 | } |
| 9834 | break; |
| 9835 | case XML_ATTRIBUTE_NODE:{ |
| 9836 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9837 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9838 | if (xmlStrEqual(name, attr->name)) { |
| 9839 | if (prefix == NULL) { |
| 9840 | if ((attr->ns == NULL) || |
| 9841 | (attr->ns->prefix == NULL)) { |
| 9842 | #ifdef DEBUG_STEP |
| 9843 | n++; |
| 9844 | #endif |
| 9845 | addNode(list, |
| 9846 | (xmlNodePtr) attr); |
| 9847 | } |
| 9848 | } else { |
| 9849 | if ((attr->ns != NULL) && |
| 9850 | (xmlStrEqual(URI, |
| 9851 | attr->ns-> |
| 9852 | href))) { |
| 9853 | #ifdef DEBUG_STEP |
| 9854 | n++; |
| 9855 | #endif |
| 9856 | addNode(list, |
| 9857 | (xmlNodePtr) attr); |
| 9858 | } |
| 9859 | } |
| 9860 | } |
| 9861 | break; |
| 9862 | } |
| 9863 | case XML_NAMESPACE_DECL: |
| 9864 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9865 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9866 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9867 | if ((ns->prefix != NULL) && (name != NULL) |
| 9868 | && (xmlStrEqual(ns->prefix, name))) { |
| 9869 | #ifdef DEBUG_STEP |
| 9870 | n++; |
| 9871 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9872 | xmlXPathNodeSetAddNs(list, |
| 9873 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9874 | } |
| 9875 | } |
| 9876 | break; |
| 9877 | default: |
| 9878 | break; |
| 9879 | } |
| 9880 | break; |
| 9881 | break; |
| 9882 | } |
| 9883 | } while (cur != NULL); |
| 9884 | |
| 9885 | /* |
| 9886 | * If there is some predicate filtering do it now |
| 9887 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9888 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9889 | xmlXPathObjectPtr obj2; |
| 9890 | |
| 9891 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9892 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9893 | CHECK_TYPE0(XPATH_NODESET); |
| 9894 | obj2 = valuePop(ctxt); |
| 9895 | list = obj2->nodesetval; |
| 9896 | obj2->nodesetval = NULL; |
| 9897 | xmlXPathFreeObject(obj2); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9898 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 9899 | xmlXPathFreeObject(obj); |
| 9900 | xmlXPathFreeNodeSet(list); |
| 9901 | return(0); |
| 9902 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9903 | } |
| 9904 | if (ret == NULL) { |
| 9905 | ret = list; |
| 9906 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9907 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9908 | xmlXPathFreeNodeSet(list); |
| 9909 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9910 | } |
| 9911 | ctxt->context->node = tmp; |
| 9912 | #ifdef DEBUG_STEP |
| 9913 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9914 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9915 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9916 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9917 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9918 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9919 | ctxt->value->boolval = 1; |
| 9920 | ctxt->value->user = obj->user; |
| 9921 | obj->user = NULL; |
| 9922 | obj->boolval = 0; |
| 9923 | } |
| 9924 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9925 | return(t); |
| 9926 | } |
| 9927 | |
| 9928 | /** |
| 9929 | * xmlXPathNodeCollectAndTestNth: |
| 9930 | * @ctxt: the XPath Parser context |
| 9931 | * @op: the XPath precompiled step operation |
| 9932 | * @indx: the index to collect |
| 9933 | * @first: pointer to the first element in document order |
| 9934 | * @last: pointer to the last element in document order |
| 9935 | * |
| 9936 | * This is the function implementing a step: based on the current list |
| 9937 | * 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] | 9938 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9939 | * |
| 9940 | * Pushes the new NodeSet resulting from the search. |
| 9941 | * Returns the number of node traversed |
| 9942 | */ |
| 9943 | static int |
| 9944 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9945 | xmlXPathStepOpPtr op, int indx, |
| 9946 | xmlNodePtr * first, xmlNodePtr * last) |
| 9947 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9948 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9949 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9950 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9951 | const xmlChar *prefix = op->value4; |
| 9952 | const xmlChar *name = op->value5; |
| 9953 | const xmlChar *URI = NULL; |
| 9954 | int n = 0, t = 0; |
| 9955 | |
| 9956 | int i; |
| 9957 | xmlNodeSetPtr list; |
| 9958 | xmlXPathTraversalFunction next = NULL; |
| 9959 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9960 | xmlNodePtr cur = NULL; |
| 9961 | xmlXPathObjectPtr obj; |
| 9962 | xmlNodeSetPtr nodelist; |
| 9963 | xmlNodePtr tmp; |
| 9964 | |
| 9965 | CHECK_TYPE0(XPATH_NODESET); |
| 9966 | obj = valuePop(ctxt); |
| 9967 | addNode = xmlXPathNodeSetAdd; |
| 9968 | if (prefix != NULL) { |
| 9969 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9970 | if (URI == NULL) { |
| 9971 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9972 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 9973 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9974 | } |
| 9975 | #ifdef DEBUG_STEP_NTH |
| 9976 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9977 | if (first != NULL) { |
| 9978 | if (*first != NULL) |
| 9979 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9980 | (*first)->name); |
| 9981 | else |
| 9982 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9983 | } |
| 9984 | if (last != NULL) { |
| 9985 | if (*last != NULL) |
| 9986 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9987 | (*last)->name); |
| 9988 | else |
| 9989 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9990 | } |
| 9991 | #endif |
| 9992 | switch (axis) { |
| 9993 | case AXIS_ANCESTOR: |
| 9994 | #ifdef DEBUG_STEP_NTH |
| 9995 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9996 | #endif |
| 9997 | first = NULL; |
| 9998 | next = xmlXPathNextAncestor; |
| 9999 | break; |
| 10000 | case AXIS_ANCESTOR_OR_SELF: |
| 10001 | #ifdef DEBUG_STEP_NTH |
| 10002 | xmlGenericError(xmlGenericErrorContext, |
| 10003 | "axis 'ancestors-or-self' "); |
| 10004 | #endif |
| 10005 | first = NULL; |
| 10006 | next = xmlXPathNextAncestorOrSelf; |
| 10007 | break; |
| 10008 | case AXIS_ATTRIBUTE: |
| 10009 | #ifdef DEBUG_STEP_NTH |
| 10010 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 10011 | #endif |
| 10012 | first = NULL; |
| 10013 | last = NULL; |
| 10014 | next = xmlXPathNextAttribute; |
| 10015 | break; |
| 10016 | case AXIS_CHILD: |
| 10017 | #ifdef DEBUG_STEP_NTH |
| 10018 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 10019 | #endif |
| 10020 | last = NULL; |
| 10021 | next = xmlXPathNextChild; |
| 10022 | break; |
| 10023 | case AXIS_DESCENDANT: |
| 10024 | #ifdef DEBUG_STEP_NTH |
| 10025 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 10026 | #endif |
| 10027 | last = NULL; |
| 10028 | next = xmlXPathNextDescendant; |
| 10029 | break; |
| 10030 | case AXIS_DESCENDANT_OR_SELF: |
| 10031 | #ifdef DEBUG_STEP_NTH |
| 10032 | xmlGenericError(xmlGenericErrorContext, |
| 10033 | "axis 'descendant-or-self' "); |
| 10034 | #endif |
| 10035 | last = NULL; |
| 10036 | next = xmlXPathNextDescendantOrSelf; |
| 10037 | break; |
| 10038 | case AXIS_FOLLOWING: |
| 10039 | #ifdef DEBUG_STEP_NTH |
| 10040 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 10041 | #endif |
| 10042 | last = NULL; |
| 10043 | next = xmlXPathNextFollowing; |
| 10044 | break; |
| 10045 | case AXIS_FOLLOWING_SIBLING: |
| 10046 | #ifdef DEBUG_STEP_NTH |
| 10047 | xmlGenericError(xmlGenericErrorContext, |
| 10048 | "axis 'following-siblings' "); |
| 10049 | #endif |
| 10050 | last = NULL; |
| 10051 | next = xmlXPathNextFollowingSibling; |
| 10052 | break; |
| 10053 | case AXIS_NAMESPACE: |
| 10054 | #ifdef DEBUG_STEP_NTH |
| 10055 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 10056 | #endif |
| 10057 | last = NULL; |
| 10058 | first = NULL; |
| 10059 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 10060 | break; |
| 10061 | case AXIS_PARENT: |
| 10062 | #ifdef DEBUG_STEP_NTH |
| 10063 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 10064 | #endif |
| 10065 | first = NULL; |
| 10066 | next = xmlXPathNextParent; |
| 10067 | break; |
| 10068 | case AXIS_PRECEDING: |
| 10069 | #ifdef DEBUG_STEP_NTH |
| 10070 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 10071 | #endif |
| 10072 | first = NULL; |
| 10073 | next = xmlXPathNextPrecedingInternal; |
| 10074 | break; |
| 10075 | case AXIS_PRECEDING_SIBLING: |
| 10076 | #ifdef DEBUG_STEP_NTH |
| 10077 | xmlGenericError(xmlGenericErrorContext, |
| 10078 | "axis 'preceding-sibling' "); |
| 10079 | #endif |
| 10080 | first = NULL; |
| 10081 | next = xmlXPathNextPrecedingSibling; |
| 10082 | break; |
| 10083 | case AXIS_SELF: |
| 10084 | #ifdef DEBUG_STEP_NTH |
| 10085 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 10086 | #endif |
| 10087 | first = NULL; |
| 10088 | last = NULL; |
| 10089 | next = xmlXPathNextSelf; |
| 10090 | break; |
| 10091 | } |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10092 | if (next == NULL) { |
| 10093 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10094 | return(0); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 10095 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10096 | |
| 10097 | nodelist = obj->nodesetval; |
| 10098 | if (nodelist == NULL) { |
| 10099 | xmlXPathFreeObject(obj); |
| 10100 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 10101 | return(0); |
| 10102 | } |
| 10103 | addNode = xmlXPathNodeSetAddUnique; |
| 10104 | #ifdef DEBUG_STEP_NTH |
| 10105 | xmlGenericError(xmlGenericErrorContext, |
| 10106 | " context contains %d nodes\n", nodelist->nodeNr); |
| 10107 | switch (test) { |
| 10108 | case NODE_TEST_NONE: |
| 10109 | xmlGenericError(xmlGenericErrorContext, |
| 10110 | " searching for none !!!\n"); |
| 10111 | break; |
| 10112 | case NODE_TEST_TYPE: |
| 10113 | xmlGenericError(xmlGenericErrorContext, |
| 10114 | " searching for type %d\n", type); |
| 10115 | break; |
| 10116 | case NODE_TEST_PI: |
| 10117 | xmlGenericError(xmlGenericErrorContext, |
| 10118 | " searching for PI !!!\n"); |
| 10119 | break; |
| 10120 | case NODE_TEST_ALL: |
| 10121 | xmlGenericError(xmlGenericErrorContext, |
| 10122 | " searching for *\n"); |
| 10123 | break; |
| 10124 | case NODE_TEST_NS: |
| 10125 | xmlGenericError(xmlGenericErrorContext, |
| 10126 | " searching for namespace %s\n", |
| 10127 | prefix); |
| 10128 | break; |
| 10129 | case NODE_TEST_NAME: |
| 10130 | xmlGenericError(xmlGenericErrorContext, |
| 10131 | " searching for name %s\n", name); |
| 10132 | if (prefix != NULL) |
| 10133 | xmlGenericError(xmlGenericErrorContext, |
| 10134 | " with namespace %s\n", prefix); |
| 10135 | break; |
| 10136 | } |
| 10137 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 10138 | #endif |
| 10139 | /* |
| 10140 | * 2.3 Node Tests |
| 10141 | * - For the attribute axis, the principal node type is attribute. |
| 10142 | * - For the namespace axis, the principal node type is namespace. |
| 10143 | * - For other axes, the principal node type is element. |
| 10144 | * |
| 10145 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10146 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10147 | * select all element children of the context node |
| 10148 | */ |
| 10149 | tmp = ctxt->context->node; |
| 10150 | list = xmlXPathNodeSetCreate(NULL); |
| 10151 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 10152 | ctxt->context->node = nodelist->nodeTab[i]; |
| 10153 | |
| 10154 | cur = NULL; |
| 10155 | n = 0; |
| 10156 | do { |
| 10157 | cur = next(ctxt, cur); |
| 10158 | if (cur == NULL) |
| 10159 | break; |
| 10160 | if ((first != NULL) && (*first == cur)) |
| 10161 | break; |
| 10162 | if (((t % 256) == 0) && |
| 10163 | (first != NULL) && (*first != NULL) && |
| 10164 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 10165 | break; |
| 10166 | if ((last != NULL) && (*last == cur)) |
| 10167 | break; |
| 10168 | if (((t % 256) == 0) && |
| 10169 | (last != NULL) && (*last != NULL) && |
| 10170 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 10171 | break; |
| 10172 | t++; |
| 10173 | switch (test) { |
| 10174 | case NODE_TEST_NONE: |
| 10175 | ctxt->context->node = tmp; |
| 10176 | STRANGE return(0); |
| 10177 | case NODE_TEST_TYPE: |
| 10178 | if ((cur->type == type) || |
| 10179 | ((type == NODE_TYPE_NODE) && |
| 10180 | ((cur->type == XML_DOCUMENT_NODE) || |
| 10181 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 10182 | (cur->type == XML_ELEMENT_NODE) || |
| 10183 | (cur->type == XML_PI_NODE) || |
| 10184 | (cur->type == XML_COMMENT_NODE) || |
| 10185 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 10186 | (cur->type == XML_TEXT_NODE))) || |
| 10187 | ((type == NODE_TYPE_TEXT) && |
| 10188 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10189 | n++; |
| 10190 | if (n == indx) |
| 10191 | addNode(list, cur); |
| 10192 | } |
| 10193 | break; |
| 10194 | case NODE_TEST_PI: |
| 10195 | if (cur->type == XML_PI_NODE) { |
| 10196 | if ((name != NULL) && |
| 10197 | (!xmlStrEqual(name, cur->name))) |
| 10198 | break; |
| 10199 | n++; |
| 10200 | if (n == indx) |
| 10201 | addNode(list, cur); |
| 10202 | } |
| 10203 | break; |
| 10204 | case NODE_TEST_ALL: |
| 10205 | if (axis == AXIS_ATTRIBUTE) { |
| 10206 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 10207 | n++; |
| 10208 | if (n == indx) |
| 10209 | addNode(list, cur); |
| 10210 | } |
| 10211 | } else if (axis == AXIS_NAMESPACE) { |
| 10212 | if (cur->type == XML_NAMESPACE_DECL) { |
| 10213 | n++; |
| 10214 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 10215 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 10216 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10217 | } |
| 10218 | } else { |
| 10219 | if (cur->type == XML_ELEMENT_NODE) { |
| 10220 | if (prefix == NULL) { |
| 10221 | n++; |
| 10222 | if (n == indx) |
| 10223 | addNode(list, cur); |
| 10224 | } else if ((cur->ns != NULL) && |
| 10225 | (xmlStrEqual(URI, cur->ns->href))) { |
| 10226 | n++; |
| 10227 | if (n == indx) |
| 10228 | addNode(list, cur); |
| 10229 | } |
| 10230 | } |
| 10231 | } |
| 10232 | break; |
| 10233 | case NODE_TEST_NS:{ |
| 10234 | TODO; |
| 10235 | break; |
| 10236 | } |
| 10237 | case NODE_TEST_NAME: |
| 10238 | switch (cur->type) { |
| 10239 | case XML_ELEMENT_NODE: |
| 10240 | if (xmlStrEqual(name, cur->name)) { |
| 10241 | if (prefix == NULL) { |
| 10242 | if (cur->ns == NULL) { |
| 10243 | n++; |
| 10244 | if (n == indx) |
| 10245 | addNode(list, cur); |
| 10246 | } |
| 10247 | } else { |
| 10248 | if ((cur->ns != NULL) && |
| 10249 | (xmlStrEqual(URI, |
| 10250 | cur->ns->href))) { |
| 10251 | n++; |
| 10252 | if (n == indx) |
| 10253 | addNode(list, cur); |
| 10254 | } |
| 10255 | } |
| 10256 | } |
| 10257 | break; |
| 10258 | case XML_ATTRIBUTE_NODE:{ |
| 10259 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 10260 | |
| 10261 | if (xmlStrEqual(name, attr->name)) { |
| 10262 | if (prefix == NULL) { |
| 10263 | if ((attr->ns == NULL) || |
| 10264 | (attr->ns->prefix == NULL)) { |
| 10265 | n++; |
| 10266 | if (n == indx) |
| 10267 | addNode(list, cur); |
| 10268 | } |
| 10269 | } else { |
| 10270 | if ((attr->ns != NULL) && |
| 10271 | (xmlStrEqual(URI, |
| 10272 | attr->ns-> |
| 10273 | href))) { |
| 10274 | n++; |
| 10275 | if (n == indx) |
| 10276 | addNode(list, cur); |
| 10277 | } |
| 10278 | } |
| 10279 | } |
| 10280 | break; |
| 10281 | } |
| 10282 | case XML_NAMESPACE_DECL: |
| 10283 | if (cur->type == XML_NAMESPACE_DECL) { |
| 10284 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 10285 | |
| 10286 | if ((ns->prefix != NULL) && (name != NULL) |
| 10287 | && (xmlStrEqual(ns->prefix, name))) { |
| 10288 | n++; |
| 10289 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 10290 | xmlXPathNodeSetAddNs(list, |
| 10291 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10292 | } |
| 10293 | } |
| 10294 | break; |
| 10295 | default: |
| 10296 | break; |
| 10297 | } |
| 10298 | break; |
| 10299 | break; |
| 10300 | } |
| 10301 | } while (n < indx); |
| 10302 | } |
| 10303 | ctxt->context->node = tmp; |
| 10304 | #ifdef DEBUG_STEP_NTH |
| 10305 | xmlGenericError(xmlGenericErrorContext, |
| 10306 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 10307 | t, list->nodeNr); |
| 10308 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10309 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 10310 | if ((obj->boolval) && (obj->user != NULL)) { |
| 10311 | ctxt->value->boolval = 1; |
| 10312 | ctxt->value->user = obj->user; |
| 10313 | obj->user = NULL; |
| 10314 | obj->boolval = 0; |
| 10315 | } |
| 10316 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10317 | return(t); |
| 10318 | } |
| 10319 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10320 | static int |
| 10321 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 10322 | xmlXPathStepOpPtr op, xmlNodePtr * first); |
| 10323 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10324 | /** |
| 10325 | * xmlXPathCompOpEvalFirst: |
| 10326 | * @ctxt: the XPath parser context with the compiled expression |
| 10327 | * @op: an XPath compiled operation |
| 10328 | * @first: the first elem found so far |
| 10329 | * |
| 10330 | * Evaluate the Precompiled XPath operation searching only the first |
| 10331 | * element in document order |
| 10332 | * |
| 10333 | * Returns the number of examined objects. |
| 10334 | */ |
| 10335 | static int |
| 10336 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 10337 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 10338 | { |
| 10339 | int total = 0, cur; |
| 10340 | xmlXPathCompExprPtr comp; |
| 10341 | xmlXPathObjectPtr arg1, arg2; |
| 10342 | |
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 | comp = ctxt->comp; |
| 10345 | switch (op->op) { |
| 10346 | case XPATH_OP_END: |
| 10347 | return (0); |
| 10348 | case XPATH_OP_UNION: |
| 10349 | total = |
| 10350 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 10351 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10352 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10353 | if ((ctxt->value != NULL) |
| 10354 | && (ctxt->value->type == XPATH_NODESET) |
| 10355 | && (ctxt->value->nodesetval != NULL) |
| 10356 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10357 | /* |
| 10358 | * limit tree traversing to first node in the result |
| 10359 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10360 | /* |
| 10361 | * OPTIMIZE TODO: This implicitely sorts |
| 10362 | * the result, even if not needed. E.g. if the argument |
| 10363 | * of the count() function, no sorting is needed. |
| 10364 | * OPTIMIZE TODO: How do we know if the node-list wasn't |
| 10365 | * aready sorted? |
| 10366 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 10367 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 10368 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10369 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 10370 | } |
| 10371 | cur = |
| 10372 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 10373 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10374 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10375 | CHECK_TYPE0(XPATH_NODESET); |
| 10376 | arg2 = valuePop(ctxt); |
| 10377 | |
| 10378 | CHECK_TYPE0(XPATH_NODESET); |
| 10379 | arg1 = valuePop(ctxt); |
| 10380 | |
| 10381 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10382 | arg2->nodesetval); |
| 10383 | valuePush(ctxt, arg1); |
| 10384 | xmlXPathFreeObject(arg2); |
| 10385 | /* optimizer */ |
| 10386 | if (total > cur) |
| 10387 | xmlXPathCompSwap(op); |
| 10388 | return (total + cur); |
| 10389 | case XPATH_OP_ROOT: |
| 10390 | xmlXPathRoot(ctxt); |
| 10391 | return (0); |
| 10392 | case XPATH_OP_NODE: |
| 10393 | if (op->ch1 != -1) |
| 10394 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10395 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10396 | if (op->ch2 != -1) |
| 10397 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10398 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10399 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10400 | return (total); |
| 10401 | case XPATH_OP_RESET: |
| 10402 | if (op->ch1 != -1) |
| 10403 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10404 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10405 | if (op->ch2 != -1) |
| 10406 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10407 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10408 | ctxt->context->node = NULL; |
| 10409 | return (total); |
| 10410 | case XPATH_OP_COLLECT:{ |
| 10411 | if (op->ch1 == -1) |
| 10412 | return (total); |
| 10413 | |
| 10414 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10415 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10416 | |
| 10417 | /* |
| 10418 | * Optimization for [n] selection where n is a number |
| 10419 | */ |
| 10420 | if ((op->ch2 != -1) && |
| 10421 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10422 | (comp->steps[op->ch2].ch1 == -1) && |
| 10423 | (comp->steps[op->ch2].ch2 != -1) && |
| 10424 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10425 | XPATH_OP_VALUE)) { |
| 10426 | xmlXPathObjectPtr val; |
| 10427 | |
| 10428 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10429 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10430 | int indx = (int) val->floatval; |
| 10431 | |
| 10432 | if (val->floatval == (float) indx) { |
| 10433 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 10434 | first, NULL); |
| 10435 | return (total); |
| 10436 | } |
| 10437 | } |
| 10438 | } |
| 10439 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 10440 | return (total); |
| 10441 | } |
| 10442 | case XPATH_OP_VALUE: |
| 10443 | valuePush(ctxt, |
| 10444 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10445 | return (0); |
| 10446 | case XPATH_OP_SORT: |
| 10447 | if (op->ch1 != -1) |
| 10448 | total += |
| 10449 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 10450 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10451 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10452 | if ((ctxt->value != NULL) |
| 10453 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10454 | && (ctxt->value->nodesetval != NULL) |
| 10455 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10456 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10457 | return (total); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10458 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 10459 | case XPATH_OP_FILTER: |
| 10460 | total =+ xmlXPathCompOpEvalFilterFirst(ctxt, op, first); |
| 10461 | return (total); |
| 10462 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10463 | default: |
| 10464 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10465 | } |
| 10466 | } |
| 10467 | |
| 10468 | /** |
| 10469 | * xmlXPathCompOpEvalLast: |
| 10470 | * @ctxt: the XPath parser context with the compiled expression |
| 10471 | * @op: an XPath compiled operation |
| 10472 | * @last: the last elem found so far |
| 10473 | * |
| 10474 | * Evaluate the Precompiled XPath operation searching only the last |
| 10475 | * element in document order |
| 10476 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10477 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10478 | */ |
| 10479 | static int |
| 10480 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 10481 | xmlNodePtr * last) |
| 10482 | { |
| 10483 | int total = 0, cur; |
| 10484 | xmlXPathCompExprPtr comp; |
| 10485 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10486 | xmlNodePtr bak; |
| 10487 | xmlDocPtr bakd; |
| 10488 | int pp; |
| 10489 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10490 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10491 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10492 | comp = ctxt->comp; |
| 10493 | switch (op->op) { |
| 10494 | case XPATH_OP_END: |
| 10495 | return (0); |
| 10496 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10497 | bakd = ctxt->context->doc; |
| 10498 | bak = ctxt->context->node; |
| 10499 | pp = ctxt->context->proximityPosition; |
| 10500 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10501 | total = |
| 10502 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10503 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10504 | if ((ctxt->value != NULL) |
| 10505 | && (ctxt->value->type == XPATH_NODESET) |
| 10506 | && (ctxt->value->nodesetval != NULL) |
| 10507 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10508 | /* |
| 10509 | * limit tree traversing to first node in the result |
| 10510 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 10511 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 10512 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10513 | *last = |
| 10514 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 10515 | nodesetval->nodeNr - |
| 10516 | 1]; |
| 10517 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10518 | ctxt->context->doc = bakd; |
| 10519 | ctxt->context->node = bak; |
| 10520 | ctxt->context->proximityPosition = pp; |
| 10521 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10522 | cur = |
| 10523 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10524 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10525 | if ((ctxt->value != NULL) |
| 10526 | && (ctxt->value->type == XPATH_NODESET) |
| 10527 | && (ctxt->value->nodesetval != NULL) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 10528 | && (ctxt->value->nodesetval->nodeNr >= 1)) { /* TODO: NOP ? */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10529 | } |
| 10530 | CHECK_TYPE0(XPATH_NODESET); |
| 10531 | arg2 = valuePop(ctxt); |
| 10532 | |
| 10533 | CHECK_TYPE0(XPATH_NODESET); |
| 10534 | arg1 = valuePop(ctxt); |
| 10535 | |
| 10536 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10537 | arg2->nodesetval); |
| 10538 | valuePush(ctxt, arg1); |
| 10539 | xmlXPathFreeObject(arg2); |
| 10540 | /* optimizer */ |
| 10541 | if (total > cur) |
| 10542 | xmlXPathCompSwap(op); |
| 10543 | return (total + cur); |
| 10544 | case XPATH_OP_ROOT: |
| 10545 | xmlXPathRoot(ctxt); |
| 10546 | return (0); |
| 10547 | case XPATH_OP_NODE: |
| 10548 | if (op->ch1 != -1) |
| 10549 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10550 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10551 | if (op->ch2 != -1) |
| 10552 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10553 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10554 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10555 | return (total); |
| 10556 | case XPATH_OP_RESET: |
| 10557 | if (op->ch1 != -1) |
| 10558 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10559 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10560 | if (op->ch2 != -1) |
| 10561 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10562 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10563 | ctxt->context->node = NULL; |
| 10564 | return (total); |
| 10565 | case XPATH_OP_COLLECT:{ |
| 10566 | if (op->ch1 == -1) |
| 10567 | return (0); |
| 10568 | |
| 10569 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10570 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10571 | |
| 10572 | /* |
| 10573 | * Optimization for [n] selection where n is a number |
| 10574 | */ |
| 10575 | if ((op->ch2 != -1) && |
| 10576 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10577 | (comp->steps[op->ch2].ch1 == -1) && |
| 10578 | (comp->steps[op->ch2].ch2 != -1) && |
| 10579 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10580 | XPATH_OP_VALUE)) { |
| 10581 | xmlXPathObjectPtr val; |
| 10582 | |
| 10583 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10584 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10585 | int indx = (int) val->floatval; |
| 10586 | |
| 10587 | if (val->floatval == (float) indx) { |
| 10588 | total += |
| 10589 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10590 | indx, NULL, |
| 10591 | last); |
| 10592 | return (total); |
| 10593 | } |
| 10594 | } |
| 10595 | } |
| 10596 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 10597 | return (total); |
| 10598 | } |
| 10599 | case XPATH_OP_VALUE: |
| 10600 | valuePush(ctxt, |
| 10601 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10602 | return (0); |
| 10603 | case XPATH_OP_SORT: |
| 10604 | if (op->ch1 != -1) |
| 10605 | total += |
| 10606 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 10607 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10608 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10609 | if ((ctxt->value != NULL) |
| 10610 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 10611 | && (ctxt->value->nodesetval != NULL) |
| 10612 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10613 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10614 | return (total); |
| 10615 | default: |
| 10616 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10617 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10618 | } |
| 10619 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10620 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 10621 | static int |
| 10622 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 10623 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 10624 | { |
| 10625 | int total = 0; |
| 10626 | xmlXPathCompExprPtr comp; |
| 10627 | xmlXPathObjectPtr res; |
| 10628 | xmlXPathObjectPtr obj; |
| 10629 | xmlNodeSetPtr oldset; |
| 10630 | xmlNodePtr oldnode; |
| 10631 | xmlDocPtr oldDoc; |
| 10632 | int i; |
| 10633 | |
| 10634 | CHECK_ERROR0; |
| 10635 | comp = ctxt->comp; |
| 10636 | /* |
| 10637 | * Optimization for ()[last()] selection i.e. the last elem |
| 10638 | */ |
| 10639 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10640 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10641 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10642 | int f = comp->steps[op->ch2].ch1; |
| 10643 | |
| 10644 | if ((f != -1) && |
| 10645 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10646 | (comp->steps[f].value5 == NULL) && |
| 10647 | (comp->steps[f].value == 0) && |
| 10648 | (comp->steps[f].value4 != NULL) && |
| 10649 | (xmlStrEqual |
| 10650 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10651 | xmlNodePtr last = NULL; |
| 10652 | |
| 10653 | total += |
| 10654 | xmlXPathCompOpEvalLast(ctxt, |
| 10655 | &comp->steps[op->ch1], |
| 10656 | &last); |
| 10657 | CHECK_ERROR0; |
| 10658 | /* |
| 10659 | * The nodeset should be in document order, |
| 10660 | * Keep only the last value |
| 10661 | */ |
| 10662 | if ((ctxt->value != NULL) && |
| 10663 | (ctxt->value->type == XPATH_NODESET) && |
| 10664 | (ctxt->value->nodesetval != NULL) && |
| 10665 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10666 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10667 | ctxt->value->nodesetval->nodeTab[0] = |
| 10668 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10669 | value-> |
| 10670 | nodesetval-> |
| 10671 | nodeNr - |
| 10672 | 1]; |
| 10673 | ctxt->value->nodesetval->nodeNr = 1; |
| 10674 | *first = *(ctxt->value->nodesetval->nodeTab); |
| 10675 | } |
| 10676 | return (total); |
| 10677 | } |
| 10678 | } |
| 10679 | |
| 10680 | if (op->ch1 != -1) |
| 10681 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10682 | CHECK_ERROR0; |
| 10683 | if (op->ch2 == -1) |
| 10684 | return (total); |
| 10685 | if (ctxt->value == NULL) |
| 10686 | return (total); |
| 10687 | |
| 10688 | #ifdef LIBXML_XPTR_ENABLED |
| 10689 | oldnode = ctxt->context->node; |
| 10690 | /* |
| 10691 | * Hum are we filtering the result of an XPointer expression |
| 10692 | */ |
| 10693 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10694 | xmlXPathObjectPtr tmp = NULL; |
| 10695 | xmlLocationSetPtr newlocset = NULL; |
| 10696 | xmlLocationSetPtr oldlocset; |
| 10697 | |
| 10698 | /* |
| 10699 | * Extract the old locset, and then evaluate the result of the |
| 10700 | * expression for all the element in the locset. use it to grow |
| 10701 | * up a new locset. |
| 10702 | */ |
| 10703 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10704 | obj = valuePop(ctxt); |
| 10705 | oldlocset = obj->user; |
| 10706 | ctxt->context->node = NULL; |
| 10707 | |
| 10708 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10709 | ctxt->context->contextSize = 0; |
| 10710 | ctxt->context->proximityPosition = 0; |
| 10711 | if (op->ch2 != -1) |
| 10712 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 10713 | res = valuePop(ctxt); |
| 10714 | if (res != NULL) |
| 10715 | xmlXPathFreeObject(res); |
| 10716 | valuePush(ctxt, obj); |
| 10717 | CHECK_ERROR0; |
| 10718 | return (total); |
| 10719 | } |
| 10720 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10721 | |
| 10722 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10723 | /* |
| 10724 | * Run the evaluation with a node list made of a |
| 10725 | * single item in the nodelocset. |
| 10726 | */ |
| 10727 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10728 | ctxt->context->contextSize = oldlocset->locNr; |
| 10729 | ctxt->context->proximityPosition = i + 1; |
| 10730 | if (tmp == NULL) |
| 10731 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10732 | else { |
| 10733 | *(tmp->nodesetval->nodeTab) = ctxt->context->node; |
| 10734 | tmp->nodesetval->nodeNr = 1; |
| 10735 | } |
| 10736 | valuePush(ctxt, tmp); |
| 10737 | if (op->ch2 != -1) |
| 10738 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 10739 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10740 | xmlXPathFreeObject(obj); |
| 10741 | return(0); |
| 10742 | } |
| 10743 | /* |
| 10744 | * The result of the evaluation need to be tested to |
| 10745 | * decided whether the filter succeeded or not |
| 10746 | */ |
| 10747 | res = valuePop(ctxt); |
| 10748 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10749 | xmlXPtrLocationSetAdd(newlocset, |
| 10750 | xmlXPathObjectCopy(oldlocset->locTab[i])); |
| 10751 | } |
| 10752 | /* |
| 10753 | * Cleanup |
| 10754 | */ |
| 10755 | if (res != NULL) |
| 10756 | xmlXPathFreeObject(res); |
| 10757 | if (ctxt->value == tmp) { |
| 10758 | valuePop(ctxt); |
| 10759 | tmp->nodesetval->nodeNr = 0; |
| 10760 | /* |
| 10761 | * REVISIT TODO: Don't free the temporary nodeset |
| 10762 | * in order to avoid massive recreation inside this |
| 10763 | * loop. |
| 10764 | */ |
| 10765 | /* xmlXPathFreeObject(res); */ |
| 10766 | } else { |
| 10767 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10768 | } |
| 10769 | ctxt->context->node = NULL; |
| 10770 | /* |
| 10771 | * Only put the first node in the result, then leave. |
| 10772 | */ |
| 10773 | if (newlocset->locNr > 0) { |
| 10774 | *first = (xmlNodePtr) oldlocset->locTab[i]->user; |
| 10775 | break; |
| 10776 | } |
| 10777 | } |
| 10778 | if (tmp != NULL) |
| 10779 | xmlXPathFreeObject(tmp); |
| 10780 | /* |
| 10781 | * The result is used as the new evaluation locset. |
| 10782 | */ |
| 10783 | xmlXPathFreeObject(obj); |
| 10784 | ctxt->context->node = NULL; |
| 10785 | ctxt->context->contextSize = -1; |
| 10786 | ctxt->context->proximityPosition = -1; |
| 10787 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10788 | ctxt->context->node = oldnode; |
| 10789 | return (total); |
| 10790 | } |
| 10791 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10792 | |
| 10793 | /* |
| 10794 | * Extract the old set, and then evaluate the result of the |
| 10795 | * expression for all the element in the set. use it to grow |
| 10796 | * up a new set. |
| 10797 | */ |
| 10798 | CHECK_TYPE0(XPATH_NODESET); |
| 10799 | obj = valuePop(ctxt); |
| 10800 | oldset = obj->nodesetval; |
| 10801 | |
| 10802 | oldnode = ctxt->context->node; |
| 10803 | oldDoc = ctxt->context->doc; |
| 10804 | ctxt->context->node = NULL; |
| 10805 | |
| 10806 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10807 | ctxt->context->contextSize = 0; |
| 10808 | ctxt->context->proximityPosition = 0; |
| 10809 | /* QUESTION TODO: Why was this code commented out? |
| 10810 | if (op->ch2 != -1) |
| 10811 | total += |
| 10812 | xmlXPathCompOpEval(ctxt, |
| 10813 | &comp->steps[op->ch2]); |
| 10814 | CHECK_ERROR0; |
| 10815 | res = valuePop(ctxt); |
| 10816 | if (res != NULL) |
| 10817 | xmlXPathFreeObject(res); |
| 10818 | */ |
| 10819 | valuePush(ctxt, obj); |
| 10820 | ctxt->context->node = oldnode; |
| 10821 | CHECK_ERROR0; |
| 10822 | } else { |
| 10823 | xmlNodeSetPtr newset; |
| 10824 | xmlXPathObjectPtr tmp = NULL; |
| 10825 | /* |
| 10826 | * Initialize the new set. |
| 10827 | * Also set the xpath document in case things like |
| 10828 | * key() evaluation are attempted on the predicate |
| 10829 | */ |
| 10830 | newset = xmlXPathNodeSetCreate(NULL); |
| 10831 | |
| 10832 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10833 | /* |
| 10834 | * Run the evaluation with a node list made of |
| 10835 | * a single item in the nodeset. |
| 10836 | */ |
| 10837 | ctxt->context->node = oldset->nodeTab[i]; |
| 10838 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 10839 | (oldset->nodeTab[i]->doc != NULL)) |
| 10840 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
| 10841 | if (tmp == NULL) |
| 10842 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10843 | else { |
| 10844 | *(tmp->nodesetval->nodeTab) = ctxt->context->node; |
| 10845 | tmp->nodesetval->nodeNr = 1; |
| 10846 | } |
| 10847 | valuePush(ctxt, tmp); |
| 10848 | ctxt->context->contextSize = oldset->nodeNr; |
| 10849 | ctxt->context->proximityPosition = i + 1; |
| 10850 | if (op->ch2 != -1) |
| 10851 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 10852 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10853 | xmlXPathFreeNodeSet(newset); |
| 10854 | xmlXPathFreeObject(obj); |
| 10855 | return(0); |
| 10856 | } |
| 10857 | /* |
| 10858 | * The result of the evaluation needs to be tested to |
| 10859 | * decide whether the filter succeeded or not |
| 10860 | */ |
| 10861 | res = valuePop(ctxt); |
| 10862 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10863 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10864 | } |
| 10865 | /* |
| 10866 | * Cleanup |
| 10867 | */ |
| 10868 | if (res != NULL) |
| 10869 | xmlXPathFreeObject(res); |
| 10870 | if (ctxt->value == tmp) { |
| 10871 | valuePop(ctxt); |
| 10872 | tmp->nodesetval->nodeNr = 0; |
| 10873 | /* |
| 10874 | * REVISIT TODO: Don't free the temporary nodeset |
| 10875 | * in order to avoid massive recreation inside this |
| 10876 | * loop. |
| 10877 | */ |
| 10878 | /* xmlXPathFreeObject(res); */ |
| 10879 | } else { |
| 10880 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10881 | } |
| 10882 | ctxt->context->node = NULL; |
| 10883 | /* |
| 10884 | * Only put the first node in the result, then leave. |
| 10885 | */ |
| 10886 | if (newset->nodeNr > 0) { |
| 10887 | *first = *(newset->nodeTab); |
| 10888 | break; |
| 10889 | } |
| 10890 | } |
| 10891 | if (tmp != NULL) |
| 10892 | xmlXPathFreeObject(tmp); |
| 10893 | /* |
| 10894 | * The result is used as the new evaluation set. |
| 10895 | */ |
| 10896 | xmlXPathFreeObject(obj); |
| 10897 | ctxt->context->node = NULL; |
| 10898 | ctxt->context->contextSize = -1; |
| 10899 | ctxt->context->proximityPosition = -1; |
| 10900 | /* may want to move this past the '}' later */ |
| 10901 | ctxt->context->doc = oldDoc; |
| 10902 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10903 | } |
| 10904 | ctxt->context->node = oldnode; |
| 10905 | return(total); |
| 10906 | } |
| 10907 | #endif /* XP_OPTIMIZED_FILTER_FIRST */ |
| 10908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10909 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10910 | * xmlXPathCompOpEval: |
| 10911 | * @ctxt: the XPath parser context with the compiled expression |
| 10912 | * @op: an XPath compiled operation |
| 10913 | * |
| 10914 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10915 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10916 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10917 | static int |
| 10918 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 10919 | { |
| 10920 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10921 | int equal, ret; |
| 10922 | xmlXPathCompExprPtr comp; |
| 10923 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10924 | xmlNodePtr bak; |
| 10925 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10926 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10927 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10928 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10929 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10930 | comp = ctxt->comp; |
| 10931 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10932 | case XPATH_OP_END: |
| 10933 | return (0); |
| 10934 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10935 | bakd = ctxt->context->doc; |
| 10936 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10937 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10938 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10939 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10940 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10941 | xmlXPathBooleanFunction(ctxt, 1); |
| 10942 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10943 | return (total); |
| 10944 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10945 | ctxt->context->doc = bakd; |
| 10946 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10947 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10948 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10949 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10950 | if (ctxt->error) { |
| 10951 | xmlXPathFreeObject(arg2); |
| 10952 | return(0); |
| 10953 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10954 | xmlXPathBooleanFunction(ctxt, 1); |
| 10955 | arg1 = valuePop(ctxt); |
| 10956 | arg1->boolval &= arg2->boolval; |
| 10957 | valuePush(ctxt, arg1); |
| 10958 | xmlXPathFreeObject(arg2); |
| 10959 | return (total); |
| 10960 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10961 | bakd = ctxt->context->doc; |
| 10962 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10963 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10964 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10965 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10966 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10967 | xmlXPathBooleanFunction(ctxt, 1); |
| 10968 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10969 | return (total); |
| 10970 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10971 | ctxt->context->doc = bakd; |
| 10972 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10973 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10974 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10975 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10976 | if (ctxt->error) { |
| 10977 | xmlXPathFreeObject(arg2); |
| 10978 | return(0); |
| 10979 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10980 | xmlXPathBooleanFunction(ctxt, 1); |
| 10981 | arg1 = valuePop(ctxt); |
| 10982 | arg1->boolval |= arg2->boolval; |
| 10983 | valuePush(ctxt, arg1); |
| 10984 | xmlXPathFreeObject(arg2); |
| 10985 | return (total); |
| 10986 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10987 | bakd = ctxt->context->doc; |
| 10988 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10989 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10990 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10991 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10992 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10993 | ctxt->context->doc = bakd; |
| 10994 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10995 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10996 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10997 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10998 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10999 | if (op->value) |
| 11000 | equal = xmlXPathEqualValues(ctxt); |
| 11001 | else |
| 11002 | equal = xmlXPathNotEqualValues(ctxt); |
| 11003 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11004 | return (total); |
| 11005 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11006 | bakd = ctxt->context->doc; |
| 11007 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11008 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11009 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11010 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11011 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11012 | ctxt->context->doc = bakd; |
| 11013 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11014 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11015 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11016 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11017 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11018 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 11019 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 11020 | return (total); |
| 11021 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11022 | bakd = ctxt->context->doc; |
| 11023 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11024 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11025 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11026 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11027 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11028 | if (op->ch2 != -1) { |
| 11029 | ctxt->context->doc = bakd; |
| 11030 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11031 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11032 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11033 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11034 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11035 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11036 | if (op->value == 0) |
| 11037 | xmlXPathSubValues(ctxt); |
| 11038 | else if (op->value == 1) |
| 11039 | xmlXPathAddValues(ctxt); |
| 11040 | else if (op->value == 2) |
| 11041 | xmlXPathValueFlipSign(ctxt); |
| 11042 | else if (op->value == 3) { |
| 11043 | CAST_TO_NUMBER; |
| 11044 | CHECK_TYPE0(XPATH_NUMBER); |
| 11045 | } |
| 11046 | return (total); |
| 11047 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11048 | bakd = ctxt->context->doc; |
| 11049 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11050 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11051 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11052 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11053 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11054 | ctxt->context->doc = bakd; |
| 11055 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11056 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11057 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11058 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11059 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11060 | if (op->value == 0) |
| 11061 | xmlXPathMultValues(ctxt); |
| 11062 | else if (op->value == 1) |
| 11063 | xmlXPathDivValues(ctxt); |
| 11064 | else if (op->value == 2) |
| 11065 | xmlXPathModValues(ctxt); |
| 11066 | return (total); |
| 11067 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11068 | bakd = ctxt->context->doc; |
| 11069 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11070 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11071 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11072 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11073 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 11074 | ctxt->context->doc = bakd; |
| 11075 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 11076 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 11077 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11078 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11079 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11080 | CHECK_TYPE0(XPATH_NODESET); |
| 11081 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11082 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11083 | CHECK_TYPE0(XPATH_NODESET); |
| 11084 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11085 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 11086 | if ((arg1->nodesetval == NULL) || |
| 11087 | ((arg2->nodesetval != NULL) && |
| 11088 | (arg2->nodesetval->nodeNr != 0))) |
| 11089 | { |
| 11090 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 11091 | arg2->nodesetval); |
| 11092 | } |
| 11093 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11094 | valuePush(ctxt, arg1); |
| 11095 | xmlXPathFreeObject(arg2); |
| 11096 | return (total); |
| 11097 | case XPATH_OP_ROOT: |
| 11098 | xmlXPathRoot(ctxt); |
| 11099 | return (total); |
| 11100 | case XPATH_OP_NODE: |
| 11101 | if (op->ch1 != -1) |
| 11102 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11103 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11104 | if (op->ch2 != -1) |
| 11105 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11106 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11107 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 11108 | return (total); |
| 11109 | case XPATH_OP_RESET: |
| 11110 | if (op->ch1 != -1) |
| 11111 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11112 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11113 | if (op->ch2 != -1) |
| 11114 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11115 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11116 | ctxt->context->node = NULL; |
| 11117 | return (total); |
| 11118 | case XPATH_OP_COLLECT:{ |
| 11119 | if (op->ch1 == -1) |
| 11120 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11121 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11122 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11123 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11124 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11125 | /* |
| 11126 | * Optimization for [n] selection where n is a number |
| 11127 | */ |
| 11128 | if ((op->ch2 != -1) && |
| 11129 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 11130 | (comp->steps[op->ch2].ch1 == -1) && |
| 11131 | (comp->steps[op->ch2].ch2 != -1) && |
| 11132 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 11133 | XPATH_OP_VALUE)) { |
| 11134 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 11135 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11136 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 11137 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 11138 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11139 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11140 | if (val->floatval == (float) indx) { |
| 11141 | total += |
| 11142 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 11143 | indx, NULL, |
| 11144 | NULL); |
| 11145 | return (total); |
| 11146 | } |
| 11147 | } |
| 11148 | } |
| 11149 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 11150 | return (total); |
| 11151 | } |
| 11152 | case XPATH_OP_VALUE: |
| 11153 | valuePush(ctxt, |
| 11154 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 11155 | return (total); |
| 11156 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11157 | xmlXPathObjectPtr val; |
| 11158 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11159 | if (op->ch1 != -1) |
| 11160 | total += |
| 11161 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11162 | if (op->value5 == NULL) { |
| 11163 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 11164 | if (val == NULL) { |
| 11165 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 11166 | return(0); |
| 11167 | } |
| 11168 | valuePush(ctxt, val); |
| 11169 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11170 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11171 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11172 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 11173 | if (URI == NULL) { |
| 11174 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11175 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11176 | op->value4, op->value5); |
| 11177 | return (total); |
| 11178 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11179 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 11180 | op->value4, URI); |
| 11181 | if (val == NULL) { |
| 11182 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 11183 | return(0); |
| 11184 | } |
| 11185 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11186 | } |
| 11187 | return (total); |
| 11188 | } |
| 11189 | case XPATH_OP_FUNCTION:{ |
| 11190 | xmlXPathFunction func; |
| 11191 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11192 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11193 | |
| 11194 | if (op->ch1 != -1) |
| 11195 | total += |
| 11196 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11197 | if (ctxt->valueNr < op->value) { |
| 11198 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11199 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11200 | ctxt->error = XPATH_INVALID_OPERAND; |
| 11201 | return (total); |
| 11202 | } |
| 11203 | for (i = 0; i < op->value; i++) |
| 11204 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 11205 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11206 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11207 | ctxt->error = XPATH_INVALID_OPERAND; |
| 11208 | return (total); |
| 11209 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11210 | if (op->cache != NULL) |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 11211 | XML_CAST_FPTR(func) = op->cache; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11212 | else { |
| 11213 | const xmlChar *URI = NULL; |
| 11214 | |
| 11215 | if (op->value5 == NULL) |
| 11216 | func = |
| 11217 | xmlXPathFunctionLookup(ctxt->context, |
| 11218 | op->value4); |
| 11219 | else { |
| 11220 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 11221 | if (URI == NULL) { |
| 11222 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11223 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11224 | op->value4, op->value5); |
| 11225 | return (total); |
| 11226 | } |
| 11227 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 11228 | op->value4, URI); |
| 11229 | } |
| 11230 | if (func == NULL) { |
| 11231 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11232 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11233 | op->value4); |
| 11234 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11235 | } |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 11236 | op->cache = XML_CAST_FPTR(func); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11237 | op->cacheURI = (void *) URI; |
| 11238 | } |
| 11239 | oldFunc = ctxt->context->function; |
| 11240 | oldFuncURI = ctxt->context->functionURI; |
| 11241 | ctxt->context->function = op->value4; |
| 11242 | ctxt->context->functionURI = op->cacheURI; |
| 11243 | func(ctxt, op->value); |
| 11244 | ctxt->context->function = oldFunc; |
| 11245 | ctxt->context->functionURI = oldFuncURI; |
| 11246 | return (total); |
| 11247 | } |
| 11248 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 11249 | bakd = ctxt->context->doc; |
| 11250 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 11251 | pp = ctxt->context->proximityPosition; |
| 11252 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11253 | if (op->ch1 != -1) |
| 11254 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 11255 | ctxt->context->contextSize = cs; |
| 11256 | ctxt->context->proximityPosition = pp; |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 11257 | ctxt->context->node = bak; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 11258 | ctxt->context->doc = bakd; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11259 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11260 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11261 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11262 | ctxt->context->doc = bakd; |
| 11263 | ctxt->context->node = bak; |
| 11264 | CHECK_ERROR0; |
| 11265 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11266 | return (total); |
| 11267 | case XPATH_OP_PREDICATE: |
| 11268 | case XPATH_OP_FILTER:{ |
| 11269 | xmlXPathObjectPtr res; |
| 11270 | xmlXPathObjectPtr obj, tmp; |
| 11271 | xmlNodeSetPtr newset = NULL; |
| 11272 | xmlNodeSetPtr oldset; |
| 11273 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 11274 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11275 | int i; |
| 11276 | |
| 11277 | /* |
| 11278 | * Optimization for ()[1] selection i.e. the first elem |
| 11279 | */ |
| 11280 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11281 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 11282 | /* |
| 11283 | * FILTER TODO: Can we assume that the inner processing |
| 11284 | * will result in an ordered list if we have an |
| 11285 | * XPATH_OP_FILTER? |
| 11286 | * What about an additional field or flag on |
| 11287 | * xmlXPathObject like @sorted ? This way we wouln'd need |
| 11288 | * to assume anything, so it would be more robust and |
| 11289 | * easier to optimize. |
| 11290 | */ |
| 11291 | ((comp->steps[op->ch1].op == XPATH_OP_SORT) || /* 18 */ |
| 11292 | (comp->steps[op->ch1].op == XPATH_OP_FILTER)) && /* 17 */ |
| 11293 | #else |
| 11294 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 11295 | #endif |
| 11296 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { /* 12 */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11297 | xmlXPathObjectPtr val; |
| 11298 | |
| 11299 | val = comp->steps[op->ch2].value4; |
| 11300 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 11301 | (val->floatval == 1.0)) { |
| 11302 | xmlNodePtr first = NULL; |
| 11303 | |
| 11304 | total += |
| 11305 | xmlXPathCompOpEvalFirst(ctxt, |
| 11306 | &comp->steps[op->ch1], |
| 11307 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11308 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11309 | /* |
| 11310 | * The nodeset should be in document order, |
| 11311 | * Keep only the first value |
| 11312 | */ |
| 11313 | if ((ctxt->value != NULL) && |
| 11314 | (ctxt->value->type == XPATH_NODESET) && |
| 11315 | (ctxt->value->nodesetval != NULL) && |
| 11316 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 11317 | ctxt->value->nodesetval->nodeNr = 1; |
| 11318 | return (total); |
| 11319 | } |
| 11320 | } |
| 11321 | /* |
| 11322 | * Optimization for ()[last()] selection i.e. the last elem |
| 11323 | */ |
| 11324 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 11325 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 11326 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 11327 | int f = comp->steps[op->ch2].ch1; |
| 11328 | |
| 11329 | if ((f != -1) && |
| 11330 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 11331 | (comp->steps[f].value5 == NULL) && |
| 11332 | (comp->steps[f].value == 0) && |
| 11333 | (comp->steps[f].value4 != NULL) && |
| 11334 | (xmlStrEqual |
| 11335 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 11336 | xmlNodePtr last = NULL; |
| 11337 | |
| 11338 | total += |
| 11339 | xmlXPathCompOpEvalLast(ctxt, |
| 11340 | &comp->steps[op->ch1], |
| 11341 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11342 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11343 | /* |
| 11344 | * The nodeset should be in document order, |
| 11345 | * Keep only the last value |
| 11346 | */ |
| 11347 | if ((ctxt->value != NULL) && |
| 11348 | (ctxt->value->type == XPATH_NODESET) && |
| 11349 | (ctxt->value->nodesetval != NULL) && |
| 11350 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 11351 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 11352 | ctxt->value->nodesetval->nodeTab[0] = |
| 11353 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 11354 | value-> |
| 11355 | nodesetval-> |
| 11356 | nodeNr - |
| 11357 | 1]; |
| 11358 | ctxt->value->nodesetval->nodeNr = 1; |
| 11359 | } |
| 11360 | return (total); |
| 11361 | } |
| 11362 | } |
| 11363 | |
| 11364 | if (op->ch1 != -1) |
| 11365 | total += |
| 11366 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11367 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11368 | if (op->ch2 == -1) |
| 11369 | return (total); |
| 11370 | if (ctxt->value == NULL) |
| 11371 | return (total); |
| 11372 | |
| 11373 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11374 | |
| 11375 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11376 | /* |
| 11377 | * Hum are we filtering the result of an XPointer expression |
| 11378 | */ |
| 11379 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 11380 | xmlLocationSetPtr newlocset = NULL; |
| 11381 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11382 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11383 | /* |
| 11384 | * Extract the old locset, and then evaluate the result of the |
| 11385 | * expression for all the element in the locset. use it to grow |
| 11386 | * up a new locset. |
| 11387 | */ |
| 11388 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 11389 | obj = valuePop(ctxt); |
| 11390 | oldlocset = obj->user; |
| 11391 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11392 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11393 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 11394 | ctxt->context->contextSize = 0; |
| 11395 | ctxt->context->proximityPosition = 0; |
| 11396 | if (op->ch2 != -1) |
| 11397 | total += |
| 11398 | xmlXPathCompOpEval(ctxt, |
| 11399 | &comp->steps[op->ch2]); |
| 11400 | res = valuePop(ctxt); |
| 11401 | if (res != NULL) |
| 11402 | xmlXPathFreeObject(res); |
| 11403 | valuePush(ctxt, obj); |
| 11404 | CHECK_ERROR0; |
| 11405 | return (total); |
| 11406 | } |
| 11407 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11408 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11409 | for (i = 0; i < oldlocset->locNr; i++) { |
| 11410 | /* |
| 11411 | * Run the evaluation with a node list made of a |
| 11412 | * single item in the nodelocset. |
| 11413 | */ |
| 11414 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11415 | ctxt->context->contextSize = oldlocset->locNr; |
| 11416 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 11417 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 11418 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11419 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11420 | if (op->ch2 != -1) |
| 11421 | total += |
| 11422 | xmlXPathCompOpEval(ctxt, |
| 11423 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 11424 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11425 | xmlXPathFreeObject(obj); |
| 11426 | return(0); |
| 11427 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11428 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11429 | /* |
| 11430 | * The result of the evaluation need to be tested to |
| 11431 | * decided whether the filter succeeded or not |
| 11432 | */ |
| 11433 | res = valuePop(ctxt); |
| 11434 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 11435 | xmlXPtrLocationSetAdd(newlocset, |
| 11436 | xmlXPathObjectCopy |
| 11437 | (oldlocset->locTab[i])); |
| 11438 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11439 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11440 | /* |
| 11441 | * Cleanup |
| 11442 | */ |
| 11443 | if (res != NULL) |
| 11444 | xmlXPathFreeObject(res); |
| 11445 | if (ctxt->value == tmp) { |
| 11446 | res = valuePop(ctxt); |
| 11447 | xmlXPathFreeObject(res); |
| 11448 | } |
| 11449 | |
| 11450 | ctxt->context->node = NULL; |
| 11451 | } |
| 11452 | |
| 11453 | /* |
| 11454 | * The result is used as the new evaluation locset. |
| 11455 | */ |
| 11456 | xmlXPathFreeObject(obj); |
| 11457 | ctxt->context->node = NULL; |
| 11458 | ctxt->context->contextSize = -1; |
| 11459 | ctxt->context->proximityPosition = -1; |
| 11460 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 11461 | ctxt->context->node = oldnode; |
| 11462 | return (total); |
| 11463 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11464 | #endif /* LIBXML_XPTR_ENABLED */ |
| 11465 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11466 | /* |
| 11467 | * Extract the old set, and then evaluate the result of the |
| 11468 | * expression for all the element in the set. use it to grow |
| 11469 | * up a new set. |
| 11470 | */ |
| 11471 | CHECK_TYPE0(XPATH_NODESET); |
| 11472 | obj = valuePop(ctxt); |
| 11473 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 11474 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11475 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 11476 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11477 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11478 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11479 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 11480 | ctxt->context->contextSize = 0; |
| 11481 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 11482 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11483 | if (op->ch2 != -1) |
| 11484 | total += |
| 11485 | xmlXPathCompOpEval(ctxt, |
| 11486 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11487 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11488 | res = valuePop(ctxt); |
| 11489 | if (res != NULL) |
| 11490 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 11491 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11492 | valuePush(ctxt, obj); |
| 11493 | ctxt->context->node = oldnode; |
| 11494 | CHECK_ERROR0; |
| 11495 | } else { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11496 | tmp = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11497 | /* |
| 11498 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 11499 | * Also set the xpath document in case things like |
| 11500 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11501 | */ |
| 11502 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11503 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11504 | for (i = 0; i < oldset->nodeNr; i++) { |
| 11505 | /* |
| 11506 | * Run the evaluation with a node list made of |
| 11507 | * a single item in the nodeset. |
| 11508 | */ |
| 11509 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 11510 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 11511 | (oldset->nodeTab[i]->doc != NULL)) |
| 11512 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11513 | if (tmp == NULL) |
| 11514 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 11515 | else { |
| 11516 | *(tmp->nodesetval->nodeTab) = ctxt->context->node; |
| 11517 | tmp->nodesetval->nodeNr = 1; |
| 11518 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11519 | valuePush(ctxt, tmp); |
| 11520 | ctxt->context->contextSize = oldset->nodeNr; |
| 11521 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11522 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11523 | if (op->ch2 != -1) |
| 11524 | total += |
| 11525 | xmlXPathCompOpEval(ctxt, |
| 11526 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 11527 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11528 | xmlXPathFreeNodeSet(newset); |
| 11529 | xmlXPathFreeObject(obj); |
| 11530 | return(0); |
| 11531 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11532 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11533 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11534 | * The result of the evaluation needs to be tested to |
| 11535 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11536 | */ |
| 11537 | res = valuePop(ctxt); |
| 11538 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 11539 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 11540 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11541 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11542 | /* |
| 11543 | * Cleanup |
| 11544 | */ |
| 11545 | if (res != NULL) |
| 11546 | xmlXPathFreeObject(res); |
| 11547 | if (ctxt->value == tmp) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11548 | valuePop(ctxt); |
| 11549 | tmp->nodesetval->nodeNr = 0; |
| 11550 | /* |
| 11551 | * REVISIT TODO: Don't free the temporary nodeset |
| 11552 | * in order to avoid massive recreation inside this |
| 11553 | * loop. |
| 11554 | */ |
| 11555 | /* xmlXPathFreeObject(res); */ |
| 11556 | } else |
| 11557 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11558 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11559 | ctxt->context->node = NULL; |
| 11560 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11561 | if (tmp != NULL) |
| 11562 | xmlXPathFreeObject(tmp); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11563 | |
| 11564 | /* |
| 11565 | * The result is used as the new evaluation set. |
| 11566 | */ |
| 11567 | xmlXPathFreeObject(obj); |
| 11568 | ctxt->context->node = NULL; |
| 11569 | ctxt->context->contextSize = -1; |
| 11570 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 11571 | /* may want to move this past the '}' later */ |
| 11572 | ctxt->context->doc = oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11573 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 11574 | } |
| 11575 | ctxt->context->node = oldnode; |
| 11576 | return (total); |
| 11577 | } |
| 11578 | case XPATH_OP_SORT: |
| 11579 | if (op->ch1 != -1) |
| 11580 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 11581 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11582 | if ((ctxt->value != NULL) && |
| 11583 | (ctxt->value->type == XPATH_NODESET) && |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 11584 | (ctxt->value->nodesetval != NULL) && |
| 11585 | (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11586 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 11587 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11588 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11589 | case XPATH_OP_RANGETO:{ |
| 11590 | xmlXPathObjectPtr range; |
| 11591 | xmlXPathObjectPtr res, obj; |
| 11592 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11593 | xmlLocationSetPtr newlocset = NULL; |
| 11594 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11595 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11596 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11597 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11598 | if (op->ch1 != -1) |
| 11599 | total += |
| 11600 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 11601 | if (op->ch2 == -1) |
| 11602 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11603 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11604 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 11605 | /* |
| 11606 | * Extract the old locset, and then evaluate the result of the |
| 11607 | * expression for all the element in the locset. use it to grow |
| 11608 | * up a new locset. |
| 11609 | */ |
| 11610 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 11611 | obj = valuePop(ctxt); |
| 11612 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11613 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11614 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11615 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11616 | ctxt->context->contextSize = 0; |
| 11617 | ctxt->context->proximityPosition = 0; |
| 11618 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 11619 | res = valuePop(ctxt); |
| 11620 | if (res != NULL) |
| 11621 | xmlXPathFreeObject(res); |
| 11622 | valuePush(ctxt, obj); |
| 11623 | CHECK_ERROR0; |
| 11624 | return (total); |
| 11625 | } |
| 11626 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11627 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11628 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11629 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11630 | * Run the evaluation with a node list made of a |
| 11631 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11632 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 11633 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 11634 | ctxt->context->contextSize = oldlocset->locNr; |
| 11635 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11636 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 11637 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11638 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11639 | if (op->ch2 != -1) |
| 11640 | total += |
| 11641 | xmlXPathCompOpEval(ctxt, |
| 11642 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 11643 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11644 | xmlXPathFreeObject(obj); |
| 11645 | return(0); |
| 11646 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11647 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11648 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11649 | if (res->type == XPATH_LOCATIONSET) { |
| 11650 | xmlLocationSetPtr rloc = |
| 11651 | (xmlLocationSetPtr)res->user; |
| 11652 | for (j=0; j<rloc->locNr; j++) { |
| 11653 | range = xmlXPtrNewRange( |
| 11654 | oldlocset->locTab[i]->user, |
| 11655 | oldlocset->locTab[i]->index, |
| 11656 | rloc->locTab[j]->user2, |
| 11657 | rloc->locTab[j]->index2); |
| 11658 | if (range != NULL) { |
| 11659 | xmlXPtrLocationSetAdd(newlocset, range); |
| 11660 | } |
| 11661 | } |
| 11662 | } else { |
| 11663 | range = xmlXPtrNewRangeNodeObject( |
| 11664 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 11665 | if (range != NULL) { |
| 11666 | xmlXPtrLocationSetAdd(newlocset,range); |
| 11667 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11668 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11669 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11670 | /* |
| 11671 | * Cleanup |
| 11672 | */ |
| 11673 | if (res != NULL) |
| 11674 | xmlXPathFreeObject(res); |
| 11675 | if (ctxt->value == tmp) { |
| 11676 | res = valuePop(ctxt); |
| 11677 | xmlXPathFreeObject(res); |
| 11678 | } |
| 11679 | |
| 11680 | ctxt->context->node = NULL; |
| 11681 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11682 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11683 | CHECK_TYPE0(XPATH_NODESET); |
| 11684 | obj = valuePop(ctxt); |
| 11685 | oldset = obj->nodesetval; |
| 11686 | ctxt->context->node = NULL; |
| 11687 | |
| 11688 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 11689 | |
| 11690 | if (oldset != NULL) { |
| 11691 | for (i = 0; i < oldset->nodeNr; i++) { |
| 11692 | /* |
| 11693 | * Run the evaluation with a node list made of a single item |
| 11694 | * in the nodeset. |
| 11695 | */ |
| 11696 | ctxt->context->node = oldset->nodeTab[i]; |
| 11697 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 11698 | valuePush(ctxt, tmp); |
| 11699 | |
| 11700 | if (op->ch2 != -1) |
| 11701 | total += |
| 11702 | xmlXPathCompOpEval(ctxt, |
| 11703 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 11704 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11705 | xmlXPathFreeObject(obj); |
| 11706 | return(0); |
| 11707 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11708 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11709 | res = valuePop(ctxt); |
| 11710 | range = |
| 11711 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 11712 | res); |
| 11713 | if (range != NULL) { |
| 11714 | xmlXPtrLocationSetAdd(newlocset, range); |
| 11715 | } |
| 11716 | |
| 11717 | /* |
| 11718 | * Cleanup |
| 11719 | */ |
| 11720 | if (res != NULL) |
| 11721 | xmlXPathFreeObject(res); |
| 11722 | if (ctxt->value == tmp) { |
| 11723 | res = valuePop(ctxt); |
| 11724 | xmlXPathFreeObject(res); |
| 11725 | } |
| 11726 | |
| 11727 | ctxt->context->node = NULL; |
| 11728 | } |
| 11729 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11730 | } |
| 11731 | |
| 11732 | /* |
| 11733 | * The result is used as the new evaluation set. |
| 11734 | */ |
| 11735 | xmlXPathFreeObject(obj); |
| 11736 | ctxt->context->node = NULL; |
| 11737 | ctxt->context->contextSize = -1; |
| 11738 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11739 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11740 | return (total); |
| 11741 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11742 | #endif /* LIBXML_XPTR_ENABLED */ |
| 11743 | } |
| 11744 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11745 | "XPath: unknown precompiled operation %d\n", op->op); |
| 11746 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11747 | } |
| 11748 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11749 | #ifdef XPATH_STREAMING |
| 11750 | /** |
| 11751 | * xmlXPathRunStreamEval: |
| 11752 | * @ctxt: the XPath parser context with the compiled expression |
| 11753 | * |
| 11754 | * Evaluate the Precompiled Streamable XPath expression in the given context. |
| 11755 | */ |
| 11756 | static xmlXPathObjectPtr |
| 11757 | xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp) { |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 11758 | int max_depth, min_depth; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11759 | int from_root; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11760 | int ret, depth; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11761 | #ifdef XP_PATTERN_TO_ANY_NODE_ENABLED |
| 11762 | int eval_all_nodes; |
| 11763 | #endif |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 11764 | xmlNodePtr cur = NULL, limit = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11765 | xmlXPathObjectPtr retval; |
| 11766 | xmlStreamCtxtPtr patstream; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11767 | |
| 11768 | int nb_nodes = 0; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11769 | |
| 11770 | if ((ctxt == NULL) || (comp == NULL)) |
| 11771 | return(NULL); |
| 11772 | max_depth = xmlPatternMaxDepth(comp); |
| 11773 | if (max_depth == -1) |
| 11774 | return(NULL); |
| 11775 | if (max_depth == -2) |
| 11776 | max_depth = 10000; |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 11777 | min_depth = xmlPatternMinDepth(comp); |
| 11778 | if (min_depth == -1) |
| 11779 | return(NULL); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11780 | from_root = xmlPatternFromRoot(comp); |
| 11781 | if (from_root < 0) |
| 11782 | return(NULL); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11783 | #if 0 |
| 11784 | printf("stream eval: depth %d from root %d\n", max_depth, from_root); |
| 11785 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11786 | |
| 11787 | retval = xmlXPathNewNodeSet(NULL); |
| 11788 | if (retval == NULL) |
| 11789 | return(NULL); |
| 11790 | |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 11791 | /* |
| 11792 | * handle the special cases of / amd . being matched |
| 11793 | */ |
| 11794 | if (min_depth == 0) { |
| 11795 | if (from_root) { |
| 11796 | xmlXPathNodeSetAddUnique(retval->nodesetval, (xmlNodePtr) ctxt->doc); |
| 11797 | } else { |
| 11798 | xmlXPathNodeSetAddUnique(retval->nodesetval, ctxt->node); |
| 11799 | } |
| 11800 | } |
| 11801 | if (max_depth == 0) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11802 | return(retval); |
| 11803 | } |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 11804 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11805 | if (from_root) { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 11806 | cur = (xmlNodePtr)ctxt->doc; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11807 | } else if (ctxt->node != NULL) { |
| 11808 | switch (ctxt->node->type) { |
| 11809 | case XML_ELEMENT_NODE: |
| 11810 | case XML_DOCUMENT_NODE: |
| 11811 | case XML_DOCUMENT_FRAG_NODE: |
| 11812 | case XML_HTML_DOCUMENT_NODE: |
| 11813 | #ifdef LIBXML_DOCB_ENABLED |
| 11814 | case XML_DOCB_DOCUMENT_NODE: |
| 11815 | #endif |
| 11816 | cur = ctxt->node; |
| 11817 | break; |
| 11818 | case XML_ATTRIBUTE_NODE: |
| 11819 | case XML_TEXT_NODE: |
| 11820 | case XML_CDATA_SECTION_NODE: |
| 11821 | case XML_ENTITY_REF_NODE: |
| 11822 | case XML_ENTITY_NODE: |
| 11823 | case XML_PI_NODE: |
| 11824 | case XML_COMMENT_NODE: |
| 11825 | case XML_NOTATION_NODE: |
| 11826 | case XML_DTD_NODE: |
| 11827 | case XML_DOCUMENT_TYPE_NODE: |
| 11828 | case XML_ELEMENT_DECL: |
| 11829 | case XML_ATTRIBUTE_DECL: |
| 11830 | case XML_ENTITY_DECL: |
| 11831 | case XML_NAMESPACE_DECL: |
| 11832 | case XML_XINCLUDE_START: |
| 11833 | case XML_XINCLUDE_END: |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11834 | break; |
| 11835 | } |
| 11836 | limit = cur; |
| 11837 | } |
| 11838 | if (cur == NULL) |
| 11839 | return(retval); |
| 11840 | |
| 11841 | patstream = xmlPatternGetStreamCtxt(comp); |
| 11842 | if (patstream == NULL) { |
| 11843 | return(retval); |
| 11844 | } |
| 11845 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11846 | #ifdef XP_PATTERN_TO_ANY_NODE_ENABLED |
| 11847 | eval_all_nodes = xmlStreamWantsAnyNode(patstream); |
| 11848 | #endif |
| 11849 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11850 | if (from_root) { |
| 11851 | ret = xmlStreamPush(patstream, NULL, NULL); |
| 11852 | if (ret < 0) { |
| 11853 | } else if (ret == 1) { |
| 11854 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11855 | } |
| 11856 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11857 | depth = 0; |
| 11858 | goto scan_children; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11859 | next_node: |
Daniel Veillard | d3ff7ef | 2006-02-27 19:43:17 +0000 | [diff] [blame] | 11860 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11861 | nb_nodes++; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11862 | |
| 11863 | switch (cur->type) { |
| 11864 | case XML_ELEMENT_NODE: |
| 11865 | #ifdef XP_PATTERN_TO_ANY_NODE_ENABLED |
| 11866 | case XML_TEXT_NODE: |
| 11867 | case XML_CDATA_SECTION_NODE: |
| 11868 | case XML_COMMENT_NODE: |
| 11869 | case XML_PI_NODE: |
| 11870 | #endif |
| 11871 | if (cur->type == XML_ELEMENT_NODE) { |
| 11872 | ret = xmlStreamPush(patstream, cur->name, |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11873 | (cur->ns ? cur->ns->href : NULL)); |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 11874 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11875 | #ifdef XP_PATTERN_TO_ANY_NODE_ENABLED |
| 11876 | else if (eval_all_nodes) |
| 11877 | ret = xmlStreamPushNode(patstream, NULL, NULL, cur->type); |
| 11878 | else |
| 11879 | break; |
| 11880 | #endif |
| 11881 | |
| 11882 | if (ret < 0) { |
| 11883 | /* NOP. */ |
| 11884 | } else if (ret == 1) { |
| 11885 | xmlXPathNodeSetAddUnique(retval->nodesetval, cur); |
| 11886 | } |
| 11887 | if ((cur->children == NULL) || (depth >= max_depth)) { |
| 11888 | ret = xmlStreamPop(patstream); |
| 11889 | while (cur->next != NULL) { |
| 11890 | cur = cur->next; |
| 11891 | if ((cur->type != XML_ENTITY_DECL) && |
| 11892 | (cur->type != XML_DTD_NODE)) |
| 11893 | goto next_node; |
| 11894 | } |
| 11895 | } |
| 11896 | default: |
| 11897 | break; |
| 11898 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11899 | |
| 11900 | scan_children: |
| 11901 | if ((cur->children != NULL) && (depth < max_depth)) { |
| 11902 | /* |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11903 | * Do not descend on entities declarations |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11904 | */ |
| 11905 | if (cur->children->type != XML_ENTITY_DECL) { |
| 11906 | cur = cur->children; |
| 11907 | depth++; |
| 11908 | /* |
| 11909 | * Skip DTDs |
| 11910 | */ |
| 11911 | if (cur->type != XML_DTD_NODE) |
| 11912 | continue; |
| 11913 | } |
| 11914 | } |
| 11915 | |
| 11916 | if (cur == limit) |
| 11917 | break; |
| 11918 | |
| 11919 | while (cur->next != NULL) { |
| 11920 | cur = cur->next; |
| 11921 | if ((cur->type != XML_ENTITY_DECL) && |
| 11922 | (cur->type != XML_DTD_NODE)) |
| 11923 | goto next_node; |
| 11924 | } |
| 11925 | |
| 11926 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11927 | cur = cur->parent; |
| 11928 | depth--; |
| 11929 | if ((cur == NULL) || (cur == limit)) |
| 11930 | goto done; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 11931 | if (cur->type == XML_ELEMENT_NODE) { |
| 11932 | ret = xmlStreamPop(patstream); |
| 11933 | } |
| 11934 | #ifdef XP_PATTERN_TO_ANY_NODE_ENABLED |
| 11935 | else if ((eval_all_nodes) && |
| 11936 | ((cur->type == XML_TEXT_NODE) || |
| 11937 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 11938 | (cur->type == XML_COMMENT_NODE) || |
| 11939 | (cur->type == XML_PI_NODE))) |
| 11940 | { |
| 11941 | ret = xmlStreamPop(patstream); |
| 11942 | } |
| 11943 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11944 | if (cur->next != NULL) { |
| 11945 | cur = cur->next; |
| 11946 | break; |
| 11947 | } |
| 11948 | } while (cur != NULL); |
| 11949 | |
| 11950 | } while ((cur != NULL) && (depth >= 0)); |
| 11951 | done: |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 11952 | #if 0 |
| 11953 | printf("stream eval: checked %d nodes selected %d\n", |
| 11954 | nb_nodes, retval->nodesetval->nodeNr); |
| 11955 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11956 | xmlFreeStreamCtxt(patstream); |
| 11957 | return(retval); |
| 11958 | } |
| 11959 | #endif /* XPATH_STREAMING */ |
| 11960 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11961 | /** |
| 11962 | * xmlXPathRunEval: |
| 11963 | * @ctxt: the XPath parser context with the compiled expression |
| 11964 | * |
| 11965 | * Evaluate the Precompiled XPath expression in the given context. |
| 11966 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11967 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11968 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 11969 | xmlXPathCompExprPtr comp; |
| 11970 | |
| 11971 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 11972 | return; |
| 11973 | |
| 11974 | if (ctxt->valueTab == NULL) { |
| 11975 | /* Allocate the value stack */ |
| 11976 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 11977 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 11978 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 11979 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11980 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11981 | } |
| 11982 | ctxt->valueNr = 0; |
| 11983 | ctxt->valueMax = 10; |
| 11984 | ctxt->value = NULL; |
| 11985 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 11986 | #ifdef XPATH_STREAMING |
| 11987 | if (ctxt->comp->stream) { |
| 11988 | xmlXPathObjectPtr ret; |
| 11989 | ret = xmlXPathRunStreamEval(ctxt->context, ctxt->comp->stream); |
| 11990 | if (ret != NULL) { |
| 11991 | valuePush(ctxt, ret); |
| 11992 | return; |
| 11993 | } |
| 11994 | } |
| 11995 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11996 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 11997 | if(comp->last < 0) { |
| 11998 | xmlGenericError(xmlGenericErrorContext, |
| 11999 | "xmlXPathRunEval: last is less than zero\n"); |
| 12000 | return; |
| 12001 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12002 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 12003 | } |
| 12004 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12005 | /************************************************************************ |
| 12006 | * * |
| 12007 | * Public interfaces * |
| 12008 | * * |
| 12009 | ************************************************************************/ |
| 12010 | |
| 12011 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 12012 | * xmlXPathEvalPredicate: |
| 12013 | * @ctxt: the XPath context |
| 12014 | * @res: the Predicate Expression evaluation result |
| 12015 | * |
| 12016 | * Evaluate a predicate result for the current node. |
| 12017 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 12018 | * the result to a boolean. If the result is a number, the result will |
| 12019 | * be converted to true if the number is equal to the position of the |
| 12020 | * context node in the context node list (as returned by the position |
| 12021 | * function) and will be converted to false otherwise; if the result |
| 12022 | * is not a number, then the result will be converted as if by a call |
| 12023 | * to the boolean function. |
| 12024 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12025 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 12026 | */ |
| 12027 | int |
| 12028 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12029 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 12030 | switch (res->type) { |
| 12031 | case XPATH_BOOLEAN: |
| 12032 | return(res->boolval); |
| 12033 | case XPATH_NUMBER: |
| 12034 | return(res->floatval == ctxt->proximityPosition); |
| 12035 | case XPATH_NODESET: |
| 12036 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12037 | if (res->nodesetval == NULL) |
| 12038 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 12039 | return(res->nodesetval->nodeNr != 0); |
| 12040 | case XPATH_STRING: |
| 12041 | return((res->stringval != NULL) && |
| 12042 | (xmlStrlen(res->stringval) != 0)); |
| 12043 | default: |
| 12044 | STRANGE |
| 12045 | } |
| 12046 | return(0); |
| 12047 | } |
| 12048 | |
| 12049 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12050 | * xmlXPathEvaluatePredicateResult: |
| 12051 | * @ctxt: the XPath Parser context |
| 12052 | * @res: the Predicate Expression evaluation result |
| 12053 | * |
| 12054 | * Evaluate a predicate result for the current node. |
| 12055 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 12056 | * the result to a boolean. If the result is a number, the result will |
| 12057 | * be converted to true if the number is equal to the position of the |
| 12058 | * context node in the context node list (as returned by the position |
| 12059 | * function) and will be converted to false otherwise; if the result |
| 12060 | * is not a number, then the result will be converted as if by a call |
| 12061 | * to the boolean function. |
| 12062 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12063 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12064 | */ |
| 12065 | int |
| 12066 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 12067 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12068 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12069 | switch (res->type) { |
| 12070 | case XPATH_BOOLEAN: |
| 12071 | return(res->boolval); |
| 12072 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 12073 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 12074 | return((res->floatval == ctxt->context->proximityPosition) && |
| 12075 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 12076 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12077 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 12078 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12079 | case XPATH_NODESET: |
| 12080 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 12081 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 12082 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12083 | return(res->nodesetval->nodeNr != 0); |
| 12084 | case XPATH_STRING: |
| 12085 | return((res->stringval != NULL) && |
| 12086 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 12087 | #ifdef LIBXML_XPTR_ENABLED |
| 12088 | case XPATH_LOCATIONSET:{ |
| 12089 | xmlLocationSetPtr ptr = res->user; |
| 12090 | if (ptr == NULL) |
| 12091 | return(0); |
| 12092 | return (ptr->locNr != 0); |
| 12093 | } |
| 12094 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12095 | default: |
| 12096 | STRANGE |
| 12097 | } |
| 12098 | return(0); |
| 12099 | } |
| 12100 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12101 | #ifdef XPATH_STREAMING |
| 12102 | /** |
| 12103 | * xmlXPathTryStreamCompile: |
| 12104 | * @ctxt: an XPath context |
| 12105 | * @str: the XPath expression |
| 12106 | * |
| 12107 | * Try to compile the XPath expression as a streamable subset. |
| 12108 | * |
| 12109 | * Returns the compiled expression or NULL if failed to compile. |
| 12110 | */ |
| 12111 | static xmlXPathCompExprPtr |
| 12112 | xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 12113 | /* |
| 12114 | * Optimization: use streaming patterns when the XPath expression can |
| 12115 | * be compiled to a stream lookup |
| 12116 | */ |
| 12117 | xmlPatternPtr stream; |
| 12118 | xmlXPathCompExprPtr comp; |
| 12119 | xmlDictPtr dict = NULL; |
| 12120 | const xmlChar **namespaces = NULL; |
| 12121 | xmlNsPtr ns; |
| 12122 | int i, j; |
| 12123 | |
| 12124 | if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) && |
| 12125 | (!xmlStrchr(str, '@'))) { |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 12126 | const xmlChar *tmp; |
| 12127 | |
| 12128 | /* |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 12129 | * We don't try to handle expressions using the verbose axis |
| 12130 | * specifiers ("::"), just the simplied form at this point. |
| 12131 | * Additionally, if there is no list of namespaces available and |
| 12132 | * there's a ":" in the expression, indicating a prefixed QName, |
| 12133 | * then we won't try to compile either. xmlPatterncompile() needs |
| 12134 | * to have a list of namespaces at compilation time in order to |
| 12135 | * compile prefixed name tests. |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 12136 | */ |
| 12137 | tmp = xmlStrchr(str, ':'); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 12138 | if ((tmp != NULL) && |
| 12139 | ((ctxt == NULL) || (ctxt->nsNr == 0) || (tmp[1] == ':'))) |
| 12140 | return(NULL); |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 12141 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12142 | if (ctxt != NULL) { |
| 12143 | dict = ctxt->dict; |
| 12144 | if (ctxt->nsNr > 0) { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 12145 | namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*)); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12146 | if (namespaces == NULL) { |
| 12147 | xmlXPathErrMemory(ctxt, "allocating namespaces array\n"); |
| 12148 | return(NULL); |
| 12149 | } |
| 12150 | for (i = 0, j = 0; (j < ctxt->nsNr); j++) { |
| 12151 | ns = ctxt->namespaces[j]; |
| 12152 | namespaces[i++] = ns->href; |
| 12153 | namespaces[i++] = ns->prefix; |
| 12154 | } |
| 12155 | namespaces[i++] = NULL; |
| 12156 | namespaces[i++] = NULL; |
| 12157 | } |
| 12158 | } |
| 12159 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 12160 | stream = xmlPatterncompile(str, dict, XML_PATTERN_XPATH, |
| 12161 | &namespaces[0]); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 12162 | if (namespaces != NULL) { |
| 12163 | xmlFree((xmlChar **)namespaces); |
| 12164 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12165 | if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) { |
| 12166 | comp = xmlXPathNewCompExpr(); |
| 12167 | if (comp == NULL) { |
| 12168 | xmlXPathErrMemory(ctxt, "allocating streamable expression\n"); |
| 12169 | return(NULL); |
| 12170 | } |
| 12171 | comp->stream = stream; |
| 12172 | comp->dict = dict; |
| 12173 | if (comp->dict) |
| 12174 | xmlDictReference(comp->dict); |
| 12175 | return(comp); |
| 12176 | } |
| 12177 | xmlFreePattern(stream); |
| 12178 | } |
| 12179 | return(NULL); |
| 12180 | } |
| 12181 | #endif /* XPATH_STREAMING */ |
| 12182 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12183 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 12184 | * xmlXPathCtxtCompile: |
| 12185 | * @ctxt: an XPath context |
| 12186 | * @str: the XPath expression |
| 12187 | * |
| 12188 | * Compile an XPath expression |
| 12189 | * |
| 12190 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 12191 | * the caller has to free the object. |
| 12192 | */ |
| 12193 | xmlXPathCompExprPtr |
| 12194 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 12195 | xmlXPathParserContextPtr pctxt; |
| 12196 | xmlXPathCompExprPtr comp; |
| 12197 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12198 | #ifdef XPATH_STREAMING |
| 12199 | comp = xmlXPathTryStreamCompile(ctxt, str); |
| 12200 | if (comp != NULL) |
| 12201 | return(comp); |
| 12202 | #endif |
| 12203 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 12204 | xmlXPathInit(); |
| 12205 | |
| 12206 | pctxt = xmlXPathNewParserContext(str, ctxt); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12207 | xmlXPathCompileExpr(pctxt, 1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 12208 | |
| 12209 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 12210 | { |
| 12211 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 12212 | return(NULL); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 12213 | } |
| 12214 | |
| 12215 | if (*pctxt->cur != 0) { |
| 12216 | /* |
| 12217 | * aleksey: in some cases this line prints *second* error message |
| 12218 | * (see bug #78858) and probably this should be fixed. |
| 12219 | * However, we are not sure that all error messages are printed |
| 12220 | * out in other places. It's not critical so we leave it as-is for now |
| 12221 | */ |
| 12222 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 12223 | comp = NULL; |
| 12224 | } else { |
| 12225 | comp = pctxt->comp; |
| 12226 | pctxt->comp = NULL; |
| 12227 | } |
| 12228 | xmlXPathFreeParserContext(pctxt); |
| 12229 | if (comp != NULL) { |
| 12230 | comp->expr = xmlStrdup(str); |
| 12231 | #ifdef DEBUG_EVAL_COUNTS |
| 12232 | comp->string = xmlStrdup(str); |
| 12233 | comp->nb = 0; |
| 12234 | #endif |
| 12235 | } |
| 12236 | return(comp); |
| 12237 | } |
| 12238 | |
| 12239 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12240 | * xmlXPathCompile: |
| 12241 | * @str: the XPath expression |
| 12242 | * |
| 12243 | * Compile an XPath expression |
| 12244 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 12245 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12246 | * the caller has to free the object. |
| 12247 | */ |
| 12248 | xmlXPathCompExprPtr |
| 12249 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 12250 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12251 | } |
| 12252 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12253 | /** |
| 12254 | * xmlXPathCompiledEval: |
| 12255 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12256 | * @ctx: the XPath context |
| 12257 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12258 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12259 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12260 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12261 | * the caller has to free the object. |
| 12262 | */ |
| 12263 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12264 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12265 | xmlXPathParserContextPtr ctxt; |
| 12266 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 12267 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 12268 | #ifndef LIBXML_THREAD_ENABLED |
| 12269 | static int reentance = 0; |
| 12270 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12271 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 12272 | CHECK_CTXT(ctx) |
| 12273 | |
| 12274 | if (comp == NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12275 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12276 | xmlXPathInit(); |
| 12277 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 12278 | #ifndef LIBXML_THREAD_ENABLED |
| 12279 | reentance++; |
| 12280 | if (reentance > 1) |
| 12281 | xmlXPathDisableOptimizer = 1; |
| 12282 | #endif |
| 12283 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12284 | #ifdef DEBUG_EVAL_COUNTS |
| 12285 | comp->nb++; |
| 12286 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 12287 | fprintf(stderr, "100 x %s\n", comp->string); |
| 12288 | comp->nb = 0; |
| 12289 | } |
| 12290 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12291 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 12292 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12293 | |
| 12294 | if (ctxt->value == NULL) { |
| 12295 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12296 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12297 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12298 | } else { |
| 12299 | res = valuePop(ctxt); |
| 12300 | } |
| 12301 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12302 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12303 | do { |
| 12304 | tmp = valuePop(ctxt); |
| 12305 | if (tmp != NULL) { |
| 12306 | if (tmp != init) |
| 12307 | stack++; |
| 12308 | xmlXPathFreeObject(tmp); |
| 12309 | } |
| 12310 | } while (tmp != NULL); |
| 12311 | if ((stack != 0) && (res != NULL)) { |
| 12312 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12313 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12314 | stack); |
| 12315 | } |
| 12316 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 12317 | xmlXPathFreeObject(res); |
| 12318 | res = NULL; |
| 12319 | } |
| 12320 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12321 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12322 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12323 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 12324 | #ifndef LIBXML_THREAD_ENABLED |
| 12325 | reentance--; |
| 12326 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12327 | return(res); |
| 12328 | } |
| 12329 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12330 | /** |
| 12331 | * xmlXPathEvalExpr: |
| 12332 | * @ctxt: the XPath Parser context |
| 12333 | * |
| 12334 | * Parse and evaluate an XPath expression in the given context, |
| 12335 | * then push the result on the context stack |
| 12336 | */ |
| 12337 | void |
| 12338 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12339 | #ifdef XPATH_STREAMING |
| 12340 | xmlXPathCompExprPtr comp; |
| 12341 | #endif |
| 12342 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 12343 | if (ctxt == NULL) return; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12344 | |
| 12345 | #ifdef XPATH_STREAMING |
| 12346 | comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base); |
| 12347 | if (comp != NULL) { |
| 12348 | if (ctxt->comp != NULL) |
| 12349 | xmlXPathFreeCompExpr(ctxt->comp); |
| 12350 | ctxt->comp = comp; |
| 12351 | if (ctxt->cur != NULL) |
| 12352 | while (*ctxt->cur != 0) ctxt->cur++; |
| 12353 | } else |
| 12354 | #endif |
| 12355 | { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12356 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12357 | } |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 12358 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 12359 | xmlXPathRunEval(ctxt); |
| 12360 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12361 | |
| 12362 | /** |
| 12363 | * xmlXPathEval: |
| 12364 | * @str: the XPath expression |
| 12365 | * @ctx: the XPath context |
| 12366 | * |
| 12367 | * Evaluate the XPath Location Path in the given context. |
| 12368 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12369 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12370 | * the caller has to free the object. |
| 12371 | */ |
| 12372 | xmlXPathObjectPtr |
| 12373 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 12374 | xmlXPathParserContextPtr ctxt; |
| 12375 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 12376 | int stack = 0; |
| 12377 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 12378 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12379 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 12380 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12381 | |
| 12382 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 12383 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12384 | |
| 12385 | if (ctxt->value == NULL) { |
| 12386 | xmlGenericError(xmlGenericErrorContext, |
| 12387 | "xmlXPathEval: evaluation failed\n"); |
| 12388 | res = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 12389 | } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL) |
| 12390 | #ifdef XPATH_STREAMING |
| 12391 | && (ctxt->comp->stream == NULL) |
| 12392 | #endif |
| 12393 | ) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 12394 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 12395 | res = NULL; |
| 12396 | } else { |
| 12397 | res = valuePop(ctxt); |
| 12398 | } |
| 12399 | |
| 12400 | do { |
| 12401 | tmp = valuePop(ctxt); |
| 12402 | if (tmp != NULL) { |
| 12403 | if (tmp != init) |
| 12404 | stack++; |
| 12405 | xmlXPathFreeObject(tmp); |
| 12406 | } |
| 12407 | } while (tmp != NULL); |
| 12408 | if ((stack != 0) && (res != NULL)) { |
| 12409 | xmlGenericError(xmlGenericErrorContext, |
| 12410 | "xmlXPathEval: %d object left on the stack\n", |
| 12411 | stack); |
| 12412 | } |
| 12413 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 12414 | xmlXPathFreeObject(res); |
| 12415 | res = NULL; |
| 12416 | } |
| 12417 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12418 | xmlXPathFreeParserContext(ctxt); |
| 12419 | return(res); |
| 12420 | } |
| 12421 | |
| 12422 | /** |
| 12423 | * xmlXPathEvalExpression: |
| 12424 | * @str: the XPath expression |
| 12425 | * @ctxt: the XPath context |
| 12426 | * |
| 12427 | * Evaluate the XPath expression in the given context. |
| 12428 | * |
| 12429 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 12430 | * the caller has to free the object. |
| 12431 | */ |
| 12432 | xmlXPathObjectPtr |
| 12433 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 12434 | xmlXPathParserContextPtr pctxt; |
| 12435 | xmlXPathObjectPtr res, tmp; |
| 12436 | int stack = 0; |
| 12437 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 12438 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12439 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 12440 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12441 | |
| 12442 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 12443 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12444 | |
| 12445 | if (*pctxt->cur != 0) { |
| 12446 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 12447 | res = NULL; |
| 12448 | } else { |
| 12449 | res = valuePop(pctxt); |
| 12450 | } |
| 12451 | do { |
| 12452 | tmp = valuePop(pctxt); |
| 12453 | if (tmp != NULL) { |
| 12454 | xmlXPathFreeObject(tmp); |
| 12455 | stack++; |
| 12456 | } |
| 12457 | } while (tmp != NULL); |
| 12458 | if ((stack != 0) && (res != NULL)) { |
| 12459 | xmlGenericError(xmlGenericErrorContext, |
| 12460 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 12461 | stack); |
| 12462 | } |
| 12463 | xmlXPathFreeParserContext(pctxt); |
| 12464 | return(res); |
| 12465 | } |
| 12466 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 12467 | /************************************************************************ |
| 12468 | * * |
| 12469 | * Extra functions not pertaining to the XPath spec * |
| 12470 | * * |
| 12471 | ************************************************************************/ |
| 12472 | /** |
| 12473 | * xmlXPathEscapeUriFunction: |
| 12474 | * @ctxt: the XPath Parser context |
| 12475 | * @nargs: the number of arguments |
| 12476 | * |
| 12477 | * Implement the escape-uri() XPath function |
| 12478 | * string escape-uri(string $str, bool $escape-reserved) |
| 12479 | * |
| 12480 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 12481 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 12482 | * or part of a URI. The effect of the function is to replace any special |
| 12483 | * character in the string by an escape sequence of the form %xx%yy..., |
| 12484 | * where xxyy... is the hexadecimal representation of the octets used to |
| 12485 | * represent the character in UTF-8. |
| 12486 | * |
| 12487 | * The set of characters that are escaped depends on the setting of the |
| 12488 | * boolean argument $escape-reserved. |
| 12489 | * |
| 12490 | * If $escape-reserved is true, all characters are escaped other than lower |
| 12491 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 12492 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 12493 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 12494 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 12495 | * A-F). |
| 12496 | * |
| 12497 | * If $escape-reserved is false, the behavior differs in that characters |
| 12498 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 12499 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 12500 | * |
| 12501 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 12502 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 12503 | * compared using string comparison functions, this function must always use |
| 12504 | * the upper-case letters A-F. |
| 12505 | * |
| 12506 | * Generally, $escape-reserved should be set to true when escaping a string |
| 12507 | * that is to form a single part of a URI, and to false when escaping an |
| 12508 | * entire URI or URI reference. |
| 12509 | * |
| 12510 | * In the case of non-ascii characters, the string is encoded according to |
| 12511 | * utf-8 and then converted according to RFC 2396. |
| 12512 | * |
| 12513 | * Examples |
| 12514 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 12515 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 12516 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 12517 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 12518 | * |
| 12519 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 12520 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 12521 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 12522 | xmlXPathObjectPtr str; |
| 12523 | int escape_reserved; |
| 12524 | xmlBufferPtr target; |
| 12525 | xmlChar *cptr; |
| 12526 | xmlChar escape[4]; |
| 12527 | |
| 12528 | CHECK_ARITY(2); |
| 12529 | |
| 12530 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 12531 | |
| 12532 | CAST_TO_STRING; |
| 12533 | str = valuePop(ctxt); |
| 12534 | |
| 12535 | target = xmlBufferCreate(); |
| 12536 | |
| 12537 | escape[0] = '%'; |
| 12538 | escape[3] = 0; |
| 12539 | |
| 12540 | if (target) { |
| 12541 | for (cptr = str->stringval; *cptr; cptr++) { |
| 12542 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 12543 | (*cptr >= 'a' && *cptr <= 'z') || |
| 12544 | (*cptr >= '0' && *cptr <= '9') || |
| 12545 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 12546 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 12547 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 12548 | (*cptr == '%' && |
| 12549 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 12550 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 12551 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 12552 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 12553 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 12554 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 12555 | (!escape_reserved && |
| 12556 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 12557 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 12558 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 12559 | *cptr == ','))) { |
| 12560 | xmlBufferAdd(target, cptr, 1); |
| 12561 | } else { |
| 12562 | if ((*cptr >> 4) < 10) |
| 12563 | escape[1] = '0' + (*cptr >> 4); |
| 12564 | else |
| 12565 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 12566 | if ((*cptr & 0xF) < 10) |
| 12567 | escape[2] = '0' + (*cptr & 0xF); |
| 12568 | else |
| 12569 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 12570 | |
| 12571 | xmlBufferAdd(target, &escape[0], 3); |
| 12572 | } |
| 12573 | } |
| 12574 | } |
| 12575 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 12576 | xmlBufferFree(target); |
| 12577 | xmlXPathFreeObject(str); |
| 12578 | } |
| 12579 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12580 | /** |
| 12581 | * xmlXPathRegisterAllFunctions: |
| 12582 | * @ctxt: the XPath context |
| 12583 | * |
| 12584 | * Registers all default XPath functions in this context |
| 12585 | */ |
| 12586 | void |
| 12587 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 12588 | { |
| 12589 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 12590 | xmlXPathBooleanFunction); |
| 12591 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 12592 | xmlXPathCeilingFunction); |
| 12593 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 12594 | xmlXPathCountFunction); |
| 12595 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 12596 | xmlXPathConcatFunction); |
| 12597 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 12598 | xmlXPathContainsFunction); |
| 12599 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 12600 | xmlXPathIdFunction); |
| 12601 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 12602 | xmlXPathFalseFunction); |
| 12603 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 12604 | xmlXPathFloorFunction); |
| 12605 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 12606 | xmlXPathLastFunction); |
| 12607 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 12608 | xmlXPathLangFunction); |
| 12609 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 12610 | xmlXPathLocalNameFunction); |
| 12611 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 12612 | xmlXPathNotFunction); |
| 12613 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 12614 | xmlXPathNameFunction); |
| 12615 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 12616 | xmlXPathNamespaceURIFunction); |
| 12617 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 12618 | xmlXPathNormalizeFunction); |
| 12619 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 12620 | xmlXPathNumberFunction); |
| 12621 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 12622 | xmlXPathPositionFunction); |
| 12623 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 12624 | xmlXPathRoundFunction); |
| 12625 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 12626 | xmlXPathStringFunction); |
| 12627 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 12628 | xmlXPathStringLengthFunction); |
| 12629 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 12630 | xmlXPathStartsWithFunction); |
| 12631 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 12632 | xmlXPathSubstringFunction); |
| 12633 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 12634 | xmlXPathSubstringBeforeFunction); |
| 12635 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 12636 | xmlXPathSubstringAfterFunction); |
| 12637 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 12638 | xmlXPathSumFunction); |
| 12639 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 12640 | xmlXPathTrueFunction); |
| 12641 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 12642 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 12643 | |
| 12644 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 12645 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 12646 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12647 | } |
| 12648 | |
| 12649 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 12650 | #define bottom_xpath |
| 12651 | #include "elfgcchack.h" |